首页 nRF905程序

nRF905程序

举报
开通vip

nRF905程序nRF905程序 发送: #include #include #include"delay.h" #include"lcd_init.h" #pragma interrupt_handler timer0_ovf_isr:10 #define uint unsigned int #define uchar unsigned char #define DATA7 (DATA_BUF&BIT(7))!= 0 #define DATA0 (DATA_BUF&BIT(0))!= 0 //-------...

nRF905程序
nRF905程序 发送: #include #include #include"delay.h" #include"lcd_init.h" #pragma interrupt_handler timer0_ovf_isr:10 #define uint unsigned int #define uchar unsigned char #define DATA7 (DATA_BUF&BIT(7))!= 0 #define DATA0 (DATA_BUF&BIT(0))!= 0 //---------------------------------------------------配置口定义--------------------------------------------------- #define TXEN_1 (PORTB|=BIT(0)) #define TXEN_0 (PORTB&=~BIT(0)) #define TRX_CE_1 (PORTB|=BIT(1)) #define TRX_CE_0 (PORTB&=~BIT(1)) #define PWR_1 (PORTB|=BIT(2)) #define PWR_0 (PORTB&=~BIT(2)) #define MISO (PINB&BIT(3)) #define MOSI_1 (PORTB|=BIT(4)) #define MOSI_0 (PORTB&=~BIT(4)) #define SCK_1 (PORTB|=BIT(5)) #define SCK_0 (PORTB&=~BIT(5)) #define CSN_1 (PORTB|=BIT(6)) #define CSN_0 (PORTB&=~BIT(6)) //---------------------------------------------------NRF905状态IO---------------------------------------------------- #define AM PINA&BIT(0) #define DR PINA&BIT(1) #define CD PINA&BIT(2) //-------------------------------------------------------nrf905控制指令------------------------------------------- #define WC 0x00 #define RC 0x10 #define WTP 0x20 #define RTP 0x21 #define WTA 0x22 #define RTA 0x23 #define RRP 0x24 unsigned char DATA_BUF; uint cnt=0,miao=0; //---------------------------------------------------发送数据缓冲区------------------------------------------------- unsigned char TxRxBuf[5]= { 0x00 }; unsigned char TxRxBuf1[]={"fuck u!"}; //------------------------------------------------NRF905寄存器配置------------------------------------------------ unsigned char RFConf[11]= { 0x00, //配置命令// 0x4c, //CH_NO,配置频段在430MHZ 0x0c, //输出功率为10db,不重发,节电为正常模式 0x44, //地址宽度设置,为4字节 0x04,0x04, //接收发送有效数据长度为32字节 0xCC,0xCC,0xCC,0xCC, //接收地址 0x58, //CRC充许,8位CRC校验,外部时钟信号不使能,16M晶振 }; uchar TxAddress[4]={0xcc,0xcc,0xcc,0xcc}; //------------------------------------------------延时------------------------------------------------------------ //---------------------------------------------------SPI读函数----------------------------------------------------- unsigned char SpiRead(void) { unsigned char j; for (j=0;j<8;j++) { DATA_BUF=DATA_BUF<<1; SCK_1; if (MISO) //读取最高位,保存至最末尾,通过左移位完成整个字节 { DATA_BUF|=BIT(0); } else { DATA_BUF&=~BIT(0); } SCK_0; } return DATA_BUF; } //-------------------------------------------------SPI写函数---------------------------------------------------------- void SpiWrite(unsigned char send) { unsigned char i; DATA_BUF=send; for (i=0;i<8;i++) { if (DATA7) //总是发送最高位 { MOSI_1; } else { MOSI_0; } SCK_1; DATA_BUF=DATA_BUF<<1; SCK_0; } } //------------------------------------------------------初始化nRF905--------------------------------------------- void nRF905Init(void) { CSN_1; // Spi disable SCK_0; // Spi clock line init low DDRA=0XFF; PORTA=0XFF; DDRA=0X00; PWR_1; // nRF905 power on TRX_CE_0; // Set nRF905 in standby mode TXEN_0; // set radio in Rx mode } //-----------------------------------------------------初始化寄存器----------------------------------------------- void Config905(void) { uchar i; CSN_0; // Spi enable for write a spi command //SpiWrite(WC); // Write config command写放配置命令 for (i=0;i<11;i++) // Write configration words 写放配置字 { SpiWrite(RFConf[i]); } CSN_1; // Disable Spi } //-----------------------------------------------------发送数据打包 --------------------------------------------------- void TxPacket(uchar *TxRxBuf) { uchar i; //Config905(); CSN_0; SpiWrite(WTP); // Write payload command for (i=0;i<4;i++) { SpiWrite(TxRxBuf[i]); // Write 32 bytes Tx data }// Spi enable for write a spi command CSN_1; delay_ms(1); // Spi disable CSN_0; // Spi enable for write a spi command SpiWrite(WTA); // Write address command for (i=0;i<4;i++) // Write 4 bytes address { SpiWrite(TxAddress[i]); } CSN_1; // Spi disable TRX_CE_1; // Set TRX_CE high,start Tx data transmission delay_ms(1); // // while (DR!=1); TRX_CE_0; // Set TRX_CE low } //----------------------------------------------------------设置发送状态 --------------------------------------------- void SetTxMode(void) { TRX_CE_0; TXEN_1; delay_ms(1); // delay for mode change(>=650us) } //-----------------------------------------------设置发送状态 --------------------------------------------------- void SetRxMode(void) { TXEN_0; TRX_CE_1; delay_ms(1); // delay for mode change(>=650us) } //-------------------------------------------------判断数据接收状态 ----------------------------------------------------- unsigned char CheckDR(void) //检查是否有新数据传入 Data Ready { DDRB=((0<检查是否已存在同频率载波 { if (CD==1) { return 1; } else { return 0; } } void timer0_init(void) { TCNT0 = 0x83; //1ms TCCR0 = 0x03; //64 TIMSK = 0x01; } void timer0_ovf_isr(void) { TCNT0 = 0x83; cnt++; if(cnt==1000) { miao++; cnt=0; if(miao>59) { miao=0; } } } //------------------------------------------------------------------------------ ----------------------------------- void main(void) { uchar i; DDRB=0XF7; DDRA=0X00; timer0_init(); SREG=0X80; nRF905Init(); Config905(); LCD_init(); while(1) { TxRxBuf[0]=miao/10; TxRxBuf[1]=miao%10; TxRxBuf[2]=cnt/100; TxRxBuf[3]=cnt/10%10; TxRxBuf[4]=cnt%10; LCD_write_char(0,0,num1[TxRxBuf[0]]); LCD_write_char(1,0,num1[TxRxBuf[1]]); LCD_write_char(2,0,':'); LCD_write_char(3,0,num1[TxRxBuf[2]]); LCD_write_char(4,0,num1[TxRxBuf[3]]); LCD_write_char(5,0,num1[TxRxBuf[4]]); LCD_write_string(0,1,TxRxBuf1); SetTxMode();// Set nRF905 in Tx mode TxPacket(TxRxBuf);// Send data by nRF905 //TxPacket(TxRxBuf1); } } 接收: #include #include #include"delay.h" #include"lcd_init.h" //---------------------------------------------------配置口定义 --------------------------------------------------- #define DATA7 (DATA_BUF&BIT(7))!= 0 #define DATA0 (DATA_BUF&BIT(0))!= 0 #define uint unsigned int #define uchar unsigned char #define TXEN_1 (PORTB|=BIT(0)) #define TXEN_0 (PORTB&=~BIT(0)) #define TRX_CE_1 (PORTB|=BIT(1)) #define TRX_CE_0 (PORTB&=~BIT(1)) #define PWR_1 (PORTB|=BIT(2)) #define PWR_0 (PORTB&=~BIT(2)) #define MISO (PINB&BIT(3)) #define MOSI_1 (PORTB|=BIT(4)) #define MOSI_0 (PORTB&=~BIT(4)) #define SCK_1 (PORTB|=BIT(5)) #define SCK_0 (PORTB&=~BIT(5)) #define CSN_1 (PORTB|=BIT(6)) #define CSN_0 (PORTB&=~BIT(6)) //---------------------------------------------------NRF905状态IO---------------------------------------------------- #define AM PINA&BIT(0) #define DR PINA&BIT(1) #define CD PINA&BIT(2) //-------------------------------------------------------nrf905控制指令------------------------------------------- #define WC 0x00 #define RC 0x10 #define WTP 0x20 #define RTP 0x21 #define WTA 0x22 #define RTA 0x23 #define RRP 0x24 unsigned char DATA_BUF; //---------------------------------------------------发送数据缓冲区------------------------------------------------- unsigned char TxRxBuf[5]; unsigned char TxRxBuf1[]; //------------------------------------------------NRF905寄存器配置------------------------------------------------ unsigned char RFConf[11]= { 0x00, //配置命令// 0x4c, //CH_NO,配置频段在430MHZ 0x0c, //输出功率为10db,不重发,节电为正常模式 0x44, //地址宽度设置,为4字节 0x04,0x04, //接收发送有效数据长度为32字节 0xCC,0xCC,0xCC,0xCC, //接收地址 0x58, //CRC充许,8位CRC校验,外部时钟信号不使能,16M晶振 }; uchar TxAddress[4]={0xcc,0xcc,0xcc,0xcc}; char tf; //------------------------------------------------延时------------------------------------------------------------ //---------------------------------------------------SPI读函数----------------------------------------------------- unsigned char SpiRead(void) { unsigned char j; for (j=0;j<8;j++) { DATA_BUF=DATA_BUF<<1; SCK_1; if (MISO) //读取最高位,保存至最末尾,通过左移位完成整个字节 { DATA_BUF|=BIT(0); } else { DATA_BUF&=~BIT(0); } SCK_0; } return DATA_BUF; } //-------------------------------------------------SPI写函数---------------------------------------------------------- void SpiWrite(unsigned char send) { unsigned char i; DATA_BUF=send; for (i=0;i<8;i++) { if (DATA7) //总是发送最高位 { MOSI_1; } else { MOSI_0; } SCK_1; DATA_BUF=DATA_BUF<<1; SCK_0; } } //------------------------------------------------------初始化nRF905--------------------------------------------- void nRF905Init(void) { CSN_1; // Spi disable SCK_0; // Spi clock line init low DDRA=0XFF; PORTA=0XFF; DDRA=0X00; PWR_1; // nRF905 power on TRX_CE_0; // Set nRF905 in standby mode TXEN_0; // set radio in Rx mode } //-----------------------------------------------------初始化寄存器----------------------------------------------- void Config905(void) { uchar i; CSN_0; // Spi enable for write a spi command //SpiWrite(WC); // Write config command写放配置命令 for (i=0;i<11;i++) // Write configration words 写放配置字 { SpiWrite(RFConf[i]); } CSN_1; // Disable Spi } //-----------------------------------------------------发送数据打包 --------------------------------------------------- void TxPacket(uchar *TxRxBuf) { uchar i; //Config905(); CSN_0; SpiWrite(WTP); // Write payload command for (i=0;i<4;i++) { SpiWrite(TxRxBuf[i]); // Write 32 bytes Tx data }// Spi enable for write a spi command CSN_1; delay_ms(1); // Spi disable CSN_0; // Spi enable for write a spi command SpiWrite(WTA); // Write address command for (i=0;i<4;i++) // Write 4 bytes address { SpiWrite(TxAddress[i]); } CSN_1; // Spi disable TRX_CE_1; // Set TRX_CE high,start Tx data transmission delay_ms(1); // // while (DR!=1); TRX_CE_0; // Set TRX_CE low } //----------------------------------------------------------设置发送状态 --------------------------------------------- void SetTxMode(void) { TRX_CE_0; TXEN_1; delay_ms(1); // delay for mode change(>=650us) } //-----------------------------------------------设置发送状态 --------------------------------------------------- void SetRxMode(void) { TXEN_0; TRX_CE_1; delay_ms(1); // delay for mode change(>=650us) } //-------------------------------------------------判断数据接收状态 ----------------------------------------------------- unsigned char CheckDR(void) //检查是否有新数据传入 Data Ready { DDRB=((0<检查是否已存在同频率载波 { if (CD==1) { return 1; } else { return 0; } } //------------------------------------------------------------------------------ ----------------------------------- void main(void) { uchar i; DDRB=0XF7; DDRA=0X00; DDRC=0XfF; nRF905Init(); Config905(); LCD_init(); while(1) { RX(); LCD_write_char(0,0,num1[TxRxBuf[0]]); w_12864(1,num1[TxRxBuf[1]]); w_12864(1,':'); w_12864(1,num1[TxRxBuf[2]]); w_12864(1,num1[TxRxBuf[3]]); w_12864(1,num1[TxRxBuf[4]]); //LCD_write_string(0,1,TxRxBuf1); //CSN_1; delay_ms(1); } }
本文档为【nRF905程序】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
下载需要: 免费 已有0 人下载
最新资料
资料动态
专题动态
is_477730
暂无简介~
格式:doc
大小:48KB
软件:Word
页数:21
分类:互联网
上传时间:2017-09-27
浏览量:22