发射程序是用51写的
#include2.h>
#include
#define uchar unsigned char
#define uint unsigned int
#define TX_ADR_WIDTH5
#define TX_PLOAD_WIDTH 4
// SPI(nRF24L01) commands
#define READ_REG0x00
#define WRITE_REG0x20
#define RD_RX_PLOAD 0x61
#define WR_TX_PLOAD 0xA0
#define FLUSH_TX0xE1
#define FLUSH_RX0xE2
#define REUSE_TX_PL 0xE3
#define NOP0xFF
#define CONFIG0x00
#define EN_AA0x01
#define EN_RXADDR0x02
#define SETUP_AW0x03
#define SETUP_RETR0x04
#define RF_CH0x05
#define RF_SETUP0x06
#define STATUS0x07
#define OBSERVE_TX0x08
#define CD0x09
#define RX_ADDR_P00x0A
#define RX_ADDR_P30x0D
#define RX_ADDR_P40x0E
#define RX_ADDR_P50x0F
#define TX_ADDR0x10
#define RX_PW_P00x11
#define RX_PW_P10x12
#define RX_PW_P20x13
#define RX_PW_P30x14
#define RX_PW_P40x15
#define RX_PW_P50x16
#define FIFO_STATUS 0x17
sbit CE= P1^2;
sbit CSN= P3^5;
sbit MISO = P1^0;
sbit MOSI = P3^3;
sbit SCK= P1^1;
sbit KEY=P1^6;
sbit LED=P3^6;
sbit IRQ=P3^2;
uchar code TX_ADDRESS[TX_ADR_WIDTH] = {0x40,0x42,0x10,0x10,0x01};// 界说一个静态发送地址
uchar dar[4]={0x00,0x01,0x50,0x96};
void delay(uchar x)//延时ms
{
uchar i,j;
for(i=0;i<110;i++)
for(j=0;j}
void delayus(uchar x)//延时us
{
for(;x>0;x–);
_nop_;
}
void write_spi(uchar dat)//写一个字节数据到nrf24l01
{
uchar i;
SCK=0;
for(i=0;i<8;i++)
{
dat=dat<<1;
MOSI=CY;
SCK=1;
delay(10);
SCK=0;
}
}
uchar read_spi()//从nrf24l01读出一个字节数据
{
uchar i,dat;
SCK=0;
for(i=0;i<8;i++)
{
delay(10);
SCK=1;
dat=dat<<1;
dat|=MISO;
SCK=0;
}
return dat;
}
void write_reg(uchar reg,uchar value)//装备寄存器
{
CSN=0;
write_spi(reg+0x20);
delay(10);
write_spi(value);
CSN=1;
}
uchar read_reg(uchar reg)//读寄存器值
{
uchar value;
CSN=0;
write_spi(reg);
delay(10);
value=read_spi();
CSN=1;
return value;
}
void write_fifo(uchar reg,uchar *buf,uchar num)//写多个字节
{
uchar i;
CSN=0;
write_spi(reg);
delay(10);
for(i=0;i{
write_spi(*(buf+i));
}
CSN=1;
}
void read_fifo(uchar reg,uchar *buf,uchar num)//读多个字节
{
uchar i;
CSN=0;
write_spi(reg);
delay(10);
for(i=0;i{
*(buf+i)=read_spi();
delay(10);
}
CSN=1;
}
void TX_Mode()//发送
{
CE = 0;
write_fifo(RX_ADDR_P0+0x20, TX_ADDRESS, TX_ADR_WIDTH);
//write_fifo(WR_TX_PLOAD, buf, 4);
write_reg(CONFIG, 0x0e);
CE=1;
delayus(10);
}
void RX_Mode()//接纳
{
CE = 0;
write_reg(CONFIG, 0x0f);
CE = 1;
delayus(130);
}
void init()//nrf24l01初始化以及单片机端口初始化
{
CE= 0;
CSN = 1;
SCK = 0;
LED = 1;
IRQ=1;
write_fifo(TX_ADDR+0x20, TX_ADDRESS, TX_ADR_WIDTH);
write_fifo(RX_ADDR_P0+0x20, TX_ADDRESS, TX_ADR_WIDTH);
write_reg(EN_AA, 0x01);
write_reg(EN_RXADDR, 0x01);
write_reg(RF_CH,40);
write_reg(RX_PW_P0,4);
write_reg(RF_SETUP, 0x07);
write_reg(CONFIG, 0x0e);
}
void main()
{
uchar status;
uchar dat[4]={0x01,0x02,0x03,0x04};
init();
status=read_reg(STATUS);
write_reg(STATUS,status) ; //铲除状况寄存器
TX_Mode();//装备为发送形式
LED=0;
while(1)
{
if(KEY==0)
{
delay(10);
if(KEY==0)//按键扫描,开释后才开端发送数据
{
while(!KEY) ;
write_fifo(WR_TX_PLOAD, dat, 4);
while(IRQ);//等候中止
if(read_reg(STATUS)&0x20)//发送成功中止
{
LED=1;
}
status=read_reg(STATUS);
write_reg(STATUS,status) ;//铲除状况寄存器
IRQ=1;
write_spi(FLUSH_TX) ;
write_spi(FLUSH_RX) ; //铲除缓存器
LED=0;
}
}
}
}
声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/zhishi/257952.html