极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 26196|回复: 6

内嵌Zigbee协议的Arduino--Microduino Core RF初探之:无线控制RGB灯

[复制链接]
发表于 2014-5-3 15:51:53 | 显示全部楼层 |阅读模式
本帖最后由 wasdpkj 于 2015-12-1 20:22 编辑



Microduino Core RF是原生支持802.15.4无线协议的的AVR核心板,Microduino对其进行了硬件包的修正,保证了兼容性。
现有Microduino模块全部通过测试:
http://www.microduino.cc/wiki/index.php?title=Main_Page/zh


采用ATmega128RFA1核心:
  • 3.3V供电
  • 16Mhz 频率
  • 128KB Flash
  • 4KB EEPROM
  • 16KB SRAM

集成802.15.4协议无线模块,支持任何基于802.15.4协议的无线模块,包括ZigBee,MAC/6LoWPAN和 RF4CE

含有22个数字I/O(其中6个支持ADC)

2路硬串口:
  • UART0_RX:D0
  • UART0_TX:D1
  • UART1_RX:D2
  • UART1_TX:D3

7路PWM:
4、5、6、7、8、9、10

6路外部中断:
  • Interrupt0 (pin SCL)
  • Interrupt1 (pin SDA)
  • Interrupt2 (pin D2)
  • Interrupt3 (pin D3)
  • Interrupt4 (pin D5)
  • Interrupt5 (pin D6)


Original Pin Name
Map Pin Name
Digital Pin
Analog Pin
interrupt
PWM
Serial
SPI
I2C
Power
VCC
+5V
+5V
VCC
+3V3
+3.3V
(OC0A/OC1C)PB7
D7
D7
yes
(OC1B)PB6
D8
D8
yes
(OC1A)PB5
D9
D9
yes
(OC2A/SS)PB4
D10
D10
yes
SS
(MOSI/PDI)PB2
D11
D11
MOSI
(MISO/PDO)PB3
D12
D12
MISO
(SCK)PB1
D13
D13
SCK
AREF
AREF
(ADC7)PF7
A0
D14
A0
(ADC6)PF6
A1
D15
A1
(ADC5)PF5
A2
D16
A2
(ADC4)PF4
A3
D17
A3
(SDA)PD1
SDA
D18
1
SDA
(SCL)PD0
SCL
D19
0
SCL
(ADC3)PF3
A6
D20
A6
(ADC2)PF2
A7
D21
A7
(RXD0)PE0
RX0
D0
0(RX)
(TXD0)PE1
TX1
D1
0(TX)
(RXD1)PD2
D2
D2
2
1(RX)
(TXD1)PD3
D3
D3
3
1(TX)
(OC3A)PE3
D4
D4
yes
(OC3B)PE4
D5
D5
4
yes
(OC3C)PE5
D6
D6
5
yes
RESET
RST
GND
GND
GND




然后你需要下载硬件支持包,覆盖到Arduino IDE(推荐1.05版本)安装目录即可。
http://pan.baidu.com/s/1pJHfkc7
上面的支持包里包含了ZigduinoRadio这个库,我们就用这个库的示例程序“ZigduinoRadioExample”来展开测试:
  1. /*

  2. Run this sketch on two Zigduinos, open the serial monitor at 9600 baud, and type in stuff
  3. Watch the Rx Zigduino output what you've input into the serial port of the Tx Zigduino

  4. */

  5. #include <ZigduinoRadio.h>

  6. void setup()
  7. {
  8.   ZigduinoRadio.begin(11);
  9.   Serial.begin(9600);
  10.   
  11.   ZigduinoRadio.attachError(errHandle);
  12.   ZigduinoRadio.attachTxDone(onXmitDone);
  13. }

  14. void loop()
  15. {
  16.   if (Serial.available())
  17.   {
  18.     ZigduinoRadio.beginTransmission();
  19.    
  20.     Serial.println();
  21.     Serial.print("Tx: ");
  22.    
  23.     while(Serial.available())
  24.     {
  25.       char c = Serial.read();
  26.       Serial.write(c);
  27.       ZigduinoRadio.write(c);
  28.     }
  29.    
  30.     Serial.println();
  31.    
  32.     ZigduinoRadio.endTransmission();
  33.   }
  34.   
  35.   if (ZigduinoRadio.available())
  36.   {
  37.     Serial.println();
  38.     Serial.print("Rx: ");
  39.    
  40.     while(ZigduinoRadio.available())
  41.       Serial.write(ZigduinoRadio.read());
  42.       
  43.     Serial.println();
  44.     Serial.print("LQI: ");
  45.     Serial.print(ZigduinoRadio.getLqi(), 10);
  46.     Serial.print(", RSSI: ");
  47.     Serial.print(ZigduinoRadio.getLastRssi(), 10);
  48.     Serial.print(" dBm, ED: ");
  49.     Serial.print(ZigduinoRadio.getLastEd(), 10);
  50.     Serial.println("dBm");
  51.   }
  52.   
  53.   delay(100);
  54. }

  55. void errHandle(radio_error_t err)
  56. {
  57.   Serial.println();
  58.   Serial.print("Error: ");
  59.   Serial.print((uint8_t)err, 10);
  60.   Serial.println();
  61. }

  62. void onXmitDone(radio_tx_done_t x)
  63. {
  64.   Serial.println();
  65.   Serial.print("TxDone: ");
  66.   Serial.print((uint8_t)x, 10);
  67.   Serial.println();
  68. }
