CC1101 433無(wú)線(xiàn)模塊,STM8串口透?jìng)?/h1>
時(shí)間:2019-06-11 10:16:04
關(guān)鍵字:
cc1101
stm8
433無(wú)線(xiàn)模塊
手機(jī)看文章掃描二維碼
隨時(shí)隨地手機(jī)看文章
掃描二維碼
隨時(shí)隨地手機(jī)看文章
前一段時(shí)間畫(huà)了一個(gè)CC1101收發(fā)小板子,但是一直沒(méi)有成功,最后發(fā)現(xiàn)把27nH的電感焊接成了27uH,最終導(dǎo)致失敗.
現(xiàn)在更換之后可以覆蓋我們公司所有的角落,已經(jīng)夠用了.
下面是STM8程序
CC1101.C
/************************************************************************************************************* ?*?文件名: CC1101.c ?*?功能: STM8?CC1101驅(qū)動(dòng)函數(shù) ?*?作者: cp1300@139.com *?創(chuàng)建時(shí)間: 2013-12-06?14:35 ?*?最后修改時(shí)間:2013-12-06 ?*?詳細(xì): 433MHZ收發(fā)芯片驅(qū)動(dòng) *************************************************************************************************************/ #include?"system.h" #include?"delay.h" #include?"CC1101.h" //CC1101命令掩碼 #define? WRITE_BURST????? 0x40 //連續(xù)寫(xiě)入 #define? READ_SINGLE????? 0x80 //讀 #define? WRITE_SINGLE????? 0x00 //寫(xiě) #define? READ_BURST??????????0xC0 //連續(xù)讀 #define? BURST_READ_FIFO 0xff //突發(fā)讀取RX?FIFO #define? BYTE_READ_FIFO 0xBF //單字節(jié)讀取?RX?FIFO #define? BURST_WRITE_FIFO 0x7f //突發(fā)寫(xiě)TX?FIFO #define? BYTE_WRITE_FIFO 0x3f //單字節(jié)寫(xiě)?TX?FIFO #define?CC1101_DATA_LEN 64 //SPI接口 //底層接口宏定義 #define?CC1101_CS_H()??? (GPIOA->ODR|=BIT3)?????? //PA3=1 #define?CC1101_CS_L()??? (GPIOA->ODR&=~BIT3)????? //PA3=0 #define?CC1101_MOSI_H() (GPIOC->ODR|=BIT6) //PC6 #define?CC1101_MOSI_L() (GPIOC->ODR&=~BIT6) //PC6 #define?CC1101_SCLK_H() (GPIOC->ODR|=BIT5) //PC5 #define?CC1101_SCLK_L() (GPIOC->ODR&=~BIT5) //PC5 #define?CC1101_GetMISO() (GPIOC->IDR&BIT7) //PC7 //CC1101?SPI讀寫(xiě)一字節(jié) //不帶片選 u8?CC1101_ReadWriteByte(u8?data) { u8?i; u8?temp?=?0; for(i?=?0;i?<?8;i?++) { if(data?&?0x80) { CC1101_MOSI_H(); } else { CC1101_MOSI_L(); } data?<<=?1;nop; CC1101_SCLK_H(); ????????????//時(shí)鐘上升沿寫(xiě)入數(shù)據(jù) temp?<?0x80)?return?0; ????????????????????????//防止誤操作,0x30以后的寄存器為只讀狀態(tài)寄存器 CC1101_CS_L(); ????????????????????????????????????????????//片選有效 ????while(CC1101_GetMISO()); status?=?CC1101_ReadWriteByte((u8)WRITE_SINGLE|RegAddr); //發(fā)送寫(xiě)命令以及寄存器索引 CC1101_ReadWriteByte(data); ????????????????????????//寫(xiě)入數(shù)據(jù) CC1101_CS_H(); ????????????????????????????????????????????//片選關(guān)閉 return?status; } #include?"LED.h" void?CC1101_Init(u8?Addr) { //初始化片選 ????GPIOx_Init(GPIOA,?BIT3,?OUT_PP_10M); ????CC1101_CS_H(); //初始化SCLK GPIOx_Init(GPIOC,?BIT5,?OUT_PP_10M); ????CC1101_SCLK_H(); //初始化MOSI GPIOx_Init(GPIOC,?BIT6,?OUT_PP_10M); ????CC1101_MOSI_H(); //初始化MISO GPIOx_Init(GPIOC,?BIT7,?IN_UP); CC1101_SCLK_L(); CC1101_MOSI_L(); ????//初始化GDO0,GDO2對(duì)應(yīng)PC3,PC4 ????GPIOx_Init(GPIOC,?BIT3,?IN_UP); ????GPIOx_Init(GPIOC,?BIT4,?IN_UP); //初始化寄存器 CC1101_Command(CC1101_CMD_SRES); ????????????????//復(fù)位 Delay_MS(10); while(CC1101_ReadReg(CC1101_REG_AGCTEST)?!=?0x3F) //檢測(cè)通信 { LED_ON(); Delay_MS(10); LED_OFF(); Delay_MS(100); } LED_OFF(); ????CC1101_WriteReg(CC1101_REG_IOCFG0,0x06);????????????//發(fā)送提示引腳 ????CC1101_WriteReg(CC1101_REG_IOCFG2,0x01);????????????//接收提示引腳 ????CC1101_WriteReg(CC1101_REG_FIFOTHR,0x0f);???????????//RX?FIFO和TX?FIFO門(mén)限 ????CC1101_WriteReg(CC1101_REG_SYNC1,0xD3);?????????????//同步詞匯,高字節(jié) ????CC1101_WriteReg(CC1101_REG_SYNC0,0x91);?????????????//同步詞匯,低字節(jié) ????CC1101_WriteReg(CC1101_REG_PKTLEN,CC1101_DATA_LEN);?//數(shù)據(jù)包長(zhǎng)度,255 ????CC1101_WriteReg(CC1101_REG_PKTCTRL1,0x04);??????????//數(shù)據(jù)包自動(dòng)控制 ????CC1101_WriteReg(CC1101_REG_PKTCTRL0,0x04);??????????//數(shù)據(jù)包自動(dòng)控制 ????CC1101_WriteReg(CC1101_REG_ADDR,0x00);??????????????//設(shè)備地址 ????CC1101_WriteReg(CC1101_REG_CHANNR,0x00);????????????//信道 ????CC1101_WriteReg(CC1101_REG_FSCTRL1,0x06);???????????//頻率合成器控制,高字節(jié) ????CC1101_WriteReg(CC1101_REG_FSCTRL0,0x00);???????????//頻率合成器控制,低字節(jié) ????CC1101_WriteReg(CC1101_REG_FREQ2,0x10);?????????????//頻率控制詞匯,高字節(jié) ????CC1101_WriteReg(CC1101_REG_FREQ1,0xb1);?????????????//頻率控制詞匯,中間字節(jié) ????CC1101_WriteReg(CC1101_REG_FREQ0,0x3b);?????????????//頻率控制詞匯,低字節(jié) //2.4KBPS ????CC1101_WriteReg(CC1101_REG_MDMCFG4,0xF6);???????????//調(diào)制器配置 ????CC1101_WriteReg(CC1101_REG_MDMCFG3,0x83);???????????//調(diào)制器配置 ????CC1101_WriteReg(CC1101_REG_MDMCFG2,0x13);???????????//調(diào)制器配置 ????CC1101_WriteReg(CC1101_REG_MDMCFG1,0x22);???????????//調(diào)制器配置 ????CC1101_WriteReg(CC1101_REG_MDMCFG0,0xf8);???????????//調(diào)制器配置 ????CC1101_WriteReg(CC1101_REG_DEVIATN,0x15);???????????//調(diào)制器背離設(shè)置 ????CC1101_WriteReg(CC1101_REG_MCSM2,0x07);?????????????//主通信控制狀態(tài)機(jī)配置 ????CC1101_WriteReg(CC1101_REG_MCSM1,0x30);?????????????//主通信控制狀態(tài)機(jī)配置 ????CC1101_WriteReg(CC1101_REG_MCSM0,0x18);?????????????//主通信控制狀態(tài)機(jī)配置 ????CC1101_WriteReg(CC1101_REG_FOCCFG,0x16);????????????//頻率偏移補(bǔ)償配置 ????CC1101_WriteReg(CC1101_REG_BSCFG,0x6c);?????????????//位同步配置 ????CC1101_WriteReg(CC1101_REG_AGCTRL2,0x03);???????????//AGC控制 ????CC1101_WriteReg(CC1101_REG_AGCTRL1,0x40);???????????//AGC控制 ????CC1101_WriteReg(CC1101_REG_AGCTRL0,0x91);???????????//AGC控制 ????CC1101_WriteReg(CC1101_REG_WOREVT1,0x87);???????????//高字節(jié)時(shí)間0暫停 ????CC1101_WriteReg(CC1101_REG_WOREVT0,0x6b);???????????//低字節(jié)時(shí)間0暫停 ????CC1101_WriteReg(CC1101_REG_WORCTRL,0xfb);???????????//電磁波激活控制 ????CC1101_WriteReg(CC1101_REG_FREND1,0x56);????????????//前末端RX配置 ????CC1101_WriteReg(CC1101_REG_FREND0,0x10);????????????//前末端TX配置 ????CC1101_WriteReg(CC1101_REG_FSCAL3,0xe9);????????????//頻率合成器校準(zhǔn) ????CC1101_WriteReg(CC1101_REG_FSCAL2,0x2a);????????????//頻率合成器校準(zhǔn) ????CC1101_WriteReg(CC1101_REG_FSCAL1,0x00);????????????//頻率合成器校準(zhǔn) ????CC1101_WriteReg(CC1101_REG_FSCAL0,0x1f);????????????//頻率合成器校準(zhǔn) ????CC1101_WriteReg(CC1101_REG_RCCTRL1,0x41);???????????//RC振蕩器配置 ????CC1101_WriteReg(CC1101_REG_RCCTRL0,0x00);???????????//RC振蕩器配置 ????CC1101_WriteReg(CC1101_REG_FSTEST,0x59);????????????//頻率合成器校準(zhǔn)控制 //10DB //CC1101_WriteReg(CC1101_REG_PATABLE0,0xc0);? //CC1101_WriteReg(CC1101_REG_PATABLE1,0xc0);? /*CC1101_WriteReg(CC1101_REG_PATABLE2,0xc0);? CC1101_WriteReg(CC1101_REG_PATABLE3,0xc0);? CC1101_WriteReg(CC1101_REG_PATABLE4,0xc0);? CC1101_WriteReg(CC1101_REG_PATABLE5,0xc0);? CC1101_WriteReg(CC1101_REG_PATABLE6,0xc0);? CC1101_WriteReg(CC1101_REG_PATABLE7,0xc0);?*/ Delay_MS(10); } /************************************************************************************************************************* *?函數(shù) : void?CC1101_WriteTxFIFO(u8?*pBuff,u8?len) *?功能 : 寫(xiě)入數(shù)據(jù)到發(fā)送緩沖區(qū) *?參數(shù) : pBuff:需要寫(xiě)入的數(shù)據(jù)緩沖區(qū)指針,len:需要寫(xiě)入的數(shù)據(jù)長(zhǎng)度 *?返回 : 無(wú) *?依賴(lài) : 底層宏定義 *?作者 : cp1300@139.com *?時(shí)間 : 2014-01-01 *?最后修改時(shí)間?:?2014-01-01 *?說(shuō)明 :? 寫(xiě)入數(shù)據(jù)到發(fā)送FIFO *************************************************************************************************************************/ void?CC1101_WriteTxFIFO(u8?*pBuff,u8?len) { ????u16?i; ????CC1101_CS_L(); ????CC1101_ReadWriteByte(BURST_WRITE_FIFO); ????for(i?=?0;i?<?len;i?++) ????{ ????????CC1101_ReadWriteByte(pBuff[i]); ????} ????CC1101_CS_H(); } /************************************************************************************************************************* *?函數(shù) : void?CC1101_ReadRxFIFO(u8?*pBuff,u8?len) *?功能 : 讀取接收緩沖區(qū) *?參數(shù) : pBuff:需要讀取的數(shù)據(jù)緩沖區(qū)指針,len:需要讀取的數(shù)據(jù)長(zhǎng)度 *?返回 : 無(wú) *?依賴(lài) : 底層宏定義 *?作者 : cp1300@139.com *?時(shí)間 : 2014-01-01 *?最后修改時(shí)間?:?2014-01-01 *?說(shuō)明 :? 從接收FIFO讀取數(shù)據(jù) *************************************************************************************************************************/ void?CC1101_ReadRxFIFO(u8?*pBuff,u8?len) { ????u16?i; ????CC1101_CS_L(); ????CC1101_ReadWriteByte(BURST_READ_FIFO); ????for(i?=?0;i?<?len;i?++) ????{ ????????pBuff[i]?=?CC1101_ReadWriteByte(0xff); ????} ????CC1101_CS_H(); } //發(fā)送數(shù)據(jù),將緩沖區(qū)數(shù)據(jù)全部發(fā)送出去 //一次最多64B,因?yàn)槭艿紽IFO限制 void?CC1101_RfDataSend(u8?*pBuff,u8?len) { ????CC1101_Command(CC1101_CMD_SIDLE);???//退出當(dāng)前模式 ????CC1101_Command(CC1101_CMD_SFTX);??? //清空發(fā)送緩沖區(qū) ????CC1101_WriteTxFIFO(pBuff,?len);???? //寫(xiě)入數(shù)據(jù)到發(fā)送緩沖區(qū) ????CC1101_Command(CC1101_CMD_STX);??? //開(kāi)始發(fā)送數(shù)據(jù) ????while(!CC1101_GDO0); ????while(CC1101_GDO0); ????CC1101_Command(CC1101_CMD_SIDLE);???//退出當(dāng)前模式 } //發(fā)送數(shù)據(jù)包 //每次發(fā)送最多65B,第一字節(jié)為長(zhǎng)度,數(shù)據(jù)多將會(huì)重復(fù)發(fā)送 //可以發(fā)送任意大小 //CC1101PackSize有效數(shù)據(jù)包大小,0-64,也就是CC1101單次發(fā)送數(shù)據(jù)大小-1 void?CC1101_RfDataSendPack(u8?*pBuff,?u16?len) { u16?i,m,n,j; m?=?len?/?(CC1101_DATA_LEN-1); //整數(shù)數(shù)據(jù)幀數(shù)量 n?=?len?%?(CC1101_DATA_LEN-1); //余數(shù) //發(fā)送整數(shù)包 for(i?=?0;i?<?m;i?++) { Delay_MS(1); CC1101_Command(CC1101_CMD_SIDLE);???//退出當(dāng)前模式 ???? CC1101_Command(CC1101_CMD_SFTX);??? //清空發(fā)送緩沖區(qū) CC1101_CS_L(); CC1101_ReadWriteByte(BURST_WRITE_FIFO); CC1101_ReadWriteByte(CC1101_DATA_LEN-1);//先寫(xiě)入包大小 for(j?=?0;j?<?(CC1101_DATA_LEN-1);j?++) { CC1101_ReadWriteByte(*pBuff++); //寫(xiě)入數(shù)據(jù)到發(fā)送緩沖區(qū) } CC1101_CS_H(); CC1101_Command(CC1101_CMD_STX);??? //開(kāi)始發(fā)送數(shù)據(jù) while(!CC1101_GDO0); while(CC1101_GDO0); //等待發(fā)送完成 } //發(fā)送余數(shù)包 if(n!=0) { Delay_MS(1); CC1101_Command(CC1101_CMD_SIDLE);???//退出當(dāng)前模式 ???? CC1101_Command(CC1101_CMD_SFTX);??? //清空發(fā)送緩沖區(qū) CC1101_CS_L(); CC1101_ReadWriteByte(BURST_WRITE_FIFO); CC1101_ReadWriteByte(n); //先寫(xiě)入包大小 for(j?=?0;j?<?n;j?++) { CC1101_ReadWriteByte(*pBuff++); //寫(xiě)入數(shù)據(jù)到發(fā)送緩沖區(qū) } CC1101_CS_H(); CC1101_Command(CC1101_CMD_STX);??? //開(kāi)始發(fā)送數(shù)據(jù) while(!CC1101_GDO0); while(CC1101_GDO0); //等待發(fā)送完成 } ????CC1101_Command(CC1101_CMD_SIDLE);???//退出當(dāng)前模式 } //讀取芯片狀態(tài) u8?CC1101_GetStatus(void) { ????return?CC1101_WriteReg(CC1101_REG_TEST2,?0x98); }
CC1101.H
/************************************************************************************************************* ?*?文件名: CC1101.c ?*?功能: STM8?CC1101驅(qū)動(dòng)函數(shù) ?*?作者: cp1300@139.com *?創(chuàng)建時(shí)間: 2013-12-06?14:35 ?*?最后修改時(shí)間:2013-12-06 ?*?詳細(xì): 433MHZ收發(fā)芯片驅(qū)動(dòng) *************************************************************************************************************/ #ifndef?_CC1101_H_ #define?_CC1101_H_ #include?"system.h" //CC1101?命令 //以寫(xiě)的方式單直接訪(fǎng)問(wèn)將觸發(fā)響應(yīng)的命令 typedef?enum { CC1101_CMD_SRES = 0x30, //重啟 CC1101_CMD_SFSTXON = 0x31, //開(kāi)啟和校準(zhǔn)頻率合成器(若MCSM0.FSAUTOCAL=1) CC1101_CMD_SXOFF = 0x32, //關(guān)閉晶體振蕩器 CC1101_CMD_SCAL = 0x33, //校準(zhǔn)頻率合成器并關(guān)斷(開(kāi)啟快速啟動(dòng))。在不設(shè)置手動(dòng)校準(zhǔn)模式(MCSM0.FS_AUTOCAL=0)的情況下,SCAL能從空閑模式濾波。 CC1101_CMD_SRX = 0x34, //啟用RX。若上一狀態(tài)為空閑且MCSM0.FS_AUTOCAL=1則首先運(yùn)行校準(zhǔn)。 CC1101_CMD_STX = 0x35, //空閑狀態(tài):?jiǎn)⒂肨X。若MCSM0.FS_AUTOCAL=1首先運(yùn)行校準(zhǔn)。若在RX狀態(tài)且CCA啟用:若信道為空則進(jìn)入TX CC1101_CMD_SIDLE = 0x36, //離開(kāi)RX/TX,關(guān)斷頻率合成器并離開(kāi)電磁波激活模式若可用 CC1101_CMD_SAFC = 0x37, //運(yùn)行22.1節(jié)列出的頻率合成器的AFC調(diào)節(jié) CC1101_CMD_SWOR = 0x38, //運(yùn)行27.5節(jié)描述的自動(dòng)RX選舉序列(電磁波激活) CC1101_CMD_SPWD = 0x39, //當(dāng)CSn為高時(shí)進(jìn)入功率降低模式。 CC1101_CMD_SFRX = 0x3a, //沖洗RX?FIFO緩沖 CC1101_CMD_SFTX = 0x3b, //沖洗TX?FIFO緩沖 CC1101_CMD_SWORRST = 0x3c, //重新設(shè)置真實(shí)時(shí)間時(shí)鐘 CC1101_CMD_SNOP = 0x3d, //無(wú)操作??赡苡脕?lái)為更簡(jiǎn)單的軟件將濾波命令變?yōu)?字節(jié)。 }CC1101_CMD_TYPE; //CC1101寄存器定義 typedef?enum { //可讀寫(xiě)的寄存器 CC1101_REG_IOCFG2 = 0x00, //GDO2輸出腳配置 CC1101_REG_IOCFG1 = 0x01, //GDO1輸出腳配置 CC1101_REG_IOCFG0 = 0x02, //GDO0輸出腳配置 CC1101_REG_FIFOTHR = 0x03, //RX?FIFO和TX?FIFO門(mén)限 CC1101_REG_SYNC1 = 0x04, //同步詞匯,高字節(jié) CC1101_REG_SYNC0 = 0x05, //同步詞匯,低字節(jié) CC1101_REG_PKTLEN = 0x06, //數(shù)據(jù)包長(zhǎng)度 CC1101_REG_PKTCTRL1 = 0x07, //數(shù)據(jù)包自動(dòng)控制 CC1101_REG_PKTCTRL0 = 0x08, //數(shù)據(jù)包自動(dòng)控制 CC1101_REG_ADDR = 0x09, //設(shè)備地址 CC1101_REG_CHANNR = 0x0a, //信道數(shù) CC1101_REG_FSCTRL1 = 0x0b, //頻率合成器控制,高字節(jié) CC1101_REG_FSCTRL0 = 0x0c, //頻率合成器控制,低字節(jié) CC1101_REG_FREQ2 = 0x0d, //頻率控制詞匯,高字節(jié) CC1101_REG_FREQ1 = 0x0e, //頻率控制詞匯,中間字節(jié) CC1101_REG_FREQ0 = 0x0f, //頻率控制詞匯,低字節(jié) CC1101_REG_MDMCFG4 = 0x10, //調(diào)制器配置 CC1101_REG_MDMCFG3 = 0x11, //調(diào)制器配置 CC1101_REG_MDMCFG2 = 0x12, //調(diào)制器配置 CC1101_REG_MDMCFG1 = 0x13, //調(diào)制器配置 CC1101_REG_MDMCFG0 = 0x14, //調(diào)制器配置 CC1101_REG_DEVIATN = 0x15, //調(diào)制器背離設(shè)置 CC1101_REG_MCSM2 = 0x16, //主通信控制狀態(tài)機(jī)配置 CC1101_REG_MCSM1 = 0x17, //主通信控制狀態(tài)機(jī)配置 CC1101_REG_MCSM0 = 0x18, //主通信控制狀態(tài)機(jī)配置 CC1101_REG_FOCCFG = 0x19, //頻率偏移補(bǔ)償配置 CC1101_REG_BSCFG = 0x1a, //位同步配置 CC1101_REG_AGCTRL2 = 0x1b, //AGC控制 CC1101_REG_AGCTRL1 = 0x1c, //AGC控制 CC1101_REG_AGCTRL0 = 0x1d, //AGC控制 CC1101_REG_WOREVT1 = 0x1e, //高字節(jié)時(shí)間0暫停 CC1101_REG_WOREVT0 = 0x1f, //低字節(jié)時(shí)間0暫停 CC1101_REG_WORCTRL = 0x20, //電磁波激活控制 CC1101_REG_FREND1 = 0x21, //前末端RX配置 CC1101_REG_FREND0 = 0x22, //前末端TX配置 CC1101_REG_FSCAL3 = 0x23, //頻率合成器校準(zhǔn) CC1101_REG_FSCAL2 = 0x24, //頻率合成器校準(zhǔn) CC1101_REG_FSCAL1 = 0x25, //頻率合成器校準(zhǔn) CC1101_REG_FSCAL0 = 0x26, //頻率合成器校準(zhǔn) CC1101_REG_RCCTRL1 = 0x27, //RC振蕩器配置 CC1101_REG_RCCTRL0 = 0x28, //RC振蕩器配置 CC1101_REG_FSTEST = 0x29, //頻率合成器校準(zhǔn)控制 CC1101_REG_PTEST = 0x2a, //產(chǎn)品測(cè)試 CC1101_REG_AGCTEST = 0x2b, //AGC測(cè)試 CC1101_REG_TEST2 = 0x2c, //不同的測(cè)試設(shè)置 CC1101_REG_TEST1 = 0x2d, //不同的測(cè)試設(shè)置 CC1101_REG_TEST0 = 0x2e, //不同的測(cè)試設(shè)置 //只讀的狀態(tài)寄存器,如果寫(xiě)入將導(dǎo)致命令濾波 CC1101_REG_PARTNUM = 0xf0, //CC2550的組成部分?jǐn)?shù)目 CC1101_REG_VERSION = 0xf1, //當(dāng)前版本數(shù) CC1101_REG_FREQEST = 0xf2, //頻率偏移估計(jì) CC1101_REG_LQI = 0xf3, //連接質(zhì)量的解調(diào)器估計(jì) CC1101_REG_RSSI = 0xf4, //接收信號(hào)強(qiáng)度指示 CC1101_REG_MARCSTATE = 0xf5, //控制狀態(tài)機(jī)狀態(tài) CC1101_REG_WORTIME1 = 0xf6, //WOR計(jì)時(shí)器高字節(jié) CC1101_REG_WORTIME0 = 0xf7, //WOR計(jì)時(shí)器低字節(jié) CC1101_REG_PKTSTATUS = 0xf8, //當(dāng)前GDOx狀態(tài)和數(shù)據(jù)包狀態(tài) CC1101_REG_VCOVCDAC = 0xf9, //PLL校準(zhǔn)模塊的當(dāng)前設(shè)定 CC1101_REG_TXBYTES = 0xfA, //TX?FIFO中的下溢和比特?cái)?shù) CC1101_REG_RXBYTES = 0xfB, //RX?FIFO中的下溢和比特?cái)?shù) //手冊(cè)上面沒(méi)有的 CC1101_REG_STATUS1 = 0xfc, // CC1101_REG_STATUS0 = 0xfd, // //功率控制 CC1101_REG_PATABLE0 = 0x40, CC1101_REG_PATABLE1 = 0x41, CC1101_REG_PATABLE2 = 0x42, CC1101_REG_PATABLE3 = 0x43, CC1101_REG_PATABLE4 = 0x44, CC1101_REG_PATABLE5 = 0x45, CC1101_REG_PATABLE6 = 0x46, CC1101_REG_PATABLE7 = 0x47, }CC1101_REG_TYPE; //IO #define?CC1101_GDO0?????????????????(GPIOC->IDR&BIT3)???????????//PC3 #define?CC1101_GDO2?????????????????(GPIOC->IDR&BIT4)???????????//PC4 void?CC1101_Init(u8?Addr); //初始化CC1101 u8?CC1101_ReadReg(CC1101_REG_TYPE?RegAddr); //讀取CC1101通用寄存器 u8?CC1101_WriteReg(CC1101_REG_TYPE?RegAddr,?u8?data);//寫(xiě)入CC1101通用寄存器 u8?CC1101_Command(CC1101_CMD_TYPE?Cmd); //發(fā)送單字節(jié)命令 u8?CC1101_GetStatus(void);//讀取芯片狀態(tài) void?CC1101_RfDataSend(u8?*pBuff,u8?len); void?CC1101_ReadRxFIFO(u8?*pBuff,u8?len); //CC1101PackSize有效數(shù)據(jù)包大小,0-64,也就是CC1101單次發(fā)送數(shù)據(jù)大小-1 void?CC1101_RfDataSendPack(u8?*pBuff,?u16?len); #endif?//CC1101
MAIN.C收發(fā)透?jìng)?/p>
#include?"system.h" #include?"uart1.h" #include?"delay.h" #include?"main.h" #include?"LED.h" #include?"cc1101.h" //串口緩沖區(qū) #define?UART_BUFF_SIZE 256 //串口接收緩沖區(qū)大小 u8?UartBuff[2][UART_BUFF_SIZE]; //串口接收緩沖區(qū) u16?UartLen1?=?0; //串口接收數(shù)據(jù)長(zhǎng)度 u16?UartLen2?=?0; //串口接收數(shù)據(jù)長(zhǎng)度 //CC1101緩沖區(qū) #define?RF_BUFF_SIZE 64 //CC1101緩沖區(qū)大小 u8?RfBuff[RF_BUFF_SIZE]; //CC1101緩沖區(qū) u8?RfLen?=?0; //CC1101接收緩沖區(qū)大小 //緩沖區(qū)選擇 u8?UART_RxNum ?=?0; //串口緩沖區(qū)選擇 ? //主函數(shù) void?main(void) { u8?LastNum?=?0; u32?delay?=?0; ????SYS_ClockInit(); //初始化系統(tǒng)時(shí)鐘為內(nèi)部晶振,?16MHZ //初始化LED LED_Init(); //初始化串口 UART1_Init(115200,?ENABLE); //初始化UART1,開(kāi)啟接收中斷 UART1_SetRxBuff(UartBuff[UART_RxNum],UART_BUFF_SIZE); //設(shè)置串口1接收緩沖區(qū) CC1101_Init(0Xaa); //CC1101_WriteReg(CC1101_REG_PATABLE1,0xc0); CC1101_WriteReg(CC1101_REG_PATABLE0,0x00); CC1101_Command(CC1101_CMD_SIDLE);???//退出當(dāng)前模式 CC1101_Command(CC1101_CMD_SRX);?????//進(jìn)入接收模式 SYS_EnableInterrupt(); //開(kāi)啟系統(tǒng)中斷 while(1) ????{ if(++delay?==?120000) { delay?=?0; UartLen1?=?UART1_GetRxCnt(); //獲取接收數(shù)據(jù)數(shù)量 if(UartLen1>0) { SYS_DisableInterrupt(); //關(guān)閉中斷 LED_ON(); LastNum?=?UART_RxNum; //記錄上一次的緩沖區(qū)編號(hào) UART_RxNum?=?(~UART_RxNum)&0x01; UART1_SetRxBuff(UartBuff[UART_RxNum],UART_BUFF_SIZE); //切換串口接收緩沖區(qū) //UART1_ClearRxCnt(); //清除接收計(jì)數(shù)器 SYS_EnableInterrupt(); //開(kāi)啟系統(tǒng)中斷 CC1101_WriteReg(CC1101_REG_PATABLE0,0xc0);//開(kāi)啟天線(xiàn)增益 CC1101_RfDataSendPack(UartBuff[LastNum],?UartLen1);//CC1101發(fā)送數(shù)據(jù)包 CC1101_WriteReg(CC1101_REG_PATABLE0,0); //關(guān)閉天線(xiàn)增益 CC1101_Command(CC1101_CMD_SIDLE);??? //退出當(dāng)前模式 CC1101_Command(CC1101_CMD_SRX);????? //進(jìn)入接收模式 LED_OFF(); } } //CC1101接收到數(shù)據(jù) if(CC1101_GDO2) ????????{ ????????????LED_ON(); ????????????RfLen?=?CC1101_ReadReg(CC1101_REG_RXBYTES); CC1101_ReadRxFIFO(RfBuff,RfLen); UART1_SendData(&RfBuff[1],?RfBuff[0]);//第0B為數(shù)據(jù)長(zhǎng)度,后面的為有效數(shù)據(jù) CC1101_Command(CC1101_CMD_SIDLE);???//退出當(dāng)前模式 ???? CC1101_Command(CC1101_CMD_SFRX);??? //清除接收緩沖區(qū) CC1101_Command(CC1101_CMD_SRX);?????//進(jìn)入接收模式 LED_OFF(); ????????} ????} }
實(shí)現(xiàn)透?jìng)?調(diào)試中發(fā)現(xiàn)問(wèn)題就是接收的時(shí)候必須關(guān)閉發(fā)射增益,否則無(wú)法接收,應(yīng)該是內(nèi)部發(fā)送接收天線(xiàn)進(jìn)行了切換.
? ?