SPI读写操作
INT8U SpiTxRxByte(INT8U dat)
{
INT8U i,temp;
temp = 0;
SCK = 0;
for(i=0; i<8; i++)
{
if(dat & 0x80)
{
MOSI = 1;
}
else MOSI = 0;
dat <<= 1;
SCK = 1;
_nop_();
_nop_();
temp <<= 1;
if(MISO)temp++;
SCK = 0;
_nop_();
_nop_();
}
return temp;
}
SPI写寄存器操作
void halSpiWriteReg(INT8U addr, INT8U value)
{
CSN = 0;
while (MISO);
SpiTxRxByte(addr); //写地址
SpiTxRxByte(value); //写入配置
CSN = 1;
}
SPI读寄存器操作
INT8U halSpiReadReg(INT8U addr)
{
INT8U temp, value;
temp = addr|READ_SINGLE;//读寄存器命令
CSN = 0;
while (MISO);
SpiTxRxByte(temp);
value = SpiTxRxByte(0);
CSN = 1;
return value;
}
CC1101初始化设置
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
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.
0x04, // PKTCTRL1 Packet automation control.
0x05, // PKTCTRL0 Packet automation control.
0x00, // ADDR Device address.
0x0c // PKTLEN Packet length.
};
数据接收流程操作
INT8U halRfReceivePacket(INT8U *rxBuffer, INT8U *length)
{
INT8U status[2];
INT8U packetLength;
INT8U i=(*length)*4; //具体多少要根据datarate和length来决定
halSpiStrobe(CCxxx0_SRX); //进入接收状态
delay(2);
while (GDO0)
{
delay(2);
--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;
}
数据发送流程操作
void halRfSendPacket(INT8U *txBuffer, INT8U size)
{
halSpiWriteReg(CCxxx0_TXFIFO, size);
halSpiWriteBurstReg(CCxxx0_TXFIFO, txBuffer, size);//写入要发送的数据
halSpiStrobe(CCxxx0_STX); //进入发送模式发送数据
// Wait for GDO0 to be set -> sync transmitted
while (!GDO0);
// Wait for GDO0 to be cleared -> end of packet
while (GDO0);
halSpiStrobe(CCxxx0_SFTX);
}