发送端程序如下:
#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 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接纳端程序如下:
#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);uchar flag_0;uchar flag_1;unsigned int num;void Delay(unsigned int s){unsigned int i;for(i=0; i 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_ctrinterrupt 1 { if(flag_0==1) PWM_Value[0]+=1;if(flag_0==2) PWM_Value[0]-=1;if(flag_1==1) PWM_Value[1]+=1;if(flag_1==2) PWM_Value[1]-=1;if(PWM_Value[0]>=2500)PWM_Value[0]=2500;if(PWM_Value[1]>=2500)PWM_Value[1]=2500;if(PWM_Value[0]<=500)PWM_Value[0]=500;if(PWM_Value[1]<=500)PWM_Value[1]=500;switch(order){case 1:PWM_OUT0=1;TH0=(65536-PWM_Value[0])>>8;TL0=(uchar)(65536-PWM_Value[0]);break; case 2:PWM_OUT0=0;TH0=(65536-(5000-PWM_Value[0]))>>8;TL0=(uchar)(65536-(5000-PWM_Value[0]));break; case 3:PWM_OUT1=1;TH0=(65536-PWM_Value[1])>>8;TL0=(uchar)(65536-PWM_Value[1]);break;case 4:PWM_OUT1=0;TH0=(65536-(5000-PWM_Value[1]))>>8;TL0=(uchar)(65536-(5000-PWM_Value[1]));break;case 5:TH0=60536>>8;TL0=(uchar)60536;break;case 6:TH0=60536>>8;TL0=(uchar)60536;order=0;break;default: order=0;break;}order++;}