1655 views|0 replies
- Last login
- 2024-10-18
- Online Time
- 124 hours
- Prestige
- 757 points
- Points
- 576 points
|
MSP430F149 cc1101 wireless transceiver source program
[Copy link]
The microcontroller source program is as follows:
- #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, // AGCCTRL2AGC control.
- 0x00, // AGCCTRL1AGC control.
- 0xB2, // AGCCTRL0AGC 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, // PKTCTRL1Packet automation control.
- 0x05, // PKTCTRL0Packet 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); //write address
- SpiTxRxByte(value); //Write configuration
- 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); //write command
- CSN_H;
- }
- INT8U halSpiReadReg(INT8U addr)
- {
- INT8U temp, value;
- temp = addr|READ_SINGLE; //Read register command
- 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; //Write the configuration register address and read command to be read
- 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; //Write the address of the status register to be read and write the read command at the same time
- CSN_L;
- while (GetMISO);
- SpiTxRxByte(temp);
- value = SpiTxRxByte(0);
- CSN_H;
- return value;
- }
- void halRfWriteRfSettings(void)
- {
- halSpiWriteReg(CCxxx0_FSCTRL0, rfSettings.FSCTRL2);//self-added
- // 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); //Write the data to be sent
- halSpiStrobe(CCxxx0_SIDLE); // The solution to GDO0 not jumping on the Internet is not available.
- halSpiStrobe(CCxxx0_STX); //Enter the sending mode to send data
-
- // 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); //Enter receiving state
- }
- INT8U halRfReceivePacket(INT8U *rxBuffer, INT8U *length)
- {
- INT8U status[2];
- INT8U packetLength;
- INT8U i=(*length)*4; // The specific amount depends on datarate and length
- halSpiStrobe(CCxxx0_SRX); //Enter receiving state
- halWait(2000);
- while (GOD0)
- {
- halWait(1000);
- --i;
- if(i<1)
- {
- return 0;
- }
- }
- if ((halSpiReadStatus(CCxxx0_RXBYTES) & BYTES_IN_RXFIFO)) //If the number of bytes received is not 0
- {
- packetLength = halSpiReadReg(CCxxx0_RXFIFO); //Read the first byte, which is the length of the frame data
- if (packetLength <= *length) //If the required valid data length is less than or equal to the length of the received data packet
- {
- halSpiReadBurstReg(CCxxx0_RXFIFO, rxBuffer, packetLength); //Read all received data
- *length = packetLength; //Change the length of received data to the length of current data
-
- // Read the 2 appended status bytes (status[0] = RSSI, status[1] = LQI)
- halSpiReadBurstReg(CCxxx0_RXFIFO, status, 2); //Read out CRC check bit
- halSpiStrobe(CCxxx0_SFRX); //Clean the receive buffer
- return (status[1] & CRC_OK); //If the verification is successful, return to receive success
- }
- else
- {
- *length = packetLength;
- halSpiStrobe(CCxxx0_SFRX); //Clean the receive buffer
- 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); //Write reset command
- while (GetMISO);
- CSN_H;
- return right;
- }
- unsigned int POWER_UP_RESET_CC1100(void)
- {
- CSN_H;
- halWait(1);
- CSN_L;
- halWait(1);
- CSN_H;
- halWait(41);
- unsigned int ret = RESET_CC1100(); //Reset CC1100
- return right;
- }
|
|