内嵌Zigbee协议的Arduino--Microduino Core RF初探之:无线控制RGB灯
本帖最后由 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 NameMap Pin NameDigital PinAnalog PininterruptPWMSerialSPII2CPower
VCC+5V+5V
VCC+3V3+3.3V
(OC0A/OC1C)PB7D7D7yes
(OC1B)PB6D8D8yes
(OC1A)PB5D9D9yes
(OC2A/SS)PB4D10D10yesSS
(MOSI/PDI)PB2D11D11MOSI
(MISO/PDO)PB3D12D12MISO
(SCK)PB1D13D13SCK
AREFAREF
(ADC7)PF7A0D14A0
(ADC6)PF6A1D15A1
(ADC5)PF5A2D16A2
(ADC4)PF4A3D17A3
(SDA)PD1SDAD181SDA
(SCL)PD0SCLD190SCL
(ADC3)PF3A6D20A6
(ADC2)PF2A7D21A7
(RXD0)PE0RX0D00(RX)
(TXD0)PE1TX1D10(TX)
(RXD1)PD2D2D221(RX)
(TXD1)PD3D3D331(TX)
(OC3A)PE3D4D4yes
(OC3B)PE4D5D54yes
(OC3C)PE5D6D65yes
RESETRST
GNDGNDGND
然后你需要下载硬件支持包,覆盖到Arduino IDE(推荐1.05版本)安装目录即可。
http://pan.baidu.com/s/1pJHfkc7
上面的支持包里包含了ZigduinoRadio这个库,我们就用这个库的示例程序“ZigduinoRadioExample”来展开测试:
/*
Run this sketch on two Zigduinos, open the serial monitor at 9600 baud, and type in stuff
Watch the Rx Zigduino output what you've input into the serial port of the Tx Zigduino
*/
#include <ZigduinoRadio.h>
void setup()
{
ZigduinoRadio.begin(11);
Serial.begin(9600);
ZigduinoRadio.attachError(errHandle);
ZigduinoRadio.attachTxDone(onXmitDone);
}
void loop()
{
if (Serial.available())
{
ZigduinoRadio.beginTransmission();
Serial.println();
Serial.print("Tx: ");
while(Serial.available())
{
char c = Serial.read();
Serial.write(c);
ZigduinoRadio.write(c);
}
Serial.println();
ZigduinoRadio.endTransmission();
}
if (ZigduinoRadio.available())
{
Serial.println();
Serial.print("Rx: ");
while(ZigduinoRadio.available())
Serial.write(ZigduinoRadio.read());
Serial.println();
Serial.print("LQI: ");
Serial.print(ZigduinoRadio.getLqi(), 10);
Serial.print(", RSSI: ");
Serial.print(ZigduinoRadio.getLastRssi(), 10);
Serial.print(" dBm, ED: ");
Serial.print(ZigduinoRadio.getLastEd(), 10);
Serial.println("dBm");
}
delay(100);
}
void errHandle(radio_error_t err)
{
Serial.println();
Serial.print("Error: ");
Serial.print((uint8_t)err, 10);
Serial.println();
}
void onXmitDone(radio_tx_done_t x)
{
Serial.println();
Serial.print("TxDone: ");
Serial.print((uint8_t)x, 10);
Serial.println();
}
这个程序实现了最基本的无线串口透传功能。
因为Core RF采用串口下载,所以你先要叠加一个FT232:
板卡选择Microduino Core RF
分别给两块Core RF下载好程序以后,打开串口监视器,发送“Hello Microduino!”
可以看到如图结果:
最基本的硬件试通了,下面一楼,就该介绍如何无线控制RGB的LED了~ 本帖最后由 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做的修改:
接收端:
#include <ZigduinoRadio.h>
#include <Adafruit_NeoPixel.h>
#define PIN 6
// Parameter 1 = number of pixels in strip
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
// NEO_RGB Pixels are wired for RGB bitstream
// NEO_GRB Pixels are wired for GRB bitstream
// NEO_KHZ400400 KHz bitstream (e.g. FLORA pixels)
// NEO_KHZ800800 KHz bitstream (e.g. High Density LED strip)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(60, PIN, NEO_GRB + NEO_KHZ800);
String inString = ""; // string to hold input
int currentColor = 0;
int red = 0, green = 0, blue = 0;
void setup() {
ZigduinoRadio.begin(11);
// Open serial communications and wait for port to open:
Serial.begin(115200);
// send an intro:
Serial.println("\n\nString toInt() RGB:");
Serial.println();
strip.begin();
strip.show(); // Initialize all pixels to 'off'
}
void loop() {
int inChar;
// Read serial input:
if (ZigduinoRadio.available() > 0) {
inChar = ZigduinoRadio.read();
}
if (Serial.available() > 0) {
inChar = Serial.read();
}
if (isDigit(inChar)) {
// convert the incoming byte to a char
// and add it to the string:
inString += (char)inChar;
}
// if you get a comma, convert to a number,
// set the appropriate color, and increment
// the color counter:
if (inChar == ',') {
// do something different for each value of currentColor:
switch (currentColor) {
case 0: // 0 = red
red = inString.toInt();
// clear the string for new input:
inString = "";
break;
case 1: // 1 = green:
green = inString.toInt();
// clear the string for new input:
inString = "";
break;
}
currentColor++;
}
// if you get a newline, you know you've got
// the last color, i.e. blue:
if (inChar == '\n') {
blue = inString.toInt();
// set the levels of the LED.
// subtract value from 255 because a higher
colorWipe(strip.Color(red, green, blue), 10);
ZigduinoRadio.beginTransmission();
ZigduinoRadio.write("ColorWipe OK \n\r");
ZigduinoRadio.endTransmission();
// print the colors:
Serial.print("Red: ");
Serial.print(red);
Serial.print(", Green: ");
Serial.print(green);
Serial.print(", Blue: ");
Serial.println(blue);
// clear the string for new input:
inString = "";
// reset the color counter:
currentColor = 0;
}
}
// Fill the dots one after the other with a color
void colorWipe(uint32_t c, uint8_t wait) {
for(uint16_t i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, c);
strip.show();
delay(wait);
}
}
发送端:
/*
Run this sketch on two Zigduinos, open the serial monitor at 9600 baud, and type in stuff
Watch the Rx Zigduino output what you've input into the serial port of the Tx Zigduino
*/
#include <ZigduinoRadio.h>
void setup()
{
ZigduinoRadio.begin(11);
Serial.begin(115200);
}
void loop()
{
if (Serial.available())
{
ZigduinoRadio.beginTransmission();
while(Serial.available())
{
char c = Serial.read();
ZigduinoRadio.write(c);
}
ZigduinoRadio.endTransmission();
}
if (ZigduinoRadio.available())
{
while(ZigduinoRadio.available())
Serial.write(ZigduinoRadio.read());
}
}
将上面程序分别下载,将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颜色了!
视频:
http://player.youku.com/player.php/sid/XNzA3MDU5NTEy/v.swf {:soso_e113:}灯光好漂亮! 好叼!!!!学习学习!!! ATmega128RFA1 不是有7路PWM嗎? 061209 发表于 2014-5-4 21:03 static/image/common/back.gif
ATmega128RFA1 不是有7路PWM嗎?
数据都是针对Microduino引出的列举的~ 不错不错!!!
页:
[1]