|
单片机结合NRF24L01对舵机进行无线控制发送端程序
[复制链接]
#include
#include
#include
typedef unsigned char uchar;
typedef unsigned char uint;
sbit MISO=P1^5;
sbit MOSI=P1^4;
sbit SCK=P1^3;
sbit CE=P1^1;
sbit CSN=P1^2;
sbit IRQ=P3^3;
sbit KEY1=P3^4;
sbit KEY2=P3^5;
sbit KEY3=P3^6;
sbit KEY4=P3^7;
#define TX_ADR_WIDTH 5
#define RX_ADR_WIDTH 5
#define TX_PLOAD_WIDTH 20
#define RX_PLOAD_WIDTH 20
uint const TX_ADDRESS[TX_ADR_WIDTH]= {0x34,0x43,0x10,0x10,0x01}; //本地地址
uint const RX_ADDRESS[RX_ADR_WIDTH]= {0x34,0x43,0x10,0x10,0x01}; //接收地址
#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 // 保留
#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 RX_ADDR_P1 0x0B // 频道1接收数据地址
#define RX_ADDR_P2 0x0C // 频道2接收数据地址
#define RX_ADDR_P3 0x0D // 频道3接收数据地址
#define RX_ADDR_P4 0x0E // 频道4接收数据地址
#define RX_ADDR_P5 0x0F // 频道5接收数据地址
#define TX_ADDR 0x10 // 发送地址寄存器
#define RX_PW_P0 0x11 // 接收频道0接收数据长度
#define RX_PW_P1 0x12 // 接收频道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栈入栈出状态寄存器设置
void Delay(unsigned int s);
void inerDelay_us(unsigned char n);
void init_NRF24L01(void);
uint SPI_RW(uint uchar);
uchar SPI_Read(uchar reg);
void SetRX_Mode(void);
uint SPI_RW_Reg(uchar reg, uchar value);
uint SPI_Read_Buf(uchar reg, uchar *pBuf, uchar uchars);
uint SPI_Write_Buf(uchar reg, uchar *pBuf, uchar uchars);
unsigned char nRF24L01_RxPacket(unsigned char* rx_buf);
void nRF24L01_TxPacket(unsigned char * tx_buf);
void Delay(unsigned int s)
{
unsigned int i;
for(i=0; i
for(i=0; i
}
uint bdata sta;
sbit RX_DR=sta^6;
sbit TX_DS=sta^5;
sbit MAX_RT=sta^4;
void inerDelay_us(unsigned char n)
{
for(;n>0;n--)
_nop_();
}
void init_NRF24L01(void)
{
inerDelay_us(100);
CE=0;
CSN=1;
SCK=0;
SPI_Write_Buf(WRITE_REG + TX_ADDR, TX_ADDRESS, TX_ADR_WIDTH);
SPI_Write_Buf(WRITE_REG + RX_ADDR_P0, RX_ADDRESS, RX_ADR_WIDTH);
SPI_RW_Reg(WRITE_REG + EN_AA, 0x01);
SPI_RW_Reg(WRITE_REG + EN_RXADDR, 0x01);
SPI_RW_Reg(WRITE_REG + RF_CH, 0);
SPI_RW_Reg(WRITE_REG + RX_PW_P0, RX_PLOAD_WIDTH);
SPI_RW_Reg(WRITE_REG + RF_SETUP, 0x07);
}
uint SPI_RW(uint uchar)
{
uint bit_ctr;
for(bit_ctr=0;bit_ctr<8;bit_ctr++)
{
MOSI = (uchar & 0x80);
uchar = (uchar << 1);
SCK = 1;
uchar |= MISO;
SCK = 0;
}
return(uchar);
}
uchar SPI_Read(uchar reg)
{
uchar reg_val;
CSN = 0;
SPI_RW(reg);
reg_val = SPI_RW(0);
CSN = 1;
return(reg_val);
}
uint SPI_RW_Reg(uchar reg, uchar value)
{
uint status;
CSN = 0;
status = SPI_RW(reg);
SPI_RW(value);
CSN = 1;
return(status);
}
uint SPI_Read_Buf(uchar reg, uchar *pBuf, uchar uchars)
{
uint status,uchar_ctr;
CSN = 0;
status = SPI_RW(reg);
for(uchar_ctr=0;uchar_ctr
pBuf[uchar_ctr] = SPI_RW(0);
CSN = 1;
return(status);
}
uint SPI_Write_Buf(uchar reg, uchar *pBuf, uchar uchars)
{
uint status,uchar_ctr;
CSN = 0;
status = SPI_RW(reg);
for(uchar_ctr=0; uchar_ctr
SPI_RW(*pBuf++);
CSN = 1;
return(status);
}
void SetRX_Mode(void)
{
CE=0;
SPI_RW_Reg(WRITE_REG + CONFIG, 0x0f);
CE = 1;
inerDelay_us(130);
}
unsigned char nRF24L01_RxPacket(unsigned char* rx_buf)
{
unsigned char revale=0;
sta=SPI_Read(STATUS);
if(RX_DR)
{
CE = 0;
SPI_Read_Buf(RD_RX_PLOAD,rx_buf,TX_PLOAD_WIDTH);
revale =1;
}
SPI_RW_Reg(WRITE_REG+STATUS,sta);
return revale;
}
void nRF24L01_TxPacket(unsigned char * tx_buf)
{
CE=0;
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);
CE=1;
inerDelay_us(10);
}
void main(void)
{
unsigned char TxBuf[20]={0};
unsigned char RxBuf[20]={0};
init_NRF24L01();
TxBuf[1] = 0;
TxBuf[2] = 0;
TxBuf[3] = 0;
TxBuf[4] = 0;
nRF24L01_TxPacket(TxBuf);
Delay(6000);
while(1)
{
if(KEY1 ==0 )
{
TxBuf[1] = 1 ;
}
if(KEY2 ==0 )
{
TxBuf[2] =1 ;
}
if(KEY3 ==0 )
{
TxBuf[3] =1 ;
}
if(KEY4 ==0 )
{
TxBuf[4] =1 ;
}
nRF24L01_TxPacket(TxBuf);
TxBuf[1] = 0x00;
TxBuf[2] = 0x00;
TxBuf[3] = 0x00;
TxBuf[4] = 0x00;
Delay(10);
SetRX_Mode();
nRF24L01_RxPacket(RxBuf);
}
}
|
|