打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Arduino教程——NRF24L01模块的使用
草稿,还未写完


测试可以通信,但非常不稳定,具体原因不详。
库下载:
RF24.zip(202.94 KB, 下载次数: 909)

原地址:https://github.com/maniacbug/RF24

常用函数:

RF24 (uint8_t _cepin, uint8_t _cspin)

构造函数
void
begin (void)

初始化
void

开始监听指定的通道
void

停止监听
bool
write (const void *buf, uint8_t len)

向指定通道发送数据
bool
available (void)

检查是否有接收到数据
bool
read (void *buf, uint8_t len)

Read the payload.
void
openWritingPipe (uint64_t address)

打开数据发送通道.
void
openReadingPipe (uint8_t number, uint64_t address)

打开数据接收通道.

硬件连接方法:(图)
将NRF24L01的SPI引脚与Arduino的SPI引脚相连,模块CE和CSN引脚可以连接到Arduino任意引脚,模块的VCC需要连接到arduino的3.3V引脚上,在MOSI和CSK引脚和arduino引脚间,最好分别串联一个100欧电阻。

使用NRF24L01需要先包含以下头文件:
[C++] 纯文本查看 复制代码
1
2
3
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"


并建立一个radio对象
[C] 纯文本查看 复制代码
1
RF24 radio(9,10);

两个参数分别为Arduino连接CE和CSN的引脚


发送端:
[C++] 纯文本查看 复制代码
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/*
*/
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
RF24 radio(9,10);
const uint64_t pipe = 0xE8E8F0F0E1LL;
void setup(void){
  Serial.begin(57600);
  radio.begin();
  radio.openWritingPipe(pipe);
  radio.printDetails();
  pinMode(2,INPUT_PULLUP);
}
char command[6]="hello";
void loop(void)
{
  uint8_t state = ! digitalRead(2);
  if (digitalRead(2)==LOW){
        Serial.print("Sending...");
        bool ok = radio.write(command,5);
        if(ok)
          Serial.println("successed");
        else
          Serial.println("failed");
      }
     delay(500); 
}



接收端:
[C++] 纯文本查看 复制代码
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
RF24 radio(9,10);
const uint64_t pipe = 0xE8E8F0F0E1LL;
void setup(void){
  Serial.begin(57600);
  radio.begin();
  radio.openReadingPipe(1,pipe);
  radio.startListening();
  radio.printDetails();
}
void loop(void){
    char command[6];
    if (radio.available()){
      radio.read( command, 5 );      Serial.println(command[0]);
      delay(500);
    }
}


通常以上程序就可以完成简单的点对点传输了,当然,RF24库还提供了其他可选配置:
void
setRetries (uint8_t delay, uint8_t count)

Set the number and delay of retries upon failed submit.
void
setChannel (uint8_t channel)

Set RF communication channel.
void
setPayloadSize (uint8_t size)

Set Static Payload Size.
uint8_t

Get Static Payload Size.
uint8_t

Get Dynamic Payload Size.
void

Enable custom payloads on the acknowledge packets.
void

Enable dynamically-sized payloads.
bool
isPVariant (void)

Determine whether the hardware is an nRF24L01+ or not.
void
setAutoAck (bool enable)

Enable or disable auto-acknowlede packets.
void
setAutoAck (uint8_t pipe, bool enable)

Enable or disable auto-acknowlede packets on a per pipeline basis.
void

Set Power Amplifier (PA) level to one of four levels.
getPALevel (void)

Fetches the current PA level.
bool

Set the transmission data rate.

Fetches the transmission data rate.
void

Set the CRC length.

Get the CRC length.
void
disableCRC (void)

Disable CRC validation.











上一篇:问一下arduino uno有定时器吗
下一篇:利用Arduino实时监测温湿度怎么弄、最好能显示在1602上
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
【CurieTimerZero】一个额外的定时器
如何禁用Windows屏保和电源管理
嵌入式开发:void*在工程中的应用
C++串口编程实例
(void (^)(void))
Arduino传感器连载之温度测量篇 | OSZINE | 发现技术之美
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服