找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2791|回复: 1
收起左侧

nRF24L01p+AVR单片机ATmage88射频收发程序

[复制链接]
ID:381818 发表于 2019-4-23 10:25 | 显示全部楼层 |阅读模式
avr单片机,基于ATmega88+nRF24L01p+oLED
可移植性极高
完整的接收和发送程序
0.png

单片机源程序如下:
  1. #define _nRF24L01_C_
  2. #include "nRF24L01.h"

  3. INT8U CE_Status = 0;
  4. /*
  5. ================================================================================
  6. Function : L01_GetCEStatus( )
  7. Description : Get the status of the CE PIN
  8. Input : NONE
  9. Output: 1:CE=1, 0:CE=0
  10. ================================================================================
  11. */
  12. INT8U L01_GetCEStatus( void )
  13. {
  14.         return CE_Status;
  15. }
  16. /*
  17. ================================================================================
  18. Function : L01_SetCE( )
  19. Description : Set the CE PIN as 1 or 0
  20. Input : -status, 1: CE=1, 0: CE=0
  21. Output: None
  22. ================================================================================
  23. */
  24. void L01_SetCE( INT8U status )
  25. {
  26.         CE_Status = status;
  27.         if( status == 0 )  { L01_CE_LOW( ); }
  28.         else               { L01_CE_HIGH( ); }
  29. }
  30. /*
  31. ================================================================================
  32. Function : L01_ReadSingleReg( )
  33. Description : Read a single register of nRF24L01
  34. Input : -Addr, The address of the register
  35. Output: The value read from the register
  36. ================================================================================
  37. */
  38. INT8U L01_ReadSingleReg( INT8U Addr )
  39. {
  40.     INT8U btmp;
  41.     L01_CSN_LOW( );//PB2输出低电平
  42.     SPI_ExchangeByte( R_REGISTER | Addr );
  43.     btmp = SPI_ExchangeByte( 0xFF );
  44.     L01_CSN_HIGH( );//PB2输出高电平
  45.     return btmp;
  46. }
  47. /*
  48. ================================================================================
  49. Function : L01_ReadMultiReg( )
  50. Description : Read several registers of nRF24L01
  51. Input : -StartAddr, The start address of the registers
  52.         -nBytes, How many registers do you want to read
  53.         -pBuff, The buffer to save the values
  54. Output: None
  55. ================================================================================
  56. */
  57. /*void L01_ReadMultiReg( INT8U StartAddr, INT8U nBytes, INT8U *pBuff )
  58. {
  59.     INT8U btmp;
  60.     L01_CSN_LOW( );
  61.     SPI_ExchangeByte( R_REGISTER | StartAddr );
  62.     for( btmp = 0; btmp < nBytes; btmp ++ )
  63.     {
  64.         *( pBuff + btmp ) = SPI_ExchangeByte( 0xFF );
  65.     }
  66.     L01_CSN_HIGH( );
  67. }

  68. ================================================================================
  69. Function : L01_WriteSingleReg( )
  70. Description : Write a single byte to a register
  71. Input : -Addr, The address of the register
  72.         -Value, The value to be written
  73. Output: None
  74. ================================================================================
  75. */
  76. void L01_WriteSingleReg( INT8U Addr, INT8U Value )
  77. {
  78.         INT8U tmp = L01_GetCEStatus( );
  79.         L01_SetCE( 0 );
  80.     L01_CSN_LOW( );
  81.     SPI_ExchangeByte( W_REGISTER | Addr );
  82.     SPI_ExchangeByte( Value );
  83.     L01_CSN_HIGH( );
  84.         L01_SetCE( tmp );
  85. }
  86. /*
  87. ================================================================================
  88. Function : L01_WriteMultiReg( )
  89. Description : Read several registers of nRF24L01
  90. Input : -StartAddr, The start address of the registers
  91.         -pBuff, The buffer store the values
  92.         -Length, How many registers do you want to write
  93. Output: None
  94. ================================================================================
  95. */
  96. void L01_WriteMultiReg( INT8U StartAddr, INT8U *pBuff, INT8U Length )
  97. {
  98.     INT8U i;
  99.         INT8U tmp = L01_GetCEStatus( );
  100.         L01_SetCE( 0 );
  101.     L01_CSN_LOW( );
  102.     SPI_ExchangeByte( W_REGISTER | StartAddr );
  103.     for( i = 0; i < Length; i ++ )
  104.     {
  105.         SPI_ExchangeByte( *( pBuff + i ) );
  106.     }
  107.     L01_CSN_HIGH( );
  108.         L01_SetCE( tmp );
  109. }
  110. /*
  111. ================================================================================
  112. Function : L01_FlushTX( )
  113. Description : Flush the TX buffer
  114. Input : None
  115. Output: None
  116. ================================================================================
  117. */
  118. void L01_FlushTX( void )
  119. {
  120.     L01_CSN_LOW( );
  121.     SPI_ExchangeByte( FLUSH_TX );
  122.     L01_CSN_HIGH( );
  123. }
  124. /*
  125. ================================================================================
  126. Function : L01_FlushRX( )
  127. Description : Flush the RX buffer
  128. Input : None
  129. Output: None
  130. ================================================================================
  131. */
  132. void L01_FlushRX( void )
  133. {
  134.     L01_CSN_LOW( );
  135.     SPI_ExchangeByte( FLUSH_RX );
  136.     L01_CSN_HIGH( );
  137. }
  138. /*
  139. ================================================================================
  140. Function : L01_ReuseTXPayload( )
  141. Description : Reuse the last transmitted payload
  142. Input : None
  143. Output: None
  144. ================================================================================
  145. */
  146. void L01_ReuseTXPayload( void )
  147. {
  148.     L01_CSN_LOW( );
  149.     SPI_ExchangeByte( REUSE_TX_PL );
  150.     L01_CSN_HIGH( );
  151. }
  152. /*
  153. ================================================================================
  154. Function : L01_Nop( )
  155. Description : nop operation of nRF24L01
  156. Input : None
  157. Output: None
  158. ================================================================================
  159. */
  160. void L01_Nop( void )
  161. {
  162.     L01_CSN_LOW( );
  163.     SPI_ExchangeByte( L01_NOP );
  164.     L01_CSN_HIGH( );
  165. }
  166. /*
  167. ================================================================================
  168. Function : L01_ReadStatusReg( )
  169. Description : Read statu register of nRF24L01
  170. Input : None
  171. Output: Statu register of nRF24L01
  172. ================================================================================
  173. */
  174. INT8U L01_ReadStatusReg( void )
  175. {
  176.     INT8U Status;
  177.     L01_CSN_LOW( );
  178.     Status = SPI_ExchangeByte( R_REGISTER + L01REG_STATUS );
  179.     L01_CSN_HIGH( );
  180.     return Status;
  181. }
  182. /*
  183. ================================================================================
  184. Function : L01_ClearIRQ( )
  185. Description : Clear IRQ cuased by nRF24L01
  186. Input : None
  187. Output: None
  188. ================================================================================
  189. */
  190. void L01_ClearIRQ( INT8U IRQ_Source )
  191. {
  192.     INT8U btmp = 0;

  193.     IRQ_Source &= ( 1<<RX_DR ) | ( 1<<TX_DS ) | ( 1<<MAX_RT );
  194.     btmp = L01_ReadStatusReg( );
  195.     L01_CSN_LOW( );
  196.         L01_WriteSingleReg( L01REG_STATUS, IRQ_Source | btmp );
  197.     L01_CSN_HIGH( );
  198.     L01_ReadStatusReg( );
  199. }
  200. /*
  201. ================================================================================
  202. Function : L01_ReadIRQSource( )
  203. Description : Read the IRQ source of nRF24L01+
  204. Input : None
  205. Output: IRQ source mask code
  206. ================================================================================
  207. */
  208. INT8U L01_ReadIRQSource( void )
  209. {
  210.     return ( L01_ReadStatusReg( ) & ( ( 1<<RX_DR ) | ( 1<<TX_DS ) | ( 1<<MAX_RT ) ) );
  211. }
  212. /*
  213. ================================================================================
  214. Function : L01_ReadTopFIFOWidth( )
  215. Description : Read the payload width of the top buffer of FIFO
  216. Input : None
  217. Output: The width of the pipe buffer
  218. ================================================================================
  219. */
  220. INT8U L01_ReadTopFIFOWidth( void )
  221. {
  222.     INT8U btmp;
  223.     L01_CSN_LOW( );
  224.     SPI_ExchangeByte( R_RX_PL_WID );
  225.     btmp = SPI_ExchangeByte( 0xFF );
  226.     L01_CSN_HIGH( );
  227.     return btmp;
  228. }
  229. /*
  230. ================================================================================
  231. Function : L01_ReadRXPayload( )
  232. Description : Read the RX payload from internal buffer
  233. Input : -pBuff, buffer to store the data
  234. Output: The length of data read
  235. ================================================================================
  236. */
  237. INT8U L01_ReadRXPayload( INT8U *pBuff )
  238. {
  239.     INT8U width, PipeNum;
  240.     PipeNum = ( L01_ReadSingleReg( L01REG_STATUS ) >> 1 ) & 0x07;
  241.     width = L01_ReadTopFIFOWidth( );

  242.     L01_CSN_LOW( );
  243.     SPI_ExchangeByte( R_RX_PAYLOAD );
  244.     for( PipeNum = 0; PipeNum < width; PipeNum ++ )
  245.     {
  246.         *( pBuff + PipeNum ) = SPI_ExchangeByte( 0xFF );
  247.     }
  248.     L01_CSN_HIGH( );
  249.     L01_FlushRX( );
  250.     return width;
  251. }
  252. /*
  253. ================================================================================
  254. Function : L01_WriteTXPayload( )
  255. Description : Write TX payload to a pipe and prx will return ack back
  256. Input : -PipeNum, number of the pipe
  257.         -pBuff, A buffer stores the data
  258.         -nBytes, How many bytes to be wrote to
  259. Output: None
  260. ================================================================================
  261. */
  262. void L01_WriteTXPayload_Ack( INT8U *pBuff, INT8U nBytes )
  263. {
  264.     INT8U btmp;
  265.     INT8U length = ( nBytes > 32 ) ? 32 : nBytes;

  266.     L01_FlushTX( );
  267.     L01_CSN_LOW( );
  268.     SPI_ExchangeByte( W_TX_PAYLOAD );
  269.     for( btmp = 0; btmp < length; btmp ++ )
  270.     {
  271.         SPI_ExchangeByte( *( pBuff + btmp ) );
  272.     }
  273.     L01_CSN_HIGH( );
  274. }
  275. /*
  276. ================================================================================
  277. Function : L01_WritePayload_NoAck( )
  278. Description : write data in tx mode, and prx won't return ack back
  279. Input : -Data, A buffer stores the address data
  280.         -Data_Length, How many bytes of the data buff
  281. Output: None
  282. ================================================================================
  283. */
  284. void L01_WriteTXPayload_NoAck( INT8U *Data, INT8U Data_Length )
  285. {
  286.     if( Data_Length > 32 || Data_Length == 0 )
  287.     {
  288.         return ;
  289.     }
  290.     L01_CSN_LOW( );
  291.     SPI_ExchangeByte( W_TX_PAYLOAD_NOACK );
  292.     while( Data_Length-- )
  293.     {
  294.         SPI_ExchangeByte( *Data++ );
  295.     }
  296.     L01_CSN_HIGH( );
  297. }
  298. /*
  299. ================================================================================
  300. Function : L01_WritePayload_InAck( )
  301. Description : write data in tx fifo when rx mode
  302. Input : -Data, A buffer stores the address data
  303.         -Data_Length, How many bytes of the data buff
  304. Output: None
  305. ================================================================================
  306. */
  307. void L01_WriteRXPayload_InAck( INT8U *pData, INT8U Data_Length )
  308. {
  309.     INT8U length = ( Data_Length > 32 ) ? 32 : Data_Length;
  310.     INT8U btmp;

  311.     L01_CSN_LOW( );
  312.     SPI_ExchangeByte( W_ACK_PAYLOAD );
  313.     for( btmp = 0; btmp < length; btmp ++ )
  314.     {
  315.         SPI_ExchangeByte( *( pData + btmp ) );
  316.     }
  317.     L01_CSN_HIGH( );
  318. }
  319. /*
  320. ================================================================================
  321. Function : L01_SetTXAddr( )
  322. Description : Write address for the own device
  323. Input : -pAddr, A buffer stores the address data
  324.         -Addr_Length, How many bytes of the address
  325. Output: None
  326. ================================================================================
  327. */
  328. void L01_SetTXAddr( INT8U *pAddr, INT8U Addr_Length )
  329. {
  330.     INT8U Length = ( Addr_Length > 5 ) ? 5 : Addr_Length;
  331.     L01_WriteMultiReg( L01REG_TX_ADDR, pAddr, Length );
  332. }
  333. /*
  334. ================================================================================
  335. Function : L01_SetRXAddr( )
  336. Description : Write address for a RX pipe
  337. Input : -PipeNum, number of the pipe
  338.         -pAddr, A buffer stores the address data
  339.         -Addr_Length, How many bytes of the address
  340. Output: None
  341. ================================================================================
  342. */
  343. void L01_SetRXAddr( INT8U PipeNum, INT8U *pAddr, INT8U Addr_Length )
  344. {
  345.     INT8U Length = ( Addr_Length > 5 ) ? 5 : Addr_Length;
  346.     INT8U pipe = ( PipeNum > 5 ) ? 5 : PipeNum;

  347.     L01_WriteMultiReg( L01REG_RX_ADDR_P0 + pipe, pAddr, Length );
  348. }
  349. /*
  350. ================================================================================
  351. Function : L01_SetSpeed )
  352. Description : Send the communication speed of the RF device
  353. Input :    speed,
  354. Output: None
  355. ================================================================================
  356. */
  357. void L01_SetSpeed( L01SPD speed )
  358. {
  359.         INT8U btmp = L01_ReadSingleReg( L01REG_RF_SETUP );

  360.         btmp &= ~( ( 1<<5 ) | ( 1<<3 ) );
  361.         if( speed == SPD_250K )                //250K
  362.         {
  363.                 btmp |= ( 1<<5 );
  364.         }
  365.         else if( speed == SPD_1M )   //1M
  366.         {
  367.                    btmp &= ~( ( 1<<5 ) | ( 1<<3 ) );
  368.         }
  369.         else if( speed == SPD_2M )   //2M
  370.         {
  371.                 btmp |= ( 1<<3 );
  372.         }

  373. ……………………

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

所有资料51hei提供下载:
avrATmega8射频收发.zip (295.88 KB, 下载次数: 22)

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

回复

使用道具 举报

ID:6428 发表于 2019-6-29 13:55 | 显示全部楼层
学习下,谢谢分享
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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