nRF24L01發(fā)送程序和顯示程序
1.nRF24L01.h文件
#include
typedef unsigned char uchar;
typedef unsigned char uint;
//****************************************IO端口定義***************************************
//sbitCE =P1^4;//3
//sbitCSN=P1^0;//4
//sbitSCK =P1^3;//5
//sbitMOSI=P1^1;//6
//sbitMISO=P3^3;//7
//sbitIRQ=P1^2;//8
sbitCE =P2^1;//3
sbitCSN=P2^2;//4
sbitSCK =P2^3;//5
sbitMOSI=P2^4;//6
sbitMISO=P2^5;//7
sbitIRQ=P2^6;//8
//sbitCE =P1^0;//3
//sbitCSN=P1^1;//4
//sbitSCK =P1^2;//5
//sbitMOSI=P1^3;//6
//sbitMISO=P1^4;//7
//sbitIRQ=P1^5;//8
//***********************************數(shù)碼管0-9編碼*******************************************
//uchar Disp_Tab[10]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90}; //0~~9共陽段碼
char TxBuf[]=
{
0x01,0x02,0x03,0x4,0x05,0x06,0x07,0x08,
0x09,0x10,0x11,0x12,0x13,0x14,0x15,0x16,
0x17,0x18,0x19,0x20};
//*********************************************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 payload
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寄存器指令*******************************************************
#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)方式Config' register address
#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)測功能
#define CD 0x09 // 地址檢測
#define RX_ADDR_P0 0x0A // 頻道0接收數(shù)據(jù)地址'RX address pipe0' register address
#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ù)長度 'RX payload width, pipe0' register address
#define RX_PW_P1 0x12 // 接收頻道1接收數(shù)據(jù)長度'RX payload width, pipe1' register address
#define RX_PW_P2 0x13 // 接收頻道2接收數(shù)據(jù)長度 'RX payload width, pipe2' register address
#define RX_PW_P3 0x14 // 接收頻道3接收數(shù)據(jù)長度 'RX payload width, pipe3' register address
#define RX_PW_P4 0x15 // 接收頻道4接收數(shù)據(jù)長度 'RX payload width, pipe4' register address
#define RX_PW_P5 0x16 // 接收頻道5接收數(shù)據(jù)長度 'RX payload width, pipe5' register address
#define FIFO_STATUS 0x17 // FIFO棧入棧出狀態(tài)寄存器設(shè)置'FIFO Status Register' register address
//**************************************************************************************
void Delay(unsigned int s);
void inerDelay_us(unsigned char n);
void init_NRF24L01(void);
uint SPI_RW(uint byte);
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
for(i=0; i
}
//******************************************************************************************
uintbdata sta; //狀態(tài)標(biāo)志
sbitRX_DR=sta^6;
sbitTX_DS=sta^5;
sbitMAX_RT=sta^4;
/******************************************************************************************
/*延時(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è)置為32字節(jié)
SPI_RW_Reg(WRITE_REG + RF_SETUP, 0x07); //設(shè)置發(fā)射速率為2MHZ,發(fā)射功率為最大值0dB TX_PWR:0dBm, Datarate:2Mbps, LNA:HCURR
SPI_RW_Reg(WRITE_REG + CONFIG, 0x0e); // IRQ收發(fā)完成中斷響應(yīng),16位CRC,主發(fā)送
}
/****************************************************************************************************
/*函數(shù):uint SPI_RW(uint uchar)
/*功能:NRF24L01的SPI寫時(shí)序Writes one byte to nRF24L01, and return the byte read from nRF24L01
during write, according to SPI protocol
最基本的函數(shù),完成GPIO 模擬SPI的功能。將輸出字節(jié)(MOSI)從MSB 循環(huán)輸出,同時(shí)將輸入字節(jié)(MISO )
從LSB 循環(huán)移入。上升沿讀入,下降沿輸出。(從SCK 被初始化為低電平可以判斷出)。
/****************************************************************************************************/
uint SPI_RW(uint byte)
{
uint bit_ctr;
for(bit_ctr=0;bit_ctr<8;bit_ctr++) // output 8-bit
{
MOSI = (byte & 0x80); // output 'byte', MSB to MOSI
byte = (byte << 1); // shift next bit into MSB..
SCK = 1; // Set SCK high..
byte |= MISO; // capture current MISO bit
SCK = 0; // ..then set SCK low again
}
return(byte); // return read byte
}
/****************************************************************************************************
/*函數(shù):uchar SPI_Read(uchar reg)
/*功能:NRF24L01的SPI時(shí)序。讀取寄存器值的函數(shù):基本思路就是通過READ_REG 命令(也就是0x00+寄存器地址),
把寄存器中的值讀出來。對于函數(shù)來說也就是把reg 寄存器的值讀到reg_val 中去。
/****************************************************************************************************/
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ù)
寄存器訪問函數(shù):用來設(shè)置24L01 的寄存器的值。基本思路就是通過WRITE_REG 命令(也就是0x20+寄存器地址)把要
設(shè)定的值寫到相應(yīng)的寄存器地址里面去,并讀取返回值。對于函數(shù)來說也就是把value 值寫到reg 寄存器中。
需要注意的是,訪問NRF24L01 之前首先要enable 芯片(CSN=0;),訪問完了以后再disable芯片(CSN=1;)。
/****************************************************************************************************/
uint SPI_RW_Reg(uchar reg, uchar value)
{
uint status;
CSN = 0; // CSN low, init SPI transaction
status = SPI_RW(reg); // select register
//check_status(status);//SPI_Read_Buf(WRITE_REG+CONFIG,test,1));
SPI_RW(value); // ..and write value to it..
//******************test************************
//{
//int i=0;
////test0 = SPI_Read_Buf(WRITE_REG +FIFO_STATUS,test,1);
//i = status/16;
//if(i<10){LCD_write_char(0,1,i+'0');}
//else{LCD_write_char(0,1,i-10+'A');}
//i = status%16;
//if(i<10){LCD_write_char(1,1,i+'0');}
//else{LCD_write_char(1,1,i-10+'A');}
//while(1);
//}
//******************end************************
CSN = 1; // CSN high again
return(status); // return nRF24L01 status byte
}
/****************************************************************************************************/
/*函數(shù):uint SPI_Read_Buf(uchar reg, uchar *pBuf, uchar uchars)
/*功能: 用于讀數(shù)據(jù),reg:為寄存器地址,pBuf:為待讀出數(shù)據(jù)地址,uchars:讀出數(shù)據(jù)的個(gè)數(shù)
接收緩沖區(qū)訪問函數(shù):主要用來在接收時(shí)讀取FIFO緩沖區(qū)中的值。基本思路就是通過READ_REG 命令把數(shù)據(jù)從接
收 FIFO(RD_RX_PLOAD)中讀出并存到數(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 pBuf[uchar_ctr] = SPI_RW(0); // Perform SPI_RW to read byte from nRF24L01 CSN = 1; // Set CSN high again 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ù) 發(fā)射緩沖區(qū)訪問函數(shù):主要用來把數(shù)組里的數(shù)放到發(fā)射 FIFO 緩沖區(qū)中?;舅悸肪褪峭ㄟ^WRITE_REG 命令把 數(shù)據(jù)存到發(fā)射 FIFO(WR_TX_PLOAD)中去。 /*********************************************************************************************************/ uint SPI_Write_Buf(uchar reg, uchar *pBuf, uchar uchars) { uint status,uchar_ctr; CSN = 0; //SPI使能 // Set CSN low, init SPI tranaction status = SPI_RW(reg); // Select register to write to and read status byte for(uchar_ctr=0; uchar_ctr SPI_RW(*pBuf++); /* { Display(*pBuf);//顯示程序 Delay(400) ; SPI_RW(*pBuf); Delay(2000) ; pBuf++; }*/ CSN = 1; // // Set CSN high again ,關(guān)閉SPI return(status); //// return nRF24L01 status byte } /****************************************************************************************************/ /*函數(shù):void SetRX_Mode(void) /*功能:數(shù)據(jù)接收配置 This function initializes one nRF24L01 device to RX Mode, set RX address, writes RX payload width, select RF channel, datarate & LNA HCURR. After init, CE is toggled high, which means that this device is now ready to receive a datapacket. /****************************************************************************************************/ 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) //接收函數(shù),返回1表示有數(shù)據(jù)收到,否則沒有數(shù)據(jù)接受到 { unsigned char revale=0; sta=SPI_Read(STATUS);// 讀取狀態(tài)寄存其來判斷數(shù)據(jù)接收狀況// read register STATUS's value if(RX_DR)// 判斷是否接收到數(shù)據(jù)// ifreceive data ready (RX_DR) interrupt { 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)志. //clear RX_DR or TX_DS or MAX_RT interrupt flag return revale; } /*********************************************************************************************************** /*函數(shù):void nRF24L01_TxPacket(unsigned char * tx_buf) /*功能:發(fā)送 tx_buf中數(shù)據(jù) /**********************************************************************************************************/ void nRF24L01_TxPacket(unsigned char * tx_buf) { int dat; 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ù)// Writes data to TX payload SPI_RW_Reg(WRITE_REG + CONFIG, 0x0e); // IRQ收發(fā)完成中斷響應(yīng),16位CRC,主接收???????????發(fā)送中被屏蔽,而接收中沒有屏蔽! // Set PWR_UP bit, enable CRC(2 bytes) & Prim:TX. MAX_RT & TX_DS enabled.. CE=1;//置高CE,激發(fā)數(shù)據(jù)發(fā)送 inerDelay_us(10); //CE=0; } 2、1602.c文件 #include sbit RS = P3^4; //定義端口 sbit RW = P3^5; sbit EN = P3^6; #define RS_CLR RS=0 #define RS_SET RS=1 #define RW_CLR RW=0 #define RW_SET RW=1 #define EN_CLR EN=0 #define EN_SET EN=1 /******************************************************************/ /* 微秒延時(shí)函數(shù) */ /******************************************************************/ void delay_us(unsigned int n) //延時(shí) 如果需要高精度延時(shí) 請嵌入?yún)R編 { if (n == 0) { return ; } while (--n); } /******************************************************************/ /* 毫秒函數(shù)聲明 */ /******************************************************************/ void delay_ms(unsigned char i) { unsigned char a, b; for (a = 1; a < i; a++) { for (b = 1; b; b++) { ; } } } /******************************************************************/ /* 寫入命令函數(shù) */ /******************************************************************/ void LCD_write_com(unsigned char com) { RS_CLR; RW_CLR; EN_SET; P0 = com; delay_us(5); EN_CLR; } /******************************************************************/ /* 寫入數(shù)據(jù)函數(shù) */ /******************************************************************/ void LCD_write_Data(unsigned char Data) { RS_SET; RW_CLR; EN_SET; P0 = Data; delay_us(5); EN_CLR; } /******************************************************************/ /* 清屏函數(shù) */ /******************************************************************/ void LCD_clear(void) { LCD_write_com(0x01); delay_ms(5);} /******************************************************************/ /* 寫入字符串函數(shù) */ /******************************************************************/ void LCD_write_str(unsigned char x,unsigned char y,unsigned char *s) { if (y == 0) { LCD_write_com(0x80 + x); } else { LCD_write_com(0xC0 + x); } while (*s) { LCD_write_Data( *s); s ++; } } /******************************************************************/ /* 寫入字節(jié)函數(shù) */ /******************************************************************/ void LCD_write_char(unsigned char x,unsigned char y,unsigned char Data) { if (y == 0) { LCD_write_com(0x80 + x); } else { LCD_write_com(0xC0 + x); } LCD_write_Data( Data); } /******************************************************************/ /* 初始化函數(shù) */ /******************************************************************/ void LCD_init(void) { LCD_write_com(0x38); /*顯示模式設(shè)置*/ delay_ms(5); LCD_write_com(0x38); delay_ms(5); LCD_write_com(0x38); delay_ms(5); LCD_write_com(0x38); LCD_write_com(0x08); /*顯示關(guān)閉*/ LCD_write_com(0x01); /*顯示清屏*/ LCD_write_com(0x06); /*顯示光標(biāo)移動(dòng)設(shè)置*/ delay_ms(5); LCD_write_com(0x0C); /*顯示開及光標(biāo)設(shè)置*/ } 3、test.h文件 char test[]={0x00,0x00}; //unsigned char test0=0xff; //***************** void check_status(unsigned char test0) { int i=0; //test0 = SPI_Read_Buf(WRITE_REG +FIFO_STATUS,test,1); i = test0/16; if(i<10){LCD_write_char(0,1,i+'0');} else{LCD_write_char(0,1,i-10+'A');} i = test0%16; if(i<10){LCD_write_char(1,1,i+'0');} else{LCD_write_char(1,1,i-10+'A');} } //***************** 4、send.c文件 #include #include"1602.h" #include"test.h" #include"nrf24l01.h" //************************************主函數(shù)************************************************************ void main(void) { LCD_init(); init_NRF24L01() ; nRF24L01_TxPacket(TxBuf);// Transmit Tx buffer data Delay(6000); LCD_write_str(0,0,"TX starting..."); while(1) { nRF24L01_TxPacket(TxBuf);// Transmit Tx buffer data //****************************************** //while(1) //{ //SPI_Read_Buf(READ_REG+FIFO_STATUS,test,1);// //check_status(test[0]); //} //****************************************** Delay(6000); SPI_RW_Reg(WRITE_REG+STATUS,0XFF); LCD_write_char(0,1,TxBuf[0]+0x30); LCD_write_char(1,1,TxBuf[1]+0x30); LCD_write_char(2,1,TxBuf[2]+0x30); LCD_write_char(3,1,TxBuf[3]+0x30); LCD_write_char(4,1,TxBuf[4]+0x30); TxBuf[4]=0x00; TxBuf[3]=0x01;TxBuf[2]=0x02;TxBuf[1]=0x03;TxBuf[0]=0x04; nRF24L01_TxPacket(TxBuf);// Transmit Tx buffer data //***************** //while(1) //{ //SPI_Read_Buf(READ_REG+FIFO_STATUS,test,1);// //check_status(test[0]); //} //******************** Delay(6000); SPI_RW_Reg(WRITE_REG+STATUS,0XFF); LCD_write_char(0,1,TxBuf[0]+0x30); LCD_write_char(1,1,TxBuf[1]+0x30); LCD_write_char(2,1,TxBuf[2]+0x30); LCD_write_char(3,1,TxBuf[3]+0x30); LCD_write_char(4,1,TxBuf[4]+0x30); TxBuf[4]=0x04; TxBuf[3]=0x03;TxBuf[2]=0x02;TxBuf[1]=0x01;TxBuf[0]=0x00; } }