复制代码


这个程序实现了最基本的无线串口透传功能。
因为Core RF采用串口下载,所以你先要叠加一个FT232:


板卡选择Microduino Core RF


分别给两块Core RF下载好程序以后,打开串口监视器,发送“Hello Microduino!”
可以看到如图结果:



最基本的硬件试通了,下面一楼,就该介绍如何无线控制RGB的LED了~

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复

使用道具 举报

 楼主| 发表于 2014-5-3 16:04:19 | 显示全部楼层
本帖最后由 wasdpkj 于 2014-5-3 16:56 编辑

现在开始研究驱动全彩LED,我这里选择了WS2812模块(TB可以搜到):
WS2812特性:
  • 5050高亮LED,内置控制芯片,仅需1个IO口即可控制多个LED
  • 芯片内置整形电路,信号畸变不会累计,稳定显示
  • 三基色256级亮度调剂,16万色真彩显示效果,扫描频率不低于400Hz/S
  • 串行连级接口,能通过一根信号线完成数据的接收与解码
  • 刷新速率30帧/秒时,低速连级模式连级数不小于512点
  • 数据收发速度最高可达800Kbps

所用到的库:
Adafruit_NeoPixel
https://github.com/adafruit/Adafruit_NeoPixel


程序我是基于IDE自带的StringTointRGB做的修改:

接收端:
  1. #include <ZigduinoRadio.h>

  2. #include <Adafruit_NeoPixel.h>

  3. #define PIN 6

  4. // Parameter 1 = number of pixels in strip
  5. // Parameter 2 = pin number (most are valid)
  6. // Parameter 3 = pixel type flags, add together as needed:
  7. //   NEO_RGB     Pixels are wired for RGB bitstream
  8. //   NEO_GRB     Pixels are wired for GRB bitstream
  9. //   NEO_KHZ400  400 KHz bitstream (e.g. FLORA pixels)
  10. //   NEO_KHZ800  800 KHz bitstream (e.g. High Density LED strip)
  11. Adafruit_NeoPixel strip = Adafruit_NeoPixel(60, PIN, NEO_GRB + NEO_KHZ800);

  12. String inString = "";    // string to hold input
  13. int currentColor = 0;
  14. int red = 0, green = 0, blue = 0;

  15. void setup() {
  16.   ZigduinoRadio.begin(11);

  17.   // Open serial communications and wait for port to open:
  18.   Serial.begin(115200);

  19.   // send an intro:
  20.   Serial.println("\n\nString toInt() RGB:");
  21.   Serial.println();

  22.   strip.begin();
  23.   strip.show(); // Initialize all pixels to 'off'
  24. }

  25. void loop() {
  26.   int inChar;
  27.   // Read serial input:
  28.   if (ZigduinoRadio.available() > 0) {
  29.     inChar = ZigduinoRadio.read();
  30.   }

  31.   if (Serial.available() > 0) {
  32.     inChar = Serial.read();
  33.   }

  34.   if (isDigit(inChar)) {
  35.     // convert the incoming byte to a char
  36.     // and add it to the string:
  37.     inString += (char)inChar;
  38.   }

  39.   // if you get a comma, convert to a number,
  40.   // set the appropriate color, and increment
  41.   // the color counter:
  42.   if (inChar == ',') {
  43.     // do something different for each value of currentColor:
  44.     switch (currentColor) {
  45.     case 0:    // 0 = red
  46.       red = inString.toInt();
  47.       // clear the string for new input:
  48.       inString = "";
  49.       break;
  50.     case 1:    // 1 = green:
  51.       green = inString.toInt();
  52.       // clear the string for new input:
  53.       inString = "";
  54.       break;
  55.     }
  56.     currentColor++;
  57.   }
  58.   // if you get a newline, you know you've got
  59.   // the last color, i.e. blue:
  60.   if (inChar == '\n') {
  61.     blue = inString.toInt();

  62.     // set the levels of the LED.
  63.     // subtract value from 255 because a higher
  64.     colorWipe(strip.Color(red, green, blue), 10);

  65.     ZigduinoRadio.beginTransmission();
  66.     ZigduinoRadio.write("ColorWipe OK \n\r");
  67.     ZigduinoRadio.endTransmission();

  68.     // print the colors:
  69.     Serial.print("Red: ");
  70.     Serial.print(red);
  71.     Serial.print(", Green: ");
  72.     Serial.print(green);
  73.     Serial.print(", Blue: ");
  74.     Serial.println(blue);

  75.     // clear the string for new input:
  76.     inString = "";
  77.     // reset the color counter:
  78.     currentColor = 0;
  79.   }
  80. }

  81. // Fill the dots one after the other with a color
  82. void colorWipe(uint32_t c, uint8_t wait) {
  83.   for(uint16_t i=0; i<strip.numPixels(); i++) {
  84.     strip.setPixelColor(i, c);
  85.     strip.show();
  86.     delay(wait);
  87.   }
  88. }
