久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 3384|回復: 1
打印 上一主題 下一主題
收起左側

nRF24L01p+AVR單片機ATmage88射頻收發程序

[復制鏈接]
跳轉到指定樓層
樓主
avr單片機,基于ATmega88+nRF24L01p+oLED
可移植性極高
完整的接收和發送程序


單片機源程序如下:
  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 共享資料的黑幣獎勵!

查看全部評分

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏2 分享淘帖 頂 踩
回復

使用道具 舉報

沙發
ID:6428 發表于 2019-6-29 13:55 | 只看該作者
學習下,謝謝分享
回復

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規則

手機版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術交流QQ群281945664

Powered by 單片機教程網

快速回復 返回頂部 返回列表
主站蜘蛛池模板: 四虎影院免费在线播放 | 久久精品小视频 | 一区在线观看 | 国产黄色免费网站 | 91精品国产色综合久久 | 久久久久91| 国产成人精品视频 | 国产一区二区三区在线视频 | 日韩天堂av | 91免费在线播放 | 亚洲激情专区 | 97人人干 | 国产精品永久在线观看 | 岛国毛片| 日韩在线小视频 | 欧洲成人 | 日日人人 | 羞羞视频网站免费看 | 91精品久久久久久久久 | 日韩精品成人一区二区三区视频 | 天天射影院 | 日本网站免费观看 | 精品欧美一区二区三区久久久 | 久久久欧洲| 欧美久久一区二区三区 | 一区二区影院 | 国产一级精品毛片 | 国产目拍亚洲精品99久久精品 | 中文字幕国产精品视频 | 欧美精品一区二区蜜桃 | 精品国产高清一区二区三区 | 高清久久久 | 亚洲69p | 欧美中国少妇xxx性高请视频 | 亚洲成人一区二区 | 日韩激情网 | 午夜视频在线播放 | 亚洲aⅴ| 男女污污网站 | 日韩不卡一区二区 | 91免费看片 |