本人刚接触这个模块、在网上找了一些资料在学习、也有下载中文版的用户手册在看、可是、、、、是这样的、想大多数网有所说、先调试一个单独的发射程序、下面是我的程序、希望各位大神指点小弟一二、
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
#define lcd P0
#define TX_ADR_WIDTH 5//发送地址宽度5字节
#define RX_ADR_WIDTH 5//接收地址宽度5字节
#define TX_PLOAD_WIDTH 32//发送数据宽度32字节
#define RX_PLOAD_WIDTH 32//接收数据的宽度32字节
uchar const TX_ADDRESS[TX_ADR_WIDTH]={0x01,0x02,0x03,0x04,0x05};
uchar const RX_ADDRESS[RX_ADR_WIDTH]={0x01,0x02,0x03,0x04,0x05};
#define WRITE_REG 0x00 // 读寄存器指令
#define FLUSH_TX 0xE1 // 冲洗发送 FIFO指令
#define FLUSH_RX 0xE2 // 冲洗接收 FIFO指令
#define NOP 0xFF // 保留
#define CONFIG 0x00 // 配置收发状态,CRC校验模式以及收发状态响应方式
#define EN_AA 0x01 // 自动应答功能设置
#define EN_RXADDR 0x02 // 可用信道设置
#define SETUP_AW 0x03 // 收发地址宽度设置
#define SETUP_RETR 0x04 // 自动重发功能设置
#define RF_CH 0x05 // 工作频率设置
#define RF_SETUP 0x06 // 发射速率、功耗功能设置
#define STATUS 0x07 // 状态寄存器
#define OBSERVE_TX 0x08 // 发送监测功能
#define CD 0x09 // 地址检测
#define RX_ADDR_P5 0x0F // 频道5接收数据地址
#define TX_ADDR 0x10 // 发送地址寄存器
#define RX_PW_P0 0x11 // 接收频道0接收数据长度
#define RX_PW_P2 0x13 // 接收频道0接收数据长度
#define RX_PW_P3 0x14 // 接收频道0接收数据长度
#define RX_PW_P4 0x15 // 接收频道0接收数据长度
#define RX_PW_P5 0x16 // 接收频道0接收数据长度
#define FIFO_STATUS 0x17 // FIFO栈入栈出状态寄存器设置
sbit lcdrs=P2^6;
sbit lcdrw=P2^5;
sbit lcde=P2^7;
sbit CE=P1^7;
sbit CSN=P3^7;
sbit SCK=P3^6;
sbit MOSI=P2^2;
sbit MISO=P2^0;
sbit IRQ=P3^3;
uchar str[4];
uchar status;
uchar SPI_RW_REG(uchar reg,uchar value);
uchar SPI_READ(uchar reg);
uchar SPI_RW(uchar unchar);
uchar * uchartostr(uchar unm);
void NRFinit(void);
void nrf24l01_txpacket(uchar *tx_buf);
uchar SPI_WRITE_BUF(uchar reg, uchar *pBUF, uchar uchars);
uchar SPI_READ_BUF(uchar reg, uchar *pBUF, uchar uchars);
void delay(uint t)
{
uchar j;
for(;t>0;t--)
for(j=110;j>0;j--);
}
void write_com(uchar com)
{
lcdrs=0;
lcdrw=0;
P0=com;
delay(5);
lcde=1;
delay(5);
lcde=0;
}
void write_data(uchar date)
{
lcdrs=1;
lcdrw=0;
P0=date;
delay(5);
lcde=1;
delay(5);
lcde=0;
}
void init()
{
lcde=0;
write_com(0x38);
write_com(0x0f);
write_com(0x06);
write_com(0x01);
write_com(0x80);
}
void display(uchar x,uchar y,uchar *p)
{
uchar i;
for(i=0;i<16;i++)
{
if(0==y) x|=0x80;
else x|=0xc0;
write_com(x);
write_data(*p);
x++;p++;
}
}
uchar * uchartostr(uchar unm) //将char值转换成字符串
{
uchar x00,xx,x0,x,n;
x00=unm/100;xx=unm%100;x0=xx/10;x=xx%10;n=0;
if(x00!=0)
{
str[n]=x00+48;
n++;
}
if(!(x00==0&x0==0))
{
str[n]=x0+48;
n++;
}
str[n]=x+48;
n++;
str[n]='\0';
return(str);
}
uchar SPI_RW(uchar byte) //写时序
{
uchar bit_ctr;
for(bit_ctr=0;bit_ctr<8;bit_ctr++)
{
MOSI=(byte&0x80);
byte=byte<<1;
SCK=1;
byte|=MISO;
SCK=0;
}
return(byte);
}
uchar SPI_READ(uchar reg) //读时序
{
uchar reg_val;
CSN=0;
SPI_RW(reg);
reg_val= SPI_RW(0);
CSN=1;
return(reg_val);
}
uchar SPI_RW_REG(uchar reg,uchar value) //写一个字节到寄存器函数
{
CSN=0;
status=SPI_RW(reg);
SPI_RW(value);
CSN=1;
return(status);
}
uchar SPI_READ_BUF(uchar reg, uchar *pBUF, uchar uchars) //读数据
{
uchar uchar_ctr;
CSN=0;
status=SPI_RW(reg);
for(uchar_ctr=0;uchar_ctr<uchars;uchar_ctr++)
{
pBUF[uchar_ctr]=SPI_RW(0);
}
CSN=1;
return(status);
}
uchar SPI_WRITE_BUF(uchar reg, uchar *pBUF, uchar uchars) //写数据
{
uchar uchar_ctr;
CSN=0;
status=SPI_RW(reg);
for(uchar_ctr=0;uchar_ctr<uchars;uchar_ctr++)
{
SPI_RW(*pBUF++);
}
CSN=1;
return(status);
}
void NRFinit(void) //初始化
{
CE=0;CSN=1;SCK=0;IRQ=1;
SPI_RW_REG(WRITE_REG + EN_AA,0X00);
SPI_RW_REG(WRITE_REG + SETUP_RETR,0X00);
SPI_RW_REG(WRITE_REG + EN_RXADDR,0X01);
SPI_RW_REG(WRITE_REG + RF_CH,1);
SPI_RW_REG(WRITE_REG + RX_PW_P0,RX_PLOAD_WIDTH);
SPI_RW_REG(WRITE_REG + RF_SETUP,0X07);
}
void nrf24l01_txpacket(uchar *tx_buf) //发送tx_buf中的数据
{
CE=0;
SPI_WRITE_BUF(WRITE_REG + TX_ADDR,TX_ADDRESS,TX_ADR_WIDTH);
SPI_WRITE_BUF(WRITE_REG + RX_PW_P0,TX_ADDRESS,TX_ADR_WIDTH);
SPI_WRITE_BUF(TX_PLOAD_WIDTH,tx_buf,TX_PLOAD_WIDTH);
//SPI_WRITE_BUF(WRITE_REG + CONFIG,0x0e);
CE=1;
delay(1);
CE=0;
delay(1);
}
void main()
{
uchar fifo_s;
delay(30);
init();
delay(100);
NRFinit();
while(1)
{
SPI_RW_REG(WRITE_REG+STATUS,0XFF);
//SPI_WRITE_BUF(TX_PLOAD_WIDTH,tx_buf,TX_PLOAD_WIDTH);
status=SPI_READ(STATUS);
fifo_s=SPI_READ(FIFO_STATUS);
write_com(0x80+7);
display(1,2,uchartostr(status));
write_com(0x80+0x40+7);
display(1,4,uchartostr(status));
status=SPI_READ(STATUS);
fifo_s=SPI_READ(FIFO_STATUS);
write_com(0x80+11);
display(1,6,uchartostr(status));
write_com(0x80+0x40+11);
display(1,8,uchartostr(status));
delay(30);
write_com(0x01);
delay(30);
}
}
程序是我在参考网上的资料还有用户手册半拷贝半自己写的、真心求教、 |