复制代码


发送端:
  1. /*

  2. Run this sketch on two Zigduinos, open the serial monitor at 9600 baud, and type in stuff
  3. Watch the Rx Zigduino output what you've input into the serial port of the Tx Zigduino

  4. */

  5. #include <ZigduinoRadio.h>

  6. void setup()
  7. {
  8.   ZigduinoRadio.begin(11);
  9.   Serial.begin(115200);

  10. }

  11. void loop()
  12. {
  13.   if (Serial.available())
  14.   {
  15.     ZigduinoRadio.beginTransmission();

  16.     while(Serial.available())
  17.     {
  18.       char c = Serial.read();
  19.       ZigduinoRadio.write(c);
  20.     }

  21.     ZigduinoRadio.endTransmission();
  22.   }

  23.   if (ZigduinoRadio.available())
  24.   {
  25.     while(ZigduinoRadio.available())
  26.     Serial.write(ZigduinoRadio.read());
  27.   }
  28. }
复制代码



将上面程序分别下载,将WS2812模块连接到接收端的Core RF,连线非常简单:DI-D6、VCC-5V、GND-GND

打开发送端的串口监视器,发送字符”0,255,0“,回车后返回”ColorWipe OK “既是成功:


这时你可以观察到接收端的LED变成绿色:


==================华丽的分割线====================
一个一个的输入字符太麻烦,令人高兴的是,IDE自带的StringTointRGB中还提供了Processing的上位机代码。
我将其打包了出来:
http://pan.baidu.com/s/1ntx60jR

确认发送端串口没被占用,打开上位机。现在你可以通过调色盘来改变LED颜色了!




视频:

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复 支持 反对

使用道具 举报

发表于 2014-5-3 17:08:50 | 显示全部楼层
{:soso_e113:}灯光好漂亮!
回复 支持 反对

使用道具 举报

发表于 2014-5-3 19:43:24 | 显示全部楼层
好叼!!!!学习学习!!!
回复 支持 反对

使用道具 举报

发表于 2014-5-4 21:03:21 | 显示全部楼层
ATmega128RFA1 不是有7路PWM嗎?
回复 支持 反对

使用道具 举报

发表于 2014-5-4 21:18:11 | 显示全部楼层
061209 发表于 2014-5-4 21:03
ATmega128RFA1 不是有7路PWM嗎?

数据都是针对Microduino引出的列举的~
回复 支持 反对

使用道具 举报

发表于 2015-7-11 15:14:32 | 显示全部楼层
不错不错!!!
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-16 17:17 , Processed in 0.075407 second(s), 24 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表