新手入门
- 积分
- 26
- 金钱
- 26
- 注册时间
- 2014-4-3
- 在线时间
- 1 小时
|
发表于 2014-5-15 21:11:32
|
显示全部楼层
#include"sys.h"
<br>#include "24cl01.h"
<br>#include "spi.h"
<br>
<br>#define Left 1
<br>#define Right 2
<br>const u8 TX_ADDRESS[TX_ADR_WIDTH]={0x34,0x43,0x10,0x10,0x01}; //·??????·
<br>const u8 RX_ADDRESS[RX_ADR_WIDTH]={0x34,0x43,0x10,0x10,0x01}; //???????·
<br>//??????24L01??IO??
<br>/*u8 tmp_buf[33]={0x31,0x30,0x29,0x28,0x27,0x26,0x25,0x24,
<br>0x23,0x22,0x21,0x20,0x19,0x18,0x17,0x16,0x15,0x14,0x13,0x12,
<br>0x11,0x10,0x09,0x08,0x07,0x06,0x05,0x04,0x03,0x02,0x01,0x00};//????????????*/
<br>const u8 ip[6]={'0','F','U','C','K',0x00};//·??????· ????0x00????LCD?????á??????
<br>const u8 ip0[6]={'0','F','U','C','K',0x00};//???????·0 ???÷??2?ì
<br>const u8 ip1[6]={'1','F','U','C','K',0x00};//???????·1 ??·??????????°?????????ó??
<br> //?¨???????????????±?ò???????????????????±?ò??????±???????
<br>const u8 ip2[6]={'2','F','U','C','K',0x00};//???????·2 ???·char5??char4??char3??char2??char1.
<br>const u8 ip3[6]={'3','F','U','C','K',0x00};//???????·3 ?¨??2345?????è??char1
<br>const u8 ip4[6]={'4','F','U','C','K',0x00};//???????·4 ?????±?ò???ò??char1??char2??char3??char4??char5
<br>const u8 ip5[6]={'5','F','U','C','K',0x00};//???????·5
<br>u8 caout[6]={0xff,0xff,0xff,0xff,0xff,0x00};//?????ì?é·??????·
<br>u8 caip0[6]={0xff,0xff,0xff,0xff,0xff,0x00};//?????ì?é???????·0
<br>u8 caip1[6]={0xff,0xff,0xff,0xff,0xff,0x00};//?????ì?é???????·1
<br>u8 caip2[6]={0xff,0xff,0xff,0xff,0xff,0x00};//?????ì?é???????·2
<br>u8 caip3[6]={0xff,0xff,0xff,0xff,0xff,0x00};//?????ì?é???????·3
<br>u8 caip4[6]={0xff,0xff,0xff,0xff,0xff,0x00};//?????ì?é???????·4
<br>u8 caip5[6]={0xff,0xff,0xff,0xff,0xff,0x00};//?????ì?é???????·5
<br>u8 careg[2]={0xff,0x00};//?????é???????÷????
<br>//const u8 TX_ADDRESS[TX_ADR_WIDTH]={0x34,0x43,0x10,0x10,0x01}; //·??????·
<br>//const u8 RX_ADDRESS[RX_ADR_WIDTH]={0x34,0x43,0x10,0x10,0x01}; //·??????·
<br>
<br>//??????24L01??IO??
<br>void NRF24L01_Init(void)
<br>{
<br> //??????PB10.11.12
<br> RCC->APB2ENR|=1<<3; //PORTB?±??????
<br> GPIOB->CRH&=0XFFF000FF;
<br> GPIOB->CRH|=0X00038300;
<br> GPIOB->ODR|=1<<10;
<br> GPIOB->ODR|=1<<12;
<br> GPIOB->ODR|=1<<11;
<br> SPI2_Init(); //??????SPI
<br> NRF24L01_CSN=1; //SPI????????
<br> NRF24L01_CE=0; //??????
<br>
<br>}
<br>//?ì??24L01??·?????
<br>//·?????:0??????;1???§°?
<br>u8 NRF24L01_Check(void)
<br>{
<br> u8 buf[5]={0XF0,0XF0,0XF0,0XF0,0XF0}; //???????°?????????ó
<br> u8 i;
<br> SPI2_SetSpeed(SPI_SPEED_4); //spi??????9Mhz?¨24L01??×??óSPI?±????10Mhz??
<br> NRF24L01_Write_Buf(0x20+0x10,buf,5);//????5??×????????·.
<br> NRF24L01_Read_Buf(0x10,buf,5); //?????????????·
<br> for(i=0;i<5;i++) if(buf!=0XF0)break;
<br> if(i!=5)return 1;//?ì??24L01?í?ó
<br> return 0; //?ì????24L01
<br>}
<br>//SPI???????÷
<br>//reg:???¨?????÷???·
<br>//value:????????
<br>u8 NRF24L01_Write_Reg(u8 reg,u8 value)
<br>{
<br> u8 status;
<br> NRF24L01_CSN=0; //????SPI????
<br> status =SPI2_ReadWriteByte(reg);//·????????÷??
<br> SPI2_ReadWriteByte(value); //?????????÷????
<br> NRF24L01_CSN=1; //????SPI????
<br> return(status); //·???×?????
<br>}
<br>//????SPI?????÷??
<br>//reg:???????????÷
<br>u8 NRF24L01_Read_Reg(u8 reg)
<br>{
<br> u8 reg_val;
<br> NRF24L01_CSN = 0; //????SPI????
<br> SPI2_ReadWriteByte(reg); //·????????÷??
<br> reg_val=SPI2_ReadWriteByte(0XFF);//?????????÷????
<br> NRF24L01_CSN = 1; //????SPI????
<br> return(reg_val); //·???×?????
<br>}
<br>//?????¨???????????¨?¤????????
<br>//reg:?????÷(????)
<br>//*pBuf:????????
<br>//len:?????¤??
<br>//·?????,??????????×????????÷??
<br>u8 NRF24L01_Read_Buf(u8 reg,u8 *pBuf,u8 len)
<br>{
<br> u8 status,u8_ctr;
<br> NRF24L01_CSN = 0; //????SPI????
<br> status=SPI2_ReadWriteByte(reg);//·????????÷??(????),??????×?????
<br> for(u8_ctr=0;u8_ctr<len;u8_ctr++)
<br> {
<br> pBuf[u8_ctr]=SPI2_ReadWriteByte(0XFF);//????????
<br> }
<br> NRF24L01_CSN=1; //??±?SPI????
<br> return status; //·?????????×?????
<br>}
<br>//?????¨?????????¨?¤????????
<br>//reg:?????÷(????)
<br>//*pBuf:????????
<br>//len:?????¤??
<br>//·?????,??????????×????????÷??
<br>u8 NRF24L01_Write_Buf(u8 reg, u8 *pBuf, u8 len)
<br>{
<br> u8 status,u8_ctr;
<br> NRF24L01_CSN = 0; //????SPI????
<br> status = SPI2_ReadWriteByte(reg);//·????????÷??(????),??????×?????
<br> for(u8_ctr=0; u8_ctr><len; u8_ctr++)SPI2_ReadWriteByte(*pBuf++); //????????
<br> NRF24L01_CSN = 1; //??±?SPI????
<br> return status; //·?????????×?????
<br>}
<br>
<br>//????NRF24L01·???????????
<br>//txbuf:??·????????×???·
<br>//·?????:·????ê??×???
<br>u8 NRF24L01_TxPacket(u8 *txbuf)
<br>{
<br> u8 sta;
<br> SPI2_SetSpeed(SPI_SPEED_4);//spi??????9Mhz?¨24L01??×??óSPI?±????10Mhz??
<br> NRF24L01_CE=0;
<br> NRF24L01_Write_Buf(0xA0,txbuf,32);//????????TX BUF 32??×???
<br> NRF24L01_CE=1;//????·???
<br> while(NRF24L01_IRQ!=0);//????·????ê??
<br> sta=NRF24L01_Read_Reg(0x07); //????×????????÷????
<br> NRF24L01_Write_Reg(0x20+0x07,sta); //????TX_DS?òMAX_RT????±ê??
<br> if(sta&0x10)//????×??ó??·?????
<br> {
<br> NRF24L01_Write_Reg(0xE1,0xff);//????TX FIFO?????÷
<br> LCD_Play1(5,10,"Send DATA",Right);
<br> return MAX_TX;
<br> }
<br> if(sta&0x20)//·????ê??
<br> {
<br> LCD_Play1(5,40,"OK!",Left);
<br>// LED0=!LED0;
<br> return TX_OK;
<br> }
<br> return 0xff;//???????ò·????§°?
<br>}
<br>//????NRF24L01·???????????
<br>//txbuf:??·????????×???·
<br>//·?????:0???????ê???????????í?ó?ú??
<br>u8 NRF24L01_RxPacket(u8 *rxbuf)
<br>{
<br> u8 sta;
<br> SPI2_SetSpeed(SPI_SPEED_4); //spi??????9Mhz?¨24L01??×??óSPI?±????10Mhz??
<br> sta=NRF24L01_Read_Reg(0x07); //????×????????÷????
<br> NRF24L01_Write_Reg(0x20+0x07,sta); //????TX_DS?òMAX_RT????±ê??
<br> careg[0]=sta;
<br> if(sta&0x40)//??????????wei 6
<br> {
<br>
<br> NRF24L01_Read_Buf(0x61,rxbuf,32);//????????
<br> NRF24L01_Write_Reg(0xe2,0xff);//????RX FIFO?????÷
<br>// LED1=!LED1; //?????????÷
<br> return 1;
<br> }
<br> return 0;//??????????????
<br>}
<br>//????????????NRF24L01??RX????
<br>//?è??RX???·,??RX?????í??,????RF????,?¨??????LNA HCURR
<br>//?±CE±????ó,??????RX????,????????????????
<br>void RX_Mode(void)//????
<br>{
<br> NRF24L01_CE=0; //?è??24L01+??±???????????????????
<br> NRF24L01_Write_Reg(0x20+0x00,0x0f);//???????????????ù±?????WR_UP,EN_CRC,16BIT_CRC,
<br> NRF24L01_Write_Reg(0x20+0x01,0x3f);//?????è??0x3f?ú±í543210????
<br>// NRF24L01_Read_Buf(0x01,careg,1); //???????ì?é?????????÷0x01??????
<br> NRF24L01_Write_Reg(0x20+0x02,0x3f);//?????¨??????0x3f?ú±í543210?¨??????
<br> NRF24L01_Write_Reg(0x20+0x05,50); //?è??RF?¨??????2.4G+?è????Mhz??·??§0x00??0x7f
<br> NRF24L01_Write_Reg(0x20+0x06,0x0f);//?è??TX·???????,0db????,2Mbps,?????ù????????
<br>// NRF24L01_Write_Reg(0x20+0x06,0xa7);//24L01+???é?è??TX·???????,0db????,250kbps,?????ù????????
<br>
<br> NRF24L01_Write_Buf(0X20+0X0a,(u8*)ip0,5);//??RX0???????·
<br>// NRF24L01_Read_Buf(0x0a,caip0,5); //?é???·?·?¨??????
<br> NRF24L01_Write_Buf(0X20+0X0b,(u8*)ip1,5);//??RX1???????·
<br>// NRF24L01_Read_Buf(0x0b,caip1,5);
<br> NRF24L01_Write_Buf(0X20+0X0c,(u8*)ip2,1);//??RX2???????·?¨????1???????ò??????±???????
<br>// NRF24L01_Read_Buf(0x0c,caip2,5);
<br> NRF24L01_Write_Buf(0X20+0X0d,(u8*)ip3,1);//??RX3???????·
<br>// NRF24L01_Read_Buf(0x0d,caip3,5);
<br> NRF24L01_Write_Buf(0X20+0X0e,(u8*)ip4,1);//??RX4???????·
<br>// NRF24L01_Read_Buf(0x0e,caip4,5);
<br> NRF24L01_Write_Buf(0X20+0X0f,(u8*)ip5,1);//??RX5???????·
<br>// NRF24L01_Read_Buf(0x0f,caip5,5);
<br> NRF24L01_Write_Buf(0X20+0X10,(u8*)ip1,5);//??TX?????·
<br>// NRF24L01_Read_Buf(0x10,caout,5);
<br>
<br> NRF24L01_Write_Reg(0x20+0x11,32);//?????¨??0?????§?????í??32×???
<br> NRF24L01_Write_Reg(0x20+0x12,32);//?????¨??1?????§?????í??32×???
<br> NRF24L01_Write_Reg(0x20+0x13,32);//?????¨??2?????§?????í??32×???
<br> NRF24L01_Write_Reg(0x20+0x14,32);//?????¨??3?????§?????í??32×???
<br> NRF24L01_Write_Reg(0x20+0x15,32);//?????¨??4?????§?????í??32×???
<br> NRF24L01_Write_Reg(0x20+0x16,32);//?????¨??5?????§?????í??32×???
<br>
<br> NRF24L01_CE = 1; //CE????,????????????
<br>
<br>
<br>}
<br>
<br>//????????????NRF24L01??TX????
<br>//?è??TX???·,??TX?????í??,?è??RX×????????????·,????TX·???????,????RF????,?¨??????LNA HCURR
<br>//PWR_UP,CRC????
<br>//?±CE±????ó,??????RX????,????????????????
<br>//CE?????ó??10us,?ò????·???.
<br>void TX_Mode(void)//·???
<br>{
<br> NRF24L01_CE=0;//???????ú
<br> NRF24L01_Write_Buf(0x20+0x10,(u8*)ip,5);//??TX???????·
<br> NRF24L01_Write_Buf(0x20+0x0a,(u8*)ip0,5); //?è??TX???????·,?÷??????????ACK
<br> NRF24L01_Write_Reg(0x20+0x00,0x0e); //?????ù±??¤×÷??????????WR_UP,EN_CRC,16BIT_CRC,????????,?????ù??????
<br> NRF24L01_Write_Reg(0x20+0x01,0x3f); //?????¨??543210??×???????
<br> NRF24L01_Write_Reg(0x20+0x02,0x3f); //?????¨??543210?????????·
<br> //NRF24L01_Write_Reg(0x20+0x03,0x03);
<br> NRF24L01_Write_Reg(0x20+0x04,0xff);//?è??×?????·??????±??:4000us + 86us;×??ó×?????·?????:15??
<br> NRF24L01_Write_Reg(0x20+0x05,50); //?è??RF?¨????×??ó??(111 1111)Mhz +2.4Ghz
<br> NRF24L01_Write_Reg(0x20+0x06,0x0f); //?è??TX·???????,0db????,2Mbps,?????ù????????
<br>// NRF24L01_Write_Reg(0x20+0x06,0xa7);//24L01+???é?è??TX·???????,0db????,250kbps,?????ù????????
<br>
<br> NRF24L01_CE=1;//CE????,10us?ó????·???
<br>}
<br>
<br>求指教为什么发送不成功> |
|