最基础的收发程序,可以在此基础上完成WOR后续功能的添加
单片机源程序如下:
- #include <in430.h>
- #include <io430.h>
- #include "cc1100.h"
- #define INT8U unsigned char
- #define INT16U unsigned int
- unsigned char PaTabel[8] = {0xC0 ,0xC0 ,0xC0 ,0xC0 ,0xC0 ,0xC0 ,0xC0 ,0xC0};
- unsigned int TestInt = 0;
- const RF_SETTINGS rfSettings =
- {
- 0x00,
- 0x08, // FSCTRL1 Frequency synthesizer control.
- 0x00, // FSCTRL0 Frequency synthesizer control.
- 0x10, // FREQ2 Frequency control word, high byte.
- 0xA7, // FREQ1 Frequency control word, middle byte.
- 0x62, // FREQ0 Frequency control word, low byte.
- 0x5B, // MDMCFG4 Modem configuration.
- 0xF8, // MDMCFG3 Modem configuration.
- 0x03, // MDMCFG2 Modem configuration.
- 0x22, // MDMCFG1 Modem configuration.
- 0xF8, // MDMCFG0 Modem configuration.
- 0x00, // CHANNR Channel number.
- 0x47, // DEVIATN Modem deviation setting (when FSK modulation is enabled).
- 0xB6, // FREND1 Front end RX configuration.
- 0x10, // FREND0 Front end RX configuration.
- 0x18, // MCSM0 Main Radio Control State Machine configuration.
- 0x1D, // FOCCFG Frequency Offset Compensation Configuration.
- 0x1C, // BSCFG Bit synchronization Configuration.
- 0xC7, // AGCCTRL2 AGC control.
- 0x00, // AGCCTRL1 AGC control.
- 0xB2, // AGCCTRL0 AGC control.
- 0xEA, // FSCAL3 Frequency synthesizer calibration.
- 0x2A, // FSCAL2 Frequency synthesizer calibration.
- 0x00, // FSCAL1 Frequency synthesizer calibration.
- 0x11, // FSCAL0 Frequency synthesizer calibration.
- 0x59, // FSTEST Frequency synthesizer calibration.
- 0x81, // TEST2 Various test settings.
- 0x35, // TEST1 Various test settings.
- 0x09, // TEST0 Various test settings.
- 0x0B, // IOCFG2 GDO2 output pin configuration.
- 0x06, // IOCFG0D GDO0 output pin configuration. Refer to SmartRF?Studio User Manual for detailed pseudo register explanation.
- 0x04, // PKTCTRL1 Packet automation control.
- 0x05, // PKTCTRL0 Packet automation control.
- 0x01, // ADDR Device address.
- 0x0c // PKTLEN Packet length.
- };
- void delay(unsigned int s)
- {
- while (s != 0)
- {
- s--;
- }
- }
- void halWait(unsigned int s)
- {
- do
- {
- _NOP();
- _NOP();
- _NOP();
- _NOP();
- _NOP();
- _NOP();
- _NOP();
- _NOP();
- _NOP();
- _NOP();
- _NOP();
- _NOP();
- _NOP();
- _NOP();
- _NOP();
- _NOP();
- }
- while(--s);
- }
- void InitSpi(void)
- {
- CSN_L;
- SCK_L;
- CSN_H;
- }
- void halSpiWriteReg(unsigned char addr, unsigned char value)
- {
- CSN_L;
- while (GetMISO);
- SpiTxRxByte(addr); //写地址
- SpiTxRxByte(value); //写入配置
- CSN_H;
- }
- void halSpiWriteBurstReg(INT8U addr, INT8U *buffer, INT8U count)
- {
- INT8U i, temp;
- temp = addr | WRITE_BURST;
- CSN_L;
- while (GetMISO);
- SpiTxRxByte(temp);
- for (i = 0; i < count; i++)
- {
- SpiTxRxByte(buffer[i]);
- }
- CSN_H;
- }
- void halSpiStrobe(INT8U strobe)
- {
- CSN_L;
- while (GetMISO);
- SpiTxRxByte(strobe); //写入命令
- CSN_H;
- }
- INT8U halSpiReadReg(INT8U addr)
- {
- INT8U temp, value;
- temp = addr|READ_SINGLE;//读寄存器命令
- CSN_L;
- while (GetMISO);
- SpiTxRxByte(temp);
- value = SpiTxRxByte(0);
- CSN_H;
- return value;
- }
- void halSpiReadBurstReg(INT8U addr, INT8U *buffer, INT8U count)
- {
- INT8U i,temp;
- temp = addr | READ_BURST; //写入要读的配置寄存器地址和读命令
- CSN_L;
- while (GetMISO);
- SpiTxRxByte(temp);
- for (i = 0; i < count; i++)
- {
- buffer[i] = SpiTxRxByte(0);
- }
- CSN_H;
- }
- INT8U halSpiReadStatus(INT8U addr)
- {
- INT8U value,temp;
- temp = addr | READ_BURST; //写入要读的状态寄存器的地址同时写入读命令
- CSN_L;
- while (GetMISO);
- SpiTxRxByte(temp);
- value = SpiTxRxByte(0);
- CSN_H;
- return value;
- }
- void halRfWriteRfSettings(void)
- {
- halSpiWriteReg(CCxxx0_FSCTRL0, rfSettings.FSCTRL2);//自已加的
- // Write register settings
- halSpiWriteReg(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);
- }
- void halRfSendPacket(INT8U *txBuffer, INT8U size)
- {
- halSpiWriteReg(CCxxx0_TXFIFO, size);
- halSpiWriteBurstReg(CCxxx0_TXFIFO, txBuffer, size); //写入要发送的数据
- halSpiStrobe(CCxxx0_SIDLE); // 网上解决GDO0不跳变的解决方法,原来没有
- halSpiStrobe(CCxxx0_STX); //进入发送模式发送数据
-
- // Wait for GDO0 to be set -> sync transmitted
- while (!GOD0);
- // Wait for GDO0 to be cleared -> end of packet
- while (GOD0);
- halSpiStrobe(CCxxx0_SFTX);
- }
- void setRxMode(void)
- {
- halSpiStrobe(CCxxx0_SRX); //进入接收状态
- }
- INT8U halRfReceivePacket(INT8U *rxBuffer, INT8U *length)
- {
- INT8U status[2];
- INT8U packetLength;
- INT8U i=(*length)*4; // 具体多少要根据datarate和length来决定
-
- halSpiStrobe(CCxxx0_SRX); //进入接收状态
-
- halWait(2000);
- while (GOD0)
- {
- halWait(1000);
- --i;
- if(i<1)
- {
- return 0;
- }
- }
- if ((halSpiReadStatus(CCxxx0_RXBYTES) & BYTES_IN_RXFIFO)) //如果接的字节数不为0
- {
- packetLength = halSpiReadReg(CCxxx0_RXFIFO);//读出第一个字节,此字节为该帧数据长度
- if (packetLength <= *length) //如果所要的有效数据长度小于等于接收到的数据包的长度
- {
- halSpiReadBurstReg(CCxxx0_RXFIFO, rxBuffer, packetLength); //读出所有接收到的数据
- *length = packetLength; //把接收数据长度的修改为当前数据的长度
-
- // Read the 2 appended status bytes (status[0] = RSSI, status[1] = LQI)
- halSpiReadBurstReg(CCxxx0_RXFIFO, status, 2); //读出CRC校验位
- halSpiStrobe(CCxxx0_SFRX); //清洗接收缓冲区
- return (status[1] & CRC_OK); //如果校验成功返回接收成功
- }
- else
- {
- *length = packetLength;
- halSpiStrobe(CCxxx0_SFRX); //清洗接收缓冲区
- return 0;
- }
- }
- else
- {
- return 0;
- }
- }
- unsigned char SpiTxRxByte(unsigned char dat)
- {
- unsigned char i,temp;
- temp = 0;
- SCK_L;
- for(i=0; i<8; i++)
- {
- if(dat & 0x80)
- {
- MOSI_H;
- }
- else
- {
- MOSI_L;
- }
- dat <<= 1;
- SCK_H;
- delay(20);
- temp <<= 1;
-
- if(GetMISO)
- {
- temp++;
- }
- SCK_L;
- delay(20);
- }
-
- return temp;
- }
- unsigned int RESET_CC1100(void)
- {
- CSN_L;
- while (GetMISO);
- unsigned int ret = SpiTxRxByte(CCxxx0_SRES); //写入复位命令
- while (GetMISO);
- CSN_H;
- return ret;
- }
- unsigned int POWER_UP_RESET_CC1100(void)
- {
- CSN_H;
- halWait(1);
- CSN_L;
- halWait(1);
- CSN_H;
- halWait(41);
- unsigned int ret = RESET_CC1100(); //复位CC1100
- return ret;
- }
复制代码
所有资料51hei提供下载:
XB.zip
(97.67 KB, 下载次数: 46)
|