<meter id="pryje"><nav id="pryje"><delect id="pryje"></delect></nav></meter>
          <label id="pryje"></label>

          新聞中心

          EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計應(yīng)用 > 51單片機對無線模塊nRF24L01簡單的控制收發(fā)程序

          51單片機對無線模塊nRF24L01簡單的控制收發(fā)程序

          作者: 時間:2016-11-19 來源:網(wǎng)絡(luò) 收藏
          它的一些物理特性如工作頻段、供電電壓、數(shù)據(jù)傳輸速率就不詳細介紹了,直接上代碼。

          1.首先是發(fā)送端:

          本文引用地址:http://www.ex-cimer.com/article/201611/318110.htm
          // Define SPI pins#include #define uchar unsigned char/***************************************************/#define TX_ADR_WIDTH   5  // 5字節(jié)寬度的發(fā)送/接收地址#define TX_PLOAD_WIDTH 4  // 數(shù)據(jù)通道有效數(shù)據(jù)寬度sbit LED = P2^1;sbit KEY1 = P3^1;sbit KEY2 = P3^2;uchar code TX_ADDRESS[TX_ADR_WIDTH] = {0x34,0x43,0x10,0x10,0x01};  // 定義一個靜態(tài)發(fā)送地址uchar RX_BUF[TX_PLOAD_WIDTH];uchar TX_BUF[TX_PLOAD_WIDTH];uchar flag;uchar DATA = 0x01;uchar bdata sta;sbit  RX_DR     = sta^6;sbit  TX_DS     = sta^5;sbit  MAX_RT = sta^4;sbit CE =  P1^5;sbit CSN=  P1^4;sbit SCK=  P1^3;sbit MOSI= P1^2;sbit MISO= P1^1;sbit IRQ = P1^0;// SPI(nRF24L01) commands#define READ_REG    0x00  // Define read command to register#define WRITE_REG   0x20  // Define write command to register#define RD_RX_PLOAD 0x61  // Define RX payload register address#define WR_TX_PLOAD 0xA0  // Define TX payload register address#define FLUSH_TX    0xE1  // Define flush TX register command#define FLUSH_RX    0xE2  // Define flush RX register command#define REUSE_TX_PL 0xE3  // Define reuse TX payload register command#define NOP         0xFF  // Define No Operation, might be used to read status register// SPI(nRF24L01) registers(addresses)#define CONFIG      0x00  // Config register address#define EN_AA       0x01  // Enable Auto Acknowledgment register address#define EN_RXADDR   0x02  // Enabled RX addresses register address#define SETUP_AW    0x03  // Setup address width register address#define SETUP_RETR  0x04  // Setup Auto. Retrans register address#define RF_CH       0x05  // RF channel register address#define RF_SETUP    0x06  // RF setup register address#define STATUS      0x07  // Status register address#define OBSERVE_TX  0x08  // Observe TX register address#define CD          0x09  // Carrier Detect register address#define RX_ADDR_P0  0x0A  // RX address pipe0 register address#define RX_ADDR_P1  0x0B  // RX address pipe1 register address#define RX_ADDR_P2  0x0C  // RX address pipe2 register address#define RX_ADDR_P3  0x0D  // RX address pipe3 register address#define RX_ADDR_P4  0x0E  // RX address pipe4 register address#define RX_ADDR_P5  0x0F  // RX address pipe5 register address#define TX_ADDR     0x10  // TX address register address#define RX_PW_P0    0x11  // RX payload width, pipe0 register address#define RX_PW_P1    0x12  // RX payload width, pipe1 register address#define RX_PW_P2    0x13  // RX payload width, pipe2 register address#define RX_PW_P3    0x14  // RX payload width, pipe3 register address#define RX_PW_P4    0x15  // RX payload width, pipe4 register address#define RX_PW_P5    0x16  // RX payload width, pipe5 register address#define FIFO_STATUS 0x17  // FIFO Status Register register addressvoid blink(char i);/**************************************************函數(shù): init_io()描述:初始化IO/**************************************************/void init_io(void){CE  = 0;        // 待機CSN = 1;        // SPI禁止SCK = 0;        // SPI時鐘置低IRQ = 1;        // 中斷復(fù)位LED = 1;        // 關(guān)閉指示燈}/**************************************************//**************************************************函數(shù):delay_ms()描述:延遲x毫秒/**************************************************/void delay_ms(uchar x){uchar i, j;i = 0;for(i=0; i射頻通道0x40SPI_RW_Reg(WRITE_REG + RX_PW_P0, TX_PLOAD_WIDTH);  // 接收通道0選擇和發(fā)送通道相同有效數(shù)據(jù)寬度SPI_RW_Reg(WRITE_REG + RF_SETUP, 0x07);            // 數(shù)據(jù)傳輸率1Mbps,發(fā)射功率0dBm,低噪聲放大器增益SPI_RW_Reg(WRITE_REG + CONFIG, 0x0f);              // CRC使能,16位CRC校驗,上電,接收模式delay_ms(150);CE = 1;                                            // 拉高CE啟動接收設(shè)備}/**************************************************//**************************************************函數(shù):TX_Mode()描述:這個函數(shù)設(shè)置nRF24L01為發(fā)送模式,(CE=1持續(xù)至少10us),130us后啟動發(fā)射,數(shù)據(jù)發(fā)送結(jié)束后,發(fā)送模塊自動轉(zhuǎn)入接收模式等待應(yīng)答信號。/**************************************************/void TX_Mode(uchar * BUF){CE = 0;SPI_Write_Buf(WRITE_REG + TX_ADDR, TX_ADDRESS, TX_ADR_WIDTH);     // 寫入發(fā)送地址SPI_Write_Buf(WRITE_REG + RX_ADDR_P0, TX_ADDRESS, TX_ADR_WIDTH);  // 為了應(yīng)答接收設(shè)備,接收通道0地址和發(fā)送地址相同SPI_Write_Buf(WR_TX_PLOAD, BUF, TX_PLOAD_WIDTH);                  // 寫數(shù)據(jù)包到TX FIFOSPI_RW_Reg(WRITE_REG + EN_AA, 0x01);       // 使能接收通道0自動應(yīng)答SPI_RW_Reg(WRITE_REG + EN_RXADDR, 0x01);   // 使能接收通道0SPI_RW_Reg(WRITE_REG + SETUP_RETR, 0x0a);  // 自動重發(fā)延時等待250us+86us,自動重發(fā)10次SPI_RW_Reg(WRITE_REG + RF_CH, 40);         // 選擇射頻通道0x40SPI_RW_Reg(WRITE_REG + RF_SETUP, 0x07);    // 數(shù)據(jù)傳輸率1Mbps,發(fā)射功率0dBm,低噪聲放大器增益SPI_RW_Reg(WRITE_REG + CONFIG, 0x0e);      // CRC使能,16位CRC校驗,上電delay_ms(150);CE = 1;}/**************************************************//**************************************************函數(shù):Check_ACK()描述:檢查接收設(shè)備有無接收到數(shù)據(jù)包,設(shè)定沒有收到應(yīng)答信號是否重發(fā)/**************************************************/uchar Check_ACK(bit clear){delay_ms(200);while(IRQ);sta = SPI_RW(NOP);                    // 返回狀態(tài)寄存器if(TX_DS){blink(3);}//blink(5);if(MAX_RT)if(clear)                         // 是否清除TX FIFO,沒有清除在復(fù)位MAX_RT中斷標志后重發(fā)SPI_RW(FLUSH_TX);SPI_RW_Reg(WRITE_REG + STATUS, sta);  // 清除TX_DS或MAX_RT中斷標志IRQ = 1;if(TX_DS)return(0x00);elsereturn(0xff);}/**************************************************//**************************************************函數(shù):CheckButtons()描述:檢查按鍵是否按下,按下則發(fā)送一字節(jié)數(shù)據(jù)/**************************************************/void CheckButtons(){if(KEY1 == 0){delay_ms(20);if(KEY1 == 0){TX_BUF[0] = 1;          // 數(shù)據(jù)送到緩存//TX_BUF[0] = 0xff;          // 數(shù)據(jù)送到緩存TX_Mode(TX_BUF);            // 把nRF24L01設(shè)置為發(fā)送模式并發(fā)送數(shù)據(jù)            //LED = ~DATA;                // 數(shù)據(jù)送到LED顯示Check_ACK(0);               // 等待發(fā)送完畢,清除TX FIFOdelay_ms(250);delay_ms(250);}}if(KEY2 == 0){delay_ms(20);if(KEY2 == 0){TX_BUF[0] = 2;          // 數(shù)據(jù)送到緩存//TX_BUF[0] = 0xff;          // 數(shù)據(jù)送到緩存TX_Mode(TX_BUF);            // 把nRF24L01設(shè)置為發(fā)送模式并發(fā)送數(shù)據(jù)            //LED = ~DATA;                // 數(shù)據(jù)送到LED顯示Check_ACK(0);               // 等待發(fā)送完畢,清除TX FIFOdelay_ms(250);delay_ms(250);}}}/**************************************************/void blink(char i){while(i--){LED = 1;delay_ms(500);LED = 0;delay_ms(500);}}/**************************************************函數(shù):main()描述:主函數(shù)/**************************************************/void main(void){init_io();                      // 初始化IOwhile(1){CheckButtons();           // 按鍵掃描}}

          這段代碼主要先看全局變量,通過對IO口的賦值(如按鍵、led、無線模塊的端口CE/IRQ等)可以知道電路圖的繪制。

          然后進入main函數(shù),初始化后就是一段按鍵掃描函數(shù),里面根據(jù)定義的按鍵,觸發(fā)后發(fā)送相應(yīng)的數(shù)據(jù),不用太過深究里面對寄存器的配置是完成了什么目的。其中里面的blink函數(shù)是用來判斷TX_DS是否為真而閃爍led燈的,這個標志位為真說明數(shù)據(jù)發(fā)送成功。

          2.然后是接收端

          // Define SPI pins#include #include #define uchar unsigned char#define uint  unsigned int#define ulong unsigned long/***************************************************/#define TX_ADR_WIDTH   5  // 5字節(jié)寬度的發(fā)送/接收地址#define TX_PLOAD_WIDTH 4  // 數(shù)據(jù)通道有效數(shù)據(jù)寬度sbit LED = P2^1;uchar code TX_ADDRESS[TX_ADR_WIDTH] = {0x34,0x43,0x10,0x10,0x01};  // 定義一個靜態(tài)發(fā)送地址uchar RX_BUF[TX_PLOAD_WIDTH];uchar TX_BUF[TX_PLOAD_WIDTH];uchar flag;uchar DATA = 0x01;uchar bdata sta;sbit  RX_DR     = sta^6;sbit  TX_DS     = sta^5;sbit  MAX_RT = sta^4;sbit CE =  P1^5;sbit CSN=  P1^4;sbit SCK=  P1^3;sbit MOSI= P1^2;sbit MISO= P1^1;sbit IRQ = P1^0;// SPI(nRF24L01) commands#define READ_REG    0x00  // Define read command to register#define WRITE_REG   0x20  // Define write command to register#define RD_RX_PLOAD 0x61  // Define RX payload register address#define WR_TX_PLOAD 0xA0  // Define TX payload register address#define FLUSH_TX    0xE1  // Define flush TX register command#define FLUSH_RX    0xE2  // Define flush RX register command#define REUSE_TX_PL 0xE3  // Define reuse TX payload register command#define NOP         0xFF  // Define No Operation, might be used to read status register// SPI(nRF24L01) registers(addresses)#define CONFIG      0x00  // Config register address#define EN_AA       0x01  // Enable Auto Acknowledgment register address#define EN_RXADDR   0x02  // Enabled RX addresses register address#define SETUP_AW    0x03  // Setup address width register address#define SETUP_RETR  0x04  // Setup Auto. Retrans register address#define RF_CH       0x05  // RF channel register address#define RF_SETUP    0x06  // RF setup register address#define STATUS      0x07  // Status register address#define OBSERVE_TX  0x08  // Observe TX register address#define CD          0x09  // Carrier Detect register address#define RX_ADDR_P0  0x0A  // RX address pipe0 register address#define RX_ADDR_P1  0x0B  // RX address pipe1 register address#define RX_ADDR_P2  0x0C  // RX address pipe2 register address#define RX_ADDR_P3  0x0D  // RX address pipe3 register address#define RX_ADDR_P4  0x0E  // RX address pipe4 register address#define RX_ADDR_P5  0x0F  // RX address pipe5 register address#define TX_ADDR     0x10  // TX address register address#define RX_PW_P0    0x11  // RX payload width, pipe0 register address#define RX_PW_P1    0x12  // RX payload width, pipe1 register address#define RX_PW_P2    0x13  // RX payload width, pipe2 register address#define RX_PW_P3    0x14  // RX payload width, pipe3 register address#define RX_PW_P4    0x15  // RX payload width, pipe4 register address#define RX_PW_P5    0x16  // RX payload width, pipe5 register address#define FIFO_STATUS 0x17  // FIFO Status Register register address//--定義SPI要使用的 IO--//sbit MOSIO = P3^4;sbit R_CLK = P3^5;sbit S_CLK = P3^6;void blink(char i);//--全局函數(shù)聲明--///**************************************************函數(shù): init_io()描述:初始化IO/**************************************************/void init_io(void){CE  = 0;        // 待機CSN = 1;        // SPI禁止SCK = 0;        // SPI時鐘置低IRQ = 1;        // 中斷復(fù)位LED = 1;        // 關(guān)閉指示燈}/**************************************************//**************************************************函數(shù):delay_ms()描述:延遲x毫秒/**************************************************/void delay_ms(uchar x){uchar i, j;i = 0;for(i=0; i

          通過main函數(shù)可以知道里面通過對RX_BUF[0]值的判斷做相應(yīng)的LED閃爍,閃爍的次數(shù)不同。

          如果要收發(fā)字符串直接往TX_BUF數(shù)組里面存放大小為TX_PLOAD_WIDTH的字符,在RX_BUF就可以接收到相應(yīng)的數(shù)據(jù)了。

          代碼有刪減過一點,不過通過用KEIL進行的編譯調(diào)試,收發(fā)基本是沒有問題的。。在下親側(cè)過。



          評論


          技術(shù)專區(qū)

          關(guān)閉
          看屁屁www成人影院,亚洲人妻成人图片,亚洲精品成人午夜在线,日韩在线 欧美成人 (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })();