|
里面的程序如圖所示。
捕獲.PNG (13.67 KB, 下載次數(shù): 41)
下載附件
2018-4-27 20:58 上傳
0.png (8.69 KB, 下載次數(shù): 47)
下載附件
2018-4-28 03:11 上傳
0.png (6.4 KB, 下載次數(shù): 49)
下載附件
2018-4-28 03:11 上傳
資料列表:
NRF24l01雙向通信
NRF24l01無線232通信
NRF24l01無線PC控制系統(tǒng)
NRF24L01無線溫度監(jiān)控系統(tǒng)
NRF24l01無線記分系統(tǒng)
這是其中一個(gè)項(xiàng)目的單片機(jī)源程序:
- #include <reg52.h>
- #include <intrins.h>
- typedef unsigned char uchar;
- typedef unsigned char uint;
- //****************************************NRF24L01端口定義***************************************
- sbit MISO =P1^3;
- sbit MOSI =P1^4;
- sbit SCK =P1^2;
- sbit CE =P1^1;
- sbit CSN =P3^2;
- sbit IRQ =P3^3;
- //************************************按鍵***************************************************
- sbit KEY1=P3^6;
- sbit KEY2=P3^7;
- //************************************數(shù)碼管位選*********************************************
- sbit led3=P2^0;
- sbit led2=P2^1;
- sbit led1=P2^2;
- sbit led0=P2^3;
- //************************************蜂明器***************************************************
- sbit BELL=P3^4;
- //***********************************數(shù)碼管0-9編碼***********************************************
- uchar count1=0,count2=0;
- uchar seg[10]={0xC0,0xCF,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90}; //0~~9段碼
- //******************************************************************************************
- uint bdata sta; //NRF24L01狀態(tài)標(biāo)志
- sbit RX_DR =sta^6;
- sbit TX_DS =sta^5;
- sbit MAX_RT =sta^4;
- //*************************************NRF24L01**************************************************
- #define TX_ADR_WIDTH 5 // 本機(jī)地址寬度設(shè)置
- #define RX_ADR_WIDTH 5 // 接收方地址寬度設(shè)置
- #define TX_PLOAD_WIDTH 20 // 4 字節(jié)數(shù)據(jù)長度
- #define RX_PLOAD_WIDTH 20 // 4 字節(jié)數(shù)據(jù)長度
- uint const TX_ADDRESS[TX_ADR_WIDTH]= {0x34,0x43,0x10,0x10,0x01}; //本地地址
- uint const RX_ADDRESS[RX_ADR_WIDTH]= {0x34,0x43,0x10,0x10,0x01}; //接收地址
- //*****************************NRF24L01寄存器指令,詳細(xì)請(qǐng)對(duì)照,Page18******************************
- #define READ_REG 0x00 // 讀寄存器指令
- #define WRITE_REG 0x20 // 寫寄存器指令
- #define RD_RX_PLOAD 0x61 // 讀取接收數(shù)據(jù)指令
- #define WR_TX_PLOAD 0xA0 // 寫待發(fā)數(shù)據(jù)指令
- #define FLUSH_TX 0xE1 // 沖洗發(fā)送 FIFO指令
- #define FLUSH_RX 0xE2 // 沖洗接收 FIFO指令
- #define REUSE_TX_PL 0xE3 // 定義重復(fù)裝載數(shù)據(jù)指令
- #define NOP 0xFF // 保留
- //****************************SPI(nRF24L01)寄存器地址,詳細(xì)請(qǐng)對(duì)照,Page18-24**********************
- #define CONFIG 0x00 // 配置收發(fā)狀態(tài),CRC校驗(yàn)?zāi)J揭约笆瞻l(fā)狀態(tài)響應(yīng)方式
- #define EN_AA 0x01 // 自動(dòng)應(yīng)答功能設(shè)置
- #define EN_RXADDR 0x02 // 可用信道設(shè)置
- #define SETUP_AW 0x03 // 收發(fā)地址寬度設(shè)置
- #define SETUP_RETR 0x04 // 自動(dòng)重發(fā)功能設(shè)置
- #define RF_CH 0x05 // 工作頻率設(shè)置
- #define RF_SETUP 0x06 // 發(fā)射速率、功耗功能設(shè)置
- #define STATUS 0x07 // 狀態(tài)寄存器
- #define OBSERVE_TX 0x08 // 發(fā)送監(jiān)測(cè)功能
- #define CD 0x09 // 地址檢測(cè)
- #define RX_ADDR_P0 0x0A // 頻道0接收數(shù)據(jù)地址
- #define RX_ADDR_P1 0x0B // 頻道1接收數(shù)據(jù)地址
- #define RX_ADDR_P2 0x0C // 頻道2接收數(shù)據(jù)地址
- #define RX_ADDR_P3 0x0D // 頻道3接收數(shù)據(jù)地址
- #define RX_ADDR_P4 0x0E // 頻道4接收數(shù)據(jù)地址
- #define RX_ADDR_P5 0x0F // 頻道5接收數(shù)據(jù)地址
- #define TX_ADDR 0x10 // 發(fā)送地址寄存器
- #define RX_PW_P0 0x11 // 接收頻道0接收數(shù)據(jù)長度
- #define RX_PW_P1 0x12 // 接收頻道0接收數(shù)據(jù)長度
- #define RX_PW_P2 0x13 // 接收頻道0接收數(shù)據(jù)長度
- #define RX_PW_P3 0x14 // 接收頻道0接收數(shù)據(jù)長度
- #define RX_PW_P4 0x15 // 接收頻道0接收數(shù)據(jù)長度
- #define RX_PW_P5 0x16 // 接收頻道0接收數(shù)據(jù)長度
- #define FIFO_STATUS 0x17 // FIFO棧入棧出狀態(tài)寄存器設(shè)置
- //************************************NRF24L01函數(shù)申明**********************************************
- void Delay(unsigned int s);
- void inerDelay_us(unsigned char n);
- void init_NRF24L01(void);
- uint SPI_RW(uint uchar);
- uchar SPI_Read(uchar reg);
- void SetRX_Mode(void);
- uint SPI_RW_Reg(uchar reg, uchar value);
- uint SPI_Read_Buf(uchar reg, uchar *pBuf, uchar uchars);
- uint SPI_Write_Buf(uchar reg, uchar *pBuf, uchar uchars);
- unsigned char nRF24L01_RxPacket(unsigned char* rx_buf);
- void nRF24L01_TxPacket(unsigned char * tx_buf);
- //*****************************************長延時(shí)*****************************************
- void Delay(unsigned int s)
- {
- unsigned int i;
- for(i=0; i<s; i++);
- for(i=0; i<s; i++);
- }
- /******************************************************************************************
- /*延時(shí)函數(shù)
- /******************************************************************************************/
- void inerDelay_us(unsigned char n)
- {
- for(;n>0;n--)
- _nop_();
- }
- //****************************************************************************************
- /*NRF24L01初始化
- //***************************************************************************************/
- void init_NRF24L01(void)
- {
- inerDelay_us(100);
- CE=0; // chip enable
- CSN=1; // Spi disable
- SCK=0; // Spi clock line init high
- SPI_Write_Buf(WRITE_REG + TX_ADDR, TX_ADDRESS, TX_ADR_WIDTH); // 寫本地地址
- SPI_Write_Buf(WRITE_REG + RX_ADDR_P0, RX_ADDRESS, RX_ADR_WIDTH); // 寫接收端地址
- SPI_RW_Reg(WRITE_REG + EN_AA, 0x01); // 頻道0自動(dòng) ACK應(yīng)答允許
- SPI_RW_Reg(WRITE_REG + EN_RXADDR, 0x01); // 允許接收地址只有頻道0,如果需要多頻道可以參考Page21
- SPI_RW_Reg(WRITE_REG + RF_CH, 0); // 設(shè)置信道工作為2.4GHZ,收發(fā)必須一致
- SPI_RW_Reg(WRITE_REG + RX_PW_P0, RX_PLOAD_WIDTH); //設(shè)置接收數(shù)據(jù)長度,本次設(shè)置為4字節(jié)
- SPI_RW_Reg(WRITE_REG + RF_SETUP, 0x07); //設(shè)置發(fā)射速率為1Mkbps,發(fā)射功率為最大值0dB
- SPI_RW_Reg(WRITE_REG + CONFIG, 0x0f); // IRQ收發(fā)完成中斷響應(yīng),16位CRC ,主接收
- }
- /****************************************************************************************************
- /*函數(shù):uint SPI_RW(uint uchar)
- /*功能:NRF24L01的SPI寫時(shí)序,詳細(xì)看時(shí)序圖,Page19
- /****************************************************************************************************/
- uint SPI_RW(uint uchar)
- {
- uint bit_ctr;
- for(bit_ctr=0;bit_ctr<8;bit_ctr++) // output 8-bit
- {
- MOSI = (uchar & 0x80); // output 'uchar', MSB to MOSI
- uchar = (uchar << 1); // shift next bit into MSB..
- SCK = 1; // Set SCK high..
- uchar |= MISO; // capture current MISO bit
- SCK = 0; // ..then set SCK low again
- }
- return(uchar); // return read uchar
- }
- /****************************************************************************************************
- /*函數(shù):uchar SPI_Read(uchar reg)
- /*功能:NRF24L01的SPI時(shí)序,詳細(xì)看時(shí)序圖,Page19
- /****************************************************************************************************/
- uchar SPI_Read(uchar reg)
- {
- uchar reg_val;
-
- CSN = 0; // CSN low, initialize SPI communication...
- SPI_RW(reg); // Select register to read from..
- reg_val = SPI_RW(0); // ..then read registervalue
- CSN = 1; // CSN high, terminate SPI communication
-
- return(reg_val); // return register value
- }
- /****************************************************************************************************/
- /*功能:NRF24L01讀寫寄存器函數(shù),
- /****************************************************************************************************/
- uint SPI_RW_Reg(uchar reg, uchar value)
- {
- uint status;
-
- CSN = 0; // CSN low, init SPI transaction
- status = SPI_RW(reg); // select register
- SPI_RW(value); // ..and write value to it..
- CSN = 1; // CSN high again
-
- return(status); // return nRF24L01 status uchar
- }
- /****************************************************************************************************/
- /*函數(shù):uint SPI_Read_Buf(uchar reg, uchar *pBuf, uchar uchars)
- /*功能: 用于讀數(shù)據(jù),reg:為寄存器地址,pBuf:為待讀出數(shù)據(jù)地址,uchars:讀出數(shù)據(jù)的個(gè)數(shù)
- /****************************************************************************************************/
- uint SPI_Read_Buf(uchar reg, uchar *pBuf, uchar uchars)
- {
- uint status,uchar_ctr;
-
- CSN = 0; // Set CSN low, init SPI tranaction
- status = SPI_RW(reg); // Select register to write to and read status uchar
-
- for(uchar_ctr=0;uchar_ctr<uchars;uchar_ctr++)
- pBuf[uchar_ctr] = SPI_RW(0); //
-
- CSN = 1;
-
- return(status); // return nRF24L01 status uchar
- }
- /*********************************************************************************************************
- /*函數(shù):uint SPI_Write_Buf(uchar reg, uchar *pBuf, uchar uchars)
- /*功能: 用于寫數(shù)據(jù):為寄存器地址,pBuf:為待寫入數(shù)據(jù)地址,uchars:寫入數(shù)據(jù)的個(gè)數(shù)
- /*********************************************************************************************************/
- uint SPI_Write_Buf(uchar reg, uchar *pBuf, uchar uchars)
- {
- uint status,uchar_ctr;
-
- CSN = 0; //SPI使能
- status = SPI_RW(reg);
- for(uchar_ctr=0; uchar_ctr<uchars; uchar_ctr++) //
- SPI_RW(*pBuf++);
- CSN = 1; //關(guān)閉SPI
- return(status); //
- }
- /*
- /****************************************************************************************************
- /*函數(shù):void SetRX_Mode(void)
- /*功能:數(shù)據(jù)接收配置
- /****************************************************************************************************/
- void SetRX_Mode(void)
- {
- CE=0;
- //SPI_RW_Reg(WRITE_REG + CONFIG, 0x0f);
- // IRQ收發(fā)完成中斷響應(yīng),16位CRC,主接收,由于已經(jīng)配置好所以不用再重復(fù)配置,只要激發(fā)就可以了
- CE = 1;
- inerDelay_us(130);
- }
- /******************************************************************************************************
- /*函數(shù):unsigned char nRF24L01_RxPacket(unsigned char* rx_buf)
- /*功能:數(shù)據(jù)讀取后放如rx_buf接收緩沖區(qū)中
- /******************************************************************************************************/
- unsigned char nRF24L01_RxPacket(unsigned char* rx_buf)
- {
- unsigned char revale=0;
- sta=SPI_Read(STATUS); // 讀取狀態(tài)寄存其來判斷數(shù)據(jù)接收狀況
- if(RX_DR) // 判斷是否接收到數(shù)據(jù)
- {
- CE = 0; //SPI使能
- SPI_Read_Buf(RD_RX_PLOAD,rx_buf,TX_PLOAD_WIDTH);// read receive payload from RX_FIFO buffer
- revale =1; //讀取數(shù)據(jù)完成標(biāo)志
- }
- SPI_RW_Reg(WRITE_REG+STATUS,sta); //接收到數(shù)據(jù)后RX_DR,TX_DS,MAX_PT都置高為1,通過寫1來清楚中斷標(biāo)志
- return revale;
- }
- /***********************************************************************************************************
- /*函數(shù):void nRF24L01_TxPacket(unsigned char * tx_buf)
- /*功能:發(fā)送 tx_buf中數(shù)據(jù)
- /**********************************************************************************************************
- void nRF24L01_TxPacket(unsigned char * tx_buf)
- {
- CE=0; //StandBy I模式
- SPI_Write_Buf(WRITE_REG + RX_ADDR_P0, TX_ADDRESS, TX_ADR_WIDTH); // 裝載接收端地址
- SPI_Write_Buf(WR_TX_PLOAD, tx_buf, TX_PLOAD_WIDTH); // 裝載數(shù)據(jù)
- SPI_RW_Reg(WRITE_REG + CONFIG, 0x0e); // IRQ收發(fā)完成中斷響應(yīng),16位CRC,主發(fā)送
- CE=1; //置高CE,激發(fā)數(shù)據(jù)發(fā)送
- inerDelay_us(10);
- }
- */
- //***********************************數(shù)據(jù)顯示**************************************************************
- void dis_zhuye()
- {
- P0=seg[count2%10];
- led0=0;
- Delay(40);
- led0=1;
- P0=seg[count2/10];
- led1=0;
- Delay(40);
- led1=1;
- P0=seg[count1%10];
- led2=0;
- Delay(40);
- led2=1;
- P0=seg[count1/10];
- led3=0;
- Delay(40);
- led3=1;
- }
- //************************************主函數(shù)************************************************************
- void main(void)
- {
- uchar RxBuf[20]={0}; //4字節(jié)發(fā)送數(shù)據(jù)緩沖區(qū)
- init_NRF24L01() ; //NRF24L01初始化配置
- Delay(6000);
- while(1)
- {
- SetRX_Mode(); //激發(fā)數(shù)據(jù)接收
- nRF24L01_RxPacket(RxBuf);
- if(RxBuf[0]) //判斷是否收到數(shù)據(jù)
- {
-
- if(RxBuf[0]==0x11) //A隊(duì)+1
- {
- count1=count1+1;
- }
- if(RxBuf[0]==0x21) //A隊(duì)+2
- {
- count1=count1+2;
- }
- if(RxBuf[0]==0x31) //A隊(duì)+3
- {
- count1=count1+3;
- }
- if(RxBuf[0]==0x41) //B隊(duì)+1
- {
- count2=count2+1;
- }
- if(RxBuf[0]==0x51) //B隊(duì)+2
- {
- count2=count2+2;
- }
- if(RxBuf[0]==0x61) //B隊(duì)+3
- {
- count2=count2+3;
- ……………………
- …………限于本文篇幅 余下代碼請(qǐng)從51黑下載附件…………
復(fù)制代碼
所有資料51hei提供下載:
NRF24L01實(shí)例源代碼.rar
(13.61 MB, 下載次數(shù): 62)
2018-4-27 20:57 上傳
點(diǎn)擊文件名下載附件
代碼沒有錯(cuò)誤 ,需要的自取 。 下載積分: 黑幣 -5
|
評(píng)分
-
查看全部評(píng)分
|