6b5c428afe1796bcd55437ed0479e093fae45e4e.svn-base 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /******************************************************************************/
  2. /* -- 居加居-- */
  3. /*
  4. // 文件名:
  5. // 说明:
  6. // 编写人员:kinve
  7. // 编写日期:2018-8-21
  8. // 程序维护:
  9. // 维护记录:
  10. //
  11. //
  12. */
  13. // 免责声明:
  14. // (c) Anxinke Corporation. All rights reserved.
  15. /******************************************************************************/
  16. #include "nRF24L01.h"
  17. uchar const TX_ADDRESS[TX_ADR_WIDTH] = {0x34, 0x43, 0x10, 0x10, 0x01}; // Define a static TX address
  18. uchar const RX_ADDRESS[RX_ADR_WIDTH] = {0x34, 0x43, 0x10, 0x10, 0x01}; // Define a static RX address
  19. union
  20. {
  21. unsigned char byte;
  22. struct
  23. {
  24. unsigned char BIT0 : 1;
  25. unsigned char BIT1 : 1;
  26. unsigned char BIT2 : 1;
  27. unsigned char BIT3 : 1;
  28. unsigned char BIT4 : 1;
  29. unsigned char BIT5 : 1;
  30. unsigned char BIT6 : 1;
  31. unsigned char BIT7 : 1;
  32. } bit_t;
  33. } sta;
  34. #define RX_DR sta.bit_t.BIT6
  35. #define TX_DS sta.bit_t.BIT5
  36. #define MAX_RT sta.bit_t.BIT4
  37. #if 0
  38. static uchar bdata sta;
  39. sbit RX_DR = sta ^ 6;
  40. sbit TX_DS = sta ^ 5;
  41. sbit MAX_RT = sta ^ 4;
  42. #endif
  43. void inerDelay_us ( unsigned char n )
  44. {
  45. for ( ; n > 0; n-- )
  46. {
  47. #ifdef N76E003_IAR
  48. asm ( "nop" );
  49. #else
  50. nop;
  51. #endif
  52. }
  53. }
  54. void nRF24L01_init ( void )
  55. {
  56. inerDelay_us ( 100 );
  57. NRF_CE = 0; // chip enable
  58. NRF_CSN = 1; // Spi disable
  59. NRF_SCK = 0; // Spi clock line init high
  60. }
  61. /**************************************************
  62. Function: SPI_RW();
  63. Description:
  64. Writes one byte to nRF24L01, and return the byte read
  65. from nRF24L01 during write, according to SPI protocol
  66. **************************************************/
  67. uchar SPI_RW ( uchar byte )
  68. {
  69. uchar bit_ctr;
  70. for ( bit_ctr = 0; bit_ctr < 8; bit_ctr++ ) // output 8-bit
  71. {
  72. NRF_MOSI = ( byte & 0x80 ); // output 'byte', MSB to NRF_MOSI
  73. byte = ( byte << 1 ); // shift next bit into MSB..
  74. NRF_SCK = 1; // Set NRF_SCK high..
  75. byte |= NRF_MISO; // capture current NRF_MISO bit
  76. NRF_SCK = 0; // ..then set NRF_SCK low again
  77. }
  78. return ( byte ); // return read byte
  79. }
  80. /**************************************************/
  81. /**************************************************
  82. Function: SPI_RW_Reg();
  83. Description:
  84. Writes value 'value' to register 'reg'
  85. **************************************************/
  86. uchar SPI_RW_Reg ( uchar reg, uchar value )
  87. {
  88. uchar status;
  89. NRF_CSN = 0; // NRF_CSN low, init SPI transaction
  90. status = SPI_RW ( reg ); // select register
  91. SPI_RW ( value ); // ..and write value to it..
  92. NRF_CSN = 1; // NRF_CSN high again
  93. return ( status ); // return nRF24L01 status byte
  94. }
  95. /**************************************************/
  96. /**************************************************
  97. Function: SPI_Read();
  98. Description:
  99. Read one byte from nRF24L01 register, 'reg'
  100. **************************************************/
  101. uchar SPI_Read ( uchar reg )
  102. {
  103. uchar reg_val;
  104. NRF_CSN = 0; // NRF_CSN low, initialize SPI communication...
  105. SPI_RW ( reg ); // Select register to read from..
  106. reg_val = SPI_RW ( 0 ); // ..then read registervalue
  107. NRF_CSN = 1; // NRF_CSN high, terminate SPI communication
  108. return ( reg_val ); // return register value
  109. }
  110. /**************************************************/
  111. /**************************************************
  112. Function: SPI_Read_Buf();
  113. Description:
  114. **************************************************/
  115. uchar SPI_Read_Buf ( uchar reg, uchar *pBuf, uchar bytes )
  116. {
  117. uchar status, byte_ctr;
  118. NRF_CSN = 0; // Set NRF_CSN low, init SPI tranaction
  119. status = SPI_RW ( reg ); // Select register to write to and read status byte
  120. for ( byte_ctr = 0; byte_ctr < bytes; byte_ctr++ )
  121. pBuf[byte_ctr] = SPI_RW ( 0 ); //
  122. NRF_CSN = 1;
  123. return ( status ); // return nRF24L01 status byte
  124. }
  125. /**************************************************/
  126. /**************************************************
  127. Function: SPI_Write_Buf();
  128. Description:
  129. Writes contents of buffer '*pBuf' to nRF24L01
  130. Typically used to write TX payload, Rx/Tx address
  131. **************************************************/
  132. uchar SPI_Write_Buf ( uchar reg, uchar *pBuf, uchar bytes )
  133. {
  134. uchar status, byte_ctr;
  135. NRF_CSN = 0;
  136. status = SPI_RW ( reg );
  137. for ( byte_ctr = 0; byte_ctr < bytes; byte_ctr++ ) //
  138. SPI_RW ( *pBuf++ );
  139. NRF_CSN = 1; // Set NRF_CSN high again
  140. return ( status ); //
  141. }
  142. /**************************************************/
  143. /**************************************************
  144. Function: RX_Mode();
  145. Description:
  146. **************************************************/
  147. void SetRX_Mode ( void )
  148. {
  149. NRF_CE = 0;
  150. SPI_Write_Buf ( WRITE_REG + RX_ADDR_P0, ( uchar * ) RX_ADDRESS, RX_ADR_WIDTH ); // Use the same address on the RX deviNRF_CE as the TX deviNRF_CE
  151. SPI_RW_Reg ( WRITE_REG + EN_AA, 0x01 ); //
  152. SPI_RW_Reg ( WRITE_REG + EN_RXADDR, 0x01 ); //
  153. SPI_RW_Reg ( WRITE_REG + RF_CH, 0 ); //
  154. SPI_RW_Reg ( WRITE_REG + RX_PW_P0, RX_PLOAD_WIDTH );
  155. SPI_RW_Reg ( WRITE_REG + RF_SETUP, 0x07 );
  156. SPI_RW_Reg ( WRITE_REG + CONFIG, 0x0f );
  157. NRF_CE = 1;
  158. inerDelay_us ( 130 );
  159. }
  160. /**************************************************/
  161. unsigned char nRF24L01_RxPacket ( unsigned char* rx_buf )
  162. {
  163. unsigned char revale = 0;
  164. //SetRX_Mode();
  165. sta.byte = SPI_Read ( STATUS ); // read register STATUS's value
  166. if ( RX_DR ) // if reNRF_CEive data ready (RX_DR) interrupt
  167. {
  168. NRF_CE = 0;
  169. SPI_Read_Buf ( RD_RX_PLOAD, rx_buf, RX_PLOAD_WIDTH ); // read reNRF_CEive payload from RX_FIFO buffer
  170. revale = 1; //we have reNRF_CEive data
  171. }
  172. SPI_RW_Reg ( WRITE_REG + STATUS, sta.byte ); // clear RX_DR or TX_DS or MAX_RT interrupt flag
  173. return revale;
  174. }
  175. /**************************************************
  176. Function: nRF24L01_TxPacket();
  177. Description:
  178. This function initializes one nRF24L01 deviNRF_CE to
  179. TX mode, set TX address, set RX address for auto.ack,
  180. fill TX payload, select RF channel, datarate & TX pwr.
  181. PWR_UP is set, CRC(2 bytes) is enabled, & PRIM:TX.
  182. ToDo: One high pulse(>10us) on NRF_CE will now send this
  183. packet and expext an acknowledgment from the RX deviNRF_CE.
  184. **************************************************/
  185. void nRF24L01_TxPacket ( unsigned char * tx_buf )
  186. {
  187. NRF_CE = 0;
  188. SPI_Write_Buf ( WRITE_REG + TX_ADDR, ( uchar * ) TX_ADDRESS, TX_ADR_WIDTH ); //
  189. SPI_Write_Buf ( WRITE_REG + RX_ADDR_P0, ( uchar * ) TX_ADDRESS, TX_ADR_WIDTH );
  190. SPI_Write_Buf ( WR_TX_PLOAD, tx_buf, TX_PLOAD_WIDTH );
  191. SPI_RW_Reg ( WRITE_REG + EN_AA, 0x01 ); //
  192. SPI_RW_Reg ( WRITE_REG + EN_RXADDR, 0x01 ); //
  193. SPI_RW_Reg ( WRITE_REG + SETUP_RETR, 0x1a ); //
  194. SPI_RW_Reg ( WRITE_REG + RF_CH, 0 ); //
  195. SPI_RW_Reg ( WRITE_REG + RF_SETUP, 0x07 ); //
  196. SPI_RW_Reg ( WRITE_REG + CONFIG, 0x0e ); //
  197. NRF_CE = 1;
  198. inerDelay_us ( 10 );
  199. //sta=SPI_Read(STATUS); // read register STATUS's value
  200. //SPI_RW_Reg(WRITE_REG+STATUS,SPI_Read(READ_REG+STATUS)); // clear interrupt flag(TX_DS)
  201. }
  202. /**************************************************/
  203. void nrf_main ( void )
  204. {
  205. unsigned char TxBuf[32] = {0}; //
  206. unsigned char RxBuf[32] = {0};
  207. nRF24L01_init() ;
  208. nRF24L01_TxPacket ( TxBuf );
  209. if ( nRF24L01_RxPacket ( RxBuf ) )
  210. {
  211. }
  212. }