#include <reg52.h>
#include <intrins.h>
typedef unsigned char uchar;
typedef unsigned int uint;
//****************************************IO端口定义***************************************
//****************************************NRF24L01端口定义***************************************
sbit CE =P1^5;
sbit CSN =P1^6;
sbit SCK =P1^7;
sbit MOSI =P3^7;
sbit MISO =P3^6;
sbit IRQ =P3^5;
sbit KEY1=P2^0; //定义按键
sbit LED=P1^0; //定义LED
//*********************************************NRF24L01*************************************
#define TX_ADR_WIDTH 5 // 5 uints TX address width
#define RX_ADR_WIDTH 5 // 5 uints RX address width
#define TX_PLOAD_WIDTH 20 // 20 uints TX payload
#define RX_PLOAD_WIDTH 20 // 20 uints TX payload
uchar const TX_ADDRESS[TX_ADR_WIDTH]= {0x34,0x43,0x10,0x10,0x01}; //本地地址0
uchar const RX_ADDRESS[TX_ADR_WIDTH]= {0x34,0x43,0x10,0x10,0x01}; //接收地址0
//***************************************NRF24L01寄存器指令*******************************************************
#define READ_REG 0x00 // 读寄存器指令
#define WRITE_REG 0x20 // 写寄存器指令
#define RD_RX_PLOAD 0x61 // 读取接收数据指令
#define WR_TX_PLOAD 0xA0 // 写待发数据指令
#define FLUSH_TX 0xE1 // 冲洗发送 FIFO指令
#define FLUSH_RX 0xE2 // 冲洗接收 FIFO指令
#define REUSE_TX_PL 0xE3 // 定义重复装载数据指令
#define NOP 0xFF // 保留
//*************************************SPI(nRF24L01)寄存器地址****************************************************
#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_P0 0x0A // 频道0接收数据地址
#define TX_ADDR 0x10 // 发送地址寄存器
#define RX_PW_P0 0x11 // 接收频道0接收数据长度
#define FIFO_STATUS 0x17 // FIFO栈入栈出状态寄存器设置
//**************************************************************************************
void Delay(unsigned int s);
void inerDelay_us(unsigned char n);
void init_NRF24L01(void);
uint SPI_RW(uint uchar);
uint SPI_RW_Reg(uchar reg, uchar value);
uchar SPI_Write_Buf(uchar reg, uchar *pBuf, uchar uchars);
void nRF24L01_TxPacket(unsigned char * tx_buf);
//*****************************************长延时*****************************************
void Delay(uint time) //int型数据为16位,所以最大值为65535
{
uint i,j; //定义变量i,j,用于循环语句
for(i=0;i<time;i++) //for循环,循环50*time次
for(j=0;j<50;j++); //for循环,循环50次
}
/******************************************************************************************
/*延时函数
/******************************************************************************************/
void inerDelay_us(unsigned char n)
{
for(;n>0;n--)
_nop_();
}
//****************************************************************************************
/*NRF24L01初始化
//***************************************************************************************/
void init_NRF24L01(void)
{
inerDelay_us(100);
CE=0; // chip enable
CSN=1; // Spi disable
SCK=0; // Spi clock line init high
SPI_Write_Buf(WRITE_REG + TX_ADDR, TX_ADDRESS, TX_ADR_WIDTH); // 写本地地址
SPI_Write_Buf(WRITE_REG + RX_ADDR_P0, RX_ADDRESS, 1); // 写接收端地址
SPI_RW_Reg(WRITE_REG + EN_AA, 0X3F); // 频道0自动 ACK应答允许
SPI_RW_Reg(WRITE_REG + EN_RXADDR,0x3F); // 允许接收地址只有频道0,如果需要多频道可以参考Page21
SPI_RW_Reg(WRITE_REG + RF_CH, 0); // 设置信道工作为2.4GHZ,收发必须一致
SPI_RW_Reg(WRITE_REG + RX_PW_P0, RX_PLOAD_WIDTH); //设置接收数据长度,本次设置为32字节
SPI_RW_Reg(WRITE_REG + RF_SETUP, 0x07); //设置发射速率为1MHZ,发射功率为最大值0dB
SPI_RW_Reg(WRITE_REG + CONFIG, 0x0e); //IRQ收发完成中断响应,主发送
}
/****************************************************************************************************
/*函数:uint SPI_RW(uint uchar)
/*功能:NRF24L01的SPI写时序
/****************************************************************************************************/
uint SPI_RW(uint uchar)
{
uint bit_ctr;
for(bit_ctr=0;bit_ctr<8;bit_ctr++) // output 8-bit
{
MOSI = (uchar & 0x80); // output 'uchar', MSB to MOSI
uchar = (uchar << 1); // shift next bit into MSB..
SCK = 1; // Set SCK high..
uchar |= MISO; // capture current MISO bit
SCK = 0; // ..then set SCK low again
}
return(uchar); // return read uchar
}
/****************************************************************************************************/
/*功能:NRF24L01读写寄存器函数
/****************************************************************************************************/
uint SPI_RW_Reg(uchar reg, uchar value)
{
uint 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 uchar
}
//***********************************************************************
/*函数:uint SPI_Write_Buf(uchar reg, uchar *pBuf, uchar uchars)
/*功能: 用于写数据:为寄存器地址,pBuf:为待写入数据地址,uchars:写入数据的个数
/*********************************************************************************************************/
uchar SPI_Write_Buf(uchar reg, uchar *pBuf, uchar Byte_Num)
{
uchar status,Byte_Count;
CSN = 0; //SPI使能
status = SPI_RW(reg);
for(Byte_Count=0; Byte_Count<Byte_Num;Byte_Count++)
{
SPI_RW(*pBuf++); //指向下一个数据
}
CSN = 1; //关闭SPI
return(status); //
}
/***********************************************************************************************************
/*函数:void nRF24L01_TxPacket(unsigned char * tx_buf)
/*功能:发送 tx_buf中数据
/**********************************************************************************************************/
void nRF24L01_TxPacket(unsigned char * tx_buf)
{
CE=0; //StandBy I模式
SPI_Write_Buf(WRITE_REG + RX_ADDR_P0, TX_ADDRESS, TX_ADR_WIDTH); // 装载接收端地址
SPI_Write_Buf(WR_TX_PLOAD, tx_buf, TX_PLOAD_WIDTH); // 装载数据
SPI_RW_Reg(WRITE_REG + CONFIG, 0x0e); // IRQ收发完成中断响应,16位CRC,主发送
CE=1; //置高CE,激发数据发送
inerDelay_us(10);
LED=~LED;
Delay(6000);
}
void main(void)
{
unsigned char tf =0;
unsigned char TxBuf[20]={0}; //
unsigned char RxBuf[20]={0};
init_NRF24L01() ;
TxBuf[1] = 1 ;
TxBuf[2] = 2 ;
nRF24L01_TxPacket(TxBuf); // Transmit Tx buffer data
Delay(6000);
while(1)
{
nRF24L01_TxPacket(TxBuf); // Transmit Tx buffer data
Delay(6000);
SPI_RW_Reg(FLUSH_TX,0x00);
SPI_RW_Reg(WRITE_REG+STATUS,0x7F);
Delay(6000);
Delay(6000);
LED=~LED;
Delay(6000);
}
// if(KEY1 ==0 )
// {
// while(KEY1==0); //等待释放按键
// TxBuf[1] = 1 ;
// tf = 1 ; LED=1;
// Delay(5000);
// }
// if (tf==1)
// {
// nRF24L01_TxPacket(TxBuf); // Transmit Tx buffer data
// TxBuf[1] = 0x00;
// TxBuf[2] = 0x00;
// tf=0;
// Delay(1000);
// }
// }
}
评论0