發(fā)射程序是用51寫的#include2.h>
#include
#define uchar unsigned char
#define uint unsigned int
本文引用地址:http://m.butianyuan.cn/article/201611/321663.htm#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};// 定義一個靜態(tài)發(fā)送地址
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)//寫一個字節(jié)數(shù)據(jù)到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讀出一個字節(jié)數(shù)據(jù)
{
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)//寫多個字節(jié)
{
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)//讀多個字節(jié)
{
uchar i;
CSN=0;
write_spi(reg);
delay(10);
for(i=0;i{
*(buf+i)=read_spi();
delay(10);
}
CSN=1;
}
void TX_Mode()//發(fā)送
{
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初始化以及單片機(jī)端口初始化
{
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) ; //清除狀態(tài)寄存器
TX_Mode();//配置為發(fā)送模式
LED=0;
while(1)
{
if(KEY==0)
{
delay(10);
if(KEY==0)//按鍵掃描,釋放后才開始發(fā)送數(shù)據(jù)
{
while(!KEY) ;
write_fifo(WR_TX_PLOAD, dat, 4);
while(IRQ);//等待中斷
if(read_reg(STATUS)&0x20)//發(fā)送成功中斷
{
LED=1;
}
status=read_reg(STATUS);
write_reg(STATUS,status) ;//清除狀態(tài)寄存器
IRQ=1;
write_spi(FLUSH_TX) ;
write_spi(FLUSH_RX) ; //清除緩存器
LED=0;
}
}
}
}
評論