找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2543|回复: 0
打印 上一主题 下一主题
收起左侧

RF1100调试原理图及程序

[复制链接]
跳转到指定楼层
楼主
cc1101通信程序

全部资料(压缩包)下载: 00190336CC1101.rar (1.74 MB, 下载次数: 8)




模块图片:


原理图:

程序说明:
1.    将改程序下载到2个开发板
2.    然后按KEY后发送信息,发送完毕后LDE闪烁,对方受到信息后同样 闪烁
3.    而且两个开发板都可以做为发送和接收,一个做为发送,对应另一即为接收,实现及时双向通信
  1. #include <reg52.h>
  2. #include <intrins.h>
  3. #define                INT8U                unsigned char
  4. #define                INT16U                unsigned int

  5. #define         WRITE_BURST             0x40                                                //连续写入
  6. #define         READ_SINGLE             0x80                                                //读
  7. #define         READ_BURST              0xC0                                                //连续读
  8. #define         BYTES_IN_RXFIFO     0x7F                                                  //接收缓冲区的有效字节数
  9. #define         CRC_OK              0x80                                                 //CRC校验通过位标志
  10. //*****************************************************************************************
  11. sbit         GDO0        =P1^0;
  12. sbit         GDO2        =P1^1;
  13. sbit        MISO        =P1^6;
  14. sbit        MOSI        =P1^7;
  15. sbit        SCK                =P1^2;
  16. sbit        CSN                =P1^5;
  17. //----------------------------------------按键端口-------------------------------------------------------
  18. sbit        KEY=P3^4;
  19. //----------------------------------------LED显示端口---------------------------------------------------
  20. sbit    LED=P3^5;
  21. //***************更多功率参数设置可详细参考DATACC1100英文文档中第48-49页的参数表******************
  22. //INT8U PaTabel[8] = {0x04 ,0x04 ,0x04 ,0x04 ,0x04 ,0x04 ,0x04 ,0x04};  //-30dBm   功率最小
  23. INT8U PaTabel[8] = {0x60 ,0x60 ,0x60 ,0x60 ,0x60 ,0x60 ,0x60 ,0x60};  //0dBm
  24. //INT8U PaTabel[8] = {0xC0 ,0xC0 ,0xC0 ,0xC0 ,0xC0 ,0xC0 ,0xC0 ,0xC0};   //10dBm     功率最大
  25. //*****************************************************************************************
  26. void SpiInit(void);
  27. void CpuInit(void);
  28. void RESET_CC1100(void);
  29. void POWER_UP_RESET_CC1100(void);
  30. void halSpiWriteReg(INT8U addr, INT8U value);
  31. void halSpiWriteBurstReg(INT8U addr, INT8U *buffer, INT8U count);
  32. void halSpiStrobe(INT8U strobe);
  33. INT8U halSpiReadReg(INT8U addr);
  34. void halSpiReadBurstReg(INT8U addr, INT8U *buffer, INT8U count);
  35. INT8U halSpiReadStatus(INT8U addr);
  36. void halRfWriteRfSettings(void);
  37. void halRfSendPacket(INT8U *txBuffer, INT8U size);
  38. INT8U halRfReceivePacket(INT8U *rxBuffer, INT8U *length);  
  39. //*****************************************************************************************
  40. // CC1100 STROBE, CONTROL AND STATUS REGSITER
  41. #define CCxxx0_IOCFG2       0x00        // GDO2 output pin configuration
  42. #define CCxxx0_IOCFG1       0x01        // GDO1 output pin configuration
  43. #define CCxxx0_IOCFG0       0x02        // GDO0 output pin configuration
  44. #define CCxxx0_FIFOTHR      0x03        // RX FIFO and TX FIFO thresholds
  45. #define CCxxx0_SYNC1        0x04        // Sync word, high INT8U
  46. #define CCxxx0_SYNC0        0x05        // Sync word, low INT8U
  47. #define CCxxx0_PKTLEN       0x06        // Packet length
  48. #define CCxxx0_PKTCTRL1     0x07        // Packet automation control
  49. #define CCxxx0_PKTCTRL0     0x08        // Packet automation control
  50. #define CCxxx0_ADDR         0x09        // Device address
  51. #define CCxxx0_CHANNR       0x0A        // Channel number
  52. #define CCxxx0_FSCTRL1      0x0B        // Frequency synthesizer control
  53. #define CCxxx0_FSCTRL0      0x0C        // Frequency synthesizer control
  54. #define CCxxx0_FREQ2        0x0D        // Frequency control word, high INT8U
  55. #define CCxxx0_FREQ1        0x0E        // Frequency control word, middle INT8U
  56. #define CCxxx0_FREQ0        0x0F        // Frequency control word, low INT8U
  57. #define CCxxx0_MDMCFG4      0x10        // Modem configuration
  58. #define CCxxx0_MDMCFG3      0x11        // Modem configuration
  59. #define CCxxx0_MDMCFG2      0x12        // Modem configuration
  60. #define CCxxx0_MDMCFG1      0x13        // Modem configuration
  61. #define CCxxx0_MDMCFG0      0x14        // Modem configuration
  62. #define CCxxx0_DEVIATN      0x15        // Modem deviation setting
  63. #define CCxxx0_MCSM2        0x16        // Main Radio Control State Machine configuration
  64. #define CCxxx0_MCSM1        0x17        // Main Radio Control State Machine configuration
  65. #define CCxxx0_MCSM0        0x18        // Main Radio Control State Machine configuration
  66. #define CCxxx0_FOCCFG       0x19        // Frequency Offset Compensation configuration
  67. #define CCxxx0_BSCFG        0x1A        // Bit Synchronization configuration
  68. #define CCxxx0_AGCCTRL2     0x1B        // AGC control
  69. #define CCxxx0_AGCCTRL1     0x1C        // AGC control
  70. #define CCxxx0_AGCCTRL0     0x1D        // AGC control
  71. #define CCxxx0_WOREVT1      0x1E        // High INT8U Event 0 timeout
  72. #define CCxxx0_WOREVT0      0x1F        // Low INT8U Event 0 timeout
  73. #define CCxxx0_WORCTRL      0x20        // Wake On Radio control
  74. #define CCxxx0_FREND1       0x21        // Front end RX configuration
  75. #define CCxxx0_FREND0       0x22        // Front end TX configuration
  76. #define CCxxx0_FSCAL3       0x23        // Frequency synthesizer calibration
  77. #define CCxxx0_FSCAL2       0x24        // Frequency synthesizer calibration
  78. #define CCxxx0_FSCAL1       0x25        // Frequency synthesizer calibration
  79. #define CCxxx0_FSCAL0       0x26        // Frequency synthesizer calibration
  80. #define CCxxx0_RCCTRL1      0x27        // RC oscillator configuration
  81. #define CCxxx0_RCCTRL0      0x28        // RC oscillator configuration
  82. #define CCxxx0_FSTEST       0x29        // Frequency synthesizer calibration control
  83. #define CCxxx0_PTEST        0x2A        // Production test
  84. #define CCxxx0_AGCTEST      0x2B        // AGC test
  85. #define CCxxx0_TEST2        0x2C        // Various test settings
  86. #define CCxxx0_TEST1        0x2D        // Various test settings
  87. #define CCxxx0_TEST0        0x2E        // Various test settings

  88. // Strobe commands
  89. #define CCxxx0_SRES         0x30        // Reset chip.
  90. #define CCxxx0_SFSTXON      0x31        // Enable and calibrate frequency synthesizer (if MCSM0.FS_AUTOCAL=1).
  91.                                         // If in RX/TX: Go to a wait state where only the synthesizer is
  92.                                         // running (for quick RX / TX turnaround).
  93. #define CCxxx0_SXOFF        0x32        // Turn off crystal oscillator.
  94. #define CCxxx0_SCAL         0x33        // Calibrate frequency synthesizer and turn it off
  95.                                         // (enables quick start).
  96. #define CCxxx0_SRX          0x34        // Enable RX. Perform calibration first if coming from IDLE and
  97.                                         // MCSM0.FS_AUTOCAL=1.
  98. #define CCxxx0_STX          0x35        // In IDLE state: Enable TX. Perform calibration first if
  99.                                         // MCSM0.FS_AUTOCAL=1. If in RX state and CCA is enabled:
  100.                                         // Only go to TX if channel is clear.
  101. #define CCxxx0_SIDLE        0x36        // Exit RX / TX, turn off frequency synthesizer and exit
  102.                                         // Wake-On-Radio mode if applicable.
  103. #define CCxxx0_SAFC         0x37        // Perform AFC adjustment of the frequency synthesizer
  104. #define CCxxx0_SWOR         0x38        // Start automatic RX polling sequence (Wake-on-Radio)
  105. #define CCxxx0_SPWD         0x39        // Enter power down mode when CSn goes high.
  106. #define CCxxx0_SFRX         0x3A        // Flush the RX FIFO buffer.
  107. #define CCxxx0_SFTX         0x3B        // Flush the TX FIFO buffer.
  108. #define CCxxx0_SWORRST      0x3C        // Reset real time clock.
  109. #define CCxxx0_SNOP         0x3D        // No operation. May be used to pad strobe commands to two
  110.                                         // INT8Us for simpler software.

  111. #define CCxxx0_PARTNUM      0x30
  112. #define CCxxx0_VERSION      0x31
  113. #define CCxxx0_FREQEST      0x32
  114. #define CCxxx0_LQI          0x33
  115. #define CCxxx0_RSSI         0x34
  116. #define CCxxx0_MARCSTATE    0x35
  117. #define CCxxx0_WORTIME1     0x36
  118. #define CCxxx0_WORTIME0     0x37
  119. #define CCxxx0_PKTSTATUS    0x38
  120. #define CCxxx0_VCO_VC_DAC   0x39
  121. #define CCxxx0_TXBYTES      0x3A
  122. #define CCxxx0_RXBYTES      0x3B

  123. #define CCxxx0_PATABLE      0x3E
  124. #define CCxxx0_TXFIFO       0x3F
  125. #define CCxxx0_RXFIFO       0x3F

  126. // RF_SETTINGS is a data structure which contains all relevant CCxxx0 registers
  127. typedef struct S_RF_SETTINGS
  128. {
  129.         INT8U FSCTRL2;                //自已加的
  130.     INT8U FSCTRL1;   // Frequency synthesizer control.
  131.     INT8U FSCTRL0;   // Frequency synthesizer control.
  132.     INT8U FREQ2;     // Frequency control word, high INT8U.
  133.     INT8U FREQ1;     // Frequency control word, middle INT8U.
  134.     INT8U FREQ0;     // Frequency control word, low INT8U.
  135.     INT8U MDMCFG4;   // Modem configuration.
  136.     INT8U MDMCFG3;   // Modem configuration.
  137.     INT8U MDMCFG2;   // Modem configuration.
  138.     INT8U MDMCFG1;   // Modem configuration.
  139.     INT8U MDMCFG0;   // Modem configuration.
  140.     INT8U CHANNR;    // Channel number.
  141.     INT8U DEVIATN;   // Modem deviation setting (when FSK modulation is enabled).
  142.     INT8U FREND1;    // Front end RX configuration.
  143.     INT8U FREND0;    // Front end RX configuration.
  144.     INT8U MCSM0;     // Main Radio Control State Machine configuration.
  145.     INT8U FOCCFG;    // Frequency Offset Compensation Configuration.
  146.     INT8U BSCFG;     // Bit synchronization Configuration.
  147.     INT8U AGCCTRL2;  // AGC control.
  148.         INT8U AGCCTRL1;  // AGC control.
  149.     INT8U AGCCTRL0;  // AGC control.
  150.     INT8U FSCAL3;    // Frequency synthesizer calibration.
  151.     INT8U FSCAL2;    // Frequency synthesizer calibration.
  152.         INT8U FSCAL1;    // Frequency synthesizer calibration.
  153.     INT8U FSCAL0;    // Frequency synthesizer calibration.
  154.     INT8U FSTEST;    // Frequency synthesizer calibration control
  155.     INT8U TEST2;     // Various test settings.
  156.     INT8U TEST1;     // Various test settings.
  157.     INT8U TEST0;     // Various test settings.
  158.     INT8U IOCFG2;    // GDO2 output pin configuration
  159.     INT8U IOCFG0;    // GDO0 output pin configuration
  160.     INT8U PKTCTRL1;  // Packet automation control.
  161.     INT8U PKTCTRL0;  // Packet automation control.
  162.     INT8U ADDR;      // Device address.
  163.     INT8U PKTLEN;    // Packet length.
  164. } RF_SETTINGS;

  165. /////////////////////////////////////////////////////////////////
  166. const RF_SETTINGS rfSettings =
  167. {
  168.         0x00,
  169.     0x08,   // FSCTRL1   Frequency synthesizer control.
  170.     0x00,   // FSCTRL0   Frequency synthesizer control.
  171.     0x10,   // FREQ2     Frequency control word, high byte.
  172.     0xA7,   // FREQ1     Frequency control word, middle byte.
  173.     0x62,   // FREQ0     Frequency control word, low byte.
  174.     0x5B,   // MDMCFG4   Modem configuration.
  175.     0xF8,   // MDMCFG3   Modem configuration.
  176.     0x03,   // MDMCFG2   Modem configuration.
  177.     0x22,   // MDMCFG1   Modem configuration.
  178.     0xF8,   // MDMCFG0   Modem configuration.

  179.     0x00,   // CHANNR    Channel number.
  180.     0x47,   // DEVIATN   Modem deviation setting (when FSK modulation is enabled).
  181.     0xB6,   // FREND1    Front end RX configuration.
  182.     0x10,   // FREND0    Front end RX configuration.
  183.     0x18,   // MCSM0     Main Radio Control State Machine configuration.
  184.     0x1D,   // FOCCFG    Frequency Offset Compensation Configuration.
  185.     0x1C,   // BSCFG     Bit synchronization Configuration.
  186.     0xC7,   // AGCCTRL2  AGC control.
  187.     0x00,   // AGCCTRL1  AGC control.
  188.     0xB2,   // AGCCTRL0  AGC control.

  189.     0xEA,   // FSCAL3    Frequency synthesizer calibration.
  190.     0x2A,   // FSCAL2    Frequency synthesizer calibration.
  191.     0x00,   // FSCAL1    Frequency synthesizer calibration.
  192.     0x11,   // FSCAL0    Frequency synthesizer calibration.
  193.     0x59,   // FSTEST    Frequency synthesizer calibration.
  194.     0x81,   // TEST2     Various test settings.
  195.     0x35,   // TEST1     Various test settings.
  196.     0x09,   // TEST0     Various test settings.
  197.     0x0B,   // IOCFG2    GDO2 output pin configuration.
  198.     0x06,   // IOCFG0D   GDO0 output pin configuration. Refer to SmartRF?Studio User Manual for detailed pseudo register explanation.

  199.     0x04,   // PKTCTRL1  Packet automation control.
  200.     0x05,   // PKTCTRL0  Packet automation control.
  201.     0x00,   // ADDR      Device address.
  202.     0x0c    // PKTLEN    Packet length.
  203. };
  204. //*****************************************************************************************
  205. //函数名:delay(unsigned int s)
  206. //输入:时间
  207. //输出:无
  208. //功能描述:普通廷时,内部用
  209. //*****************************************************************************************               
  210. static void delay(unsigned int s)
  211. {
  212.         unsigned int i;
  213.         for(i=0; i<s; i++);
  214.         for(i=0; i<s; i++);
  215. }


  216. void halWait(INT16U timeout) {
  217.     do {
  218.         _nop_();
  219.                 _nop_();
  220.                 _nop_();
  221.                 _nop_();
  222.                 _nop_();
  223.                 _nop_();
  224.                 _nop_();
  225.                 _nop_();
  226.                 _nop_();
  227.                 _nop_();
  228.                 _nop_();
  229.                 _nop_();
  230.                 _nop_();
  231.                 _nop_();
  232.                 _nop_();
  233.     } while (--timeout);
  234. }


  235. void SpiInit(void)
  236. {
  237.         CSN=0;
  238.         SCK=0;
  239.         CSN=1;
  240. }

  241. /*****************************************************************************************
  242. //函数名:CpuInit()
  243. //输入:无
  244. //输出:无
  245. //功能描述:SPI初始化程序
  246. /*****************************************************************************************/
  247. void CpuInit(void)
  248. {
  249.         SpiInit();
  250.         delay(5000);
  251. }
  252.         

  253. //*****************************************************************************************
  254. //函数名:SpisendByte(INT8U dat)
  255. //输入:发送的数据
  256. //输出:无
  257. //功能描述:SPI发送一个字节
  258. //*****************************************************************************************
  259. INT8U SpiTxRxByte(INT8U dat)
  260. {
  261.         INT8U i,temp;
  262.         temp = 0;
  263.         
  264.         SCK = 0;
  265.         for(i=0; i<8; i++)
  266.         {
  267.                 if(dat & 0x80)
  268.                 {
  269.                         MOSI = 1;
  270.                 }
  271.                 else MOSI = 0;
  272.                 dat <<= 1;

  273.                 SCK = 1;
  274.                 _nop_();
  275.                 _nop_();

  276.                 temp <<= 1;
  277.                 if(MISO)temp++;
  278.                 SCK = 0;
  279.                 _nop_();
  280.                 _nop_();        
  281.         }
  282.         return temp;
  283. }

  284. //*****************************************************************************************
  285. //函数名:void RESET_CC1100(void)
  286. //输入:无
  287. //输出:无
  288. //功能描述:复位CC1100
  289. //*****************************************************************************************
  290. void RESET_CC1100(void)
  291. {
  292.         CSN = 0;
  293.         while (MISO);
  294.     SpiTxRxByte(CCxxx0_SRES);                 //写入复位命令
  295.         while (MISO);
  296.     CSN = 1;
  297. }

  298. //*****************************************************************************************
  299. //函数名:void POWER_UP_RESET_CC1100(void)
  300. //输入:无
  301. //输出:无
  302. //功能描述:上电复位CC1100
  303. //*****************************************************************************************
  304. void POWER_UP_RESET_CC1100(void)
  305. {
  306.         CSN = 1;
  307.         halWait(1);
  308.         CSN = 0;
  309.         halWait(1);
  310.         CSN = 1;
  311.         halWait(41);
  312.         RESET_CC1100();                   //复位CC1100
  313. }

  314. //*****************************************************************************************
  315. //函数名:void halSpiWriteReg(INT8U addr, INT8U value)
  316. //输入:地址和配置字
  317. //输出:无
  318. //功能描述:SPI写寄存器
  319. //*****************************************************************************************
  320. void halSpiWriteReg(INT8U addr, INT8U value)
  321. {
  322.     CSN = 0;
  323.     while (MISO);
  324.     SpiTxRxByte(addr);                //写地址
  325.     SpiTxRxByte(value);                //写入配置
  326.     CSN = 1;
  327. }

  328. //*****************************************************************************************
  329. //函数名:void halSpiWriteBurstReg(INT8U addr, INT8U *buffer, INT8U count)
  330. //输入:地址,写入缓冲区,写入个数
  331. //输出:无
  332. //功能描述:SPI连续写配置寄存器
  333. //*****************************************************************************************
  334. void halSpiWriteBurstReg(INT8U addr, INT8U *buffer, INT8U count)
  335. {
  336.     INT8U i, temp;
  337.         temp = addr | WRITE_BURST;
  338.     CSN = 0;
  339.     while (MISO);
  340.     SpiTxRxByte(temp);
  341.     for (i = 0; i < count; i++)
  342.          {
  343.         SpiTxRxByte(buffer[i]);
  344.     }
  345.     CSN = 1;
  346. }

  347. //*****************************************************************************************
  348. //函数名:void halSpiStrobe(INT8U strobe)
  349. //输入:命令
  350. //输出:无
  351. //功能描述:SPI写命令
  352. //*****************************************************************************************
  353. void halSpiStrobe(INT8U strobe)
  354. {
  355.     CSN = 0;
  356.     while (MISO);
  357.     SpiTxRxByte(strobe);                //写入命令
  358.     CSN = 1;
  359. }





  360. //*****************************************************************************************
  361. //函数名:INT8U halSpiReadReg(INT8U addr)
  362. //输入:地址
  363. //输出:该寄存器的配置字
  364. //功能描述:SPI读寄存器
  365. //*****************************************************************************************
  366. INT8U halSpiReadReg(INT8U addr)
  367. {
  368.         INT8U temp, value;
  369.     temp = addr|READ_SINGLE;//读寄存器命令
  370.         CSN = 0;
  371.         while (MISO);
  372.         SpiTxRxByte(temp);
  373.         value = SpiTxRxByte(0);
  374.         CSN = 1;
  375.         return value;
  376. }


  377. //*****************************************************************************************
  378. //函数名:void halSpiReadBurstReg(INT8U addr, INT8U *buffer, INT8U count)
  379. //输入:地址,读出数据后暂存的缓冲区,读出配置个数
  380. //输出:无
  381. //功能描述:SPI连续写配置寄存器
  382. //*****************************************************************************************
  383. void halSpiReadBurstReg(INT8U addr, INT8U *buffer, INT8U count)
  384. {
  385.     INT8U i,temp;
  386.         temp = addr | READ_BURST;                //写入要读的配置寄存器地址和读命令
  387.     CSN = 0;
  388.     while (MISO);
  389.         SpiTxRxByte(temp);   
  390.     for (i = 0; i < count; i++)
  391.         {
  392.         buffer[i] = SpiTxRxByte(0);
  393.     }
  394.     CSN = 1;
  395. }


  396. //*****************************************************************************************
  397. //函数名:INT8U halSpiReadReg(INT8U addr)
  398. //输入:地址
  399. //输出:该状态寄存器当前值
  400. //功能描述:SPI读状态寄存器
  401. //*****************************************************************************************
  402. INT8U halSpiReadStatus(INT8U addr)
  403. {
  404.     INT8U value,temp;
  405.         temp = addr | READ_BURST;                //写入要读的状态寄存器的地址同时写入读命令
  406.     CSN = 0;
  407.     while (MISO);
  408.     SpiTxRxByte(temp);
  409.         value = SpiTxRxByte(0);
  410.         CSN = 1;
  411.         return value;
  412. }
  413. //*****************************************************************************************
  414. //函数名:void halRfWriteRfSettings(RF_SETTINGS *pRfSettings)
  415. //输入:无
  416. //输出:无
  417. //功能描述:配置CC1100的寄存器
  418. //*****************************************************************************************
  419. void halRfWriteRfSettings(void)
  420. {

  421.         halSpiWriteReg(CCxxx0_FSCTRL0,  rfSettings.FSCTRL2);//自已加的
  422.     // Write register settings
  423.     halSpiWriteReg(CCxxx0_FSCTRL1,  rfSettings.FSCTRL1);
  424.     halSpiWriteReg(CCxxx0_FSCTRL0,  rfSettings.FSCTRL0);
  425.     halSpiWriteReg(CCxxx0_FREQ2,    rfSettings.FREQ2);
  426.     halSpiWriteReg(CCxxx0_FREQ1,    rfSettings.FREQ1);
  427.     halSpiWriteReg(CCxxx0_FREQ0,    rfSettings.FREQ0);
  428.     halSpiWriteReg(CCxxx0_MDMCFG4,  rfSettings.MDMCFG4);
  429.     halSpiWriteReg(CCxxx0_MDMCFG3,  rfSettings.MDMCFG3);
  430.     halSpiWriteReg(CCxxx0_MDMCFG2,  rfSettings.MDMCFG2);
  431.     halSpiWriteReg(CCxxx0_MDMCFG1,  rfSettings.MDMCFG1);
  432.     halSpiWriteReg(CCxxx0_MDMCFG0,  rfSettings.MDMCFG0);
  433.     halSpiWriteReg(CCxxx0_CHANNR,   rfSettings.CHANNR);
  434.     halSpiWriteReg(CCxxx0_DEVIATN,  rfSettings.DEVIATN);
  435.     halSpiWriteReg(CCxxx0_FREND1,   rfSettings.FREND1);
  436.     halSpiWriteReg(CCxxx0_FREND0,   rfSettings.FREND0);
  437.     halSpiWriteReg(CCxxx0_MCSM0 ,   rfSettings.MCSM0 );
  438.     halSpiWriteReg(CCxxx0_FOCCFG,   rfSettings.FOCCFG);
  439.     halSpiWriteReg(CCxxx0_BSCFG,    rfSettings.BSCFG);
  440.     halSpiWriteReg(CCxxx0_AGCCTRL2, rfSettings.AGCCTRL2);
  441.         halSpiWriteReg(CCxxx0_AGCCTRL1, rfSettings.AGCCTRL1);
  442.     halSpiWriteReg(CCxxx0_AGCCTRL0, rfSettings.AGCCTRL0);
  443.     halSpiWriteReg(CCxxx0_FSCAL3,   rfSettings.FSCAL3);
  444.         halSpiWriteReg(CCxxx0_FSCAL2,   rfSettings.FSCAL2);
  445.         halSpiWriteReg(CCxxx0_FSCAL1,   rfSettings.FSCAL1);
  446.     halSpiWriteReg(CCxxx0_FSCAL0,   rfSettings.FSCAL0);
  447.     halSpiWriteReg(CCxxx0_FSTEST,   rfSettings.FSTEST);
  448.     halSpiWriteReg(CCxxx0_TEST2,    rfSettings.TEST2);
  449.     halSpiWriteReg(CCxxx0_TEST1,    rfSettings.TEST1);
  450.     halSpiWriteReg(CCxxx0_TEST0,    rfSettings.TEST0);
  451.     halSpiWriteReg(CCxxx0_IOCFG2,   rfSettings.IOCFG2);
  452.     halSpiWriteReg(CCxxx0_IOCFG0,   rfSettings.IOCFG0);   
  453.     halSpiWriteReg(CCxxx0_PKTCTRL1, rfSettings.PKTCTRL1);
  454.     halSpiWriteReg(CCxxx0_PKTCTRL0, rfSettings.PKTCTRL0);
  455.     halSpiWriteReg(CCxxx0_ADDR,     rfSettings.ADDR);
  456.     halSpiWriteReg(CCxxx0_PKTLEN,   rfSettings.PKTLEN);
  457. }

  458. //*****************************************************************************************
  459. //函数名:void halRfSendPacket(INT8U *txBuffer, INT8U size)
  460. //输入:发送的缓冲区,发送数据个数
  461. //输出:无
  462. //功能描述:CC1100发送一组数据
  463. //*****************************************************************************************

  464. void halRfSendPacket(INT8U *txBuffer, INT8U size)
  465. {
  466.         halSpiWriteReg(CCxxx0_TXFIFO, size);
  467.     halSpiWriteBurstReg(CCxxx0_TXFIFO, txBuffer, size);        //写入要发送的数据

  468.     halSpiStrobe(CCxxx0_STX);                //进入发送模式发送数据

  469.     // Wait for GDO0 to be set -> sync transmitted
  470.     while (!GDO0);
  471.     // Wait for GDO0 to be cleared -> end of packet
  472.     while (GDO0);
  473.         halSpiStrobe(CCxxx0_SFTX);
  474. }


  475. void setRxMode(void)
  476. {
  477.     halSpiStrobe(CCxxx0_SRX);                //进入接收状态
  478. }

  479. /*
  480. // Bit masks corresponding to STATE[2:0] in the status byte returned on MISO
  481. #define CCxx00_STATE_BM                 0x70
  482. #define CCxx00_FIFO_BYTES_AVAILABLE_BM  0x0F
  483. #define CCxx00_STATE_TX_BM              0x20
  484. #define CCxx00_STATE_TX_UNDERFLOW_BM    0x70
  485. #define CCxx00_STATE_RX_BM              0x10
  486. #define CCxx00_STATE_RX_OVERFLOW_BM     0x60
  487. #define CCxx00_STATE_IDLE_BM            0x00

  488. static INT8U RfGetRxStatus(void)
  489. {
  490.         INT8U temp, spiRxStatus1,spiRxStatus2;
  491.         INT8U i=4;// 循环测试次数
  492.     temp = CCxxx0_SNOP|READ_SINGLE;//读寄存器命令
  493.         CSN = 0;
  494.         while (MISO);
  495.         SpiTxRxByte(temp);
  496.         spiRxStatus1 = SpiTxRxByte(0);
  497.         do
  498.         {
  499.                 SpiTxRxByte(temp);
  500.                 spiRxStatus2 = SpiTxRxByte(0);
  501.                 if(spiRxStatus1 == spiRxStatus2)
  502.                 {
  503.                         if( (spiRxStatus1 & CCxx00_STATE_BM) == CCxx00_STATE_RX_OVERFLOW_BM)
  504.                         {
  505.                halSpiStrobe(CCxxx0_SFRX);
  506.                            return 0;
  507.                         }
  508.                     return 1;
  509.                 }
  510.                  spiRxStatus1=spiRxStatus2;
  511.         }
  512.         while(i--);
  513.         CSN = 1;
  514.     return 0;        
  515. }
  516. */
  517. INT8U halRfReceivePacket(INT8U *rxBuffer, INT8U *length)
  518. {
  519.     INT8U status[2];
  520.     INT8U packetLength;
  521.         INT8U i=(*length)*4;  // 具体多少要根据datarate和length来决定

  522.     halSpiStrobe(CCxxx0_SRX);                //进入接收状态
  523.         //delay(5);
  524.     //while (!GDO1);
  525.     //while (GDO1);
  526.         delay(2);
  527.         while (GDO0)
  528.         {
  529.                 delay(2);
  530.                 --i;
  531.                 if(i<1)
  532.                    return 0;            
  533.         }         
  534.     if ((halSpiReadStatus(CCxxx0_RXBYTES) & BYTES_IN_RXFIFO)) //如果接的字节数不为0
  535.         {
  536.         packetLength = halSpiReadReg(CCxxx0_RXFIFO);//读出第一个字节,此字节为该帧数据长度
  537.         if (packetLength <= *length)                 //如果所要的有效数据长度小于等于接收到的数据包的长度
  538.                 {
  539.             halSpiReadBurstReg(CCxxx0_RXFIFO, rxBuffer, packetLength); //读出所有接收到的数据
  540.             *length = packetLength;                                //把接收数据长度的修改为当前数据的长度
  541.         
  542.             // Read the 2 appended status bytes (status[0] = RSSI, status[1] = LQI)
  543.             halSpiReadBurstReg(CCxxx0_RXFIFO, status, 2);         //读出CRC校验位
  544.                         halSpiStrobe(CCxxx0_SFRX);                //清洗接收缓冲区
  545.             return (status[1] & CRC_OK);                        //如果校验成功返回接收成功
  546.         }
  547.                  else
  548.                 {
  549.             *length = packetLength;
  550.             halSpiStrobe(CCxxx0_SFRX);                //清洗接收缓冲区
  551.             return 0;
  552.         }
  553.     }
  554.         else
  555.          return 0;
  556. }
  557. /*
  558. void Delay(unsigned int s)
  559. {
  560.         unsigned int i;
  561.         for(i=0; i<s; i++);
  562.         for(i=0; i<s; i++);
  563. }

  564. */
  565. void main(void)
  566. {
  567.         INT8U leng =0;
  568.         INT8U tf =0;
  569.         INT8U TxBuf[8]={0};         // 8字节, 如果需要更长的数据包,请正确设置
  570.         INT8U RxBuf[8]={0};        
  571.         CpuInit();
  572.         POWER_UP_RESET_CC1100();
  573.         halRfWriteRfSettings();
  574.         halSpiWriteBurstReg(CCxxx0_PATABLE, PaTabel, 8);
  575.         //TxBuf[0] = 8 ;
  576.         TxBuf[1] = 1 ;
  577.         TxBuf[2] = 1 ;
  578.         halRfSendPacket(TxBuf,8);        // Transmit Tx buffer data
  579.         delay(6000);
  580.         P0=0xBF;
  581.         LED=0;
  582.         while(1)
  583.         {
  584.             //setRxMode();
  585.             if(KEY ==0 )
  586.                   {

  587.                     TxBuf[1] = 1 ;
  588.                     tf = 1 ;
  589.                         //while(KEY1 ==0);
  590.             }
  591.            if (tf==1)
  592.        {        
  593.                      LED=0;
  594.                         halRfSendPacket(TxBuf,8);        // Transmit Tx buffer data
  595.                         TxBuf[1] = 0xff;
  596.                         TxBuf[2] = 0xff;
  597.                         tf=0;
  598.                         
  599.                         delay(500);
  600.                         LED=1;
  601.            }
  602.                 leng =8; // 预计接受8 bytes
  603.                    if(halRfReceivePacket(RxBuf,&leng))
  604.                 {
  605.                                                 
  606.                         if(        RxBuf[1]==1)
  607.                         {                 
  608.                         LED=0;
  609.                
  610.                         }
  611.                 delay(1000);
  612.                         LED=1;
  613.                 }

  614.                 RxBuf[1] = 0xff;
  615.                 RxBuf[2] = 0xff;
  616.         }
  617.         
  618. }



复制代码


评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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