STC89C52單片機(jī)驅(qū)動(dòng)CC1101無線模塊的接收C語言程序 作者: 時(shí)間:2016-11-30 來源:網(wǎng)絡(luò) 加入技術(shù)交流群 掃碼加入和技術(shù)大咖面對(duì)面交流海量資料庫(kù)查詢 收藏 void halSpiReadBurstReg(INT8U addr, INT8U *buffer, INT8U count){INT8U i,temp;temp = addr | READ_BURST;//寫入要讀的配置寄存器地址和讀命令CSN = 0;while (MISO);SpiTxRxByte(temp); for (i = 0; i < count; i++){buffer[i] = SpiTxRxByte(0);}CSN = 1;}//******************************//函數(shù)名:INT8U halSpiReadReg(INT8U addr)//輸入:地址//輸出:該狀態(tài)寄存器當(dāng)前值//功能描述:SPI讀狀態(tài)寄存器//*******************************INT8U halSpiReadStatus(INT8U addr){INT8U value,temp;temp = addr | READ_BURST;//寫入要讀的狀態(tài)寄存器的地址同時(shí)寫入讀命令CSN = 0;while (MISO);SpiTxRxByte(temp);value = SpiTxRxByte(0);CSN = 1;return value;}//*********************************//函數(shù)名:void halRfWriteRfSettings(RF_SETTINGS *pRfSettings)//輸入:無//輸出:無//功能描述:配置CC1100的寄存器//********************************void halRfWriteRfSettings(void){halSpiWriteReg(CCxxx0_FSCTRL0, rfSettings.FSCTRL2);// Write register settingshalSpiWriteReg(CCxxx0_FSCTRL1, rfSettings.FSCTRL1);halSpiWriteReg(CCxxx0_FSCTRL0, rfSettings.FSCTRL0);halSpiWriteReg(CCxxx0_FREQ2, rfSettings.FREQ2);halSpiWriteReg(CCxxx0_FREQ1, rfSettings.FREQ1);halSpiWriteReg(CCxxx0_FREQ0, rfSettings.FREQ0);halSpiWriteReg(CCxxx0_MDMCFG4, rfSettings.MDMCFG4);halSpiWriteReg(CCxxx0_MDMCFG3, rfSettings.MDMCFG3);halSpiWriteReg(CCxxx0_MDMCFG2, rfSettings.MDMCFG2);halSpiWriteReg(CCxxx0_MDMCFG1, rfSettings.MDMCFG1);halSpiWriteReg(CCxxx0_MDMCFG0, rfSettings.MDMCFG0);halSpiWriteReg(CCxxx0_CHANNR, rfSettings.CHANNR);halSpiWriteReg(CCxxx0_DEVIATN, rfSettings.DEVIATN);halSpiWriteReg(CCxxx0_FREND1, rfSettings.FREND1);halSpiWriteReg(CCxxx0_FREND0, rfSettings.FREND0);halSpiWriteReg(CCxxx0_MCSM0 , rfSettings.MCSM0 );halSpiWriteReg(CCxxx0_FOCCFG, rfSettings.FOCCFG);halSpiWriteReg(CCxxx0_BSCFG, rfSettings.BSCFG);halSpiWriteReg(CCxxx0_AGCCTRL2, rfSettings.AGCCTRL2);halSpiWriteReg(CCxxx0_AGCCTRL1, rfSettings.AGCCTRL1);halSpiWriteReg(CCxxx0_AGCCTRL0, rfSettings.AGCCTRL0);halSpiWriteReg(CCxxx0_FSCAL3, rfSettings.FSCAL3);halSpiWriteReg(CCxxx0_FSCAL2, rfSettings.FSCAL2);halSpiWriteReg(CCxxx0_FSCAL1, rfSettings.FSCAL1);halSpiWriteReg(CCxxx0_FSCAL0, rfSettings.FSCAL0);halSpiWriteReg(CCxxx0_FSTEST, rfSettings.FSTEST);halSpiWriteReg(CCxxx0_TEST2, rfSettings.TEST2);halSpiWriteReg(CCxxx0_TEST1, rfSettings.TEST1);halSpiWriteReg(CCxxx0_TEST0, rfSettings.TEST0);halSpiWriteReg(CCxxx0_IOCFG2, rfSettings.IOCFG2);halSpiWriteReg(CCxxx0_IOCFG0, rfSettings.IOCFG0); halSpiWriteReg(CCxxx0_PKTCTRL1, rfSettings.PKTCTRL1);halSpiWriteReg(CCxxx0_PKTCTRL0, rfSettings.PKTCTRL0);halSpiWriteReg(CCxxx0_ADDR, rfSettings.ADDR);halSpiWriteReg(CCxxx0_PKTLEN, rfSettings.PKTLEN);}//**********************************************//函數(shù)名:void halRfSendPacket(INT8U *txBuffer, INT8U size)//輸入:發(fā)送的緩沖區(qū),發(fā)送數(shù)據(jù)個(gè)數(shù)//輸出:無//功能描述:CC1100發(fā)送一組數(shù)據(jù)//*****************************************void halRfSendPacket(INT8U *txBuffer, INT8U size){halSpiWriteReg(CCxxx0_TXFIFO, size);halSpiWriteBurstReg(CCxxx0_TXFIFO, txBuffer, size);//寫入要發(fā)送的數(shù)據(jù)halSpiStrobe(CCxxx0_STX);//進(jìn)入發(fā)送模式發(fā)送數(shù)據(jù)// Wait for GDO0 to be set -> sync transmittedwhile (!GDO0);// Wait for GDO0 to be cleared -> end of packetwhile (GDO0);halSpiStrobe(CCxxx0_SFTX);}void setRxMode(void){halSpiStrobe(CCxxx0_SRX);//進(jìn)入接收狀態(tài)}//---------------------------------------------------------------------------INT8U halRfReceivePacket(INT8U *rxBuffer, INT8U *length){INT8U status[2];INT8U packetLength;INT8U i=(*length)*4; // 具體多少要根據(jù)datarate和length來決定halSpiStrobe(CCxxx0_SRX);//進(jìn)入接收狀態(tài)//delay(5);//while (!GDO1);//while (GDO1);delay(2);while (GDO0){delay(2);--i;if(i<1)return 0; }if ((halSpiReadStatus(CCxxx0_RXBYTES) & BYTES_IN_RXFIFO)) //如果接的字節(jié)數(shù)不為0{packetLength = halSpiReadReg(CCxxx0_RXFIFO);//讀出第一個(gè)字節(jié),此字節(jié)為該幀數(shù)據(jù)長(zhǎng)度if (packetLength <= *length) //如果所要的有效數(shù)據(jù)長(zhǎng)度小于等于接收到的數(shù)據(jù)包的長(zhǎng)度{halSpiReadBurstReg(CCxxx0_RXFIFO, rxBuffer, packetLength); //讀出所有接收到的數(shù)據(jù)*length = packetLength;//把接收數(shù)據(jù)長(zhǎng)度的修改為當(dāng)前數(shù)據(jù)的長(zhǎng)度// Read the 2 appended status bytes (status[0] = RSSI, status[1] = LQI)halSpiReadBurstReg(CCxxx0_RXFIFO, status, 2); //讀出CRC校驗(yàn)位halSpiStrobe(CCxxx0_SFRX);//清洗接收緩沖區(qū)return (status[1] & CRC_OK);//如果校驗(yàn)成功返回接收成功}else{*length = packetLength;halSpiStrobe(CCxxx0_SFRX);//清洗接收緩沖區(qū)return 0;}}elsereturn 0;}//*****************************************************************************************void disdignit(){char i;if(temp[0]){for(i=0;i<3;i++){P0=0xC6;led0=0;delay1(40);led0=1;P0=seg[temp[1]];led1=0;delay1(40);led1=1;P0=seg1[temp[5]];led2=0;delay1(40);led2=1;P0=seg[temp[4]];led3=0;delay1(40);led3=1;}}}//********************************************************************************void StartUART( void ){ //波特率4800SCON = 0x50;TMOD = 0x20;TH1 = 0xFA;TL1 = 0xFA;PCON = 0x00;TR1 = 1;}void R_S_Byte(INT8U R_Byte){SBUF = R_Byte; while( TI == 0 );//查詢法TI = 0;}void main(void){INT8U i,leng =0;INT8U RxBuf[8]={0};// 8字節(jié), 如果需要更長(zhǎng)的數(shù)據(jù)包,請(qǐng)正確設(shè)置CpuInit();POWER_UP_RESET_CC1100();halRfWriteRfSettings();halSpiWriteBurstReg(CCxxx0_PATABLE, PaTabel, 8);//發(fā)射功率設(shè)置delay(6000);StartUART();while(1){leng =4; // 預(yù)計(jì)接受8 bytesif(halRfReceivePacket(RxBuf,&leng)) //判斷是否接收到數(shù)據(jù){temp[0]=RxBuf[3]; //符號(hào)位temp[2]=((RxBuf[2]<<4)|RxBuf[1]);//整數(shù)位temp[1]=RxBuf[0];//小數(shù)位temp[4]=RxBuf[2];//十位temp[5]=RxBuf[1];/*for(i=0;i<3;i++){R_S_Byte(temp[2-i]);delay(100);} */disdignit();disdignit();R_S_Byte(t);disdignit();disdignit();disdignit();R_S_Byte(0x30+temp[4]);R_S_Byte(0x30+temp[5]);R_S_Byte(.);R_S_Byte(0x30+temp[1]);disdignit();disdignit();}if(temp[2]>=0x30)//大于30度時(shí)報(bào)警,0x30轉(zhuǎn)換成10進(jìn)制為48{BELL=0; //打開蜂明器}else{BELL=1; //關(guān)閉蜂明器}}} 上一頁(yè) 1 2 3 下一頁(yè)
評(píng)論