nRF24l01模块NF24库使用问题-Arduino中文社区 - Powered by Discuz!

Arduino中文社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 2478|回复: 3

[未解决] nRF24l01模块NF24库使用问题

[复制链接]
发表于 2021-9-3 20:36 | 显示全部楼层 |阅读模式
我希望用NRF24L01模块进行通信,代码我使用的是NF24库的官方示例代码:
  1. #include <SPI.h>
  2. #include "printf.h"
  3. #include "RF24.h"
  4.   
  5. // instantiate an object for the nRF24L01 transceiver
  6. RF24 radio(7, 8); // using pin 7 for the CE pin, and pin 8 for the CSN pin
  7.   
  8. // Let these addresses be used for the pair
  9. uint8_t address[][6] = {"1Node", "2Node"};
  10. // It is very helpful to think of an address as a path instead of as
  11. // an identifying device destination
  12.   
  13. // to use different addresses on a pair of radios, we need a variable to
  14. // uniquely identify which address this radio will use to transmit
  15. bool radioNumber = 1; // 0 uses address[0] to transmit, 1 uses address[1] to transmit
  16.   
  17. // Used to control whether this node is sending or receiving
  18. bool role = false;  // true = TX role(send), false = RX role(receive)
  19.   
  20. // For this example, we'll be using a payload containing
  21. // a single float number that will be incremented
  22. // on every successful transmission
  23. float payload = 0.0;
  24.   
  25. void setup() {
  26.   
  27.    Serial.begin(115200);
  28.    while (!Serial) {
  29.      // some boards need to wait to ensure access to serial over USB
  30.    }
  31.   
  32.    // initialize the transceiver on the SPI bus
  33.    if (!radio.begin()) {
  34.      Serial.println(F("radio hardware is not responding!!"));
  35.      while (1) {} // hold in infinite loop
  36.    }
  37.   
  38.    // print example's introductory prompt
  39.    Serial.println(F("RF24/examples/GettingStarted"));
  40.   
  41.    // To set the radioNumber via the Serial monitor on startup
  42.    Serial.println(F("Which radio is this? Enter '0' or '1'. Defaults to '0'"));
  43.    while (!Serial.available()) {
  44.      // wait for user input
  45.    }
  46.    char input = Serial.parseInt();
  47.    radioNumber = input == 1;
  48.    Serial.print(F("radioNumber = "));
  49.    Serial.println((int)radioNumber);
  50.   
  51.    // role variable is hardcoded to RX behavior, inform the user of this
  52.    Serial.println(F("*** PRESS 'T' to begin transmitting to the other node"));
  53.   
  54.    // Set the PA Level low to try preventing power supply related problems
  55.    // because these examples are likely run with nodes in close proximity to
  56.    // each other.
  57.    radio.setPALevel(RF24_PA_LOW);  // RF24_PA_MAX is default.
  58.   
  59.    // save on transmission time by setting the radio to only transmit the
  60.    // number of bytes we need to transmit a float
  61.    radio.setPayloadSize(sizeof(payload)); // float datatype occupies 4 bytes
  62.   
  63.    // set the TX address of the RX node into the TX pipe
  64.    radio.openWritingPipe(address[radioNumber]);     // always uses pipe 0
  65.   
  66.    // set the RX address of the TX node into a RX pipe
  67.    radio.openReadingPipe(1, address[!radioNumber]); // using pipe 1
  68.   
  69.    // additional setup specific to the node's role
  70.    if (role) {
  71.      radio.stopListening();  // put radio in TX mode
  72.    } else {
  73.      radio.startListening(); // put radio in RX mode
  74.    }
  75.   
  76.    // For debugging info
  77.    // printf_begin();             // needed only once for printing details
  78.    // radio.printDetails();       // (smaller) function that prints raw register values
  79.    // radio.printPrettyDetails(); // (larger) function that prints human readable data
  80.   
  81. } // setup
  82.   
  83. void loop() {
  84.   
  85.    if (role) {
  86.      // This device is a TX node
  87.   
  88.      unsigned long start_timer = micros();                    // start the timer
  89.      bool report = radio.write(&payload, sizeof(float));      // transmit & save the report
  90.      unsigned long end_timer = micros();                      // end the timer
  91.   
  92.      if (report) {
  93.        Serial.print(F("Transmission successful! "));          // payload was delivered
  94.        Serial.print(F("Time to transmit = "));
  95.        Serial.print(end_timer - start_timer);                 // print the timer result
  96.        Serial.print(F(" us. Sent: "));
  97.        Serial.println(payload);                               // print payload sent
  98.        payload += 0.01;                                       // increment float payload
  99.      } else {
  100.        Serial.println(F("Transmission failed or timed out")); // payload was not delivered
  101.      }
  102.   
  103.      // to make this example readable in the serial monitor
  104.      delay(1000);  // slow transmissions down by 1 second
  105.   
  106.    } else {
  107.      // This device is a RX node
  108.   
  109.      uint8_t pipe;
  110.      if (radio.available(&pipe)) {             // is there a payload? get the pipe number that recieved it
  111.        uint8_t bytes = radio.getPayloadSize(); // get the size of the payload
  112.        radio.read(&payload, bytes);            // fetch payload from FIFO
  113.        Serial.print(F("Received "));
  114.        Serial.print(bytes);                    // print the size of the payload
  115.        Serial.print(F(" bytes on pipe "));
  116.        Serial.print(pipe);                     // print the pipe number
  117.        Serial.print(F(": "));
  118.        Serial.println(payload);                // print the payload's value
  119.      }
  120.    } // role
  121.   
  122.    if (Serial.available()) {
  123.      // change the role via the serial monitor
  124.   
  125.      char c = toupper(Serial.read());
  126.      if (c == 'T' && !role) {
  127.        // Become the TX node
  128.   
  129.        role = true;
  130.        Serial.println(F("*** CHANGING TO TRANSMIT ROLE -- PRESS 'R' TO SWITCH BACK"));
  131.        radio.stopListening();
  132.   
  133.      } else if (c == 'R' && role) {
  134.        // Become the RX node
  135.   
  136.        role = false;
  137.        Serial.println(F("*** CHANGING TO RECEIVE ROLE -- PRESS 'T' TO SWITCH BACK"));
  138.        radio.startListening();
  139.      }
  140.    }
  141.   
  142. } // loop
