//
引脚分布,
IRQ PA2
CE PA4
CS PA3
SPI1 (记住是SPI1的接口)
SCK PA5
MISO PA6
MOSI PA7
接好引脚,初始化好就可以使用了。记住,里面的文件配置都不用改,改引脚是ok 的
http://download.youkuaiyun.com/download/hes_c/10140988
这是RX初始化
NRF24L01_Init();
while(NRF24L01_Check())
{
led_on();
printf("NRF NO\r\n");
delay_ms(100);
}
printf("NRF OK\r\n");
NRF24L01_RX_Mode();
这是TX初始化
NRF24L01_Init();
while(NRF24L01_Check())
{
led_on();
printf("NRF NO\r\n");
delay_ms(100);
}
printf("NRF OK\r\n");
NRF24L01_TX_Mode();
因为NRF通信会比较容易掉包
所有下面写了一个通信校验和加密(就不给全部代码了)
这是发送,添加校验和加密
//发送信息
int send_mes(u32 ID,u32 t_TX_voltage,u32 t_TX_electricity,u32 t_RX_voltage,u32 t_RX_electricity)
{
int n=0;
u32 temp=0;
temp=t_TX_voltage+t_TX_electricity+t_RX_voltage+t_RX_electricity+BCD+ID;
//ID
buf_mes[n++]=(ID>>24)&0x000000ff;
buf_mes[n++]=(ID>>16)&0x000000ff;
buf_mes[n++]=(ID>>8)&0x000000ff;
buf_mes[n++]=(ID>>0)&0x000000ff;
//TX电压
buf_mes[n++]=(t_TX_voltage>>24)&0x000000ff;
buf_mes[n++]=(t_TX_voltage>>16)&0x000000ff;
buf_mes[n++]=(t_TX_voltage>>8)&0x000000ff;
buf_mes[n++]=(t_TX_voltage>>0)&0x000000ff;
//TX电流
buf_mes[n++]=(t_TX_electricity>>24)&0x000000ff;
buf_mes[n++]=(t_TX_electricity>>16)&0x000000ff;
buf_mes[n++]=(t_TX_electricity>>8)&0x000000ff;
buf_mes[n++]=(t_TX_electricity>>0)&0x000000ff;
//RX电压
buf_mes[n++]=(t_RX_voltage>>24)&0x000000ff;
buf_mes[n++]=(t_RX_voltage>>16)&0x000000ff;
buf_mes[n++]=(t_RX_voltage>>8)&0x000000ff;
buf_mes[n++]=(t_RX_voltage>>0)&0x000000ff;
//RX电流
buf_mes[n++]=(t_RX_electricity>>24)&0x000000ff;
buf_mes[n++]=(t_RX_electricity>>16)&0x000000ff;
buf_mes[n++]=(t_RX_electricity>>8)&0x000000ff;
buf_mes[n++]=(t_RX_electricity>>0)&0x000000ff;
//校验码
buf_mes[n++]=(temp>>24)&0x000000ff;
buf_mes[n++]=(temp>>16)&0x000000ff;
buf_mes[n++]=(temp>>8)&0x000000ff;
buf_mes[n++]=(temp>>0)&0x000000ff;
buf_mes[32]='\0';
if(NRF24L01_TxPacket(buf_mes)==TX_OK)
{
return 1;
//printf("NRF24L01 Send Success\r\n");
}
else
{
return 0;
//printf("NRF24L01 Send Fail\r\n");
}
}
下面是接收解密和校验
//接收信息
int recv_mes()
{
u32 temp=0,temp1=1;
u32 n=0;
if(NRF24L01_RxPacket(R_buf_mes)==0)//一旦接收到信息,
{
//数据合成
RX_ID=R_buf_mes[n++]<<24|R_buf_mes[n++]<<16|R_buf_mes[n++]<<8|R_buf_mes[n++];
r_TX_voltage=R_buf_mes[n++]<<24|R_buf_mes[n++]<<16|R_buf_mes[n++]<<8|R_buf_mes[n++];
r_TX_electricity=R_buf_mes[n++]<<24|R_buf_mes[n++]<<16|R_buf_mes[n++]<<8|R_buf_mes[n++];
r_RX_voltage=R_buf_mes[n++]<<24|R_buf_mes[n++]<<16|R_buf_mes[n++]<<8|R_buf_mes[n++];
r_RX_electricity=R_buf_mes[n++]<<24|R_buf_mes[n++]<<16|R_buf_mes[n++]<<8|R_buf_mes[n++];
temp=R_buf_mes[n++]<<24|R_buf_mes[n++]<<16|R_buf_mes[n++]<<8|R_buf_mes[n++];
data_flag=1;
}
temp1=r_TX_voltage+r_TX_electricity+r_RX_voltage+r_RX_electricity+BCD+RX_ID;
if(temp==temp1) //验证成功
return 1;
return 0;
}
关于变量就不上了,其中接收缓存和发送缓存 是R_buf_mes[33], buf_mes[33]