|
本帖最后由 histamine 于 2013-3-29 22:57 编辑
简单介绍下,详细信息及更高级的使用方法请自己阅读rf22库的文档
使用RF22库
http://www.airspayce.com/mikem/arduino/RF22/
(该库适用的Hope-RF RF22B模块其实就是si4432模块)
si4432与5v arduino通讯需要用一块cd4050进行电平转换
硬件连接原理图:
面包板连接示意图:
实际演示需要两块si4432模块和两块arduino uno开发板:
两块arduino uno分别烧入以下代码(即RF22库自带的client/server example中):
client:- // rf22_client.pde
- // -*- mode: C++ -*-
- // Example sketch showing how to create a simple messageing client
- // with the RF22 class. RF22 class does not provide for addressing or reliability.
- // It is designed to work with the other example rf22_server
- #include <SPI.h>
- #include <RF22.h>
- // Singleton instance of the radio
- RF22 rf22;
- void setup()
- {
- Serial.begin(9600);
- if (!rf22.init())
- Serial.println("RF22 init failed");
- // Defaults after init are 434.0MHz, 0.05MHz AFC pull-in, modulation FSK_Rb2_4Fd36
- }
- void loop()
- {
- while (1)
- {
- Serial.println("Sending to rf22_server");
- // Send a message to rf22_server
- uint8_t data[] = "Hello World!";
- rf22.send(data, sizeof(data));
-
- rf22.waitPacketSent();
- // Now wait for a reply
- uint8_t buf[RF22_MAX_MESSAGE_LEN];
- uint8_t len = sizeof(buf);
- if (rf22.waitAvailableTimeout(500))
- {
- // Should be a message for us now
- if (rf22.recv(buf, &len))
- {
- Serial.print("got reply: ");
- Serial.println((char*)buf);
- }
- else
- {
- Serial.println("recv failed");
- }
- }
- else
- {
- Serial.println("No reply, is rf22_server running?");
- }
- }
- }
复制代码 server:- // rf22_server.pde
- // -*- mode: C++ -*-
- // Example sketch showing how to create a simple messageing server
- // with the RF22 class. RF22 class does not provide for addressing or reliability.
- // It is designed to work with the other example rf22_client
- #include <SPI.h>
- #include <RF22.h>
- // Singleton instance of the radio
- RF22 rf22;
- void setup()
- {
- Serial.begin(9600);
- if (!rf22.init())
- Serial.println("RF22 init failed");
- // Defaults after init are 434.0MHz, 0.05MHz AFC pull-in, modulation FSK_Rb2_4Fd36
- }
- void loop()
- {
- while (1)
- {
- rf22.waitAvailable();
-
- // Should be a message for us now
- uint8_t buf[RF22_MAX_MESSAGE_LEN];
- uint8_t len = sizeof(buf);
- if (rf22.recv(buf, &len))
- {
- Serial.print("got request: ");
- Serial.println((char*)buf);
-
- // Send a reply
- uint8_t data[] = "And hello back to you";
- rf22.send(data, sizeof(data));
- rf22.waitPacketSent();
- Serial.println("Sent a reply");
- }
- else
- {
- Serial.println("recv failed");
- }
- }
- }
复制代码 通过串口输出,即可看到两块arduino uno正在通过si4432模块进行通讯:
再补充几句:
一块arduino uno其实可以同时接两块si4432模块,这两个模块的nSEL引脚要连接到arduino不同的引脚上,nIRQ引脚分别连接到arduino的D2(interrupt 0)和D3(interrupt 1),然后初始化RF22对象时需要手动指定slaveSelectPin和interrupt两个参数
RF22库还支持组网功能,见RF22Mesh和RF22Router两个类的文档 |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?注册
x
|