新手上路
- 积分
- 36
- 金钱
- 36
- 注册时间
- 2013-5-7
- 在线时间
- 0 小时
|
楼主 |
发表于 2013-5-7 15:16:08
|
显示全部楼层
我用厂家给我的程序测试我的无线模块,为什么没反应,硬件焊接没有问题。。。程序如下
<br>#include <reg52.h>
<br>#include <intrins.h>
<br>
<br>typedef unsigned char uchar;
<br>typedef unsigned char uint;
<br>//****************************************IO端口定义***************************************
<br>//****************************************NRF24L01端口定义***************************************
<br>sbit MISO =P1^5;
<br>sbit MOSI =P1^1;
<br>sbit SCK =P1^6;
<br>sbit CE =P1^7;
<br>sbit CSN =P1^2;
<br>sbit IRQ =P1^0;
<br>//************************************按键***************************************************
<br>sbit KEY1=P3^4;
<br>//************************************蜂明器***************************************************
<br>sbit LED=P3^5;
<br>//*********************************************NRF24L01*************************************
<br>#define TX_ADR_WIDTH 5 // 5 uints TX address width
<br>#define RX_ADR_WIDTH 5 // 5 uints RX address width
<br>#define TX_PLOAD_WIDTH 20 // 20 uints TX payload
<br>#define RX_PLOAD_WIDTH 20 // 20 uints TX payload
<br>uint const TX_ADDRESS[TX_ADR_WIDTH]= {0x34,0x43,0x10,0x10,0x01}; //本地地址
<br>uint const RX_ADDRESS[RX_ADR_WIDTH]= {0x34,0x43,0x10,0x10,0x01}; //接收地址
<br>//***************************************NRF24L01寄存器指令*******************************************************
<br>#define READ_REG 0x00 // 读寄存器指令
<br>#define WRITE_REG 0x20 // 写寄存器指令
<br>#define RD_RX_PLOAD 0x61 // 读取接收数据指令
<br>#define WR_TX_PLOAD 0xA0 // 写待发数据指令
<br>#define FLUSH_TX 0xE1 // 冲洗发送 FIFO指令
<br>#define FLUSH_RX 0xE2 // 冲洗接收 FIFO指令
<br>#define REUSE_TX_PL 0xE3 // 定义重复装载数据指令
<br>#define NOP 0xFF // 保留
<br>//*************************************SPI(nRF24L01)寄存器地址****************************************************
<br>#define CONFIG 0x00 // 配置收发状态,CRC校验模式以及收发状态响应方式
<br>#define EN_AA 0x01 // 自动应答功能设置
<br>#define EN_RXADDR 0x02 // 可用信道设置
<br>#define SETUP_AW 0x03 // 收发地址宽度设置
<br>#define SETUP_RETR 0x04 // 自动重发功能设置
<br>#define RF_CH 0x05 // 工作频率设置
<br>#define RF_SETUP 0x06 // 发射速率、功耗功能设置
<br>#define STATUS 0x07 // 状态寄存器
<br>#define OBSERVE_TX 0x08 // 发送监测功能
<br>#define CD 0x09 // 地址检测
<br>#define RX_ADDR_P0 0x0A // 频道0接收数据地址
<br>#define RX_ADDR_P1 0x0B // 频道1接收数据地址
<br>#define RX_ADDR_P2 0x0C // 频道2接收数据地址
<br>#define RX_ADDR_P3 0x0D // 频道3接收数据地址
<br>#define RX_ADDR_P4 0x0E // 频道4接收数据地址
<br>#define RX_ADDR_P5 0x0F // 频道5接收数据地址
<br>#define TX_ADDR 0x10 // 发送地址寄存器
<br>#define RX_PW_P0 0x11 // 接收频道0接收数据长度
<br>#define RX_PW_P1 0x12 // 接收频道0接收数据长度
<br>#define RX_PW_P2 0x13 // 接收频道0接收数据长度
<br>#define RX_PW_P3 0x14 // 接收频道0接收数据长度
<br>#define RX_PW_P4 0x15 // 接收频道0接收数据长度
<br>#define RX_PW_P5 0x16 // 接收频道0接收数据长度
<br>#define FIFO_STATUS 0x17 // FIFO栈入栈出状态寄存器设置
<br>//**************************************************************************************
<br>void Delay(unsigned int s);
<br>void inerDelay_us(unsigned char n);
<br>void init_NRF24L01(void);
<br>uint SPI_RW(uint uchar);
<br>uchar SPI_Read(uchar reg);
<br>void SetRX_Mode(void);
<br>uint SPI_RW_Reg(uchar reg, uchar value);
<br>uint SPI_Read_Buf(uchar reg, uchar *pBuf, uchar uchars);
<br>uint SPI_Write_Buf(uchar reg, uchar *pBuf, uchar uchars);
<br>unsigned char nRF24L01_RxPacket(unsigned char* rx_buf);
<br>void nRF24L01_TxPacket(unsigned char * tx_buf);
<br>//*****************************************长延时*****************************************
<br>void Delay(unsigned int s)
<br>{
<br> unsigned int i;
<br> for(i=0; i<s; i++);
<br> for(i=0; i><s; i++);
<br>}
<br>//******************************************************************************************
<br>uint bdata sta; //状态标志
<br>sbit RX_DR =sta^6;
<br>sbit TX_DS =sta^5;
<br>sbit MAX_RT =sta^4;
<br>/******************************************************************************************
<br>/*延时函数
<br>/******************************************************************************************/
<br>void inerDelay_us(unsigned char n)
<br>{
<br> for(;n>0;n--)
<br> _nop_();
<br>}
<br>//****************************************************************************************
<br>/*NRF24L01初始化
<br>//***************************************************************************************/
<br>void init_NRF24L01(void)
<br>{
<br> inerDelay_us(100);
<br> CE=0; // chip enable
<br> CSN=1; // Spi disable
<br> SCK=0; // Spi clock line init high
<br> SPI_Write_Buf(WRITE_REG + TX_ADDR, TX_ADDRESS, TX_ADR_WIDTH); // 写本地地址
<br> SPI_Write_Buf(WRITE_REG + RX_ADDR_P0, RX_ADDRESS, RX_ADR_WIDTH); // 写接收端地址
<br> SPI_RW_Reg(WRITE_REG + EN_AA, 0x01); // 频道0自动 ACK应答允许
<br> SPI_RW_Reg(WRITE_REG + EN_RXADDR, 0x01); // 允许接收地址只有频道0,如果需要多频道可以参考Page21
<br> SPI_RW_Reg(WRITE_REG + RF_CH, 0); // 设置信道工作为2.4GHZ,收发必须一致
<br> SPI_RW_Reg(WRITE_REG + RX_PW_P0, RX_PLOAD_WIDTH); //设置接收数据长度,本次设置为20字节
<br> SPI_RW_Reg(WRITE_REG + RF_SETUP, 0x07); //设置发射速率为1MHZ,发射功率为最大值0dB
<br>}
<br>/****************************************************************************************************
<br>/*函数:uint SPI_RW(uint uchar)
<br>/*功能:NRF24L01的SPI写时序
<br>/****************************************************************************************************/
<br>uint SPI_RW(uint uchar)
<br>{
<br> uint bit_ctr;
<br> for(bit_ctr=0;bit_ctr<8;bit_ctr++) // output 8-bit
<br> {
<br> MOSI = (uchar & 0x80); // output 'uchar', MSB to MOSI
<br> uchar = (uchar << 1); // shift next bit into MSB..
<br> SCK = 1; // Set SCK high..
<br> uchar |= MISO; // capture current MISO bit
<br> SCK = 0; // ..then set SCK low again
<br> }
<br> return(uchar); // return read uchar
<br>}
<br>/****************************************************************************************************
<br>/*函数:uchar SPI_Read(uchar reg)
<br>/*功能:NRF24L01的SPI时序
<br>/****************************************************************************************************/
<br>uchar SPI_Read(uchar reg)
<br>{
<br> uchar reg_val;
<br>
<br> CSN = 0; // CSN low, initialize SPI communication...
<br> SPI_RW(reg); // Select register to read from..
<br> reg_val = SPI_RW(0); // ..then read registervalue
<br> CSN = 1; // CSN high, terminate SPI communication
<br>
<br> return(reg_val); // return register value
<br>}
<br>/****************************************************************************************************/
<br>/*功能:NRF24L01读写寄存器函数
<br>/****************************************************************************************************/
<br>uint SPI_RW_Reg(uchar reg, uchar value)
<br>{
<br> uint status;
<br>
<br> CSN = 0; // CSN low, init SPI transaction
<br> status = SPI_RW(reg); // select register
<br> SPI_RW(value); // ..and write value to it..
<br> CSN = 1; // CSN high again
<br>
<br> return(status); // return nRF24L01 status uchar
<br>}
<br>/****************************************************************************************************/
<br>/*函数:uint SPI_Read_Buf(uchar reg, uchar *pBuf, uchar uchars)
<br>/*功能: 用于读数据,reg:为寄存器地址,pBuf:为待读出数据地址,uchars:读出数据的个数
<br>/****************************************************************************************************/
<br>uint SPI_Read_Buf(uchar reg, uchar *pBuf, uchar uchars)
<br>{
<br> uint status,uchar_ctr;
<br>
<br> CSN = 0; // Set CSN low, init SPI tranaction
<br> status = SPI_RW(reg); // Select register to write to and read status uchar
<br>
<br> for(uchar_ctr=0;uchar_ctr<uchars;uchar_ctr++)
<br> pBuf[uchar_ctr] = SPI_RW(0); //
<br>
<br> CSN = 1;
<br>
<br> return(status); // return nRF24L01 status uchar
<br>}
<br>/*********************************************************************************************************
<br>/*函数:uint SPI_Write_Buf(uchar reg, uchar *pBuf, uchar uchars)
<br>/*功能: 用于写数据:为寄存器地址,pBuf:为待写入数据地址,uchars:写入数据的个数
<br>/*********************************************************************************************************/
<br>uint SPI_Write_Buf(uchar reg, uchar *pBuf, uchar uchars)
<br>{
<br> uint status,uchar_ctr;
<br>
<br> CSN = 0; //SPI使能
<br> status = SPI_RW(reg);
<br> for(uchar_ctr=0; uchar_ctr><uchars; uchar_ctr++) //
<br> SPI_RW(*pBuf++);
<br> CSN = 1; //关闭SPI
<br> return(status); //
<br>}
<br>/****************************************************************************************************/
<br>/*函数:void SetRX_Mode(void)
<br>/*功能:数据接收配置
<br>/****************************************************************************************************/
<br>void SetRX_Mode(void)
<br>{
<br> CE=0;
<br> SPI_RW_Reg(WRITE_REG + CONFIG, 0x0f); // IRQ收发完成中断响应,16位CRC ,主接收
<br> CE = 1;
<br> inerDelay_us(130);
<br>}
<br>/******************************************************************************************************/
<br>/*函数:unsigned char nRF24L01_RxPacket(unsigned char* rx_buf)
<br>/*功能:数据读取后放如rx_buf接收缓冲区中
<br>/******************************************************************************************************/
<br>unsigned char nRF24L01_RxPacket(unsigned char* rx_buf)
<br>{
<br> unsigned char revale=0;
<br> sta=SPI_Read(STATUS); // 读取状态寄存其来判断数据接收状况
<br> if(RX_DR) // 判断是否接收到数据
<br> {
<br> CE = 0; //SPI使能
<br> SPI_Read_Buf(RD_RX_PLOAD,rx_buf,TX_PLOAD_WIDTH);// read receive payload from RX_FIFO buffer
<br> revale =1; //读取数据完成标志
<br> }
<br> SPI_RW_Reg(WRITE_REG+STATUS,sta); //接收到数据后RX_DR,TX_DS,MAX_PT都置高为1,通过写1来清楚中断标志
<br> return revale;
<br>}
<br>/***********************************************************************************************************
<br>/*函数:void nRF24L01_TxPacket(unsigned char * tx_buf)
<br>/*功能:发送 tx_buf中数据
<br>/**********************************************************************************************************/
<br>void nRF24L01_TxPacket(unsigned char * tx_buf)
<br>{
<br> CE=0; //StandBy I模式
<br> SPI_Write_Buf(WRITE_REG + RX_ADDR_P0, TX_ADDRESS, TX_ADR_WIDTH); // 装载接收端地址
<br> SPI_Write_Buf(WR_TX_PLOAD, tx_buf, TX_PLOAD_WIDTH); // 装载数据
<br> SPI_RW_Reg(WRITE_REG + CONFIG, 0x0e); // IRQ收发完成中断响应,16位CRC,主发送
<br> CE=1; //置高CE,激发数据发送
<br> inerDelay_us(10);
<br>}
<br>//************************************主函数************************************************************
<br>void main(void)
<br>{
<br> unsigned char tf =0;
<br> unsigned char TxBuf[20]={0}; //
<br> unsigned char RxBuf[20]={0};
<br> init_NRF24L01() ;
<br> TxBuf[1] = 1 ;
<br> TxBuf[2] = 1 ;
<br> nRF24L01_TxPacket(TxBuf); // Transmit Tx buffer data
<br> Delay(6000);
<br> while(1)
<br> {
<br> if(KEY1 ==0 )
<br> {
<br> TxBuf[1] = 1 ;
<br> tf = 1 ; LED=0;
<br> }
<br> if (tf==1)
<br> {
<br> nRF24L01_TxPacket(TxBuf); // Transmit Tx buffer data
<br> TxBuf[1] = 0x00;
<br> TxBuf[2] = 0x00;
<br> tf=0;
<br> Delay(1000);LED=1;
<br> }
<br>//***********************************************************************************************
<br> SetRX_Mode();
<br> nRF24L01_RxPacket(RxBuf);
<br> if( RxBuf[1]==1)
<br> {
<br> LED=0;
<br> }
<br> Delay(1000);LED=1;
<br> RxBuf[1] = 0x00;
<br> RxBuf[2] = 0x00;
<br> }
<br>
<br>}> |
|