复制代码
我使用的是Arduino UNO,运行结果只显示:radio hardware is not responding!!
也就是说the MCU failed to communicate with the radio hardware(引用RF24库文档)
连线方面我是将NRF24L01模块的GND连接Arduino板的GND,VCC连接3.3V(5V我也试了也不行),CE连接管脚7,CSN连接管脚8
请问我这问题可能出在哪呢?
发表于 2021-9-4 00:16 | 显示全部楼层
别一上来就整这么多复杂的玩意,这样只会把自己玩死。
NRF24L01故障排查-思路方法汇总
https://www.arduino.cn/forum.php ... &fromuid=179950
(出处: Arduino中文社区)
 楼主| 发表于 2021-9-4 12:53 | 显示全部楼层
frankhan747 发表于 2021-9-4 00:16
别一上来就整这么多复杂的玩意,这样只会把自己玩死。
NRF24L01故障排查-思路方法汇总
https://www.arduino ...

我是用这里的程序有一样的问题,从最开始的begin()函数就返回false
发表于 2021-9-5 07:03 | 显示全部楼层
路人甲、 发表于 2021-9-4 12:53
我是用这里的程序有一样的问题,从最开始的begin()函数就返回false

begin()函数返回false,说明你的nRF芯片根本无法和主控交互,完全就是坏的。建议直接找卖家对线
如果卖家不退货也不换货,建议挂出来以便后来者避坑
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|Archiver|手机版|Arduino中文社区

GMT+8, 2024-11-28 23:36 , Processed in 0.099628 second(s), 15 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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