该模块只需要发送,用的单片机为AT90S2313。程序如下: #define CS_ON PORTD&=~(1<PD3) #define CS_OFF PORTD|=(1<PD3) #define SCK_ON PORTD|=(1<PD4) #define SCK_OFF PORTD&=~(1<PD4) #define MOSI_ON PORTD|=(1<PD5) #define MOSI_OFF PORTD&=~(1<PD5)
typedef struct S_RF_SETTINGS { INT8U FSCTRL2; // INT8U FSCTRL1; // Frequency synthesizer control. INT8U FSCTRL0; // Frequency synthesizer control. INT8U FREQ2; // Frequency control word, high INT8U. INT8U FREQ1; // Frequency control word, middle INT8U. INT8U FREQ0; // Frequency control word, low INT8U. INT8U MDMCFG4; // Modem configuration. INT8U MDMCFG3; // Modem configuration. INT8U MDMCFG2; // Modem configuration. INT8U MDMCFG1; // Modem configuration. INT8U MDMCFG0; // Modem configuration. INT8U CHANNR; // Channel number. INT8U DEVIATN; // Modem deviation setting (when FSK modulation is enabled). INT8U FREND1; // Front end RX configuration. INT8U FREND0; // Front end RX configuration. INT8U MCSM0; // Main Radio Control State Machine configuration. INT8U FOCCFG; // Frequency Offset Compensation Configuration. INT8U BSCFG; // Bit synchronization Configuration. INT8U AGCCTRL2; // AGC control. INT8U AGCCTRL1; // AGC control. INT8U AGCCTRL0; // AGC control. INT8U FSCAL3; // Frequency synthesizer calibration. INT8U FSCAL2; // Frequency synthesizer calibration. INT8U FSCAL1; // Frequency synthesizer calibration. INT8U FSCAL0; // Frequency synthesizer calibration. INT8U FSTEST; // Frequency synthesizer calibration control INT8U TEST2; // Various test settings. INT8U TEST1; // Various test settings. INT8U TEST0; // Various test settings. INT8U IOCFG2; // GDO2 output pin configuration INT8U IOCFG0; // GDO0 output pin configuration INT8U PKTCTRL1; // Packet automation control. INT8U PKTCTRL0; // Packet automation control. INT8U ADDR; // Device address. INT8U PKTLEN; // Packet length. } RF_SETTINGS;
void OpenSpi() { SCK_OFF; MOSI_OFF; CS_ON; while (PIND&(1<<6)); }
void CloseSpi() { CS_OFF; }
void SPI_WriteByte(unsigned char Data) {
unsigned char i; for (i=0; i<8; i++) { if (Data&0x80) MOSI_ON; else MOSI_OFF; _NOP(); _NOP(); _NOP();
SCK_ON; _NOP(); _NOP(); _NOP(); _NOP(); SCK_OFF;
Data<<=1; } }
unsigned char SPI_ReadByte() {
unsigned char i; unsigned char Data; for (i=0; i<8; i++) {
Data<<=1; _NOP(); _NOP(); SCK_ON; if (PIND&(1<<6)) Data |= 0x01; else Data &= 0xfe; _NOP(); _NOP(); _NOP(); _NOP(); SCK_OFF; _NOP(); _NOP(); } return Data; }
void halWait(INT16U timeout) { do { _NOP(); _NOP(); _NOP(); _NOP(); _NOP(); _NOP(); _NOP(); _NOP(); _NOP(); _NOP(); _NOP(); _NOP(); _NOP(); _NOP(); } while (--timeout); }// halWait
void RESET_CC2500(void) { OpenSpi(); while (PIND&(1<<6)); SPI_WriteByte(CCxxx0_SRES); CloseSpi(); }
void POWER_UP_RESET_CCxxx0(void) { CloseSpi(); halWait(1); OpenSpi(); halWait(1); CloseSpi(); halWait(41); RESET_CC2500(); }
//<主机通过SPI接口向CC2500配置寄存器读写入信息> INT8U halSpiReadReg(INT8U addr) { INT8U temp, value; temp = addr|READ_SINGLE; //读寄存器命令 OpenSpi(); while (PIND&(1<<6)); SPI_WriteByte(temp); value = SPI_ReadByte(0); CloseSpi(); return value; }
void halSpiWriteReg(INT8U addr, INT8U value) { OpenSpi(); while(PIND&(1<<6)); SPI_WriteByte(addr); //写地址 SPI_WriteByte(value); //写入配置 CloseSpi(); }
void halSpiStrobe(BYTE strobe) { INT8U temp; temp = strobe; OpenSpi(); while (PIND&(1<<6)); SPI_WriteByte(temp); CloseSpi(); }
void halSpiWriteBurstReg(BYTE addr, BYTE *buffer, BYTE count) { INT8U i,temp; temp = addr | WRITE_BURST; OpenSpi(); while (PIND&(1<<6)); SPI_WriteByte(temp); for (i = 0; i < count; i++) { SPI_WriteByte(buffer); } CloseSpi(); }
// 配置RF2500 void halRfWriteRfSettings(RF_SETTINGS *pRfSettings) { halSpiWriteReg(CCxxx0_FSCTRL0, pRfSettings->FSCTRL2); // Write register settings halSpiWriteReg(CCxxx0_FSCTRL1, pRfSettings->FSCTRL1); halSpiWriteReg(CCxxx0_FSCTRL0, pRfSettings->FSCTRL0); halSpiWriteReg(CCxxx0_FREQ2, pRfSettings->FREQ2); halSpiWriteReg(CCxxx0_FREQ1, pRfSettings->FREQ1); halSpiWriteReg(CCxxx0_FREQ0, pRfSettings->FREQ0); halSpiWriteReg(CCxxx0_MDMCFG4, pRfSettings->MDMCFG4); halSpiWriteReg(CCxxx0_MDMCFG3, pRfSettings->MDMCFG3); halSpiWriteReg(CCxxx0_MDMCFG2, pRfSettings->MDMCFG2); halSpiWriteReg(CCxxx0_MDMCFG1, pRfSettings->MDMCFG1); halSpiWriteReg(CCxxx0_MDMCFG0, pRfSettings->MDMCFG0); halSpiWriteReg(CCxxx0_CHANNR, pRfSettings->CHANNR); halSpiWriteReg(CCxxx0_DEVIATN, pRfSettings->DEVIATN); halSpiWriteReg(CCxxx0_FREND1, pRfSettings->FREND1); halSpiWriteReg(CCxxx0_FREND0, pRfSettings->FREND0); halSpiWriteReg(CCxxx0_MCSM0 , pRfSettings->MCSM0 ); halSpiWriteReg(CCxxx0_FOCCFG, pRfSettings->FOCCFG); halSpiWriteReg(CCxxx0_BSCFG, pRfSettings->BSCFG); halSpiWriteReg(CCxxx0_AGCCTRL2, pRfSettings->AGCCTRL2); halSpiWriteReg(CCxxx0_AGCCTRL1, pRfSettings->AGCCTRL1); halSpiWriteReg(CCxxx0_AGCCTRL0, pRfSettings->AGCCTRL0); halSpiWriteReg(CCxxx0_FSCAL3, pRfSettings->FSCAL3); halSpiWriteReg(CCxxx0_FSCAL2, pRfSettings->FSCAL2); halSpiWriteReg(CCxxx0_FSCAL1, pRfSettings->FSCAL1); halSpiWriteReg(CCxxx0_FSCAL0, pRfSettings->FSCAL0); halSpiWriteReg(CCxxx0_FSTEST, pRfSettings->FSTEST); halSpiWriteReg(CCxxx0_TEST2, pRfSettings->TEST2); halSpiWriteReg(CCxxx0_TEST1, pRfSettings->TEST1); halSpiWriteReg(CCxxx0_TEST0, pRfSettings->TEST0); halSpiWriteReg(CCxxx0_IOCFG2, pRfSettings->IOCFG2); halSpiWriteReg(CCxxx0_IOCFG0, pRfSettings->IOCFG0); halSpiWriteReg(CCxxx0_PKTCTRL1, pRfSettings->PKTCTRL1); halSpiWriteReg(CCxxx0_PKTCTRL0, pRfSettings->PKTCTRL0); halSpiWriteReg(CCxxx0_ADDR, pRfSettings->ADDR); halSpiWriteReg(CCxxx0_PKTLEN, pRfSettings->PKTLEN); }
//其中,rfSettings需要定义并按照需要初始化适当的值。比如配置如下: // RF output power = 0 dBm // RX filterbandwidth = 540.000000 kHz // Deviation = 0.000000 // Datarate = 250.000000 kbps // Modulation = (7) MSK // Manchester enable = (0) Manchester disabled // RF Frequency = 2480.000000 MHz // Channel spacing = 199.951172 kHz // Channel number = 0 // Optimization = Sensitivity // Sync mode = (3) 30/32 sync word bits detected // Format of RX/TX data = (0) Normal mode, use FIFOs for RX and TX // CRC operation = (1) CRC calculation in TX and CRC check in RX enabled // Forward Error Correction = (0) FEC disabled // Length configuration = (1) Variable length packets, packet length configured by the first received byte after sync word. // Packetlength = 255 // Preamble count = (2) 4 bytes // Append status = 1 // Address check = (11) No address check // FIFO autoflush = 0 // Device address = 0 // GDO0 signal selection = ( 6) // GDO2 signal selection = (11) Serial Clock
const RF_SETTINGS rfSettings = { 0x00, 0x0B, // 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. 0x2D, // MDMCFG4 Modem configuration. 0x3B, // MDMCFG3 Modem configuration. 0x73, // MDMCFG2 Modem configuration. 0x22, // MDMCFG1 Modem configuration. 0xF8, // MDMCFG0 Modem configuration. 0x00, // CHANNR Channel number. 0x00, // 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. 0x0A, // FSCAL2 Frequency synthesizer calibration. 0x00, // FSCAL1 Frequency synthesizer calibration. 0x11, // FSCAL0 Frequency synthesizer calibration. 0x59, // FSTEST Frequency synthesizer calibration. 0x88, // TEST2 Various test settings. 0x31, // TEST1 Various test settings. 0x0B, // TEST0 Various test settings. 0x0B, // IOCFG2 GDO2 output pin configuration. 0x06, // IOCFG0D GDO0 output pin configuration. 0x04, // PKTCTRL1 Packet automation control. 0x04, // PKTCTRL0 Packet automation control. 0x00, // ADDR Device address. 0x0c // PKTLEN Packet length. }; BYTE paTable = 0xFE;
//使用CC2500发送数据 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 (!(PIND&(1<<1))); // Wait for GDO0 to be cleared -> end of packet while (PIND&(1<<1)); halSpiStrobe(CCxxx0_SFTX); }
void main(void) { Init(); POWER_UP_RESET_CCxxx0(); halRfWriteRfSettings(&rfSettings); halSpiWriteReg(CCxxx0_PATABLE, paTable); while(1) { keynum=GetKey(0); if(!keynum) halRfSendPacket(keynum,1); } }
void Init(void) //初始化串行口和波特率发生器 { PORTB=0xF0; PORTD=0x08; DDRB=0x0F; DDRD=0xBB; } 主程序就是一个键盘扫描程序,然后将按下的键通过CC2500发送。 上电后,GDO0脚一直输出方波。不知道程序哪儿有问题,请大家帮帮忙! |
|