找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 3766|回复: 1
收起左侧

51单片机驱动si4432程序

[复制链接]
ID:82008 发表于 2015-6-4 14:17 | 显示全部楼层 |阅读模式
  1. #include "radio.h"
  2. #include"spi.h"
  3. #include"uart.h"
  4. #define TxGPIOSetting()   SpiWriteRegister(0x0E, 0x02)
  5. #define RxGPIOSetting()   SpiWriteRegister(0x0E, 0x01)
  6. #define IdleGPIOSetting() SpiWriteRegister(0x0E, 0x01)
  7. void RF_init(void)
  8. {
  9.         //Set the physical parameters
  10.         //Frequency:       433MHz
  11.         //Modulation type: GFSK
  12.         //CrytalTolerance: Rx,10ppm; Tx, 10ppm
  13.         //Data rate:       9.6Kbps/1.2Kbps
  14.         //AFC:             Enable
  15.         //Frequency deviation: 20KHz

  16.         //set the center frequency to 433 MHz
  17.         SpiWriteRegister(0x75, 0x53);                                                                                                                        //write data to the Frequency Band Select register            
  18.         SpiWriteRegister(0x76, 0x4B);                                                                                                                        //write data to the Nominal Carrier Frequency1 register
  19.         SpiWriteRegister(0x77, 0x00);                                                                                                                          //write data to the Nominal Carrier Frequency0 register

  20.         //set the desired TX data rate (1.2kbps)
  21.         SpiWriteRegister(0x6E, 0x09);                                                                                                                        //write data to the TXDataRate 1 register
  22.         SpiWriteRegister(0x6F, 0xD5);                                                                                                                        //write data to the TXDataRate 0 register
  23.         SpiWriteRegister(0x70, 0x2C);                                                                                                                        //write data to the Modulation Mode Control 1 register
  24.         SpiWriteRegister(0x58, 0x80);

  25.         //set the TX power to MAX. dBm
  26.         SpiWriteRegister(0x6D, 0x1F);                                                                                                                        //write data to the TX Power register

  27.         //set the Tx deviation register (+-20kHz)
  28.         SpiWriteRegister(0x72, 0x20);                                                                                                                        //write data to the Frequency Deviation register

  29.                                                                 /*set the modem parameters according to the exel calculator(parameters: 1.2 kbps, deviation: 20 kHz*/
  30.         SpiWriteRegister(0x1C, 0x2C);                                                                                                                        //write data to the IF Filter Bandwidth register               
  31.         SpiWriteRegister(0x20, 0x41);                                                                                                                        //write data to the Clock Recovery Oversampling Ratio register               
  32.         SpiWriteRegister(0x21, 0x60);                                                                                                                        //write data to the Clock Recovery Offset 2 register               
  33.         SpiWriteRegister(0x22, 0x27);                                                                                                                        //write data to the Clock Recovery Offset 1 register               
  34.         SpiWriteRegister(0x23, 0x52);                                                                                                                        //write data to the Clock Recovery Offset 0 register               
  35.         SpiWriteRegister(0x24, 0x00);                                                                                                                        //write data to the Clock Recovery Timing Loop Gain 1 register               
  36.         SpiWriteRegister(0x25, 0x04);                                                                                                                        //write data to the Clock Recovery Timing Loop Gain 0 register               
  37.         SpiWriteRegister(0x1D, 0x40);                                                                                                                        //write data to the AFC Loop Gearshift Override register               
  38.         SpiWriteRegister(0x1E, 0x0A);
  39.         SpiWriteRegister(0x2A, 0x0F);                                                                                                                        //write data to the AFC Limiter register               
  40.         SpiWriteRegister(0x1F, 0x03);
  41.         SpiWriteRegister(0x69, 0x60);
  42.        
  43.                                                         /*set the packet structure and the modulation type*/
  44.         //set the preamble length to 10bytes if the antenna diversity is used and set to 5bytes if not
  45.         SpiWriteRegister(0x34, 0x0C);                                                                                                                        //write data to the Preamble Length register
  46.         //set preamble detection threshold to 20bits
  47.         SpiWriteRegister(0x35, 0x2A);                                                                                                                         //write data to the Preamble Detection Control  register

  48.         //Disable header bytes; set variable packet length (the length of the payload is defined by the
  49.         //received packet length field of the packet); set the synch word to two bytes long
  50.         SpiWriteRegister(0x33, 0x02);                                                                                                                        //write data to the Header Control2 register   
  51.        
  52.         //Set the sync word pattern to 0x2DD4
  53.         SpiWriteRegister(0x36, 0x2D);                                                                                                                        //write data to the Sync Word 3 register
  54.         SpiWriteRegister(0x37, 0xD4);                                                                                                                        //write data to the Sync Word 2 register

  55.         //enable the TX & RX packet handler and CRC-16 (IBM) check
  56.         SpiWriteRegister(0x30, 0x8D);                                                                                                                        //write data to the Data Access Control register
  57.         //Disable the receive header filters
  58.            SpiWriteRegister(0x32, 0x00 );                                                                                                                        //write data to the Header Control1 register            

  59.         //enable FIFO mode and GFSK modulation
  60.         SpiWriteRegister(0x71, 0x63);                                                                                                                        //write data to the Modulation Mode Control 2 register

  61.                                                                                         /*set the GPIO's according the testcard type*/
  62.            SpiWriteRegister(0x0B, 0xCA);                                                                                                                        //Set GPIO0 output
  63.            SpiWriteRegister(0x0C, 0xCA);                                                                                                                        //Set GPIO1 output
  64.            SpiWriteRegister(0x0D, 0xCA);                                                                                                                //Set GPIO2 output Rx Data

  65.         //set  Crystal Oscillator Load Capacitance register
  66.         SpiWriteRegister(0x09, CRYSTAL_CAPACITANCE);                                                                                        //write data to the Crystal Oscillator Load Capacitance register
  67. }

  68. idata U8 ItStatus1,ItStatus2;

  69. //------------------------------------------------------------------------------------------------
  70. // Function Name :RFSetRxMode
  71. //
  72. // Return Value : None
  73. // Parameters   : None
  74. // Notes        : 设置模块工作在接收状态下,当一切准备好的时候,调用该函数可以让模块工作于接收模式下
  75. //
  76. //-----------------------------------------------------------------------------------------------
  77. void RFSetRxMode(void)
  78. {  
  79.         //read interrupt status registers to release all pending interrupts
  80.         ItStatus1 = SpiReadRegister(0x03);                                                                                                            //read the Interrupt Status1 register
  81.         ItStatus2 = SpiReadRegister(0x04);                                                                                                            //read the Interrupt Status2 register

  82.         //Set the Rx switcher,利用GPIO口控制模块内部的天线开关。
  83.         RxGPIOSetting();

  84.         /*enable receiver chain*/
  85.         SpiWriteRegister(0x07, 0x05);  
  86. }

  87. //------------------------------------------------------------------------------------------------
  88. // Function Name :RFSetTxMode
  89. //
  90. // Return Value : None
  91. // Parameters   : None
  92. // Notes        : 设置模块工作在接收状态下,当一切准备好的时候,调用该函数可以让模块工作于发射状态模式下
  93. //
  94. //-----------------------------------------------------------------------------------------------
  95. void RFSetTxMode(void)
  96. {
  97.         //Read interrupt status regsiters. It clear all pending interrupts and the nIRQ pin goes back to high.
  98.         ItStatus1 = SpiReadRegister(0x03);                                                                                                //read the Interrupt Status1 register
  99.         ItStatus2 = SpiReadRegister(0x04);                                                                                                //read the Interrupt Status2 register

  100.         //Set the Rx switcher,利用GPIO口控制模块内部的天线开关。
  101.         TxGPIOSetting();

  102.         /*enable transmitter*/
  103.         //The radio forms the packet and send it automatically.
  104.         SpiWriteRegister(0x07, 0x09);
  105. }

  106. //------------------------------------------------------------------------------------------------
  107. // Function Name :RFSetIdleMode
  108. //
  109. // Return Value : None
  110. // Parameters   : None
  111. // Notes        : 设置模块工作在空闲状态下。当模块不需要接收数据也不需要发射数据的时候可以调用该函数以便省电;
  112. //
  113. //-----------------------------------------------------------------------------------------------
  114. void RFSetIdleMode(void)
  115. {
  116.         //The radio will work in idle mode
  117.         SpiWriteRegister(0x07, 0x01);                                                                                                        //write 0x09 to the Operating Function Control 1 register

  118.         IdleGPIOSetting();
  119.        
  120.         //Read interrupt status regsiters. It clear all pending interrupts and the nIRQ pin goes back to high.
  121.         ItStatus1 = SpiReadRegister(0x03);                                                                                                //read the Interrupt Status1 register
  122.         ItStatus2 = SpiReadRegister(0x04);                                                                                                //read the Interrupt Status2 register
  123. }

  124. //------------------------------------------------------------------------------------------------
  125. // Function Name :RFFIFOSendData
  126. //
  127. // Return Value : None
  128. // Parameters   : uint8 length: the length of the content which will be sent out. This value should
  129. //                not more than 64bytes because the FIFO is 64bytes. If it is bigger than 64, the
  130. //                content should be sent out for more times according to the FIFO empty interrupt.
  131. //                uint8 *payload: point to the content.
  132. // Notes        : In FIFO mode, send out one packet data.
  133. //                调用该函数可以把数据写入模块的FIFO中并把数据发送出去;数据长度不能大于64个字节;
  134. //
  135. //-----------------------------------------------------------------------------------------------
  136. void RFFIFOSendData(U8 length, U8 *payload)
  137. {
  138.         U8 i;

  139.         RFSetIdleMode();
  140.         //turn on the Tx LED to show the packet transmission
  141.         //TxLEDOn();                                                                                                                                                        
  142.         /*SET THE CONTENT OF THE PACKET*/
  143.         //set the length of the payload.       
  144.         SpiWriteRegister(0x3E, length);

  145.         //fill the payload into the transmit FIFO
  146.         for(i = 0; i < length; i++)
  147.         {
  148.                 SpiWriteRegister(0x7F, payload[i]);       
  149.         }

  150.         //Disable all other interrupts and enable the packet sent interrupt only.
  151.         //This will be used for indicating the successfull packet transmission for the MCU
  152.         SpiWriteRegister(0x05, 0x04);                                                                                                        //write 0x04 to the Interrupt Enable 1 register       
  153.         SpiWriteRegister(0x06, 0x00);                                                                                                        //write 0x00 to the Interrupt Enable 2 register       

  154.         RFSetTxMode();

  155.         /*wait for the packet sent interrupt*/
  156.         //The MCU just needs to wait for the 'ipksent' interrupt.
  157.         //等待发射完成中断的到来,如果中断采用非查询方式,则本程序到这里可以结束,但必须在中断服务程序中读取状态寄存器0x03和0x04,以便释放中断
  158.         while(NIRQ == 1);
  159.         UART_Send_Str("已发送..............\n");//调试信息时候用
  160.         //read interrupt status registers to release the interrupt flags
  161.         ItStatus1 = SpiReadRegister(0x03);                                                                                                //read the Interrupt Status1 register
  162.         ItStatus2 = SpiReadRegister(0x04);                                                                                                //read the Interrupt Status2 register

  163.         //turn off the Tx LED
  164.         //TurnOffAllLEDs();
  165. }
复制代码


51单片机驱动程序4432.rar

59.3 KB, 下载次数: 42, 下载积分: 黑币 -5

回复

使用道具 举报

ID:136504 发表于 2016-9-7 16:13 | 显示全部楼层
HW3000 433M 国产第一家433无线模块,完美替换si4432,4463.cc1101。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表