發(fā)送端程序如下:本文引用地址:http://www.ex-cimer.com/article/201611/318436.htm#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 // 讀取接收數(shù)據(jù)指令#define WR_TX_PLOAD 0xA0 // 寫待發(fā)數(shù)據(jù)指令#define FLUSH_TX 0xE1 // 沖洗發(fā)送 FIFO指令#define FLUSH_RX 0xE2 // 沖洗接收 FIFO指令#define REUSE_TX_PL 0xE3 // 定義重復(fù)裝載數(shù)據(jù)指令#define NOP 0xFF // 保留#define CONFIG 0x00 // 配置收發(fā)狀態(tài),CRC校驗(yàn)?zāi)J揭约笆瞻l(fā)狀態(tài)響應(yīng)方式#define EN_AA 0x01 // 自動(dòng)應(yīng)答功能設(shè)置#define EN_RXADDR 0x02 // 可用信道設(shè)置#define SETUP_AW 0x03 // 收發(fā)地址寬度設(shè)置#define SETUP_RETR 0x04 // 自動(dòng)重發(fā)功能設(shè)置#define RF_CH 0x05 // 工作頻率設(shè)置#define RF_SETUP 0x06 // 發(fā)射速率、功耗功能設(shè)置#define STATUS 0x07 // 狀態(tài)寄存器#define OBSERVE_TX 0x08 // 發(fā)送監(jiān)測(cè)功能#define CD 0x09 // 地址檢測(cè) #define RX_ADDR_P0 0x0A // 頻道0接收數(shù)據(jù)地址#define RX_ADDR_P1 0x0B // 頻道1接收數(shù)據(jù)地址#define RX_ADDR_P2 0x0C // 頻道2接收數(shù)據(jù)地址#define RX_ADDR_P3 0x0D // 頻道3接收數(shù)據(jù)地址#define RX_ADDR_P4 0x0E // 頻道4接收數(shù)據(jù)地址#define RX_ADDR_P5 0x0F // 頻道5接收數(shù)據(jù)地址#define TX_ADDR 0x10 // 發(fā)送地址寄存器#define RX_PW_P0 0x11 // 接收頻道0接收數(shù)據(jù)長(zhǎng)度#define RX_PW_P1 0x12 // 接收頻道0接收數(shù)據(jù)長(zhǎng)度#define RX_PW_P2 0x13 // 接收頻道0接收數(shù)據(jù)長(zhǎng)度#define RX_PW_P3 0x14 // 接收頻道0接收數(shù)據(jù)長(zhǎng)度#define RX_PW_P4 0x15 // 接收頻道0接收數(shù)據(jù)長(zhǎng)度#define RX_PW_P5 0x16 // 接收頻道0接收數(shù)據(jù)長(zhǎng)度#define FIFO_STATUS 0x17 // FIFO棧入棧出狀態(tài)寄存器設(shè)置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; i0;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 // 讀取接收數(shù)據(jù)指令#define WR_TX_PLOAD 0xA0 // 寫待發(fā)數(shù)據(jù)指令#define FLUSH_TX 0xE1 // 沖洗發(fā)送 FIFO指令#define FLUSH_RX 0xE2 // 沖洗接收 FIFO指令#define REUSE_TX_PL 0xE3 // 定義重復(fù)裝載數(shù)據(jù)指令#define NOP 0xFF // 保留#define CONFIG 0x00 // 配置收發(fā)狀態(tài),CRC校驗(yàn)?zāi)J揭约笆瞻l(fā)狀態(tài)響應(yīng)方式#define EN_AA 0x01 // 自動(dòng)應(yīng)答功能設(shè)置#define EN_RXADDR 0x02 // 可用信道設(shè)置#define SETUP_AW 0x03 // 收發(fā)地址寬度設(shè)置#define SETUP_RETR 0x04 // 自動(dòng)重發(fā)功能設(shè)置#define RF_CH 0x05 // 工作頻率設(shè)置#define RF_SETUP 0x06 // 發(fā)射速率、功耗功能設(shè)置#define STATUS 0x07 // 狀態(tài)寄存器#define OBSERVE_TX 0x08 // 發(fā)送監(jiān)測(cè)功能#define CD 0x09 // 地址檢測(cè) #define RX_ADDR_P0 0x0A // 頻道0接收數(shù)據(jù)地址#define RX_ADDR_P1 0x0B // 頻道1接收數(shù)據(jù)地址#define RX_ADDR_P2 0x0C // 頻道2接收數(shù)據(jù)地址#define RX_ADDR_P3 0x0D // 頻道3接收數(shù)據(jù)地址#define RX_ADDR_P4 0x0E // 頻道4接收數(shù)據(jù)地址#define RX_ADDR_P5 0x0F // 頻道5接收數(shù)據(jù)地址#define TX_ADDR 0x10 // 發(fā)送地址寄存器#define RX_PW_P0 0x11 // 接收頻道0接收數(shù)據(jù)長(zhǎng)度#define RX_PW_P1 0x12 // 接收頻道0接收數(shù)據(jù)長(zhǎng)度#define RX_PW_P2 0x13 // 接收頻道0接收數(shù)據(jù)長(zhǎng)度#define RX_PW_P3 0x14 // 接收頻道0接收數(shù)據(jù)長(zhǎng)度#define RX_PW_P4 0x15 // 接收頻道0接收數(shù)據(jù)長(zhǎng)度#define RX_PW_P5 0x16 // 接收頻道0接收數(shù)據(jù)長(zhǎng)度#define FIFO_STATUS 0x17 // FIFO棧入棧出狀態(tài)寄存器設(shè)置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; i0;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++;}
評(píng)論