找回密码
 立即注册

QQ登录

只需一步,快速开始

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

亲测cc1101驱动程序源码,有问题随时联系我

[复制链接]
跳转到指定楼层
楼主
亲自使用的cc1101驱动程序,使用0x06方式接收中断 ,目前公司产品大规模使用中,有需要的小伙伴可以试一下



单片机源程序如下:
  1. /**
  2. *@file      CC1101.c
  3. *@brief     这个文件定义了CC1101驱动的功能相关的源码
  4. *@author    phang
  5. *@date                        2015/11/4 18:02:50
  6. *@version   ver 0.1
  7. *@copyright seenboom
  8. */
  9. #include "CC1101.H"

  10. //10, 7, 5, 0, -5, -10, -15, -20, dbm output power, 0x12 == -30dbm
  11. const u8 PaTabel[] = { 0xc0, 0xC8, 0x84, 0x60, 0x68, 0x34, 0x1D, 0x0E};

  12. // Sync word qualifier mode = 30/32 sync word bits detected
  13. // CRC autoflush = false
  14. // Channel spacing = 199.951172
  15. // Data format = Normal mode
  16. // Data rate = 2.00224
  17. // RX filter BW = 58.035714
  18. // PA ramping = false
  19. // Preamble count = 4
  20. // Whitening = false
  21. // Address config = No address check
  22. // Carrier frequency = 400.199890
  23. // Device address = 0
  24. // TX power = 10
  25. // Manchester enable = false
  26. // CRC enable = true
  27. // Deviation = 5.157471
  28. // Packet length mode = Variable packet length mode. Packet length configured by the first byte after sync word
  29. // Packet length = 255
  30. // Modulation format = GFSK
  31. // Base frequency = 399.999939
  32. // Modulated = true
  33. // Channel number = 1
  34. // PA table
  35. #define PA_TABLE {0xc2,0x00,0x00,0x00,0x00,0x00,0x00,0x00,}

  36. /** 初始化用的寄存器表,寄存器名和寄存器值 **/

  37. static const u8 CC1101InitData[22][2]=
  38. {
  39.   {CC1101_IOCFG0,      0x06},
  40.   {CC1101_FIFOTHR,     0x47},
  41.   {CC1101_PKTCTRL0,    0x05},
  42.   {CC1101_CHANNR,      0x01},
  43.   {CC1101_FSCTRL1,     0x06},
  44.   {CC1101_FREQ2,       0x0F},
  45.   {CC1101_FREQ1,       0x62},
  46.   {CC1101_FREQ0,       0x76},
  47.   {CC1101_MDMCFG4,     0xF6},
  48.   {CC1101_MDMCFG3,     0x43},
  49.   {CC1101_MDMCFG2,     0x13},
  50.   {CC1101_DEVIATN,     0x15},
  51.   {CC1101_MCSM0,       0x18},
  52.   {CC1101_FOCCFG,      0x16},
  53.   {CC1101_WORCTRL,     0xFB},
  54.   {CC1101_FSCAL3,      0xE9},
  55.   {CC1101_FSCAL2,      0x2A},
  56.   {CC1101_FSCAL1,      0x00},
  57.   {CC1101_FSCAL0,      0x1F},
  58.   {CC1101_TEST2,       0x81},
  59.   {CC1101_TEST1,       0x35},
  60.   {CC1101_MCSM1,       0x3B},
  61. };


  62. /*read a byte from the specified register*/
  63. u8 CC1101ReadReg( u8 addr );

  64. /*Read some bytes from the rigisters continously*/
  65. void CC1101ReadMultiReg( u8 addr, u8 *buff, u8 size );

  66. /*Write a byte to the specified register*/
  67. void CC1101WriteReg( u8 addr, u8 value );

  68. /*Flush the TX buffer of CC1101*/
  69. void CC1101ClrTXBuff( void );

  70. /*Flush the RX buffer of CC1101*/
  71. void CC1101ClrRXBuff( void );

  72. /*Get received count of CC1101*/
  73. u8 CC1101GetRXCnt( void );

  74. /*Reset the CC1101 device*/
  75. void CC1101Reset( void );

  76. /*Write some bytes to the specified register*/
  77. void CC1101WriteMultiReg( u8 addr, u8 *buff, u8 size );
  78. u8 SPI_ExchangeByte( u8 input );

  79. /** CC1101射频需要用的关键函数 **/
  80. u8 SPI_ExchangeByte( u8 input )
  81. {
  82.     //wait for last transmitt finishing
  83.     /** 等待SPI状态,BSY指SPI是否忙 **/
  84.         while (SPI_GetFlagStatus(SPI_FLAG_BSY))
  85.         {

  86.         };
  87.          SPI_SendData( input );
  88.         //wait for receiving a byte
  89.         while (SPI_GetFlagStatus(SPI_FLAG_BSY))
  90.         {

  91.         };
  92.         return SPI_ReceiveData();
  93. }


  94. /*
  95. ================================================================================
  96. Function : CC1101WORInit( )
  97.     Initialize the WOR function of CC1101
  98. INPUT    : None
  99. OUTPUT   : None
  100. ================================================================================
  101. */
  102. void  CC1101WORInit( void )
  103. {

  104.     CC1101WriteReg(CC1101_MCSM0,0x18);
  105.     CC1101WriteReg(CC1101_WORCTRL,0x78); //Wake On Radio Control
  106.     CC1101WriteReg(CC1101_MCSM2,0x00);
  107.     CC1101WriteReg(CC1101_WOREVT1,0x8C);
  108.     CC1101WriteReg(CC1101_WOREVT0,0xA0);
  109.         
  110.         CC1101WriteCmd( CC1101_SWORRST );
  111. }
  112. /*
  113. ================================================================================
  114. Function : CC1101ReadReg( )
  115.     read a byte from the specified register
  116. INPUT    : addr, The address of the register
  117. OUTPUT   : the byte read from the rigister
  118. ================================================================================
  119. */
  120. u8 CC1101ReadReg( u8 addr )
  121. {
  122.     u8 i;
  123.     CC_CSN_LOW( );
  124.     SPI_ExchangeByte( addr | READ_SINGLE);
  125.     i = SPI_ExchangeByte( 0xFF );
  126.     CC_CSN_HIGH( );
  127.     return i;
  128. }
  129. /*
  130. ================================================================================
  131. Function : CC1101ReadMultiReg( )
  132.     Read some bytes from the rigisters continously
  133. INPUT    : addr, The address of the register
  134.            buff, The buffer stores the data
  135.            size, How many bytes should be read
  136. OUTPUT   : None
  137. ================================================================================
  138. */
  139. void CC1101ReadMultiReg( u8 addr, u8 *buff, u8 size )
  140. {
  141.     u8 i, j;
  142.     CC_CSN_LOW( );
  143.     SPI_ExchangeByte( addr | READ_BURST);
  144.     for( i = 0; i < size; i ++ )
  145.     {
  146.         for( j = 0; j < 20; j ++ );
  147.         *( buff + i ) = SPI_ExchangeByte( 0xFF );
  148.     }
  149.     CC_CSN_HIGH( );
  150. }
  151. /*
  152. ================================================================================
  153. Function : CC1101ReadStatus( )
  154.     Read a status register
  155. INPUT    : addr, The address of the register
  156. OUTPUT   : the value read from the status register
  157. ================================================================================
  158. */
  159. u8 CC1101ReadStatus( u8 addr )
  160. {
  161.     u8 i;
  162.     CC_CSN_LOW( );
  163.     SPI_ExchangeByte( addr | READ_BURST);
  164.     i = SPI_ExchangeByte( 0xFF );
  165.     CC_CSN_HIGH( );
  166.     return i;
  167. }
  168. /*
  169. ================================================================================
  170. Function : CC1101SetTRMode( )
  171.     Set the device as TX mode or RX mode
  172. INPUT    : mode selection
  173. OUTPUT   : None
  174. ================================================================================
  175. */
  176. void CC1101SetTRMode( TRMODE mode )
  177. {
  178.     if( mode == TX_MODE )
  179.     {
  180.         CC1101WriteReg(CC1101_IOCFG0,0x46);
  181.         CC1101WriteCmd( CC1101_STX );
  182.     }
  183.     else if( mode == RX_MODE )
  184.     {
  185.         CC1101WriteReg(CC1101_IOCFG0,0x46);
  186.         CC1101WriteCmd( CC1101_SRX );
  187.     }
  188. }
  189. /*
  190. ================================================================================
  191. Function : CC1101WriteReg( )
  192.     Write a byte to the specified register
  193. INPUT    : addr, The address of the register
  194.            value, the byte you want to write
  195. OUTPUT   : None
  196. ================================================================================
  197. */
  198. void CC1101WriteReg( u8 addr, u8 value )
  199. {
  200.     CC_CSN_LOW( );
  201.     SPI_ExchangeByte( addr );
  202.     SPI_ExchangeByte( value );
  203.     CC_CSN_HIGH( );
  204. }
  205. /*
  206. ================================================================================
  207. Function : CC1101WriteMultiReg( )
  208.     Write some bytes to the specified register
  209. INPUT    : addr, The address of the register
  210.            buff, a buffer stores the values
  211.            size, How many byte should be written
  212. OUTPUT   : None
  213. ================================================================================
  214. */
  215. void CC1101WriteMultiReg( u8 addr, u8 *buff, u8 size )
  216. {
  217.     u8 i;
  218.     CC_CSN_LOW( );
  219.     SPI_ExchangeByte( addr | WRITE_BURST );
  220.     for( i = 0; i < size; i ++ )
  221.     {
  222.         SPI_ExchangeByte( *( buff + i ) );
  223.     }
  224.     CC_CSN_HIGH( );
  225. }
  226. /*
  227. ================================================================================
  228. Function : CC1101WriteCmd( )
  229.     Write a command byte to the device
  230. INPUT    : command, the byte you want to write
  231. OUTPUT   : None
  232. ================================================================================
  233. */
  234. void CC1101WriteCmd( u8 command )
  235. {
  236.     CC_CSN_LOW( );
  237.     SPI_ExchangeByte( command );
  238.     CC_CSN_HIGH( );
  239. }
  240. /*
  241. ================================================================================
  242. Function : CC1101Reset( )
  243.     Reset the CC1101 device
  244. INPUT    : None
  245. OUTPUT   : None
  246. ================================================================================
  247. */
  248. void CC1101Reset( void )
  249. {
  250.     u8 x;

  251.     CC_CSN_HIGH( );
  252.     CC_CSN_LOW( );
  253.     CC_CSN_HIGH( );
  254.     for( x = 0; x < 100; x ++ );
  255.     CC1101WriteCmd( CC1101_SRES );
  256. }
  257. /*
  258. ================================================================================
  259. Function : CC1101SetIdle( )
  260.     Set the CC1101 into IDLE mode
  261. INPUT    : None
  262. OUTPUT   : None
  263. ================================================================================
  264. */
  265. void CC1101SetIdle( void )
  266. {
  267.     CC1101WriteCmd(CC1101_SIDLE);
  268. }
  269. /*
  270. ================================================================================
  271. Function : CC1101ClrTXBuff( )
  272.     Flush the TX buffer of CC1101
  273. INPUT    : None
  274. OUTPUT   : None
  275. ================================================================================
  276. */
  277. void CC1101ClrTXBuff( void )
  278. {
  279.     CC1101SetIdle();//MUST BE IDLE MODE
  280.     CC1101WriteCmd( CC1101_SFTX );
  281. }
  282. /*
  283. ================================================================================
  284. Function : CC1101ClrRXBuff( )
  285.     Flush the RX buffer of CC1101
  286. INPUT    : None
  287. OUTPUT   : None
  288. ================================================================================
  289. */
  290. void CC1101ClrRXBuff( void )
  291. {
  292.     CC1101SetIdle();//MUST BE IDLE MODE
  293.     CC1101WriteCmd( CC1101_SFRX );
  294. }
  295. /*
  296. ================================================================================
  297. Function : CC1101SendPacket( )
  298.     Send a packet
  299. INPUT    : txbuffer, The buffer stores data to be sent
  300.            size, How many bytes should be sent
  301.            mode, Broadcast or address check packet
  302. OUTPUT   : None
  303. ================================================================================
  304. */
  305. void CC1101SendPacket( u8 *txbuffer, u8 size, TX_DATA_MODE mode )
  306. {
  307.     u8 address;
  308.     if( mode == BROADCAST )             { address = 0; }
  309.     else if( mode == ADDRESS_CHECK )    { address = CC1101ReadReg( CC1101_ADDR ); }

  310.     CC1101ClrTXBuff( );
  311.    
  312.     if( ( CC1101ReadReg( CC1101_PKTCTRL1 ) & ~0x03 ) != 0 )
  313.     {
  314.         CC1101WriteReg( CC1101_TXFIFO, size + 1 );
  315.         CC1101WriteReg( CC1101_TXFIFO, address );
  316.     }
  317.     else
  318.     {
  319.         CC1101WriteReg( CC1101_TXFIFO, size );
  320.     }

  321.     CC1101WriteMultiReg( CC1101_TXFIFO, txbuffer, size );
  322.     CC1101SetTRMode( TX_MODE );
  323.     /** Wait for GDO0 to be set -> sync transmitted **/
  324.     while(!GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_4 ));
  325.     /** Wait for GDO0 to be cleared -> end of packet **/
  326.     while(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_4 ));

  327.     CC1101ClrTXBuff( );
  328. }
  329. /*
  330. ================================================================================
  331. Function : CC1101GetRXCnt( )
  332.     Get received count of CC1101
  333. INPUT    : None
  334. OUTPUT   : How many bytes hae been received
  335. ================================================================================
  336. */
  337. u8 CC1101GetRXCnt( void )
  338. {
  339.     return ( CC1101ReadStatus( CC1101_RXBYTES )  & BYTES_IN_RXFIFO );
  340. }
  341. /*
  342. ================================================================================
  343. Function : CC1101SetAddress( )
  344.     Set the address and address mode of the CC1101
  345. INPUT    : address, The address byte
  346.            AddressMode, the address check mode
  347. OUTPUT   : None
  348. ================================================================================
  349. */
  350. void CC1101SetAddress( u8 address, ADDR_MODE AddressMode)
  351. {
  352.     u8 btmp = CC1101ReadReg( CC1101_PKTCTRL1 ) & ~0x03;
  353.     CC1101WriteReg(CC1101_ADDR, address);
  354.     if     ( AddressMode == BROAD_ALL )     {}
  355.     else if( AddressMode == BROAD_NO  )     { btmp |= 0x01; }
  356.     else if( AddressMode == BROAD_0   )     { btmp |= 0x02; }
  357.     else if( AddressMode == BROAD_0AND255 ) { btmp |= 0x03; }   
  358. }
  359. /*
  360. ================================================================================
  361. Function : CC1101SetSYNC( )
  362.     Set the SYNC bytes of the CC1101
  363. INPUT    : sync, 16bit sync
  364. OUTPUT   : None
  365. ================================================================================
  366. */
  367. void CC1101SetSYNC( u16 sync )
  368. {
  369.     CC1101WriteReg(CC1101_SYNC1, 0xFF & ( sync>>8 ) );
  370.     CC1101WriteReg(CC1101_SYNC0, 0xFF & sync );
  371. }
  372. /*
  373. ================================================================================
  374. Function : CC1101RecPacket( )
  375.     Receive a packet
  376. INPUT    : rxBuffer, A buffer store the received data
  377. OUTPUT   : 1:received count, 0:no data
  378. ================================================================================
  379. */
  380. u8 CC1101RecPacket( u8 *rxBuffer )
  381. {
  382.     u8 status[2];
  383.     u8 pktLen;
  384.     volatile u16 x , j = 0;

  385.     if ( CC1101GetRXCnt( ) != 0 )
  386.     {
  387.         pktLen = CC1101ReadReg(CC1101_RXFIFO);           // Read length byte
  388.         if( ( CC1101ReadReg( CC1101_PKTCTRL1 ) & ~0x03 ) != 0 )
  389.         {
  390.             x = CC1101ReadReg(CC1101_RXFIFO);
  391.         }
  392.         if( pktLen == 0 )           { return 0; }
  393. ……………………

  394. …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码

所有资料51hei提供下载:
cc1101驱动.rar (4.34 KB, 下载次数: 83)


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

使用道具 举报

沙发
ID:431714 发表于 2018-11-23 11:35 | 只看该作者
大哥,请问你这个程序调过低功耗吗?功耗多少?
回复

使用道具 举报

板凳
ID:161979 发表于 2018-12-5 16:52 | 只看该作者
有没有完整的主程序可以调用
回复

使用道具 举报

地板
ID:442655 发表于 2018-12-9 14:51 | 只看该作者
大哥,请问你这个程序调过低功耗吗?
回复

使用道具 举报

5#
ID:442655 发表于 2018-12-9 17:20 | 只看该作者
你好,有低功耗的配置吗?我的低功耗模式未调通!
回复

使用道具 举报

6#
ID:381175 发表于 2018-12-12 11:49 | 只看该作者
下了看看
回复

使用道具 举报

7#
ID:223999 发表于 2019-3-6 10:57 | 只看该作者
听说长时间接收会死机?
回复

使用道具 举报

8#
ID:577139 发表于 2019-8-30 08:15 | 只看该作者
老哥,带地址检测的源码有吗?想做个参考
回复

使用道具 举报

9#
ID:950179 发表于 2021-7-12 11:09 | 只看该作者
老哥这些作用是什么  PaTabel[] = { 0xc0, 0xC8, 0x84, 0x60, 0x68, 0x34, 0x1D, 0x0E};
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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