nRF24L01 发送数据完毕后,状态寄存器读出来的数据都是0xff,为什么?
我没有资料在 旁边,但我去年刚完成了一个nRF24L01
无线收发的项目,客户已验收
记得一点是这样写的,希望有帮助
#define tx sta^5 //位定义,状态寄存器的第5位
#define max sta^4
wtb(0xa0,mode);//向TX FIFO 写入一个值mode
ce=1;
for(n=43;n>0;n--);//进入接受模式130us,然后下面的是校验说明书上的STATUS内容
sta=wob(0xff); //读状态,wob函数 是write one byte,自己编的程序向24L01寄存器写1个命令
if(tx) //若状态寄存器的第5位 被置1
{
wtb(0x27,0xff); //些入1 清除标志位
wob(0xe1); //清除TX fifo
sta=wob(0xff); //再读一次STATUS,看tx是否被成功清零,成功清0就闪灯,tmp是一个临时 char型变量,前面已定义,
if(tx==0)
{
while(tmp--)
{led1=1;delay(20000);led1=0;delay(20000);} //灯闪烁的次数是为了测试程序是否成功用
}
}
nRF24L01在中断标志置位后,必须写1清零,不然可能死机
发送接收成功,硬件会自动清FIFO,不必手动清0 FIFO,希望有所帮助,
if(max) //达到最大重发次数,清中断标志,清FIFO
{
wtb(0x27,0xff);
wob(0xe1);
sta=wob(0xff);
}
做无线数据传输用的芯片除了NRF24L01还有什么?它们各有什么优缺点,选择NRF24L01有什么好处?
一、无线传输芯片
我接触过两款,第一款自己用过,第二款本打算采用,考虑成本就没用得上了。
1、NRF24L01价格便宜,对于短距离比较合适,编程不难,功耗低最大只60mw,在空中传输的速率达到1Mbps。因为它每次最大只能传输32有效字节宽度,所以不适合数据量较大的场合。再者,
2、PTR2000采用抗干扰能力较强的FSK调制/解调方式,其工作频率稳定可靠、外围元件少、功耗极低且便于设计生产,这些优异特性使得PTR200非常适合于便携及手持产品的设计。另外,由于它采用了低发射功率、高灵敏度设计,因而可满足无线管制的要求且无需使用许可证,是目前低功率无线数传的理想选择。
二、温度传感器
温度传感器用过AD590,DS18B20
AD22100KT 带信号调理比率输出型温度传感器
AD22105AR 可编程温控开关电阻可编程温度控制器 SOIC
AD590JH —55℃~150℃测温范围温度传感器 TO-52
AD590KH —55℃~150℃测温范围温度传感器 TO-52
AD592AN 低价格,精密单片温度传感器 TO-92
AD592BN 低价格,精密单片温度传感器 TO-92
AD7416AR 片内带D/A 数字输出温度传感器 LM35升级品可8片级联(工业级)SOIC
ADXL105JQC ±1g-±5g带温度补偿加速度传感器(民用级)QC-14
ADXL202AQC ±2g双路加速度传感器(工业级)QC-14
DS18B20 集成数字温度传感器,价格便宜,电路简单,编程容易,不需要模数转换,直接接单片机。精度没有前面的高。
三、温度采集方案
如果比较简单,对温度精度要求不是很高,可以用DS18B20。AD590的话26元左右一个,有点贵,你的模数转换芯片转换位数还不能太小,要不然不能体现AD590的优势,这也意味着成本的增加。
求一个nrf24l01模块简易收发程序,越简单越好,只传输一个数就可以了,单向传输 恳求
#include
#include
#include "api.h"
/*
*This file is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTYT;
*
*uart:9600BPS
*
*/
/***************************************************/
#define uchar unsigned char
#define TX_ADR_WIDTH 5 // 5 bytes TX(RX) address width
#define TX_PLOAD_WIDTH 20 // 20 bytes TX payload
uchar const TX_ADDRESS[TX_ADR_WIDTH] = {0x34,0x43,0x10,0x10,0x01}; // Define a static TX address
uchar rx_buf[TX_PLOAD_WIDTH];
uchar tx_buf[TX_PLOAD_WIDTH];
uchar flag;
uchar rx_com_buffer[10];
uchar tx_com_buffer[10];
uchar i;
uchar accept_flag;
/**************************************************/
sbit CE = P1^6;
sbit CSN= P1^7;
sbit SCK= P1^4;
sbit MOSI= P1^5;
sbit MISO= P1^3;
sbit IRQ = P1^2;
sbit LED1= P3^7;
/**************************************************/
uchar bdata sta;
sbit RX_DR =sta^6;
sbit TX_DS =sta^5;
sbit MAX_RT =sta^4;
/**************************************************/
/**************************************************
Function: init_io();
Description:
flash led one time,chip enable(ready to TX or RX Mode),
Spi disable,Spi clock line init high
/**************************************************/
#define KEY 0xaa
void init_io(void)
{
CE=0; // chip enable
CSN=1; // Spi disable
SCK=0; // Spi clock line init high
}
/**************************************************/
/**************************************************
Function: Inituart();
Description:
set uart working mode
/**************************************************/
void Inituart(void)
{
SM0=0; //设置串行口工作方式为方式1。SM0=0,SM1=0为工作方式0.依次类推
SM1=1;
REN=1; //串行口接收允许。REN=0时,禁止接收。
TMOD=0x20; //定时器1工作方式2.
TH1=0xfd; //相应波特率设初值计算方法。 初值X=(256-11059200/(12*32*9600))
TL1=0xfd; //9600为你要设置的波特率。11059200为晶振频率。X的值最后要换算成16进制
TR1=1; //定时器T1开始工作,TR1=0,T1停止工作
}
/**************************************************/
/**************************************************
Function: init_int0();
Description:
enable int0 interrupt;
/**************************************************/
void init_int0(void)
{
EA=1;
EX0=1; // Enable int0 interrupt.
}
void delay_ms(unsigned int x)
{
unsigned int i,j;
i=0;
for(i=0;i<x;i++)
{
j=108;
while(j--);
}
}
/**************************************************/
/**************************************************
Function: SPI_RW();
Description:
Writes one byte to nRF24L01, and return the byte read
from nRF24L01 during write, according to SPI protocol
/**************************************************/
uchar SPI_RW(uchar byte)
{
uchar 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
}
/**************************************************/
/**************************************************
Function: SPI_RW_Reg();
Description:
Writes value 'value' to register 'reg'
/**************************************************/
uchar SPI_RW_Reg(BYTE reg, BYTE value)
{
uchar 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 byte
}
/**************************************************/
/**************************************************
Function: SPI_Read();
Description:
Read one byte from nRF24L01 register, 'reg'
/**************************************************/
BYTE SPI_Read(BYTE reg)
{
BYTE 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
}
/**************************************************/
/**************************************************
Function: SPI_Read_Buf();
Description:
Reads 'bytes' #of bytes from register 'reg'
Typically used to read RX payload, Rx/Tx address
/**************************************************/
uchar SPI_Read_Buf(BYTE reg, BYTE *pBuf, BYTE bytes)
{
uchar status,byte_ctr;
CSN = 0; // Set CSN low, init SPI tranaction
status = SPI_RW(reg); // Select register to write to and read status byte
for(byte_ctr=0;byte_ctr<bytes;byte_ctr++)
pBuf[byte_ctr] = SPI_RW(0); // Perform SPI_RW to read byte from nRF24L01
CSN = 1; // Set CSN high again
return(status); // return nRF24L01 status byte
}
/**************************************************/
/**************************************************
Function: SPI_Write_Buf();
Description:
Writes contents of buffer '*pBuf' to nRF24L01
Typically used to write TX payload, Rx/Tx address
/**************************************************/
uchar SPI_Write_Buf(BYTE reg, BYTE *pBuf, BYTE bytes)
{
uchar status,byte_ctr;
CSN = 0; // Set CSN low, init SPI tranaction
status = SPI_RW(reg); // Select register to write to and read status byte
for(byte_ctr=0; byte_ctr<bytes; byte_ctr++) // then write all byte in buffer(*pBuf)
SPI_RW(*pBuf++);
CSN = 1; // Set CSN high again
return(status); // return nRF24L01 status byte
}
/**************************************************/
/**************************************************
Function: RX_Mode();
Description:
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 RX_Mode(void)
{
CE=0;
SPI_Write_Buf(WRITE_REG + RX_ADDR_P0, TX_ADDRESS, TX_ADR_WIDTH); // Use the same address on the RX device as the TX device
SPI_RW_Reg(WRITE_REG + EN_AA, 0x01); // Enable Auto.Ack:Pipe0
SPI_RW_Reg(WRITE_REG + EN_RXADDR, 0x01); // Enable Pipe0
SPI_RW_Reg(WRITE_REG + RF_CH, 40); // Select RF channel 40
SPI_RW_Reg(WRITE_REG + RX_PW_P0, TX_PLOAD_WIDTH); // Select same RX payload width as TX Payload width
SPI_RW_Reg(WRITE_REG + RF_SETUP, 0x07); // TX_PWR:0dBm, Datarate:2Mbps, LNA:HCURR
SPI_RW_Reg(WRITE_REG + CONFIG, 0x0f); // Set PWR_UP bit, enable CRC(2 bytes) & Prim:RX. RX_DR enabled..
CE = 1; // Set CE pin high to enable RX device
// This device is now ready to receive one packet of 16 bytes payload from a TX device sending to address
// '3443101001', with auto acknowledgment, retransmit count of 10, RF channel 40 and datarate = 2Mbps.
}
/**************************************************/
/**************************************************
Function: TX_Mode();
Description:
This function initializes one nRF24L01 device to
TX mode, set TX address, set RX address for auto.ack,
fill TX payload, select RF channel, datarate & TX pwr.
PWR_UP is set, CRC(2 bytes) is enabled, & PRIM:TX.
ToDo: One high pulse(>10us) on CE will now send this
packet and expext an acknowledgment from the RX device.
/**************************************************/
void TX_Mode(void)
{
CE=0;
SPI_Write_Buf(WRITE_REG + TX_ADDR, TX_ADDRESS, TX_ADR_WIDTH); // Writes TX_Address to nRF24L01
SPI_Write_Buf(WRITE_REG + RX_ADDR_P0, TX_ADDRESS, TX_ADR_WIDTH); // RX_Addr0 same as TX_Adr for Auto.Ack
SPI_Write_Buf(WR_TX_PLOAD, tx_buf, TX_PLOAD_WIDTH); // Writes data to TX payload
SPI_RW_Reg(WRITE_REG + EN_AA, 0x01); // Enable Auto.Ack:Pipe0
SPI_RW_Reg(WRITE_REG + EN_RXADDR, 0x01); // Enable Pipe0
SPI_RW_Reg(WRITE_REG + SETUP_RETR, 0x1a); // 500us + 86us, 10 retrans...
SPI_RW_Reg(WRITE_REG + RF_CH, 40); // Select RF channel 40
SPI_RW_Reg(WRITE_REG + RF_SETUP, 0x07); // TX_PWR:0dBm, Datarate:2Mbps, LNA:HCURR
SPI_RW_Reg(WRITE_REG + CONFIG, 0x0e); // Set PWR_UP bit, enable CRC(2 bytes) & Prim:TX. MAX_RT & TX_DS enabled..
CE=1;
}
/**************************************************/
/**************************************************
Function: check_ACK();
Description:
check if have "Data sent TX FIFO interrupt",if TX_DS=1,
all led light and after delay 100ms all led close
/**************************************************
void check_ACK()
{
uchar test;
test=SPI_Read(READ_REG+STATUS); // read register STATUS's
test=test&0x20; // check if have Data sent TX FIFO interrupt (TX_DS=1)
if(test==0x20) // TX_DS =1
{
P0=0x00; // turn on all led
delay100(); // delay 100ms
P0=0xff;
}
}
/**************************************************/
/**************************************************
Function: TxData();
Description:
write data x to SBUF
/**************************************************/
void TxData_com(void)
{
for(i=0;i<10;i++)
{
SBUF=tx_com_buffer[i]; // write data x to SBUF
while(TI==0);
TI=0;
}
accept_flag=0;
}
void TxData (uchar x)
{
SBUF=x; // write data x to SBUF
while(TI==0);
TI=0;
}
void RxData(void)
{
if(RI) // 是否有数据到来
{
RI = 0;
rx_com_buffer[i] = SBUF; // 暂存接收到的数据
i++;
if(i>10)
{
accept_flag=1;
i=0;
}
}
}
/**************************************************/
/**************************************************
Function: CheckButtons();
Description:
check buttons ,if have press,read the key values,
turn on led and transmit it; after transmition,
if received ACK, clear TX_DS interrupt and enter RX Mode;
turn off the led
/**************************************************/
/*void CheckButtons()
{
uchar Temp,xx,Tempi;
P0=0xff;
Temp=P0&KEY; //read key value from port P0
if (Temp!=KEY)
{
delay_ms(10);
Temp=P0&KEY; // read key value from port P0
if (Temp!=KEY)
{
xx=Temp;
Tempi=Temp>>1; // Left shift 4 bits
P0=Tempi; // Turn On the led
tx_buf[0]=Tempi; // Save to tx_buf[0]
TX_Mode(); // set TX Mode and transmitting
TxData(xx); // send data to uart
//check_ACK(); // if have acknowledgment from RX device,turn on all led
SPI_RW_Reg(WRITE_REG+STATUS,SPI_Read(READ_REG+STATUS)); // clear interrupt flag(TX_DS)
delay_ms(500);
P0=0xff; // Turn off the led
RX_Mode(); // set receive mode
while((P0&KEY)!=KEY);
}
}
} */
/**************************************************/
/**************************************************
Function: main();
Description:
control all subprogrammes;
/**************************************************/
void main(void)
{
uchar ia;
init_io(); // Initialize IO port
LED1=1;
Inituart(); // initialize 232 uart
RX_Mode(); // set RX mode
while(1)
{
RxData();
if(accept_flag==1)
{
LED1=0 ;
accept_flag=0;
for(ia=0;ia<5;ia++)
{
tx_buf[ia]=tx_com_buffer[ia];
}
TX_Mode();
SPI_RW_Reg(WRITE_REG+STATUS,SPI_Read(READ_REG+STATUS)); // clear interrupt flag(TX_DS)
delay_ms(100);
LED1=1;
RX_Mode();
}
if(!IRQ)
{
sta=SPI_Read(STATUS); // read register STATUS's value
if(RX_DR) // if receive data ready (RX_DR) interrupt
{
SPI_Read_Buf(RD_RX_PLOAD,rx_buf,TX_PLOAD_WIDTH);// read receive payload from RX_FIFO buffer;
for(ia=0;ia<5;ia++)
{
tx_com_buffer[ia]=rx_buf[ia];
}
LED1=0;
flag=0; // set flag=0
TxData_com();
LED1=1;
}
if(MAX_RT)
{
SPI_RW_Reg(FLUSH_TX,0);
}
SPI_RW_Reg(WRITE_REG+STATUS,0xff);// clear RX_DR or TX_DS or MAX_RT interrupt flag
IRQ=1;
RX_Mode();
}
}
}