2.4G实时收发电报机(全双工)


ChangeCode
原创
发布时间: 2025-06-14 14:57:53 | 阅读数 0收藏数 0评论数 0
封面
上周做的电报机只能发送,不能接受,而且也无法做到实时通信,这次带来一个可以实时通信的电报机,也就是全双工工作,而且,将NRF24L01替换成了国产的SI24R1芯片,二者相差无几,引脚和代码都适用。

准备工作:

材料:


材料名称

数量

型号/说明/备注

Arduino nano

2

排母

2

2 X 4

4

1 X 14

SR24R1

2

/

蜂鸣器/喇叭

2

/

端子

2

/

按键

2

/

4节电池盒

2

/

LED

2

/


工具:


工具名称

数量

型号/说明/备注

电烙铁

1

/

剥线钳

1

/

漆包线

若干

/

1

SI24R1芯片封装


工具名称

数量

型号/说明/备注

电烙铁

1

/

剥线钳

1

/

漆包线

若干

/

2

SI24R1芯片介绍

介绍:超低功耗高性能 2.4GHz GFSK 无线收发器芯片

  1. Si24R1是一款2.4GHz的低功耗无线收发器模块,基于Nordic Semiconductor的nRF24L01+芯片。
  2. 该模块具有良好的性能和稳定的数据传输能力,常被用于无线通信领域。
  3. Si24R1模块可以通过SPI接口与主控芯片(如单片机)进行通信,实现无线数据传输。
  4. 它具有较远的传输距离、多通道选择、多种工作模式等特点,适用于各种无线控制、监测、数据采集等场景。
3

芯片与arduino nano连接表

主要的连接方式和NRF24L01一样。

4

电路图


5

准备部件

我们需要准备两个这样的部件

6

代码更改

接下来,看看程序中都改了哪些代码吧。

  1. 需要额外添加一条通讯通道,使电报机可以通过接收通道/发送通道接收/发送信息
  2. 使用按键时,为了防止按键抖动,我们需要给按键焊接一个上拉电阻,并且在程序中设置”上拉模式”
  3. 同时打开”写”和”读”模式,保证模块能正常收发信息
  4. 在LOOP函数中,再根据情况打开/关闭接收模式,当发送数据后,再打开/关闭接收模式

这便是主要更改的地方

以下是A机和B机的程序

程序A

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(9, 10); // CE, CSN
const byte addresses[][6] = { "00001", "00002" };
//发送地址和接收地址
int button_pin = 2;
boolean button_state = 0;
boolean button_state1 = 0;
void setup() {
pinMode(button_pin, INPUT_PULLUP);
pinMode(3, OUTPUT);
radio.begin();
radio.openWritingPipe(addresses[1]);
radio.openReadingPipe(1, addresses[0]);
radio.setPALevel(RF24_PA_MIN);
}
void loop() {
delay(5);
radio.stopListening(); //设置为发送端
button_state = digitalRead(button_pin);
radio.write(&button_state, sizeof(button_state)); //发送数据
delay(5);
Serial.print(button_state);
radio.startListening(); //设置为接收端
if (radio.available()) { //等待接收数据
radio.read(&button_state1, sizeof(button_state1)); //读取数据
if (button_state1 == 1) {
noTone(6);
digitalWrite(3, LOW);
} else {
tone(6, 800);
digitalWrite(3, HIGH);
}
}
Serial.print(" ");
Serial.println(button_state1);

/*if (digitalRead(2)) {
digitalWrite(3, LOW);
} else {
digitalWrite(3, HIGH);
}*/
}

----------------------------------------------------分割线---------------------------------------------------------

程序B

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(9, 10); // CE, CSN
const byte addresses[][6] = { "00001", "00002" };
//发送地址和接收地址
int button_pin = 2;
boolean button_state = 0;
boolean button_state1 = 0;
void setup() {
pinMode(button_pin, INPUT_PULLUP);
pinMode(3, OUTPUT);
Serial.begin(9600);
radio.begin();
radio.openWritingPipe(addresses[0]);
radio.openReadingPipe(1, addresses[1]);
radio.setPALevel(RF24_PA_MIN);
}
void loop() {
delay(5);
radio.startListening();
//设置为接收端
if (radio.available())
//等待接收数据
{
radio.read(&button_state, sizeof(button_state));
if (button_state == 1) {
noTone(6);
digitalWrite(3, LOW);
} else {
tone(6, 800);
digitalWrite(3, HIGH);
}
delay(5);
Serial.print(button_state);
radio.stopListening();
//设置为发送端
button_state1 = digitalRead(button_pin);
radio.write(&button_state1, sizeof(button_state1)); //发送数据
Serial.print(" ");
Serial.println(button_state1);

/*if (digitalRead(2)) {
digitalWrite(3, LOW);
} else {
digitalWrite(3, HIGH);
}*/
}
}

INO
NRF_A.ino
1.17KB
INO
NRF_B.ino
1.16KB
7

成品


阅读记录0
点赞0
收藏0
禁止 本文未经作者允许授权,禁止转载
猜你喜欢
评论/提问(已发布 0 条)
评论 评论
收藏 收藏
分享 分享
pdf下载 下载