分享

Arduino教程——NRF24L01模块的使用

 开启美好每一天 2015-11-16
草稿,还未写完


测试可以通信,但非常不稳定,具体原因不详。
库下载: 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上

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多