基于51的CC1101接收程序 下载本文

void halSpiWriteReg(INT8U addr, INT8U value) {

CSN = 0;

while (MISO);

SpiTxRxByte(addr); //写地址 SpiTxRxByte(value); //写入配置 CSN = 1; }

//*****************************************************************************************

//函数名:void halSpiWriteBurstReg(INT8U addr, INT8U *buffer, INT8U count) //输入:地址,写入缓冲区,写入个数 //输出:无

//功能描述:SPI连续写配置寄存器

//*****************************************************************************************

void halSpiWriteBurstReg(INT8U addr, INT8U *buffer, INT8U count) {

INT8U i, temp; temp = addr | WRITE_BURST; CSN = 0;

while (MISO);

SpiTxRxByte(temp); for (i = 0; i < count; i++) {

SpiTxRxByte(buffer[i]); }

CSN = 1; }

//*****************************************************************************************

//函数名:void halSpiStrobe(INT8U strobe) //输入:命令 //输出:无

//功能描述:SPI写命令

//*****************************************************************************************

void halSpiStrobe(INT8U strobe) {

CSN = 0;

while (MISO);

SpiTxRxByte(strobe); //写入命令 CSN = 1; }

//*****************************************************************************************

//函数名:INT8U halSpiReadReg(INT8U addr) //输入:地址

//输出:该寄存器的配置字 //功能描述: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; }

//*****************************************************************************************

//函数名:void halSpiReadBurstReg(INT8U addr, INT8U *buffer, INT8U count) //输入:地址,读出数据后暂存的缓冲区,读出配置个数 //输出:无

//功能描述:SPI连续写配置寄存器

//*****************************************************************************************

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; }

//*****************************************************************************************

//函数名:INT8U halSpiReadReg(INT8U addr) //输入:地址

//输出:该状态寄存器当前值 //功能描述:SPI读状态寄存器

//*****************************************************************************************

INT8U halSpiReadStatus(INT8U addr) {

INT8U value,temp; temp = addr | READ_BURST; //写入要读的状态寄存器的地址同时写入读命令 CSN = 0;

while (MISO);

SpiTxRxByte(temp); value = SpiTxRxByte(0); CSN = 1; return value; }

//*****************************************************************************************

//函数名:void halRfWriteRfSettings(RF_SETTINGS *pRfSettings) //输入:无 //输出:无

//功能描述:配置CC1100的寄存器

//*****************************************************************************************

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); }

INT8U halRfReceivePacket(INT8U *rxBuffer, INT8U *length) {

INT8U status[2]; INT8U packetLength;

INT8U i=(*length)*4; // 具体多少要根据datarate和length来决定 // halSpiStrobe(CCxxx0_SIDLE);

halSpiStrobe(CCxxx0_SRX); //进入接收状态 //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)) //如果接的字节数不为0 {

packetLength = halSpiReadReg(CCxxx0_RXFIFO);//读出第一个字节,此字节为该帧数据长度

if (packetLength <= *length) //如果所要的有效数据长度小于等于接收到的数据包的长度 {

halSpiReadBurstReg(CCxxx0_RXFIFO, rxBuffer, packetLength); //读出所有接收到