|
- #include "radio.h"
- #include"spi.h"
- #include"uart.h"
- #define TxGPIOSetting() SpiWriteRegister(0x0E, 0x02)
- #define RxGPIOSetting() SpiWriteRegister(0x0E, 0x01)
- #define IdleGPIOSetting() SpiWriteRegister(0x0E, 0x01)
- void RF_init(void)
- {
- //Set the physical parameters
- //Frequency: 433MHz
- //Modulation type: GFSK
- //CrytalTolerance: Rx,10ppm; Tx, 10ppm
- //Data rate: 9.6Kbps/1.2Kbps
- //AFC: Enable
- //Frequency deviation: 20KHz
- //set the center frequency to 433 MHz
- SpiWriteRegister(0x75, 0x53); //write data to the Frequency Band Select register
- SpiWriteRegister(0x76, 0x4B); //write data to the Nominal Carrier Frequency1 register
- SpiWriteRegister(0x77, 0x00); //write data to the Nominal Carrier Frequency0 register
- //set the desired TX data rate (1.2kbps)
- SpiWriteRegister(0x6E, 0x09); //write data to the TXDataRate 1 register
- SpiWriteRegister(0x6F, 0xD5); //write data to the TXDataRate 0 register
- SpiWriteRegister(0x70, 0x2C); //write data to the Modulation Mode Control 1 register
- SpiWriteRegister(0x58, 0x80);
- //set the TX power to MAX. dBm
- SpiWriteRegister(0x6D, 0x1F); //write data to the TX Power register
- //set the Tx deviation register (+-20kHz)
- SpiWriteRegister(0x72, 0x20); //write data to the Frequency Deviation register
- /*set the modem parameters according to the exel calculator(parameters: 1.2 kbps, deviation: 20 kHz*/
- SpiWriteRegister(0x1C, 0x2C); //write data to the IF Filter Bandwidth register
- SpiWriteRegister(0x20, 0x41); //write data to the Clock Recovery Oversampling Ratio register
- SpiWriteRegister(0x21, 0x60); //write data to the Clock Recovery Offset 2 register
- SpiWriteRegister(0x22, 0x27); //write data to the Clock Recovery Offset 1 register
- SpiWriteRegister(0x23, 0x52); //write data to the Clock Recovery Offset 0 register
- SpiWriteRegister(0x24, 0x00); //write data to the Clock Recovery Timing Loop Gain 1 register
- SpiWriteRegister(0x25, 0x04); //write data to the Clock Recovery Timing Loop Gain 0 register
- SpiWriteRegister(0x1D, 0x40); //write data to the AFC Loop Gearshift Override register
- SpiWriteRegister(0x1E, 0x0A);
- SpiWriteRegister(0x2A, 0x0F); //write data to the AFC Limiter register
- SpiWriteRegister(0x1F, 0x03);
- SpiWriteRegister(0x69, 0x60);
-
- /*set the packet structure and the modulation type*/
- //set the preamble length to 10bytes if the antenna diversity is used and set to 5bytes if not
- SpiWriteRegister(0x34, 0x0C); //write data to the Preamble Length register
- //set preamble detection threshold to 20bits
- SpiWriteRegister(0x35, 0x2A); //write data to the Preamble Detection Control register
- //Disable header bytes; set variable packet length (the length of the payload is defined by the
- //received packet length field of the packet); set the synch word to two bytes long
- SpiWriteRegister(0x33, 0x02); //write data to the Header Control2 register
-
- //Set the sync word pattern to 0x2DD4
- SpiWriteRegister(0x36, 0x2D); //write data to the Sync Word 3 register
- SpiWriteRegister(0x37, 0xD4); //write data to the Sync Word 2 register
- //enable the TX & RX packet handler and CRC-16 (IBM) check
- SpiWriteRegister(0x30, 0x8D); //write data to the Data Access Control register
- //Disable the receive header filters
- SpiWriteRegister(0x32, 0x00 ); //write data to the Header Control1 register
- //enable FIFO mode and GFSK modulation
- SpiWriteRegister(0x71, 0x63); //write data to the Modulation Mode Control 2 register
- /*set the GPIO's according the testcard type*/
- SpiWriteRegister(0x0B, 0xCA); //Set GPIO0 output
- SpiWriteRegister(0x0C, 0xCA); //Set GPIO1 output
- SpiWriteRegister(0x0D, 0xCA); //Set GPIO2 output Rx Data
- //set Crystal Oscillator Load Capacitance register
- SpiWriteRegister(0x09, CRYSTAL_CAPACITANCE); //write data to the Crystal Oscillator Load Capacitance register
- }
- idata U8 ItStatus1,ItStatus2;
- //------------------------------------------------------------------------------------------------
- // Function Name :RFSetRxMode
- //
- // Return Value : None
- // Parameters : None
- // Notes : 设置模块工作在接收状态下,当一切准备好的时候,调用该函数可以让模块工作于接收模式下
- //
- //-----------------------------------------------------------------------------------------------
- void RFSetRxMode(void)
- {
- //read interrupt status registers to release all pending interrupts
- ItStatus1 = SpiReadRegister(0x03); //read the Interrupt Status1 register
- ItStatus2 = SpiReadRegister(0x04); //read the Interrupt Status2 register
- //Set the Rx switcher,利用GPIO口控制模块内部的天线开关。
- RxGPIOSetting();
- /*enable receiver chain*/
- SpiWriteRegister(0x07, 0x05);
- }
- //------------------------------------------------------------------------------------------------
- // Function Name :RFSetTxMode
- //
- // Return Value : None
- // Parameters : None
- // Notes : 设置模块工作在接收状态下,当一切准备好的时候,调用该函数可以让模块工作于发射状态模式下
- //
- //-----------------------------------------------------------------------------------------------
- void RFSetTxMode(void)
- {
- //Read interrupt status regsiters. It clear all pending interrupts and the nIRQ pin goes back to high.
- ItStatus1 = SpiReadRegister(0x03); //read the Interrupt Status1 register
- ItStatus2 = SpiReadRegister(0x04); //read the Interrupt Status2 register
- //Set the Rx switcher,利用GPIO口控制模块内部的天线开关。
- TxGPIOSetting();
- /*enable transmitter*/
- //The radio forms the packet and send it automatically.
- SpiWriteRegister(0x07, 0x09);
- }
- //------------------------------------------------------------------------------------------------
- // Function Name :RFSetIdleMode
- //
- // Return Value : None
- // Parameters : None
- // Notes : 设置模块工作在空闲状态下。当模块不需要接收数据也不需要发射数据的时候可以调用该函数以便省电;
- //
- //-----------------------------------------------------------------------------------------------
- void RFSetIdleMode(void)
- {
- //The radio will work in idle mode
- SpiWriteRegister(0x07, 0x01); //write 0x09 to the Operating Function Control 1 register
- IdleGPIOSetting();
-
- //Read interrupt status regsiters. It clear all pending interrupts and the nIRQ pin goes back to high.
- ItStatus1 = SpiReadRegister(0x03); //read the Interrupt Status1 register
- ItStatus2 = SpiReadRegister(0x04); //read the Interrupt Status2 register
- }
- //------------------------------------------------------------------------------------------------
- // Function Name :RFFIFOSendData
- //
- // Return Value : None
- // Parameters : uint8 length: the length of the content which will be sent out. This value should
- // not more than 64bytes because the FIFO is 64bytes. If it is bigger than 64, the
- // content should be sent out for more times according to the FIFO empty interrupt.
- // uint8 *payload: point to the content.
- // Notes : In FIFO mode, send out one packet data.
- // 调用该函数可以把数据写入模块的FIFO中并把数据发送出去;数据长度不能大于64个字节;
- //
- //-----------------------------------------------------------------------------------------------
- void RFFIFOSendData(U8 length, U8 *payload)
- {
- U8 i;
- RFSetIdleMode();
- //turn on the Tx LED to show the packet transmission
- //TxLEDOn();
- /*SET THE CONTENT OF THE PACKET*/
- //set the length of the payload.
- SpiWriteRegister(0x3E, length);
- //fill the payload into the transmit FIFO
- for(i = 0; i < length; i++)
- {
- SpiWriteRegister(0x7F, payload[i]);
- }
- //Disable all other interrupts and enable the packet sent interrupt only.
- //This will be used for indicating the successfull packet transmission for the MCU
- SpiWriteRegister(0x05, 0x04); //write 0x04 to the Interrupt Enable 1 register
- SpiWriteRegister(0x06, 0x00); //write 0x00 to the Interrupt Enable 2 register
- RFSetTxMode();
- /*wait for the packet sent interrupt*/
- //The MCU just needs to wait for the 'ipksent' interrupt.
- //等待发射完成中断的到来,如果中断采用非查询方式,则本程序到这里可以结束,但必须在中断服务程序中读取状态寄存器0x03和0x04,以便释放中断
- while(NIRQ == 1);
- UART_Send_Str("已发送..............\n");//调试信息时候用
- //read interrupt status registers to release the interrupt flags
- ItStatus1 = SpiReadRegister(0x03); //read the Interrupt Status1 register
- ItStatus2 = SpiReadRegister(0x04); //read the Interrupt Status2 register
- //turn off the Tx LED
- //TurnOffAllLEDs();
- }
复制代码
|
|