找回密码
 立即注册

QQ登录

只需一步,快速开始

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

基于MSP430F149的cc1101无线收发源程序

[复制链接]
跳转到指定楼层
楼主
最基础的收发程序,可以在此基础上完成WOR后续功能的添加



单片机源程序如下:
  1. #include <in430.h>
  2. #include <io430.h>
  3. #include "cc1100.h"

  4. #define                INT8U                unsigned char
  5. #define                INT16U                unsigned int

  6. unsigned char PaTabel[8] = {0xC0 ,0xC0 ,0xC0 ,0xC0 ,0xC0 ,0xC0 ,0xC0 ,0xC0};
  7. unsigned int TestInt = 0;


  8. const RF_SETTINGS rfSettings =
  9. {
  10.     0x00,
  11.     0x08,   // FSCTRL1   Frequency synthesizer control.
  12.     0x00,   // FSCTRL0   Frequency synthesizer control.
  13.     0x10,   // FREQ2     Frequency control word, high byte.
  14.     0xA7,   // FREQ1     Frequency control word, middle byte.
  15.     0x62,   // FREQ0     Frequency control word, low byte.
  16.     0x5B,   // MDMCFG4   Modem configuration.
  17.     0xF8,   // MDMCFG3   Modem configuration.
  18.     0x03,   // MDMCFG2   Modem configuration.
  19.     0x22,   // MDMCFG1   Modem configuration.
  20.     0xF8,   // MDMCFG0   Modem configuration.
  21.     0x00,   // CHANNR    Channel number.
  22.     0x47,   // DEVIATN   Modem deviation setting (when FSK modulation is enabled).
  23.     0xB6,   // FREND1    Front end RX configuration.
  24.     0x10,   // FREND0    Front end RX configuration.
  25.     0x18,   // MCSM0     Main Radio Control State Machine configuration.
  26.     0x1D,   // FOCCFG    Frequency Offset Compensation Configuration.
  27.     0x1C,   // BSCFG     Bit synchronization Configuration.
  28.     0xC7,   // AGCCTRL2  AGC control.
  29.     0x00,   // AGCCTRL1  AGC control.
  30.     0xB2,   // AGCCTRL0  AGC control.
  31.     0xEA,   // FSCAL3    Frequency synthesizer calibration.
  32.     0x2A,   // FSCAL2    Frequency synthesizer calibration.
  33.     0x00,   // FSCAL1    Frequency synthesizer calibration.
  34.     0x11,   // FSCAL0    Frequency synthesizer calibration.
  35.     0x59,   // FSTEST    Frequency synthesizer calibration.
  36.     0x81,   // TEST2     Various test settings.
  37.     0x35,   // TEST1     Various test settings.
  38.     0x09,   // TEST0     Various test settings.
  39.     0x0B,   // IOCFG2    GDO2 output pin configuration.
  40.     0x06,   // IOCFG0D   GDO0 output pin configuration. Refer to SmartRF?Studio User Manual for detailed pseudo register explanation.
  41.     0x04,   // PKTCTRL1  Packet automation control.
  42.     0x05,   // PKTCTRL0  Packet automation control.
  43.     0x01,   // ADDR      Device address.
  44.     0x0c    // PKTLEN    Packet length.
  45. };

  46. void delay(unsigned int s)
  47. {
  48.     while (s != 0)
  49.     {
  50.         s--;
  51.     }
  52. }

  53. void halWait(unsigned int s)
  54. {
  55. do
  56.     {
  57.         _NOP();
  58.         _NOP();
  59.         _NOP();
  60.         _NOP();
  61.         _NOP();
  62.         _NOP();
  63.         _NOP();
  64.         _NOP();
  65.         _NOP();
  66.         _NOP();
  67.         _NOP();
  68.         _NOP();
  69.         _NOP();
  70.         _NOP();
  71.         _NOP();
  72.         _NOP();
  73.     }
  74.     while(--s);
  75. }

  76. void InitSpi(void)
  77. {
  78.         CSN_L;
  79.         SCK_L;
  80.         CSN_H;
  81. }

  82. void halSpiWriteReg(unsigned char addr, unsigned char value)
  83. {
  84.     CSN_L;
  85.     while (GetMISO);
  86.     SpiTxRxByte(addr);                //写地址
  87.     SpiTxRxByte(value);                //写入配置
  88.     CSN_H;
  89. }

  90. void halSpiWriteBurstReg(INT8U addr, INT8U *buffer, INT8U count)
  91. {
  92.     INT8U i, temp;
  93.     temp = addr | WRITE_BURST;
  94.     CSN_L;
  95.     while (GetMISO);
  96.     SpiTxRxByte(temp);
  97.     for (i = 0; i < count; i++)
  98.     {
  99.         SpiTxRxByte(buffer[i]);
  100.     }
  101.     CSN_H;
  102. }

  103. void halSpiStrobe(INT8U strobe)
  104. {
  105.     CSN_L;
  106.     while (GetMISO);
  107.     SpiTxRxByte(strobe);                //写入命令
  108.     CSN_H;
  109. }

  110. INT8U halSpiReadReg(INT8U addr)
  111. {
  112.     INT8U temp, value;
  113.     temp = addr|READ_SINGLE;//读寄存器命令
  114.     CSN_L;
  115.     while (GetMISO);
  116.     SpiTxRxByte(temp);
  117.     value = SpiTxRxByte(0);
  118.     CSN_H;
  119.     return value;
  120. }

  121. void halSpiReadBurstReg(INT8U addr, INT8U *buffer, INT8U count)
  122. {
  123.     INT8U i,temp;
  124.     temp = addr | READ_BURST;                //写入要读的配置寄存器地址和读命令
  125.     CSN_L;
  126.     while (GetMISO);
  127.         SpiTxRxByte(temp);   
  128.     for (i = 0; i < count; i++)
  129.     {
  130.         buffer[i] = SpiTxRxByte(0);
  131.     }
  132.     CSN_H;
  133. }

  134. INT8U halSpiReadStatus(INT8U addr)
  135. {
  136.     INT8U value,temp;
  137.     temp = addr | READ_BURST;                //写入要读的状态寄存器的地址同时写入读命令
  138.     CSN_L;
  139.     while (GetMISO);
  140.     SpiTxRxByte(temp);
  141.     value = SpiTxRxByte(0);
  142.     CSN_H;
  143.     return value;
  144. }

  145. void halRfWriteRfSettings(void)
  146. {
  147.     halSpiWriteReg(CCxxx0_FSCTRL0,  rfSettings.FSCTRL2);//自已加的
  148.     // Write register settings
  149.     halSpiWriteReg(CCxxx0_FSCTRL1,  rfSettings.FSCTRL1);
  150.     halSpiWriteReg(CCxxx0_FSCTRL0,  rfSettings.FSCTRL0);
  151.     halSpiWriteReg(CCxxx0_FREQ2,    rfSettings.FREQ2);
  152.     halSpiWriteReg(CCxxx0_FREQ1,    rfSettings.FREQ1);
  153.     halSpiWriteReg(CCxxx0_FREQ0,    rfSettings.FREQ0);
  154.     halSpiWriteReg(CCxxx0_MDMCFG4,  rfSettings.MDMCFG4);
  155.     halSpiWriteReg(CCxxx0_MDMCFG3,  rfSettings.MDMCFG3);
  156.     halSpiWriteReg(CCxxx0_MDMCFG2,  rfSettings.MDMCFG2);
  157.     halSpiWriteReg(CCxxx0_MDMCFG1,  rfSettings.MDMCFG1);
  158.     halSpiWriteReg(CCxxx0_MDMCFG0,  rfSettings.MDMCFG0);
  159.     halSpiWriteReg(CCxxx0_CHANNR,   rfSettings.CHANNR);
  160.     halSpiWriteReg(CCxxx0_DEVIATN,  rfSettings.DEVIATN);
  161.     halSpiWriteReg(CCxxx0_FREND1,   rfSettings.FREND1);
  162.     halSpiWriteReg(CCxxx0_FREND0,   rfSettings.FREND0);
  163.     halSpiWriteReg(CCxxx0_MCSM0 ,   rfSettings.MCSM0 );
  164.     halSpiWriteReg(CCxxx0_FOCCFG,   rfSettings.FOCCFG);
  165.     halSpiWriteReg(CCxxx0_BSCFG,    rfSettings.BSCFG);
  166.     halSpiWriteReg(CCxxx0_AGCCTRL2, rfSettings.AGCCTRL2);
  167.     halSpiWriteReg(CCxxx0_AGCCTRL1, rfSettings.AGCCTRL1);
  168.     halSpiWriteReg(CCxxx0_AGCCTRL0, rfSettings.AGCCTRL0);
  169.     halSpiWriteReg(CCxxx0_FSCAL3,   rfSettings.FSCAL3);
  170.     halSpiWriteReg(CCxxx0_FSCAL2,   rfSettings.FSCAL2);
  171.     halSpiWriteReg(CCxxx0_FSCAL1,   rfSettings.FSCAL1);
  172.     halSpiWriteReg(CCxxx0_FSCAL0,   rfSettings.FSCAL0);
  173.     halSpiWriteReg(CCxxx0_FSTEST,   rfSettings.FSTEST);
  174.     halSpiWriteReg(CCxxx0_TEST2,    rfSettings.TEST2);
  175.     halSpiWriteReg(CCxxx0_TEST1,    rfSettings.TEST1);
  176.     halSpiWriteReg(CCxxx0_TEST0,    rfSettings.TEST0);
  177.     halSpiWriteReg(CCxxx0_IOCFG2,   rfSettings.IOCFG2);
  178.     halSpiWriteReg(CCxxx0_IOCFG0,   rfSettings.IOCFG0);   
  179.     halSpiWriteReg(CCxxx0_PKTCTRL1, rfSettings.PKTCTRL1);
  180.     halSpiWriteReg(CCxxx0_PKTCTRL0, rfSettings.PKTCTRL0);
  181.     halSpiWriteReg(CCxxx0_ADDR,     rfSettings.ADDR);
  182.     halSpiWriteReg(CCxxx0_PKTLEN,   rfSettings.PKTLEN);
  183. }

  184. void halRfSendPacket(INT8U *txBuffer, INT8U size)
  185. {
  186.     halSpiWriteReg(CCxxx0_TXFIFO, size);
  187.     halSpiWriteBurstReg(CCxxx0_TXFIFO, txBuffer, size);        //写入要发送的数据
  188.     halSpiStrobe(CCxxx0_SIDLE); // 网上解决GDO0不跳变的解决方法,原来没有
  189.     halSpiStrobe(CCxxx0_STX);                //进入发送模式发送数据
  190.    
  191.     // Wait for GDO0 to be set -> sync transmitted
  192.     while (!GOD0);
  193.     // Wait for GDO0 to be cleared -> end of packet
  194.     while (GOD0);
  195.     halSpiStrobe(CCxxx0_SFTX);
  196. }


  197. void setRxMode(void)
  198. {
  199.     halSpiStrobe(CCxxx0_SRX);                //进入接收状态
  200. }

  201. INT8U halRfReceivePacket(INT8U *rxBuffer, INT8U *length)
  202. {
  203.   INT8U status[2];
  204.   INT8U packetLength;
  205.   INT8U i=(*length)*4;                  // 具体多少要根据datarate和length来决定
  206.   
  207.   halSpiStrobe(CCxxx0_SRX);                //进入接收状态
  208.   
  209.   halWait(2000);
  210.   while (GOD0)
  211.   {
  212.     halWait(1000);
  213.     --i;
  214.     if(i<1)
  215.     {
  216.       return 0;
  217.     }
  218.   }         
  219.   if ((halSpiReadStatus(CCxxx0_RXBYTES) & BYTES_IN_RXFIFO)) //如果接的字节数不为0
  220.   {
  221.     packetLength = halSpiReadReg(CCxxx0_RXFIFO);//读出第一个字节,此字节为该帧数据长度
  222.     if (packetLength <= *length)                 //如果所要的有效数据长度小于等于接收到的数据包的长度
  223.     {
  224.       halSpiReadBurstReg(CCxxx0_RXFIFO, rxBuffer, packetLength); //读出所有接收到的数据
  225.       *length = packetLength;                                //把接收数据长度的修改为当前数据的长度
  226.       
  227.       // Read the 2 appended status bytes (status[0] = RSSI, status[1] = LQI)
  228.       halSpiReadBurstReg(CCxxx0_RXFIFO, status, 2);         //读出CRC校验位
  229.       halSpiStrobe(CCxxx0_SFRX);                //清洗接收缓冲区
  230.       return (status[1] & CRC_OK);                        //如果校验成功返回接收成功
  231.     }
  232.     else
  233.     {
  234.       *length = packetLength;
  235.       halSpiStrobe(CCxxx0_SFRX);                //清洗接收缓冲区
  236.       return 0;
  237.     }
  238.   }
  239.   else
  240.   {
  241.     return 0;
  242.   }
  243. }

  244. unsigned char SpiTxRxByte(unsigned char dat)
  245. {
  246.     unsigned char i,temp;
  247.     temp = 0;
  248.     SCK_L;
  249.     for(i=0; i<8; i++)
  250.     {
  251.         if(dat & 0x80)
  252.         {
  253.             MOSI_H;
  254.         }
  255.         else
  256.         {
  257.             MOSI_L;
  258.         }
  259.         dat <<= 1;
  260.         SCK_H;
  261.         delay(20);
  262.         temp <<= 1;
  263.          
  264.         if(GetMISO)
  265.         {
  266.             temp++;
  267.         }
  268.         SCK_L;
  269.         delay(20);
  270.     }
  271.    
  272.     return temp;
  273. }

  274. unsigned int RESET_CC1100(void)
  275. {
  276.     CSN_L;
  277.     while (GetMISO);
  278.     unsigned int ret = SpiTxRxByte(CCxxx0_SRES);                 //写入复位命令
  279.     while (GetMISO);
  280.     CSN_H;
  281.     return ret;
  282. }

  283. unsigned int POWER_UP_RESET_CC1100(void)
  284. {
  285.     CSN_H;
  286.     halWait(1);
  287.     CSN_L;
  288.     halWait(1);
  289.     CSN_H;
  290.     halWait(41);
  291.     unsigned int ret = RESET_CC1100();                   //复位CC1100
  292.     return ret;
  293. }
复制代码

所有资料51hei提供下载:
XB.zip (97.67 KB, 下载次数: 46)


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

使用道具 举报

沙发
ID:442655 发表于 2018-12-9 14:56 | 只看该作者
有低功耗程序吗?
回复

使用道具 举报

板凳
ID:223999 发表于 2019-2-25 14:34 | 只看该作者
这个正是在用的
回复

使用道具 举报

地板
ID:55767 发表于 2019-6-17 22:23 | 只看该作者
请问代码中单片机的使用的晶振频率是多少啊
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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