找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 6421|回复: 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, 下载次数: 140)


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏4 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:985522 发表于 2022-1-1 22:25 | 只看该作者
学习玄学学习玄学学习玄学
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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