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

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

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

Arduino 2.4G通信實驗 nRF24L01模塊的mirf庫下載

[復制鏈接]
跳轉到指定樓層
樓主
最近在做Arduino的 nRF24L01模塊的實驗,應用別人的代碼發現找不到mirf.h路徑,在Arduino的庫管理里沒能找到相應庫,所以在網上找了一個,親測可用,在這里分享給大家。

單片機源程序如下:
  1. /**
  2. * Mirf
  3. *
  4. * Additional bug fixes and improvements
  5. *  11/03/2011:
  6. *   Switched spi library.
  7. *  07/13/2010:
  8. *   Added example to read a register
  9. *  11/12/2009:
  10. *   Fix dataReady() to work correctly
  11. *   Renamed keywords to keywords.txt ( for IDE ) and updated keyword list
  12. *   Fixed client example code to timeout after one second and try again
  13. *    when no response received from server
  14. * By: Nathan Isburgh <nathan@mrroot.net>
  15. * $Id: mirf.cpp 67 2010-07-13 13:25:53Z nisburgh $
  16. *
  17. *
  18. *
  19. * Significant changes to remove depencence on interupts and auto ack support.
  20. *
  21. * Aaron Shrimpton <aaronds@gmail.com>
  22. *
  23. */

  24. /*
  25.     Copyright (c) 2007 Stefan Engelke <mbox@stefanengelke.de>

  26.     Permission is hereby granted, free of charge, to any person
  27.     obtaining a copy of this software and associated documentation
  28.     files (the "Software"), to deal in the Software without
  29.     restriction, including without limitation the rights to use, copy,
  30.     modify, merge, publish, distribute, sublicense, and/or sell copies
  31.     of the Software, and to permit persons to whom the Software is
  32.     furnished to do so, subject to the following conditions:

  33.     The above copyright notice and this permission notice shall be
  34.     included in all copies or substantial portions of the Software.

  35.     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  36.     EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  37.     MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  38.     NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  39.     HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  40.     WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  41.     OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  42.     DEALINGS IN THE SOFTWARE.

  43.     $Id: mirf.cpp 67 2010-07-13 13:25:53Z nisburgh $
  44. */

  45. #include "Mirf.h"
  46. // Defines for setting the MiRF registers for transmitting or receiving mode

  47. Nrf24l Mirf = Nrf24l();

  48. Nrf24l::Nrf24l(){
  49.         cePin = 8;
  50.         csnPin = 7;
  51.         channel = 1;
  52.         payload = 16;
  53.         spi = NULL;
  54. }

  55. void Nrf24l::transferSync(uint8_t *dataout,uint8_t *datain,uint8_t len){
  56.         uint8_t i;
  57.         for(i = 0;i < len;i++){
  58.                 datain[i] = spi->transfer(dataout[i]);
  59.         }
  60. }

  61. void Nrf24l::transmitSync(uint8_t *dataout,uint8_t len){
  62.         uint8_t i;
  63.         for(i = 0;i < len;i++){
  64.                 spi->transfer(dataout[i]);
  65.         }
  66. }


  67. void Nrf24l::init()
  68. // Initializes pins to communicate with the MiRF module
  69. // Should be called in the early initializing phase at startup.
  70. {   
  71.     pinMode(cePin,OUTPUT);
  72.     pinMode(csnPin,OUTPUT);

  73.     ceLow();
  74.     csnHi();

  75.     // Initialize spi module
  76.     spi->begin();

  77. }


  78. void Nrf24l::config()
  79. // Sets the important registers in the MiRF module and powers the module
  80. // in receiving mode
  81. // NB: channel and payload must be set now.
  82. {
  83.     // Set RF channel
  84.         configRegister(RF_CH,channel);

  85.     // Set length of incoming payload
  86.         configRegister(RX_PW_P0, payload);
  87.         configRegister(RX_PW_P1, payload);

  88.     // Start receiver
  89.     powerUpRx();
  90.     flushRx();
  91. }

  92. void Nrf24l::setRADDR(uint8_t * adr)
  93. // Sets the receiving address
  94. {
  95.         ceLow();
  96.         writeRegister(RX_ADDR_P1,adr,mirf_ADDR_LEN);
  97.         ceHi();
  98. }

  99. void Nrf24l::setTADDR(uint8_t * adr)
  100. // Sets the transmitting address
  101. {
  102.         /*
  103.          * RX_ADDR_P0 must be set to the sending addr for auto ack to work.
  104.          */

  105.         writeRegister(RX_ADDR_P0,adr,mirf_ADDR_LEN);
  106.         writeRegister(TX_ADDR,adr,mirf_ADDR_LEN);
  107. }

  108. extern bool Nrf24l::dataReady()
  109. // Checks if data is available for reading
  110. {
  111.     // See note in getData() function - just checking RX_DR isn't good enough
  112.         uint8_t status = getStatus();

  113.     // We can short circuit on RX_DR, but if it's not set, we still need
  114.     // to check the FIFO for any pending packets
  115.     if ( status & (1 << RX_DR) ) return 1;
  116.     return !rxFifoEmpty();
  117. }

  118. extern bool Nrf24l::rxFifoEmpty(){
  119.         uint8_t fifoStatus;

  120.         readRegister(FIFO_STATUS,&fifoStatus,sizeof(fifoStatus));
  121.         return (fifoStatus & (1 << RX_EMPTY));
  122. }



  123. extern void Nrf24l::getData(uint8_t * data)
  124. // Reads payload bytes into data array
  125. {
  126.     csnLow();                               // Pull down chip select
  127.     spi->transfer( R_RX_PAYLOAD );            // Send cmd to read rx payload
  128.     transferSync(data,data,payload); // Read payload
  129.     csnHi();                               // Pull up chip select
  130.     // NVI: per product spec, p 67, note c:
  131.     //  "The RX_DR IRQ is asserted by a new packet arrival event. The procedure
  132.     //  for handling this interrupt should be: 1) read payload through SPI,
  133.     //  2) clear RX_DR IRQ, 3) read FIFO_STATUS to check if there are more
  134.     //  payloads available in RX FIFO, 4) if there are more data in RX FIFO,
  135.     //  repeat from step 1)."
  136.     // So if we're going to clear RX_DR here, we need to check the RX FIFO
  137.     // in the dataReady() function
  138.     configRegister(STATUS,(1<<RX_DR));   // Reset status register
  139. }

  140. void Nrf24l::configRegister(uint8_t reg, uint8_t value)
  141. // Clocks only one byte into the given MiRF register
  142. {
  143.     csnLow();
  144.     spi->transfer(W_REGISTER | (REGISTER_MASK & reg));
  145.     spi->transfer(value);
  146.     csnHi();
  147. }

  148. void Nrf24l::readRegister(uint8_t reg, uint8_t * value, uint8_t len)
  149. // Reads an array of bytes from the given start position in the MiRF registers.
  150. {
  151.     csnLow();
  152.     spi->transfer(R_REGISTER | (REGISTER_MASK & reg));
  153.     transferSync(value,value,len);
  154.     csnHi();
  155. }

  156. void Nrf24l::writeRegister(uint8_t reg, uint8_t * value, uint8_t len)
  157. // Writes an array of bytes into inte the MiRF registers.
  158. {
  159.     csnLow();
  160.     spi->transfer(W_REGISTER | (REGISTER_MASK & reg));
  161.     transmitSync(value,len);
  162.     csnHi();
  163. }


  164. void Nrf24l::send(uint8_t * value)
  165. // Sends a data package to the default address. Be sure to send the correct
  166. // amount of bytes as configured as payload on the receiver.
  167. {
  168.     uint8_t status;
  169.     status = getStatus();

  170.     while (PTX) {
  171.             status = getStatus();

  172.             if((status & ((1 << TX_DS)  | (1 << MAX_RT)))){
  173.                     PTX = 0;
  174.                     break;
  175.             }
  176.     }                  // Wait until last paket is send

  177.     ceLow();
  178.    
  179.     powerUpTx();       // Set to transmitter mode , Power up
  180.    
  181.     csnLow();                    // Pull down chip select
  182.     spi->transfer( FLUSH_TX );     // Write cmd to flush tx fifo
  183.     csnHi();                    // Pull up chip select
  184.    
  185.     csnLow();                    // Pull down chip select
  186.     spi->transfer( W_TX_PAYLOAD ); // Write cmd to write payload
  187.     transmitSync(value,payload);   // Write payload
  188.     csnHi();                    // Pull up chip select

  189.     ceHi();                     // Start transmission
  190. }

  191. /**
  192. * isSending.
  193. *
  194. * Test if chip is still sending.
  195. * When sending has finished return chip to listening.
  196. *
  197. */

  198. bool Nrf24l::isSending(){
  199.         uint8_t status;
  200.         if(PTX){
  201.                 status = getStatus();
  202.                    
  203.                 /*
  204.                  *  if sending successful (TX_DS) or max retries exceded (MAX_RT).
  205.                  */

  206.                 if((status & ((1 << TX_DS)  | (1 << MAX_RT)))){
  207.                         powerUpRx();
  208.                         return false;
  209.                 }

  210.                 return true;
  211.         }
  212.         return false;
  213. }

  214. uint8_t Nrf24l::getStatus(){
  215.         uint8_t rv;
  216.         readRegister(STATUS,&rv,1);
  217.         return rv;
  218. }

  219. void Nrf24l::powerUpRx(){
  220.         PTX = 0;
  221.         ceLow();
  222.         configRegister(CONFIG, mirf_CONFIG | ( (1<<PWR_UP) | (1<<PRIM_RX) ) );
  223.         ceHi();
  224.         configRegister(STATUS,(1 << TX_DS) | (1 << MAX_RT));
  225. ……………………

  226. …………限于本文篇幅 余下代碼請從51黑下載附件…………
復制代碼

所有資料51hei提供下載:
Mirf.rar (10.1 KB, 下載次數: 143)


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

使用道具 舉報

沙發
ID:985522 發表于 2022-1-1 22:25 | 只看該作者
學習玄學學習玄學學習玄學
回復

使用道具 舉報

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

本版積分規則

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

Powered by 單片機教程網

快速回復 返回頂部 返回列表
主站蜘蛛池模板: 天天综合久久 | 2023亚洲天堂 | 成人久久网| 高清国产午夜精品久久久久久 | 久久99精品久久久久蜜桃tv | 久久久久久久久淑女av国产精品 | 国产精品久久久久久福利一牛影视 | 久久久久久久久精 | 国产一伦一伦一伦 | 91 久久 | 网站国产 | 91欧美精品成人综合在线观看 | 国产一区二区不卡 | 在线激情视频 | 婷婷丁香在线视频 | 欧美日韩国产一区二区三区 | 精品亚洲一区二区三区 | 野狼在线社区2017入口 | 日韩av在线一区二区三区 | 国产做a爱片久久毛片 | 国产精品a一区二区三区网址 | 97超碰在线免费 | 毛片av免费看 | 天堂成人国产精品一区 | 在线免费观看黄色 | 老司机67194精品线观看 | 亚洲一区二区三区四区五区午夜 | 国产乱码精品一区二区三区av | 久久亚洲国产 | 综合久久99 | 在线观看国产视频 | 国产精品国产三级国产aⅴ无密码 | 亚洲一区视频在线 | 国产精品久久久久无码av | 久久久久久亚洲精品 | 亚洲精品www | 欧美精品在线免费 | 午夜电影福利 | 99pao成人国产永久免费视频 | 日本在线中文 | 天天操夜夜操 |