|
发表于 2014-3-25 19:02:54
|
显示全部楼层
本帖最后由 windingway 于 2014-3-25 19:04 编辑
我移植的程序已经初步测试好了,你的程序我就不再移植了,看了看基本上是一个模式.这种事干一次就好.
我的测试代码很简单,发送端发送4个数,如果接收端收到,并向发送端发出响应,那么发送端把这4个数加1后,再次发送,如此循环..
发送端和接收端只有main.c文件不同,其他文件一致
hardware.h保存硬件连线配置
NRF24L01.c和NRF24L01.h是无线模块的功能函数
main.c为发送端和接收端的测试.
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
hardware.h
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- #ifndef HARDWARE_H
- #define HARDWARE_H
- #include <htc.h>
- #define _XTAL_FREQ 4000000
- #define INPUT 1
- #define OUTPUT 0
- #define HIGH 1
- #define LOW 0
- #define _NRF_CE RA0
- #define _NRF_CSN RA1
- #define _NRF_IRQ RA2
- #define _DIR_NRF_CE TRISA0
- #define _DIR_NRF_CSN TRISA1
- #define _DIR_NRF_IRQ TRISA2
- #define _SPI_SCK RC3
- #define _SPI_MISO RC4
- #define _SPI_MOSI RC5
- #define _DIR_SPI_SCK TRISC3
- #define _DIR_SPI_MISO TRISC4
- #define _DIR_SPI_MOSI TRISC5
- #endif
复制代码
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
NRF24L01.h
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- #ifndef NRF24L01_H
- #define NRF24L01_H
- /*******************************************************/
- #define TX_ADDR_WITDH 5 //发送地址宽度设置为5个字节
- #define RX_ADDR_WITDH 5 //接收地址宽度设置为5个字节
- #define TX_DATA_WITDH 4//发送数据宽度4个字节
- #define RX_DATA_WITDH 4//接收数据宽度4个字节
- /*******************命令寄存器***************************/
- #define R_REGISTER 0x00//读取配置寄存器
- #define W_REGISTER 0x20//写配置寄存器
- #define R_RX_PAYLOAD 0x61//读取RX有效数据
- #define W_TX_PAYLOAD 0xa0//写TX有效数据
- #define FLUSH_TX 0xe1//清除TXFIFO寄存器
- #define FLUSH_RX 0xe2//清除RXFIFO寄存器
- #define REUSE_TX_PL 0xe3//重新使用上一包有效数据
- #define NRF_NOP 0xff//空操作
- /******************寄存器地址****************************/
- #define NRF_CONFIG 0x00//配置寄存器
- #define EN_AA 0x01//使能自动应答
- #define EN_RXADDR 0x02//接收通道使能0-5个通道
- #define SETUP_AW 0x03//设置数据通道地址宽度3-5
- #define SETUP_RETR 0x04//建立自动重发
- #define RF_CH 0x05//射频通道设置
- #define RF_SETUP 0x06//射频寄存器
- #define NRF_STATUS_ADR 0x07//状态寄存器
- #define OBSERVE_TX 0x08//发送检测寄存器
- #define NRF_CD 0x09//载波
- #define RX_ADDR_P0 0x0a//数据通道0接收地址
- #define RX_ADDR_P1 0x0b//数据通道1接收地址
- #define RX_ADDR_P2 0x0c//数据通道2接收地址
- #define RX_ADDR_P3 0x0d//数据通道3接收地址
- #define RX_ADDR_P4 0x0e//数据通道4接收地址
- #define RX_ADDR_P5 0x0f//数据通道5接收地址
- #define TX_ADDR 0x10//发送地址
- #define RX_PW_P0 0x11//P0通道数据宽度设置
- #define RX_PW_P1 0x12//P1通道数据宽度设置
- #define RX_PW_P2 0x13//P2通道数据宽度设置
- #define RX_PW_P3 0x14//P3通道数据宽度设置
- #define RX_PW_P4 0x15//P4通道数据宽度设置
- #define RX_PW_P5 0x16//P5通道数据宽度设置
- #define FIFO_STATUS 0x17//FIFO状态寄存器
- /*STATUS寄存器的位mask*/
- #define bitRX_DR 0b01000000
- #define bitTX_DS 0b00100000
- #define bitMAX_RT 0b00010000
- #define bitPX_P_NO 0b00001110
- #define bit_TX_FULL 0b00000001
- /*初始化 NRF24L01*/
- void Nrf_Init(void);
- /*SPI通信 NRF24L01*/
- /*data为地址或数据*/
- unsigned char Nrf_SPI(unsigned char data);
- /*清空 TX FIFO*/
- void Nrf_Flush_TX(void);
- /*清空 RX FIFO*/
- void Nrf_Flush_RX(void);
- /*从NRF24L01读取一个字节的寄存器信息*/
- /*address为寄存器地址*/
- unsigned char Nrf_Read_Reg(unsigned char address);
- /*向NRF24L01的寄存器写入一个字节*/
- /*address为寄存器地址,data为要写入寄存器的数据*/
- void Nrf_Write_Reg(unsigned char address,unsigned char data);
- /*读取NRF24L01的寄存器,保存在data[]中*/
- /*address为寄存器地址,*data为保存数组地址,length为保存字节数*/
- /*当address为RX FIFO时,可保存NRF24L01收到的数据*/
- void Nrf_Read_RX(unsigned char address,
- unsigned char *data,unsigned char length);
- /*读取NRF24L01的寄存器,保存在data[]中*/
- /*address为寄存器地址,*data为发送数据数组地址,length为发送字节数*/
- /*当address为TX FIFO时,可向FIFO写入数据*/
- void Nrf_Write_TX(unsigned char address,
- unsigned char *data,unsigned char length);
- /*设置NRF24L01为接收模式,并启动发射*/
- /**data为要传输的数据数组地址*/
- void Nrf_Set_Tx(unsigned char *data);
- /*设置NRF24L01为掉电模式*/
- void Nrf_Power_Down(void);
- /*设置NRF24L01为发送模式*/
- void Nrf_Set_Rx(void);
- /*产生IRQ中断后,检查NRF24L01的状态*/
- unsigned char Nrf_Check_ACK(void);
- /*读取NRF24L01接收到的数据*/
- /* *data为保存数组的地址*/
- unsigned char Nrf_Recevie_Data(unsigned char *data);
- unsigned char NRF_STATUS;
- unsigned char RX_ADR0[5] = {0x34,0x43,0x10,0x10,0x01};//pipe0 address
- #endif
复制代码
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
NRF24L01.c
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- #include "NRF24L01.h"
- #include "hardware.h"
- void Nrf_Init(void)
- {
- Nrf_Write_Reg(W_REGISTER+NRF_CONFIG,0x02);//power up
- Nrf_Write_Reg(W_REGISTER+NRF_STATUS_ADR,0xff);//clear interrupt
- Nrf_Flush_TX(); //clear TX FIFO
- Nrf_Flush_RX(); //clear RX FIFO
- }
- /*This function use hardware SPI of PIC16F883*/
- unsigned char Nrf_SPI(unsigned char data)
- {
- unsigned char readback;
- SSPBUF = data;
- while (SSPIF==0) {}
- while(BF==0){}
- readback = SSPBUF;
- return readback;
- }
- void Nrf_Flush_TX(void)
- {
- _NRF_CSN= 0; //enable SPI
- NRF_STATUS = Nrf_SPI(FLUSH_TX); //write Reg address
- //first write,NRF24L01 will return STATUS register
- _NRF_CSN= 1; //disable SPI
- }
- void Nrf_Flush_RX(void)
- {
- _NRF_CSN= 0; //enable SPI
- NRF_STATUS = Nrf_SPI(FLUSH_RX); //write Reg address
- //first write,NRF24L01 will return STATUS register
- _NRF_CSN= 1; //disable SPI
- }
- unsigned char Nrf_Read_Reg(unsigned char address)
- {
- unsigned char readback;
- _NRF_CSN= 0; //enable SPI
- NRF_STATUS = Nrf_SPI(address); //write Reg address
- //first write,NRF24L01 will return STATUS register
- readback = Nrf_SPI(0); //read Reg
- _NRF_CSN= 1; //disable SPI
- return readback;
- }
- void Nrf_Write_Reg(unsigned char address,unsigned char data)
- {
- _NRF_CSN= 0; //enable SPI
- NRF_STATUS = Nrf_SPI(address); //write Reg address
- //first write,NRF24L01 will return STATUS register
- Nrf_SPI(data);
- _NRF_CSN= 1; //disable SPI
- }
- void Nrf_Read_RX(unsigned char address,
- unsigned char *data,unsigned char length)
- {
- unsigned char i;
- _NRF_CSN= 0; //enable SPI
- NRF_STATUS = Nrf_SPI(address); //write Reg address
- for(i=0;i<length;i++) //read registers, and save
- {
- *(data+i) = Nrf_SPI(0);
- }
- _NRF_CSN= 1; //disable SPI
- }
- void Nrf_Write_TX(unsigned char address,
- unsigned char *data,unsigned char length)
- {
- unsigned char i;
- _NRF_CSN= 0; //enable SPI
- NRF_STATUS = Nrf_SPI(address); //write Reg address
- for(i=0;i<length;i++) //write
- {
- Nrf_SPI(*(data+i));
- }
- _NRF_CSN= 1; //disable SPI
- }
- void Nrf_Set_Tx(unsigned char *data)
- {
- _NRF_CE= 0;
- Nrf_Write_TX(W_REGISTER+TX_ADDR,RX_ADR0,TX_ADDR_WITDH);//Pipe0
- Nrf_Write_TX(W_REGISTER+RX_ADDR_P0,RX_ADR0,TX_ADDR_WITDH); //Pipe0
- Nrf_Write_TX(W_TX_PAYLOAD,data,TX_DATA_WITDH);
- Nrf_Write_Reg(W_REGISTER+EN_AA,0x01); //enable auto ACK of Pipe0
- Nrf_Write_Reg(W_REGISTER+EN_RXADDR,0x01); //enable Piep0
- Nrf_Write_Reg(W_REGISTER+SETUP_RETR,0x0a); //retry 10 times,250us delay
- Nrf_Write_Reg(W_REGISTER+RF_CH,0x40); //use channel 0x40
- Nrf_Write_Reg(W_REGISTER+RF_SETUP,0x0f); //data rate 2Mbps
- Nrf_Write_Reg(W_REGISTER+NRF_CONFIG,0x0e); //enable CRC,2bytes
- _NRF_CE= 1;
- __delay_us(20); //wait to transmit
- // _NRF_CE= 0;
- }
- void Nrf_Power_Down(void)
- {
- _NRF_CE= 0;
- Nrf_Write_Reg(W_REGISTER+NRF_CONFIG,0x0d); //PRX,PowerDown,16bitCRC
- _NRF_CE= 1;
- __delay_us(20);
- }
- void Nrf_Set_Rx(void)
- {
- _NRF_CE= 0;
- Nrf_Write_TX(W_REGISTER+RX_ADDR_P0,RX_ADR0,TX_ADDR_WITDH); //Pipe0
- Nrf_Write_Reg(W_REGISTER+EN_AA,0x01); //enable pipe0 auto ACK
- Nrf_Write_Reg(W_REGISTER+EN_RXADDR,0x01); //enable pipe0
- Nrf_Write_Reg(W_REGISTER+RF_CH,0x40); //use channel 0x40
- Nrf_Write_Reg(W_REGISTER+RX_PW_P0,TX_DATA_WITDH); //pipe0 payload width
- Nrf_Write_Reg(W_REGISTER+RF_SETUP,0x0f); //data rate 2Mbps
- Nrf_Write_Reg(W_REGISTER+NRF_CONFIG,0x0f); //enable CRC,2bytes
- _NRF_CE= 1; //Start active RX mode
- __delay_us(150);
- }
- unsigned char Nrf_Check_ACK(void)
- {
- NRF_STATUS = Nrf_Read_Reg(R_REGISTER+NRF_STATUS_ADR);
- if(NRF_STATUS&bitTX_DS) //PTX received ACK from PRX
- {
- Nrf_Flush_TX();
- Nrf_Write_Reg(W_REGISTER+NRF_STATUS_ADR,bitTX_DS); //clear TX_DS bit
- return 1;
- }
- else if(NRF_STATUS&bitMAX_RT) //PTX re-transmit over maximum number
- {
- Nrf_Flush_TX();
- Nrf_Write_Reg(W_REGISTER+NRF_STATUS_ADR,bitMAX_RT); //clear MAX_RT bit
- return 2;
- }
- else if(NRF_STATUS&bitRX_DR) //PRX received new data
- {
- return 3;
- }
- else
- return 0;
-
- }
- unsigned char Nrf_Recevie_Data(unsigned char *data)
- {
- unsigned char rx_address,stat;
- NRF_STATUS = Nrf_Read_Reg(R_REGISTER+NRF_STATUS_ADR); //read status register
- if(NRF_STATUS&bitRX_DR) //PRX received new data
- {
- rx_address = NRF_STATUS & bitPX_P_NO; //pipe number
- switch(rx_address)
- {
- case 0x00: Nrf_Read_RX(R_RX_PAYLOAD,data,RX_DATA_WITDH);//pipe0
- stat = 1;
- break;
- default: stat = 0;
- break;
- }
- Nrf_Write_Reg(W_REGISTER+NRF_STATUS_ADR,bitRX_DR); //clear RX_DR bit
- _NRF_CSN= 0;
- Nrf_SPI(FLUSH_RX); //flush RX FIFO
- _NRF_CSN= 1;
- }
- return stat;
- }
复制代码
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
发射端 main.c
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- #include "hardware.h"
- #include "NRF24L01.h"
- __CONFIG(XT & WDTDIS & LVPDIS & DEBUGEN);
- extern unsigned char NRF_STATUS;
- unsigned char Data_TX[4]={4,3,2,1};
- unsigned char Data_RX[4];
- unsigned char Trans_Status;
- /*initialize PIC16F883*/
- void Init_Sys(void)
- {
- ANSEL = ANSEL & 0b11111000; //RA0,RA1,RA2 as digital I/O
- _DIR_NRF_CE= OUTPUT;
- _DIR_NRF_CSN= OUTPUT;
- _DIR_NRF_IRQ= INPUT;
-
- _NRF_CE= 0;
- _NRF_CSN= 1;
-
- _DIR_SPI_SCK= OUTPUT;
- _DIR_SPI_MISO= INPUT;
- _DIR_SPI_MOSI= OUTPUT;
-
- //set up MSSP mode as SPI master
- CKP= 0;
- CKE= 1;
- SMP= 0;
- SSPM3= 0;SSPM2= 0;SSPM1= 0;SSPM0= 0;
- SSPEN= 1;
- }
- void main(void)
- {
- unsigned char i;
- Nrf_Init(); //initialize NRF24L01
- NRF_STATUS = Nrf_Read_Reg(R_REGISTER+NRF_STATUS_ADR); //get status of NRF24L01,for debug..
- while(1)
- {
- Nrf_Set_Tx(Data_TX); //set NRF24L01 as transmitter,then transmit Data_TX[]
- while(_NRF_IRQ){} //wait for ACK from receiver
- Trans_Status = Nrf_Check_ACK(); //get transmit status
- if(Trans_Status==2) //no ACK,for debug
- {
- Trans_Status = 2;
- NRF_STATUS = Nrf_Read_Reg(R_REGISTER+NRF_STATUS_ADR);
- Nrf_Write_Reg(W_REGISTER+NRF_STATUS_ADR,0xff);//clear interrupt
- }
- if(Trans_Status==1) //received ACK
- {
- Data_TX[0]=i;
- Data_TX[1]=i;
- Data_TX[2]=i;
- Data_TX[3]=i;
- i++;
- __delay_ms(10);
- }
- }
- }
复制代码
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
接收端 main.c
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- #include "hardware.h"
- #include "NRF24L01.h"
- __CONFIG(XT & WDTDIS & LVPDIS & DEBUGEN);
- extern unsigned char NRF_STATUS;
- unsigned char Data_TX[4]={1,2,3,4};
- unsigned char Data_RX[4]={0,0,0,0};
- unsigned char Trans_Status;
- /*initialize PIC16F883*/
- void Init_Sys(void)
- {
- ANSEL = ANSEL & 0b11111000; //RA0,RA1,RA2 as digital I/O
- _DIR_NRF_CE= OUTPUT;
- _DIR_NRF_CSN= OUTPUT;
- _DIR_NRF_IRQ= INPUT;
-
- _NRF_CE= 0;
- _NRF_CSN= 1;
-
- _DIR_SPI_SCK= OUTPUT;
- _DIR_SPI_MISO= INPUT;
- _DIR_SPI_MOSI= OUTPUT;
-
- //set up MSSP mode as SPI master
- CKP= 0;
- CKE= 1;
- SMP= 0;
- SSPM3= 0;SSPM2= 0;SSPM1= 0;SSPM0= 0;
- SSPEN= 1;
- }
- void main(void)
- {
- Nrf_Init(); //initialize NRF24L01
- Nrf_Set_Rx(); //set NRF24L01 as receiver
- while(1)
- {
- while(_NRF_IRQ){NRF_STATUS = Nrf_Read_Reg(R_REGISTER+NRF_STATUS_ADR);/*for debug*/} //wait for data from transimitter
- Trans_Status = Nrf_Check_ACK(); ////get transmit status
- if(Trans_Status==3) //received valid data
- {
- Trans_Status = 3; //for debug
- Trans_Status = Nrf_Recevie_Data(Data_RX); //save data in Data_RX[]
- NRF_STATUS = Nrf_Read_Reg(R_REGISTER+NRF_STATUS_ADR); //frodebug
- _NRF_CE= 1; //re-enable receiver
- }
- }
- }
复制代码
还有一些逻辑分析仪抓的图,写注释比较麻烦就先不上传了
|
|