智能车制作

 找回密码
 注册

扫一扫,访问微社区

查看: 3081|回复: 3
打印 上一主题 下一主题

关于CC1101上单复位死循环的问题。

[复制链接]

4

主题

52

帖子

0

精华

金牌会员

Rank: 6Rank: 6

积分
2211
QQ
威望
1116
贡献
679
兑换币
727
注册时间
2013-5-1
在线时间
208 小时
跳转到指定楼层
1#
发表于 2013-8-18 22:35:03 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
5贡献
      最近想做遥控智能车,手边正好友CC1101的模块就用了,搞了几天了,上电复位到这while (MISO);就死循环了,真心求教,自学不易啊。。。。。
下边是代码:
  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^3;
  12. sbit GDO2 =P1^1;
  13. sbit MISO =P1^6;
  14. sbit MOSI =P1^5;
  15. sbit SCK =P1^7;
  16. sbit CSN =P1^2;
  17. //*****************************************************************************************
  18. sbit KeyStop =P2^2;
  19. sbit KeyGo =P2^3;
  20. sbit KeyBack =P2^4;
  21. sbit KeyLeft =P2^5;
  22. sbit KeyRight =P2^6;
  23. //*****************************************************************************************
  24. sbit st=P2^7;
  25. sbit LED=P2^1;
  26. //*****************************************************************************************
  27. INT8U PaTabel[8] = {0x60 ,0x60 ,0x60 ,0x60 ,0x60 ,0x60 ,0x60 ,0x60};
  28. //*****************************************************************************************
  29. void SpiInit(void);
  30. void CpuInit(void);
  31. void RESET_CC1100(void);
  32. void POWER_UP_RESET_CC1100(void);
  33. void halSpiWriteReg(INT8U addr, INT8U value);
  34. void halSpiWriteBurstReg(INT8U addr, INT8U *buffer, INT8U count);
  35. void halSpiStrobe(INT8U strobe);
  36. INT8U halSpiReadReg(INT8U addr);
  37. void halSpiReadBurstReg(INT8U addr, INT8U *buffer, INT8U count);
  38. INT8U halSpiReadStatus(INT8U addr);
  39. void halRfWriteRfSettings(void);
  40. void halRfSendPacket(INT8U *txBuffer, INT8U size);
  41. INT8U halRfReceivePacket(INT8U *rxBuffer, INT8U *length);
  42. //*****************************************************************************************
  43. // CC1100 STROBE, CONTROL AND STATUS REGSITER
  44. #define CCxxx0_IOCFG2 0x00 // GDO2 output pin configuration
  45. #define CCxxx0_IOCFG1 0x01 // GDO1 output pin configuration
  46. #define CCxxx0_IOCFG0 0x02 // GDO0 output pin configuration
  47. #define CCxxx0_FIFOTHR 0x03 // RX FIFO and TX FIFO thresholds
  48. #define CCxxx0_SYNC1 0x04 // Sync word, high INT8U
  49. #define CCxxx0_SYNC0 0x05 // Sync word, low INT8U
  50. #define CCxxx0_PKTLEN 0x06 // Packet length
  51. #define CCxxx0_PKTCTRL1 0x07 // Packet automation control
  52. #define CCxxx0_PKTCTRL0 0x08 // Packet automation control
  53. #define CCxxx0_ADDR 0x09 // Device address
  54. #define CCxxx0_CHANNR 0x0A // Channel number
  55. #define CCxxx0_FSCTRL1 0x0B // Frequency synthesizer control
  56. #define CCxxx0_FSCTRL0 0x0C // Frequency synthesizer control
  57. #define CCxxx0_FREQ2 0x0D // Frequency control word, high INT8U
  58. #define CCxxx0_FREQ1 0x0E // Frequency control word, middle INT8U
  59. #define CCxxx0_FREQ0 0x0F // Frequency control word, low INT8U
  60. #define CCxxx0_MDMCFG4 0x10 // Modem configuration
  61. #define CCxxx0_MDMCFG3 0x11 // Modem configuration
  62. #define CCxxx0_MDMCFG2 0x12 // Modem configuration
  63. #define CCxxx0_MDMCFG1 0x13 // Modem configuration
  64. #define CCxxx0_MDMCFG0 0x14 // Modem configuration
  65. #define CCxxx0_DEVIATN 0x15 // Modem deviation setting
  66. #define CCxxx0_MCSM2 0x16 // Main Radio Control State Machine configuration
  67. #define CCxxx0_MCSM1 0x17 // Main Radio Control State Machine configuration
  68. #define CCxxx0_MCSM0 0x18 // Main Radio Control State Machine configuration
  69. #define CCxxx0_FOCCFG 0x19 // Frequency Offset Compensation configuration
  70. #define CCxxx0_BSCFG 0x1A // Bit Synchronization configuration
  71. #define CCxxx0_AGCCTRL2 0x1B // AGC control
  72. #define CCxxx0_AGCCTRL1 0x1C // AGC control
  73. #define CCxxx0_AGCCTRL0 0x1D // AGC control
  74. #define CCxxx0_WOREVT1 0x1E // High INT8U Event 0 timeout
  75. #define CCxxx0_WOREVT0 0x1F // Low INT8U Event 0 timeout
  76. #define CCxxx0_WORCTRL 0x20 // Wake On Radio control
  77. #define CCxxx0_FREND1 0x21 // Front end RX configuration
  78. #define CCxxx0_FREND0 0x22 // Front end TX configuration
  79. #define CCxxx0_FSCAL3 0x23 // Frequency synthesizer calibration
  80. #define CCxxx0_FSCAL2 0x24 // Frequency synthesizer calibration
  81. #define CCxxx0_FSCAL1 0x25 // Frequency synthesizer calibration
  82. #define CCxxx0_FSCAL0 0x26 // Frequency synthesizer calibration
  83. #define CCxxx0_RCCTRL1 0x27 // RC oscillator configuration
  84. #define CCxxx0_RCCTRL0 0x28 // RC oscillator configuration
  85. #define CCxxx0_FSTEST 0x29 // Frequency synthesizer calibration control
  86. #define CCxxx0_PTEST 0x2A // Production test
  87. #define CCxxx0_AGCTEST 0x2B // AGC test
  88. #define CCxxx0_TEST2 0x2C // Various test settings
  89. #define CCxxx0_TEST1 0x2D // Various test settings
  90. #define CCxxx0_TEST0 0x2E // Various test settings

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

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

  126. #define CCxxx0_PATABLE 0x3E
  127. #define CCxxx0_TXFIFO 0x3F
  128. #define CCxxx0_RXFIFO 0x3F

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

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

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

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

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


  219. void halWait(INT16U timeout) {
  220. do {
  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. _nop_();
  234. _nop_();
  235. _nop_();
  236. } while (--timeout);
  237. }


  238. void SpiInit(void)
  239. {
  240. CSN=0;
  241. SCK=0;
  242. CSN=1;
  243. }

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


  255. //*****************************************************************************************
  256. //函数名:SpisendByte(INT8U dat)
  257. //输入:发送的数据
  258. //输出:无
  259. //功能描述:SPI发送一个字节
  260. //*****************************************************************************************
  261. INT8U SpiTxRxByte(INT8U dat)
  262. {
  263. INT8U i,temp;
  264. temp = 0;

  265. SCK = 0;
  266. for(i=0; i<8; i++)
  267. {
  268. if(dat & 0x80)
  269. {
  270. MOSI = 1;
  271. }
  272. else MOSI = 0;
  273. dat <<= 1;

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

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

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

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

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

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

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





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


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


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

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

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

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

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

  470. // Wait for GDO0 to be set -> sync transmitted
  471. while (!GDO0);
  472. // Wait for GDO0 to be cleared -> end of packet
  473. while (GDO0);
  474. halSpiStrobe(CCxxx0_SFTX);
  475. }
  476. //*****************************************************************************************
  477. void setRxMode(void)
  478. {
  479. halSpiStrobe(CCxxx0_SRX); //进入接收状态
  480. }
  481. //*****************************************************************************************
  482. INT8U halRfReceivePacket(INT8U *rxBuffer, INT8U *length)
  483. {
  484. INT8U status[2];
  485. INT8U packetLength;
  486. INT8U i=(*length)*4; // 具体多少要根据datarate和length来决定

  487. halSpiStrobe(CCxxx0_SRX); //进入接收状态
  488. delay(2);
  489. while (GDO0)
  490. {
  491. delay(2);
  492. --i;
  493. if(i<1)
  494. return 0;
  495. }
  496. if ((halSpiReadStatus(CCxxx0_RXBYTES) & BYTES_IN_RXFIFO)) //如果接的字节数不为0
  497. {
  498. packetLength = halSpiReadReg(CCxxx0_RXFIFO);//读出第一个字节,此字节为该帧数据长度
  499. if (packetLength <= *length) //如果所要的有效数据长度小于等于接收到的数据包的长度
  500. {
  501. halSpiReadBurstReg(CCxxx0_RXFIFO, rxBuffer, packetLength); //读出所有接收到的数据
  502. *length = packetLength; //把接收数据长度的修改为当前数据的长
  503. // Read the 2 appended status bytes (status[0] = RSSI, status[1] = LQI)
  504. halSpiReadBurstReg(CCxxx0_RXFIFO, status, 2); //读出CRC校验位
  505. halSpiStrobe(CCxxx0_SFRX); //清洗接收缓冲区
  506. return (status[1] & CRC_OK); //如果校验成功返回接收成功
  507. }
  508. else
  509. {
  510. *length = packetLength;
  511. halSpiStrobe(CCxxx0_SFRX); //清洗接收缓冲区
  512. return 0;
  513. }
  514. }
  515. else
  516. return 0;
  517. }

  518. //----------------------------------------主函数---------------------------------------------------
  519. void main(void)
  520. {
  521. INT8U leng =0;
  522. INT8U tf =0;
  523. INT8U TxBuf[8]={0}; // 8字节, 如果需要更长的数据包,请正确设置
  524. INT8U RxBuf[8]={0};
  525. CpuInit();
  526. POWER_UP_RESET_CC1100();
  527. halRfWriteRfSettings();
  528. halSpiWriteBurstReg(CCxxx0_PATABLE, PaTabel, 8);
  529. halRfSendPacket(TxBuf,8); // Transmit Tx buffer data
  530. delay(6000);
  531. while(1)
  532. {
  533. LED=0;
  534. if(KeyStop ==0 )
  535. {
  536. TxBuf[0] = 1 ;
  537. tf = 1 ;
  538. st=~st;
  539. }
  540. if(KeyGo ==0 )
  541. {
  542. TxBuf[1] = 1 ;
  543. tf = 1 ;
  544. st=~st;
  545. }
  546. if(KeyBack ==0 )
  547. {
  548. TxBuf[2] = 1 ;
  549. tf = 1 ;
  550. st=~st;
  551. }
  552. if(KeyLeft ==0)
  553. {
  554. TxBuf[3] = 1;
  555. tf=1;
  556. st=~st;
  557. }
  558. if(KeyRight ==0)
  559. {
  560. TxBuf[4] = 1;
  561. tf=1;
  562. st=~st;
  563. }

  564. if (tf==1)
  565. {
  566. halRfSendPacket(TxBuf,8); // Transmit Tx buffer data
  567. LED=1;
  568. TxBuf[0] = 0xff;
  569. TxBuf[1] = 0xff;
  570. TxBuf[2] = 0xff;
  571. TxBuf[3] = 0xff;
  572. TxBuf[4] = 0xff;
  573. tf=0;
  574. }
  575. delay(3000);
  576. }

  577. }


复制代码


最佳答案

查看完整内容

600 多行的代码,叹为观止。

21

主题

896

帖子

0

精华

常驻嘉宾

Rank: 8Rank: 8

积分
4068
威望
2252
贡献
1296
兑换币
1483
注册时间
2012-7-30
在线时间
260 小时
毕业学校
克莱德大学
2#
发表于 2013-8-18 22:35:04 | 只看该作者
600 多行的代码,叹为观止。
回复

使用道具 举报

4

主题

52

帖子

0

精华

金牌会员

Rank: 6Rank: 6

积分
2211
QQ
威望
1116
贡献
679
兑换币
727
注册时间
2013-5-1
在线时间
208 小时
3#
 楼主| 发表于 2013-8-20 10:44:28 | 只看该作者
rubick 发表于 2013-8-18 23:27
600 多行的代码,叹为观止。

没人管我哎
回复

使用道具 举报

0

主题

31

帖子

0

精华

金牌会员

Rank: 6Rank: 6

积分
1265
QQ
威望
489
贡献
322
兑换币
330
注册时间
2015-3-7
在线时间
227 小时
毕业学校
长安大学
4#
发表于 2015-4-24 11:19:35 | 只看该作者
你好,我也想做一个,想问一下能不能用手机软件射频发送数据给1101,?
回复

使用道具 举报

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

本版积分规则

关于我们|联系我们|小黑屋|智能车制作 ( 黑ICP备2022002344号

GMT+8, 2024-11-5 21:43 , Processed in 0.148246 second(s), 26 queries , Gzip On.

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

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