f2d1bd233c885f36c2fda358357169d3ac824692.svn-base 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. C51 COMPILER V9.53.0.0 NRF_24L01 08/21/2018 09:14:46 PAGE 1
  2. C51 COMPILER V9.53.0.0, COMPILATION OF MODULE NRF_24L01
  3. OBJECT MODULE PLACED IN .\Output\NRF_24L01.obj
  4. COMPILER INVOKED BY: D:\Keil_v5\C51\BIN\C51.EXE Code\User\Driver\NRF_24L01.C OPTIMIZE(8,SPEED) BROWSE INCDIR(Code/Includ
  5. -e;Code/User;Code/User/Common;Code/User/Driver) DEFINE(FOSC_160000) DEBUG OBJECTEXTEND PRINT(.\Output\LST\NRF_24L01.lst)
  6. -TABS(2) OBJECT(.\Output\NRF_24L01.obj)
  7. line level source
  8. 1 /******************************************************************************/
  9. 2 /* -- 深圳安信可科技有限公司 -- */
  10. 3 /*
  11. 4 // 文件名:
  12. 5 // 说明:
  13. 6 // 编写人员:
  14. 7 // 编写日期:
  15. 8 // 程序维护:
  16. 9 // 维护记录:
  17. 10 //
  18. 11 //
  19. 12 */
  20. 13 // 免责声明:该程序仅用于学习与交流
  21. 14 // (c) Anxinke Corporation. All rights reserved.
  22. 15 /******************************************************************************/
  23. 16 //#include "typedef.h"
  24. 17 #include <intrins.h>
  25. 18 #include <string.h>
  26. 19 #include "NRF_24L01.h"
  27. *** ERROR C129 IN LINE 88 OF Code\User\Driver\NRF_24L01.h: missing ';' before 'SPI_RW'
  28. 20 //#include "UART.H"
  29. 21 #include "delay.h"
  30. 22
  31. 23 #define uchar unsigned char
  32. 24
  33. 25
  34. 26 uchar const TX_ADDRESS[TX_ADR_WIDTH] = {0x34, 0x43, 0x10, 0x10, 0x01}; // Define a static TX address
  35. 27
  36. 28 idata uchar rx_buf[TX_PLOAD_WIDTH];
  37. 29
  38. 30 idata uchar tx_buf[TX_PLOAD_WIDTH];
  39. 31
  40. 32 uchar flag;
  41. 33
  42. 34
  43. 35
  44. 36 /**************************************************/
  45. 37
  46. 38
  47. 39
  48. 40 //sbit LED1= P3^7;
  49. 41 /**************************************************/
  50. 42 unsigned char bdata sta;
  51. 43
  52. 44 sbit RX_DR = sta ^ 6;
  53. 45
  54. 46 sbit TX_DS = sta ^ 5;
  55. 47
  56. 48 sbit MAX_RT = sta ^ 4;
  57. 49 /**************************************************/
  58. 50
  59. 51 /**************************************************
  60. 52 Function: init_io();
  61. C51 COMPILER V9.53.0.0 NRF_24L01 08/21/2018 09:14:46 PAGE 2
  62. 53 Description:
  63. 54 flash led one time,chip enable(ready to TX or RX Mode),
  64. 55 Spi disable,Spi clock line init high
  65. 56 /**************************************************/
  66. 57 #define KEY 0xaa
  67. 58 void init_nrf24l01_io ( void )
  68. 59 {
  69. 60 CE = 0; // chip enable
  70. 61 CSN = 1; // Spi disable
  71. 62 SCK = 0; // Spi clock line init high
  72. 63 }
  73. 64 /**************************************************
  74. 65 Function: SPI_RW();
  75. 66
  76. 67 Description:
  77. 68 Writes one byte to nRF24L01, and return the byte read
  78. 69 from nRF24L01 during write, according to SPI protocol
  79. 70 /**************************************************/
  80. 71 bdata unsigned char st = 0;
  81. 72 sbit st_1 = st ^ 0;
  82. 73 sbit st_2 = st ^ 1;
  83. 74 sbit st_3 = st ^ 2;
  84. 75 sbit st_4 = st ^ 3;
  85. 76 sbit st_5 = st ^ 4;
  86. 77 sbit st_6 = st ^ 5;
  87. 78 sbit st_7 = st ^ 6;
  88. 79 sbit st_8 = st ^ 7;
  89. 80 bdata unsigned char st1 = 0;
  90. 81 sbit st_11 = st1 ^ 0;
  91. 82 sbit st_12 = st1 ^ 1;
  92. 83 sbit st_13 = st1 ^ 2;
  93. 84 sbit st_14 = st1 ^ 3;
  94. 85 sbit st_15 = st1 ^ 4;
  95. 86 sbit st_16 = st1 ^ 5;
  96. 87 sbit st_17 = st1 ^ 6;
  97. 88 sbit st_18 = st1 ^ 7;
  98. 89 /*
  99. 90 uchar SPI_RW(uchar byte)
  100. 91 {
  101. 92 uchar bit_ctr;
  102. 93 for(bit_ctr=0;bit_ctr<8;bit_ctr++) // output 8-bit
  103. 94 {
  104. 95 MOSI = (byte & 0x80); // output 'byte', MSB to MOSI
  105. 96 byte = (byte << 1); // shift next bit into MSB..
  106. 97 SCK = 1; // Set SCK high..
  107. 98 MISO=1;
  108. 99 byte |= MISO; // capture current MISO bit
  109. 100 SCK = 0; // ..then set SCK low again
  110. 101 }
  111. 102 return(byte); // return read byte
  112. 103 }
  113. 104 */
  114. 105
  115. 106 uchar SPI_RW ( uchar byte )
  116. 107 {
  117. 108 //uchar bit_ctr;
  118. 109
  119. 110 st = byte;
  120. 111
  121. 112 MOSI = st_8;
  122. 113 SCK = 1;
  123. 114 st_18 = MISO;
  124. C51 COMPILER V9.53.0.0 NRF_24L01 08/21/2018 09:14:46 PAGE 3
  125. 115 SCK = 0;
  126. 116
  127. 117 MOSI = st_7;
  128. 118 SCK = 1;
  129. 119 st_17 = MISO;
  130. 120 SCK = 0;
  131. 121
  132. 122 MOSI = st_6;
  133. 123 SCK = 1;
  134. 124 st_16 = MISO;
  135. 125 SCK = 0;
  136. 126
  137. 127 MOSI = st_5;
  138. 128 SCK = 1;
  139. 129 st_15 = MISO;
  140. 130 SCK = 0;
  141. 131
  142. 132 MOSI = st_4;
  143. 133 SCK = 1;
  144. 134 st_14 = MISO;
  145. 135 SCK = 0;
  146. 136
  147. 137 MOSI = st_3;
  148. 138 SCK = 1;
  149. 139 st_13 = MISO;
  150. 140 SCK = 0;
  151. 141
  152. 142 MOSI = st_2;
  153. 143 SCK = 1;
  154. 144 st_12 = MISO;
  155. 145 SCK = 0;
  156. 146
  157. 147 MOSI = st_1;
  158. 148 SCK = 1;
  159. 149 st_11 = MISO;
  160. 150 SCK = 0;
  161. 151 return ( st1 ); // return read byte
  162. 152 }
  163. 153 /**************************************************/
  164. 154
  165. 155 /**************************************************
  166. 156 Function: SPI_RW_Reg();
  167. 157
  168. 158 Description:
  169. 159 Writes value 'value' to register 'reg'
  170. 160 /**************************************************/
  171. 161 uchar SPI_RW_Reg ( BYTE reg, BYTE value )
  172. 162 {
  173. 163 uchar status;
  174. 164 CSN = 0; // CSN low, init SPI transaction
  175. 165 status = SPI_RW ( reg ); // select register
  176. 166 SPI_RW ( value ); // ..and write value to it..
  177. 167 CSN = 1; // CSN high again
  178. 168 return ( status ); // return nRF24L01 status byte
  179. 169 }
  180. 170 /**************************************************/
  181. 171
  182. 172 /**************************************************
  183. 173 Function: SPI_Read();
  184. 174
  185. 175 Description:
  186. 176 Read one byte from nRF24L01 register, 'reg'
  187. C51 COMPILER V9.53.0.0 NRF_24L01 08/21/2018 09:14:46 PAGE 4
  188. 177 /**************************************************/
  189. 178 BYTE SPI_Read ( BYTE reg )
  190. 179 {
  191. 180 BYTE reg_val;
  192. 181
  193. 182 CSN = 0; // CSN low, initialize SPI communication...
  194. 183 SPI_RW ( reg ); // Select register to read from..
  195. 184 reg_val = SPI_RW ( 0 ); // ..then read registervalue
  196. 185 CSN = 1; // CSN high, terminate SPI communication
  197. 186
  198. 187 return ( reg_val ); // return register value
  199. 188 }
  200. 189 /**************************************************/
  201. 190
  202. 191 /**************************************************
  203. 192 Function: SPI_Read_Buf();
  204. 193
  205. 194 Description:
  206. 195 Reads 'bytes' #of bytes from register 'reg'
  207. 196 Typically used to read RX payload, Rx/Tx address
  208. 197 /**************************************************/
  209. 198 uchar SPI_Read_Buf ( BYTE reg, BYTE *pBuf, BYTE bytes )
  210. 199 {
  211. 200 uchar status, byte_ctr;
  212. 201
  213. 202 CSN = 0; // Set CSN low, init SPI tranaction
  214. 203 status = SPI_RW ( reg ); // Select register to write to and read status byte
  215. 204
  216. 205 for ( byte_ctr = 0; byte_ctr < bytes; byte_ctr++ )
  217. 206 pBuf[byte_ctr] = SPI_RW ( 0 ); // Perform SPI_RW to read byte from nRF24L01
  218. 207
  219. 208 CSN = 1; // Set CSN high again
  220. 209
  221. 210 return ( status ); // return nRF24L01 status byte
  222. 211 }
  223. 212 /**************************************************/
  224. 213
  225. 214 /**************************************************
  226. 215 Function: SPI_Write_Buf();
  227. 216
  228. 217 Description:
  229. 218 Writes contents of buffer '*pBuf' to nRF24L01
  230. 219 Typically used to write TX payload, Rx/Tx address
  231. 220 /**************************************************/
  232. 221 uchar SPI_Write_Buf ( BYTE reg, BYTE *pBuf, BYTE bytes )
  233. 222 {
  234. 223 uchar status, byte_ctr;
  235. 224
  236. 225 CSN = 0; // Set CSN low, init SPI tranaction
  237. 226 status = SPI_RW ( reg ); // Select register to write to and read status byte
  238. 227
  239. 228 for ( byte_ctr = 0; byte_ctr < bytes; byte_ctr++ ) // then write all byte in buffer(*pBuf)
  240. 229 SPI_RW ( *pBuf++ );
  241. 230
  242. 231 CSN = 1; // Set CSN high again
  243. 232 return ( status ); // return nRF24L01 status byte
  244. 233 }
  245. 234 /**************************************************/
  246. 235
  247. 236 /**************************************************
  248. 237 Function: RX_Mode();
  249. 238
  250. C51 COMPILER V9.53.0.0 NRF_24L01 08/21/2018 09:14:46 PAGE 5
  251. 239 Description:
  252. 240 This function initializes one nRF24L01 device to
  253. 241 RX Mode, set RX address, writes RX payload width,
  254. 242 select RF channel, datarate & LNA HCURR.
  255. 243 After init, CE is toggled high, which means that
  256. 244 this device is now ready to receive a datapacket.
  257. 245 /**************************************************/
  258. 246 void power_off()
  259. 247 {
  260. 248 CE = 0;
  261. 249 SPI_RW_Reg ( WRITE_REG + CONFIG, 0x0D );
  262. 250 CE = 1;
  263. 251 Timer3_Delay10us ( 2 );
  264. 252 }
  265. 253 void ifnnrf_rx_mode ( void )
  266. 254 {
  267. 255 power_off();
  268. 256 CE = 0;
  269. 257 SPI_Write_Buf ( WRITE_REG + RX_ADDR_P0, TX_ADDRESS, TX_ADR_WIDTH ); // Use the same address on the RX
  270. -device as the TX device
  271. 258
  272. 259 SPI_RW_Reg ( WRITE_REG + EN_AA, 0x01 ); // Enable Auto.Ack:Pipe0
  273. 260 SPI_RW_Reg ( WRITE_REG + EN_RXADDR, 0x01 ); // Enable Pipe0
  274. 261 SPI_RW_Reg ( WRITE_REG + RF_CH, 40 ); // Select RF channel 40
  275. 262 SPI_RW_Reg ( WRITE_REG + RX_PW_P0, TX_PLOAD_WIDTH ); // Select same RX payload width as TX Payload wid
  276. -th
  277. 263 SPI_RW_Reg ( WRITE_REG + RF_SETUP, 0x27 ); // TX_PWR:0dBm, Datarate:2Mbps, LNA:HCURR
  278. 264 SPI_RW_Reg ( WRITE_REG + CONFIG, 0x0f ); // Set PWR_UP bit, enable CRC(2 bytes) & Prim:RX. RX_DR enab
  279. -led..
  280. 265
  281. 266 CE = 1; // Set CE pin high to enable RX device
  282. 267
  283. 268 // This device is now ready to receive one packet of 16 bytes payload from a TX device sending to add
  284. -ress
  285. 269 // '3443101001', with auto acknowledgment, retransmit count of 10, RF channel 40 and datarate = 2Mbps
  286. -.
  287. 270
  288. 271 }
  289. 272 /**************************************************/
  290. 273
  291. 274 /**************************************************
  292. 275 Function: TX_Mode();
  293. 276
  294. 277 Description:
  295. 278 This function initializes one nRF24L01 device to
  296. 279 TX mode, set TX address, set RX address for auto.ack,
  297. 280 fill TX payload, select RF channel, datarate & TX pwr.
  298. 281 PWR_UP is set, CRC(2 bytes) is enabled, & PRIM:TX.
  299. 282
  300. 283 ToDo: One high pulse(>10us) on CE will now send this
  301. 284 packet and expext an acknowledgment from the RX device.
  302. 285 /**************************************************/
  303. 286 void ifnnrf_tx_mode ( void )
  304. 287 {
  305. 288 power_off();
  306. 289 CE = 0;
  307. 290
  308. 291 SPI_Write_Buf ( WRITE_REG + TX_ADDR, TX_ADDRESS, TX_ADR_WIDTH ); // Writes TX_Address to nRF24L01
  309. 292 SPI_Write_Buf ( WRITE_REG + RX_ADDR_P0, TX_ADDRESS, TX_ADR_WIDTH ); // RX_Addr0 same as TX_Adr for Aut
  310. -o.Ack
  311. 293 SPI_Write_Buf ( WR_TX_PLOAD, tx_buf, TX_PLOAD_WIDTH ); // Writes data to TX payload
  312. 294
  313. C51 COMPILER V9.53.0.0 NRF_24L01 08/21/2018 09:14:46 PAGE 6
  314. 295 SPI_RW_Reg ( WRITE_REG + EN_AA, 0x01 ); // Enable Auto.Ack:Pipe0
  315. 296 SPI_RW_Reg ( WRITE_REG + EN_RXADDR, 0x01 ); // Enable Pipe0
  316. 297 SPI_RW_Reg ( WRITE_REG + SETUP_RETR, 0x1a ); // 500us + 86us, 10 retrans...
  317. 298 SPI_RW_Reg ( WRITE_REG + RF_CH, 40 ); // Select RF channel 40
  318. 299 SPI_RW_Reg ( WRITE_REG + RF_SETUP, 0x27 ); // TX_PWR:0dBm, Datarate:2Mbps, LNA:HCURR
  319. 300 SPI_RW_Reg ( WRITE_REG + CONFIG, 0x0e ); // Set PWR_UP bit, enable CRC(2 bytes) & Prim:TX. MAX_RT & T
  320. -X_DS enabled..
  321. 301 CE = 1;
  322. 302
  323. 303 }
  324. 304
  325. 305 void SPI_CLR_Reg ( BYTE R_T )
  326. 306 {
  327. 307 CSN = 0;
  328. 308
  329. 309 if ( R_T == 1 ) // CSN low, init SPI transaction
  330. 310 SPI_RW ( FLUSH_TX ); // ..and write value to it..
  331. 311 else
  332. 312 SPI_RW ( FLUSH_RX ); // ..and write value to it..
  333. 313
  334. 314 CSN = 1; // CSN high again
  335. 315 }
  336. 316
  337. 317 void ifnnrf_CLERN_ALL()
  338. 318 {
  339. 319 SPI_CLR_Reg ( 0 );
  340. 320 SPI_CLR_Reg ( 1 );
  341. 321 SPI_RW_Reg ( WRITE_REG + STATUS, 0xff );
  342. 322 IRQ = 1;
  343. 323 }
  344. 324
  345. 325
  346. 326
  347. 327 /*测试*/
  348. 328 bool nrf24l01_send ( BYTE *pBuf, BYTE bytes )
  349. 329 {
  350. 330 BYTE time = 100; //
  351. 331 IRQ = 1; //中断=1
  352. 332 SPI_RW_Reg ( WRITE_REG + STATUS, 0xff ); //清写寄存器
  353. 333
  354. 334 //发送
  355. 335 memcpy ( tx_buf, pBuf, bytes < TX_PLOAD_WIDTH ? bytes : TX_PLOAD_WIDTH );
  356. 336 ifnnrf_tx_mode();
  357. 337
  358. 338 //返回状态
  359. 339 while ( IRQ && time>0 ){time--;}
  360. 340
  361. 341 sta = SPI_Read ( STATUS ); //读取状态
  362. 342 SPI_RW_Reg ( WRITE_REG + STATUS, 0xff ); //清写寄存器
  363. 343
  364. 344 if ( ! ( sta & STA_MARK_TX ) ) //发送成功
  365. 345 {
  366. 346 ifnnrf_CLERN_ALL();
  367. 347 return 0;
  368. 348 }
  369. 349
  370. 350 //切换接收模式
  371. 351 ifnnrf_rx_mode();
  372. 352 IRQ = 1;
  373. 353 return 1;
  374. 354 }
  375. 355
  376. C51 COMPILER V9.53.0.0 NRF_24L01 08/21/2018 09:14:46 PAGE 7
  377. 356 BYTE nrf24l01_recv ( BYTE *pBuf )
  378. 357 {
  379. 358 BYTE time = 100; //
  380. 359 if ( IRQ == 0 ) //中断信号
  381. 360 {
  382. 361 sta = SPI_Read ( STATUS );
  383. 362 SPI_RW_Reg ( WRITE_REG + STATUS, 0xff ); //清写寄存器
  384. 363
  385. 364 if ( sta & STA_MARK_RX ) //接收成功
  386. 365 {
  387. 366 SPI_Read_Buf ( RD_RX_PLOAD, rx_buf, TX_PLOAD_WIDTH );
  388. 367 //处理rx_buf
  389. 368
  390. 369 //清理rx_buf
  391. 370 memset ( rx_buf, 0, TX_PLOAD_WIDTH );
  392. 371 }
  393. 372 else
  394. 373 {
  395. 374 ifnnrf_CLERN_ALL();
  396. 375 ifnnrf_rx_mode();
  397. 376 while ( IRQ == 0 && time>0 ){time--;}
  398. 377 }
  399. 378 }
  400. 379 }
  401. 380
  402. 381
  403. 382
  404. 383 void if_main ( void )
  405. 384 {
  406. 385 Timer3_Delay100ms ( 1 );
  407. 386 init_nrf24l01_io();
  408. 387 ifnnrf_rx_mode();
  409. 388
  410. 389 }
  411. 390
  412. 391
  413. 392
  414. 393
  415. C51 COMPILATION COMPLETE. 0 WARNING(S), 1 ERROR(S)