初级会员
- 积分
- 65
- 金钱
- 65
- 注册时间
- 2014-5-22
- 在线时间
- 12 小时
|
关于NRF24L01的程序,我之前也在这个版面问过,原子给了我一个链接,我测试过,那个程序似乎有些问题,当然也可能是我不明白具体该怎么使用,并不是个人攻击哈,我也是初学STM32,尤其是无线通信部分,理解初学者的心情,所以我把自己写的程序放上来,让初学的同学们做一个参考。<br />
<br />
我之前做过CC2530,所以zigbee程序的编写流程对我影响比较大。我认为无线收发程序应该是在发送端和接收端烧写进去不同的程序,这样可以增加灵活性,而且也便于理解。<br />
<br />
简单介绍一下程序的情况,<span>我的程序实现1收2发功能,实际上也可以多几路发送,据说最多可以实现1收6发,这个我暂时没有条件。我这个是单位给买的开发板,总共5块mini,最多是1收4发,所以有条件的可以自己做一个多路收发的测试,我就不写了。<br />
<br />
</span>两个发射机都是用PC0来采集AD信号,再将采集出来的信号传送到接收端上,接收端使用KEY0来切换显示两个发射机。通道选择比较高深,我暂时不会,所以我在不同的发射机上发送不同的字符串,比如说第一个发射机发送A_x.xx,第二个发射机发送B_x.xx(x.xx是代表AD采集出来的数值,例如1.23),接收端在按键按下后,可以根据接收过来数据的前缀,进行不同的显示,也就达到通道选择的目的了。方法比较笨,但是可以实现。<br />
<br />
不多说了,我先放上NRF24L01的代码(中文注释拷出来后就成乱码了,大家凑合看吧,重要的地方我会重新写一下的)。<br />
<br />
<div style="background-color:#E8E8E8;">
[mw_shl_code=c,true]#include "24l01.h"
#include "delay.h"
#include "spi.h"
#include "stm32f10x_spi.h"
#include "lcd.h"
#include "led.h"
//////////////////////////////////////////////////////////////////////////////////
//±????ò???©?§?°??????????×÷???í???????????????ü????????
//ALIENTEK MiniSTM32??·?°?
//NRF24L01?????ú??
//??????×?@ALIENTEK
//????????:www.openedv.com
//????????:2014/3/12
//°?±???V1.0
//°??¨?ù??????°?±?????
//Copyright(C) ?????????í??×????????????? 2009-2019
//All rights reserved
//////////////////////////////////////////////////////////////////////////////////
const u8 TX_ADDRESS[TX_ADR_WIDTH]={0x34,0x43,0x10,0x10,0x01}; //地址很重要,这几行地<span style="background-color:#E53333;"></span>址一定要写对,可以看看大神们写的帖子
const u8 RX_ADDRESS0[RX_ADR_WIDTH]={0x34,0x43,0x10,0x10,0x01}; //??0????
const u8 RX_ADDRESS1[RX_ADR_WIDTH]={0xc2,0xc2,0xc2,0xc2,0xc1}; //??1????
const u8 RX_ADDRESS2[RX_ADR_WIDTH]={0xb3}; //??2????
const u8 RX_ADDRESS3[RX_ADR_WIDTH]={0xa5}; //??3????
const u8 RX_ADDRESS4[RX_ADR_WIDTH]={0x35}; //??4????
const u8 RX_ADDRESS5[RX_ADR_WIDTH]={0x8f}; //??5????
//const u8 TX_ADDRESS[TX_ADR_WIDTH]={0x34,0x43,0x10,0x10,0x01}; //·??????·
//const u8 RX_ADDRESS[RX_ADR_WIDTH]={0x34,0x43,0x10,0x10,0x01}; //·??????·
//??????24L01??IO??
void NRF24L01_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOC, ENABLE );
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP ; //???ì????
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP ; //???ì????
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_SetBits(GPIOC,GPIO_Pin_4);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU ; //????????
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_SetBits(GPIOA,GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4);
SPI1_Init(); //??????SPI
SPI_Cmd(SPI1, DISABLE); //
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; //?è??SPI???ò?ò?????ò??????????:SPI?è???????????ò?????¤
SPI_InitStructure.SPI_Mode = SPI_Mode_Master; //?è??SPI?¤×÷????:?è?????÷SPI
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; //?è??SPI???????ó??:SPI·???????8?????á??
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; //???????®???±????????:?±???ü????????
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; //?????????????????±????
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; //NSS???????????¨NSS?????©?????í???¨????SSI???©???í:????NSS??????SSI??????
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256; //?¨???¨?????¤·???????:?¨?????¤·???????256
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; //???¨??????????MSB??????LSB??????:??????????MSB??????
SPI_InitStructure.SPI_CRCPolynomial = 7; //CRC?????????à????
SPI_Init(SPI1, &SPI_InitStructure); //?ù??SPI_InitStruct?????¨???????????????èSPIx?????÷
NRF24L01_CE=0; //????24L01
NRF24L01_CSN=1; //SPI????????
}
//?ì??24L01??·?????
//·?????:0??????;1???§°?
u8 NRF24L01_Check(void)
{
u8 buf[5]={0XA5,0XA5,0XA5,0XA5,0XA5};
u8 i;
SPI1_SetSpeed(SPI_BaudRatePrescaler_8); //spi??????9Mhz?¨24L01??×??óSPI?±????10Mhz?©
NRF24L01_Write_Buf(NRF_WRITE_REG+TX_ADDR,buf,5);//????5??×????????·.
NRF24L01_Read_Buf(TX_ADDR,buf,5); //?????????????·
for(i=0;i<5;i++)if(buf!=0XA5)break;
if(i!=5)return 1;//?ì??24L01?í?ó
return 0; //?ì????24L01
}
//SPI???????÷
//reg:???¨?????÷???·
//value:????????
u8 NRF24L01_Write_Reg(u8 reg,u8 value)
{
u8 status;
NRF24L01_CSN=0; //????SPI????
status =SPI1_ReadWriteByte(reg);//·????????÷??
SPI1_ReadWriteByte(value); //?????????÷????
NRF24L01_CSN=1; //????SPI????
return(status); //·???×?????
}
//????SPI?????÷??
//reg:???????????÷
u8 NRF24L01_Read_Reg(u8 reg)
{
u8 reg_val;
NRF24L01_CSN = 0; //????SPI????
SPI1_ReadWriteByte(reg); //·????????÷??
reg_val=SPI1_ReadWriteByte(0XFF);//?????????÷????
NRF24L01_CSN = 1; //????SPI????
return(reg_val); //·???×?????
}
//?????¨???????????¨?¤????????
//reg:?????÷(????)
//*pBuf:????????
//len:?????¤??
//·?????,??????????×????????÷??
u8 NRF24L01_Read_Buf(u8 reg,u8 *pBuf,u8 len)
{
u8 status,u8_ctr;
NRF24L01_CSN = 0; //????SPI????
status=SPI1_ReadWriteByte(reg);//·????????÷??(????),??????×?????
for(u8_ctr=0;u8_ctr<len;u8_ctr++)pBuf[u8_ctr]=SPI1_ReadWriteByte(0XFF);//????????
NRF24L01_CSN=1; //??±?SPI????
return status; //·?????????×?????
}
//?????¨?????????¨?¤????????
//reg:?????÷(????)
//*pBuf:????????
//len:?????¤??
//·?????,??????????×????????÷??
u8 NRF24L01_Write_Buf(u8 reg, u8 *pBuf, u8 len)
{
u8 status,u8_ctr;
NRF24L01_CSN = 0; //????SPI????
status = SPI1_ReadWriteByte(reg);//·????????÷??(????),??????×?????
for(u8_ctr=0; u8_ctr<len; u8_ctr++)SPI1_ReadWriteByte(*pBuf++); //????????
NRF24L01_CSN = 1; //??±?SPI????
return status; //·?????????×?????
}
//????NRF24L01·???????????
//txbuf:??·????????×???·
//·?????:·????ê??×???
u8 NRF24L01_TxPacket(u8 *txbuf)
{
u8 sta;
SPI1_SetSpeed(SPI_BaudRatePrescaler_8);//spi??????9Mhz?¨24L01??×??óSPI?±????10Mhz?©
NRF24L01_CE=0;
NRF24L01_Write_Buf(WR_TX_PLOAD,txbuf,TX_PLOAD_WIDTH);//????????TX BUF 32??×???
NRF24L01_CE=1;//????·???
while(NRF24L01_IRQ!=0);//????·????ê??
sta=NRF24L01_Read_Reg(STATUS); //????×????????÷????
NRF24L01_Write_Reg(NRF_WRITE_REG+STATUS,sta); //????TX_DS?òMAX_RT????±ê??
if(sta&MAX_TX)//????×??ó??·?????
{
NRF24L01_Write_Reg(FLUSH_TX,0xff);//????TX FIFO?????÷
return MAX_TX;
}
if(sta&TX_OK)//·????ê??
{
return TX_OK;
}
return 0xff;//???????ò·????§°?
}
//????NRF24L01·???????????
//txbuf:??·????????×???·
//·?????:0???????ê???????????í?ó?ú??
u8 NRF24L01_RxPacket(u8 *rxbuf)
{
u8 sta;
SPI1_SetSpeed(SPI_BaudRatePrescaler_8); //spi??????9Mhz?¨24L01??×??óSPI?±????10Mhz?©
sta=NRF24L01_Read_Reg(STATUS); //????×????????÷????
NRF24L01_Write_Reg(NRF_WRITE_REG+STATUS,sta); //????TX_DS?òMAX_RT????±ê??
if(sta&RX_OK)//??????????
{
NRF24L01_Read_Buf(RD_RX_PLOAD,rxbuf,RX_PLOAD_WIDTH);//????????
NRF24L01_Write_Reg(FLUSH_RX,0xff);//????RX FIFO?????÷
return 0;
}
return 1;//??????????????
}
//????????????NRF24L01??RX????
//?è??RX???·,??RX?????í??,????RF????,?¨??????LNA HCURR
//?±CE±????ó,??????RX????,????????????????
void NRF24L01_RX_Mode(void)
{
NRF24L01_CE=0;
// NRF24L01_Write_Reg(FLUSH_RX,0xff);//??TX FIFO???
NRF24L01_Write_Buf(NRF_WRITE_REG+RX_ADDR_P0,(u8*)RX_ADDRESS0,RX_ADR_WIDTH);//?RX????
NRF24L01_Write_Buf(NRF_WRITE_REG+RX_ADDR_P1,(u8*)RX_ADDRESS1,RX_ADR_WIDTH);//?RX????
NRF24L01_Write_Buf(NRF_WRITE_REG+RX_ADDR_P2,(u8*)RX_ADDRESS2,1);//?RX????
NRF24L01_Write_Buf(NRF_WRITE_REG+RX_ADDR_P3,(u8*)RX_ADDRESS3,1);//?RX????
NRF24L01_Write_Buf(NRF_WRITE_REG+RX_ADDR_P4,(u8*)RX_ADDRESS4,1);//?RX????
NRF24L01_Write_Buf(NRF_WRITE_REG+RX_ADDR_P5,(u8*)RX_ADDRESS5,1);//?RX????
NRF24L01_Write_Reg(NRF_WRITE_REG+EN_AA,0x3f); //?????¨??0??×???????
NRF24L01_Write_Reg(NRF_WRITE_REG+EN_RXADDR,0x3f); //?????¨??0?????????·
NRF24L01_Write_Reg(NRF_WRITE_REG+RF_CH,40); //?è??RF?¨??????
NRF24L01_Write_Reg(NRF_WRITE_REG+RX_PW_P0,RX_PLOAD_WIDTH);//?????¨??0?????§?????í??
NRF24L01_Write_Reg(NRF_WRITE_REG+RX_PW_P1,RX_PLOAD_WIDTH);//????0???????
NRF24L01_Write_Reg(NRF_WRITE_REG+RX_PW_P2,RX_PLOAD_WIDTH);//????0???????
NRF24L01_Write_Reg(NRF_WRITE_REG+RX_PW_P3,RX_PLOAD_WIDTH);//????0???????
NRF24L01_Write_Reg(NRF_WRITE_REG+RX_PW_P4,RX_PLOAD_WIDTH);//????0???????
NRF24L01_Write_Reg(NRF_WRITE_REG+RX_PW_P5,RX_PLOAD_WIDTH);//????0???????
NRF24L01_Write_Reg(NRF_WRITE_REG+RF_SETUP,0x0f); //?è??TX·???????,0db????,2Mbps,?????ù????????
NRF24L01_Write_Reg(NRF_WRITE_REG+CONFIG, 0x0f); //?????ù±??¤×÷??????????WR_UP,EN_CRC,16BIT_CRC,????????
NRF24L01_CE = 1; //CE????,????????????
}
//????????????NRF24L01??TX????
//?è??TX???·,??TX?????í??,?è??RX×????????????·,????TX·???????,????RF????,?¨??????LNA HCURR
//PWR_UP,CRC????
//?±CE±????ó,??????RX????,????????????????
//CE?????ó??10us,?ò????·???.
void NRF24L01_TX_Mode(void)
{
NRF24L01_CE=0;
NRF24L01_Write_Reg(FLUSH_TX,0xff);//??TX FIFO???
NRF24L01_Write_Buf(NRF_WRITE_REG+TX_ADDR,(u8*)TX_ADDRESS,TX_ADR_WIDTH);//??TX???????·
NRF24L01_Write_Buf(NRF_WRITE_REG+RX_ADDR_P0,(u8*)RX_ADDRESS0,RX_ADR_WIDTH); //?è??TX???????·,?÷??????????ACK
// NRF24L01_Write_Buf(NRF_WRITE_REG+RX_ADDR_P0,(u8*)RX_ADDRESS1,RX_ADR_WIDTH); //?è??TX???????·,?÷??????????ACK
NRF24L01_Write_Reg(NRF_WRITE_REG+EN_AA,0x01); //?????¨??0??×???????
NRF24L01_Write_Reg(NRF_WRITE_REG+EN_RXADDR,0x01); //?????¨??0?????????·
NRF24L01_Write_Reg(NRF_WRITE_REG+SETUP_RETR,0x1a);//?è??×?????·??????±??:500us + 86us;×??ó×?????·?????:10??
NRF24L01_Write_Reg(NRF_WRITE_REG+RF_CH,40); //?è??RF?¨????40
NRF24L01_Write_Reg(NRF_WRITE_REG+RF_SETUP,0x0f); //?è??TX·???????,0db????,2Mbps,?????ù????????
NRF24L01_Write_Reg(NRF_WRITE_REG+CONFIG,0x0e); //?????ù±??¤×÷??????????WR_UP,EN_CRC,16BIT_CRC,????????,?????ù??????
NRF24L01_CE=1;//CE????,10us?ó????·???
}
[/mw_shl_code]
</div>
<br />
<br />
<br /> |
|