|
本帖最后由 eddiewwm 于 2019-11-21 09:41 编辑
在論壇上看到有人說 LGT8f328P在配合 nRF24L01使用時會出現問題,而用 arduino nano卻不會。問題特別在使用庫時會出現,但可惜沒有說明是那個庫。
因之前用SPI點LCD屏時,LGT8F328P確是會在某些庫出現問題,所以,這次特以 RF24庫作測試,看看以SPI控制nRF2401,會出現甚麼樣的問題。
發射端程序 - /*
- /*
- * Arduino Wireless Communication Tutorial
- * Example 1 - Transmitter Code
- *
- * by Dejan Nedelkovski, www.HowToMechatronics.com
- *
- * Library: TMRh20/RF24, https://github.com/tmrh20/RF24/
- */
- #include <SPI.h>
- #include <nRF24L01.h>
- #include <RF24.h>
- RF24 radio(7, 8); // CE, CSN
- const byte address[6] = "00001";
- void setup() {
- radio.begin();
- radio.openWritingPipe(address);
- radio.setPALevel(RF24_PA_MIN);
- radio.stopListening();
- }
- void loop() {
- const char text[] = "Hello World";
- radio.write(&text, sizeof(text));
- delay(1000);
- }
复制代码
接收端程序 - /*
- * Arduino Wireless Communication Tutorial
- * Example 1 - Receiver Code
- *
- * by Dejan Nedelkovski, www.HowToMechatronics.com
- *
- * Library: TMRh20/RF24, https://github.com/tmrh20/RF24/
- */
- #include <SPI.h>
- #include <nRF24L01.h>
- #include <RF24.h>
- RF24 radio(7, 8); // CE, CSN
- const byte address[6] = "00001";
- void setup() {
- Serial.begin(9600);
- radio.begin();
- radio.openReadingPipe(0, address);
- radio.setPALevel(RF24_PA_MIN);
- radio.startListening();
- }
- void loop() {
- if (radio.available()) {
- char text[32] = "";
- radio.read(&text, sizeof(text));
- Serial.println(text);
- }
- }
复制代码
測試結果正常,沒發現問題。
(註:nRF2401模塊應在電源多加一10uF以上的電容,並要特別留意發射端的電源供應是否能提供充夠的電流,特別是瞬間電流)
Reference:
https://howtomechatronics.com/tu ... -nrf24l01-tutorial/ |
|