NRF2401詳細(xì)注釋編程程序.doc_第1頁
NRF2401詳細(xì)注釋編程程序.doc_第2頁
NRF2401詳細(xì)注釋編程程序.doc_第3頁
NRF2401詳細(xì)注釋編程程序.doc_第4頁
NRF2401詳細(xì)注釋編程程序.doc_第5頁
已閱讀5頁,還剩6頁未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

#include #include typedef unsigned char uchar;typedef unsigned char uint;/*NRF24L01端口定義*sbit MISO =P15;sbit MOSI =P14;sbit SCK =P13;sbit CE =P11;sbit CSN =P12;sbit IRQ =P16;/*按鍵*sbit KEY1=P26;sbit KEY2=P25;/*數(shù)碼管位選*sbit led1=P24;sbit led2=P35;/*NRF24L01*#define TX_ADR_WIDTH 5 / 5 uints TX address width#define RX_ADR_WIDTH 5 / 5 uints RX address width#define TX_PLOAD_WIDTH 20 / 20 uints TX payload#define RX_PLOAD_WIDTH 20 / 20 uints TX payloaduint const TX_ADDRESSTX_ADR_WIDTH= 0x34,0x43,0x10,0x10,0x01; /本地地址uint const RX_ADDRESSRX_ADR_WIDTH= 0x34,0x43,0x10,0x10,0x01; /接收地址/*NRF24L01寄存器指令*#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)寄存器地址*#define CONFIG 0x00 / 配置收發(fā)狀態(tài),CRC校驗(yàn)?zāi)J揭约笆瞻l(fā)狀態(tài)響應(yīng)方式#define EN_AA 0x01 / 自動應(yīng)答功能設(shè)置#define EN_RXADDR 0x02 / 可用信道設(shè)置#define SETUP_AW 0x03 / 收發(fā)地址寬度設(shè)置#define SETUP_RETR 0x04 / 自動重發(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)測功能#define CD 0x09 / 地址檢測 #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è)置/*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; is; i+); for(i=0; i0;n-) _nop_();/*/*NRF24L01初始化/*/void init_NRF24L01(void) inerDelay_us(100); CE=0; / chip enable CSN=1; / Spi disable SCK=0; / 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自動 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è)置為32字節(jié) SPI_RW_Reg(WRITE_REG + RF_SETUP, 0x07); /設(shè)置發(fā)射速率為1MHZ,發(fā)射功率為最大值0dB/*/*函數(shù):uint SPI_RW(uint uchar)/*功能:NRF24L01的SPI寫時(shí)序/*/uint SPI_RW(uint uchar) uint bit_ctr; for(bit_ctr=0;bit_ctr8;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í)序/*/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_ctruchars;uchar_ctr+) pBufuchar_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_ctruchars; 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 ,主接收 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ù)*void main(void) unsigned char tf =0; unsigned char TxBuf20=0; / unsigned char RxBuf20=0; init_NRF

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論