一、模块介绍:
8*8*8 RGB Led cube 是我们刚设计的一款超炫RGB三色光立方模块,具有arduino兼容接口,控制接口采用SPI接口。内置几种演示模式。
二、接线图:
三、演示代码:
#include <SPI.h>
#define STATUS_PIN 9
#define CS_PIN 10
#define MOSI_PIN 12
#define MISO_PIN 11
#define SCK_PIN 13
#define SPI_MODE SPI_MODE1
#ifdef PCDUINO_IDE
#define SpiExchangeByte(x) SPI.transfer(x,SPI_CONTINUE)
#else
#define SpiExchangeByte(x) SPI.transfer(x)
#endif
void SetDefaultTask(char task,long repeatTime)
{
bool DeviceStatus;
unsigned short count;
DeviceStatus = digitalRead(STATUS_PIN);
digitalWrite(CS_PIN,LOW);
SpiExchangeByte(2);
while(digitalRead(STATUS_PIN) == DeviceStatus);
DeviceStatus = !DeviceStatus;
SpiExchangeByte(task);//X
while(digitalRead(STATUS_PIN) == DeviceStatus);
DeviceStatus = !DeviceStatus;
SpiExchangeByte(0);//Y
while(digitalRead(STATUS_PIN) == DeviceStatus);
DeviceStatus = !DeviceStatus;
SpiExchangeByte(0);//Z
while(digitalRead(STATUS_PIN) == DeviceStatus);
DeviceStatus = !DeviceStatus;
SpiExchangeByte(repeatTime & 0xff);//R
while(digitalRead(STATUS_PIN) == DeviceStatus);
DeviceStatus = !DeviceStatus;
SpiExchangeByte((repeatTime>>8) & 0xff);//G
while(digitalRead(STATUS_PIN) == DeviceStatus);
DeviceStatus = !DeviceStatus;
SpiExchangeByte((repeatTime>>16) & 0xff);//B
while(digitalRead(STATUS_PIN) == DeviceStatus);
DeviceStatus = !DeviceStatus;
SpiExchangeByte((repeatTime>>24) & 0xff);//RSV
while(digitalRead(STATUS_PIN) == DeviceStatus);
digitalWrite(CS_PIN,HIGH);
}
void SetLedCubeColor(long color)
{
bool DeviceStatus;
unsigned short count;
DeviceStatus = digitalRead(STATUS_PIN);
digitalWrite(CS_PIN,LOW);
SpiExchangeByte(1);
while(digitalRead(STATUS_PIN) == DeviceStatus);
DeviceStatus = !DeviceStatus;
SpiExchangeByte(0);//X
while(digitalRead(STATUS_PIN) == DeviceStatus);
DeviceStatus = !DeviceStatus;
SpiExchangeByte(0);//Y
while(digitalRead(STATUS_PIN) == DeviceStatus);
DeviceStatus = !DeviceStatus;
SpiExchangeByte(0);//Z
while(digitalRead(STATUS_PIN) == DeviceStatus);
DeviceStatus = !DeviceStatus;
SpiExchangeByte(color & 0xff);//R
while(digitalRead(STATUS_PIN) == DeviceStatus);
DeviceStatus = !DeviceStatus;
SpiExchangeByte((color>>8) & 0xff);//G
while(digitalRead(STATUS_PIN) == DeviceStatus);
DeviceStatus = !DeviceStatus;
SpiExchangeByte((color>>16) & 0xff);//B
while(digitalRead(STATUS_PIN) == DeviceStatus);
DeviceStatus = !DeviceStatus;
SpiExchangeByte((color>>24) & 0xff);//RSV
while(digitalRead(STATUS_PIN) == DeviceStatus);
digitalWrite(CS_PIN,HIGH);
}
void SetPonitColor(char x,char y,char z,long color)
{
bool DeviceStatus;
unsigned short count;
DeviceStatus = digitalRead(STATUS_PIN);
digitalWrite(CS_PIN,LOW);
SpiExchangeByte(3);
while(digitalRead(STATUS_PIN) == DeviceStatus);
DeviceStatus = !DeviceStatus;
SpiExchangeByte(x);//X
while(digitalRead(STATUS_PIN) == DeviceStatus);
DeviceStatus = !DeviceStatus;
SpiExchangeByte(y);//Y
while(digitalRead(STATUS_PIN) == DeviceStatus);
DeviceStatus = !DeviceStatus;
SpiExchangeByte(z);//Z
while(digitalRead(STATUS_PIN) == DeviceStatus);
DeviceStatus = !DeviceStatus;
SpiExchangeByte(color & 0xff);//R
while(digitalRead(STATUS_PIN) == DeviceStatus);
DeviceStatus = !DeviceStatus;
SpiExchangeByte((color>>8) & 0xff);//G
while(digitalRead(STATUS_PIN) == DeviceStatus);
DeviceStatus = !DeviceStatus;
SpiExchangeByte((color>>16) & 0xff);//B
while(digitalRead(STATUS_PIN) == DeviceStatus);
DeviceStatus = !DeviceStatus;
SpiExchangeByte((color>>24) & 0xff);//RSV
while(digitalRead(STATUS_PIN) == DeviceStatus);
digitalWrite(CS_PIN,HIGH);
}
void SendImageData(void)
{
unsigned short tmpShort;
bool DeviceStatus;
unsigned short count;
DeviceStatus = digitalRead(STATUS_PIN);
digitalWrite(CS_PIN,LOW);
SpiExchangeByte(4);
while(digitalRead(STATUS_PIN) == DeviceStatus);
DeviceStatus = !DeviceStatus;
SpiExchangeByte(0);//X
while(digitalRead(STATUS_PIN) == DeviceStatus);
DeviceStatus = !DeviceStatus;
SpiExchangeByte(0);//Y
while(digitalRead(STATUS_PIN) == DeviceStatus);
DeviceStatus = !DeviceStatus;
SpiExchangeByte(0);//Z
while(digitalRead(STATUS_PIN) == DeviceStatus);
DeviceStatus = !DeviceStatus;
SpiExchangeByte(0);//R
while(digitalRead(STATUS_PIN) == DeviceStatus);
DeviceStatus = !DeviceStatus;
SpiExchangeByte(0);//G
while(digitalRead(STATUS_PIN) == DeviceStatus);
DeviceStatus = !DeviceStatus;
SpiExchangeByte(0);//B
while(digitalRead(STATUS_PIN) == DeviceStatus);
DeviceStatus = !DeviceStatus;
SpiExchangeByte(0);//RSV
while(digitalRead(STATUS_PIN) == DeviceStatus);
DeviceStatus = !DeviceStatus;
for(tmpShort =0;tmpShort < 512*3;tmpShort ++)
{
SpiExchangeByte((char)tmpShort);
while(digitalRead(STATUS_PIN) == DeviceStatus);
DeviceStatus = !DeviceStatus;
}
digitalWrite(CS_PIN,HIGH);
}
void setup() {
// put your setup code here, to run once:
pinMode(STATUS_PIN, INPUT);
pinMode(CS_PIN, OUTPUT);
pinMode(MOSI_PIN, OUTPUT);
pinMode(MISO_PIN, INPUT);
pinMode(SCK_PIN, OUTPUT);
digitalWrite(CS_PIN,HIGH);
digitalWrite(STATUS_PIN,HIGH);
digitalWrite(SCK_PIN,LOW);
SPI.begin();
SPI.setBitOrder(MSBFIRST);
SPI.setDataMode(SPI_MODE);
SPI.setClockDivider(SPI_CLOCK_DIV4);
SetDefaultTask(0,15);
SetLedCubeColor(0x00000000);
SendImageData();
delay(5000);
}
void loop() {
// put your main code here, to run repeatedly:
printf("SetLedCubeColor(R);\n");
SetLedCubeColor(0x00ff0000);
delay(1000);
printf("SetLedCubeColor(G);\n");
SetLedCubeColor(0x0000ff00);
delay(1000);
printf("SetLedCubeColor(B);\n");
SetLedCubeColor(0x000000ff);
delay(1000);
printf("SetDefaultTask(1);\n");
SetDefaultTask(1,20);
delay(5000);
printf("SetDefaultTask(2);\n");
SetDefaultTask(2,15);
delay(5000);
printf("SetDefaultTask(3);\n");
SetDefaultTask(3,15);
delay(5000);
}
四、模块命令介绍:
模块目前支持4种命令。命令格式为【cmd】【x】【y】【z】【r】【g】【b】【rsv】 其中cmd为命令,X,Y,Z分别代表三个轴的坐标值,r,g,b分别代表该坐标的红绿蓝三个颜色值,rsv为保留字节。 命令0×01:将整个光立方设置成某种颜色,X,Y,Z无效。 命令0×02:设置演示模式,X代表模式,Y,Z无效,r,g,b,rsv代表循环次数。目前3种演示模式。 命令0×03:设置某个点的颜色,X,Y,Z分别代表三个轴的坐标,r,g,b分别代表该坐标的红绿蓝三个颜色值。 命令0×04:从坐标(0,0,0)开始发送数据,,X,Y,Z,r,g,b,rsv无效。命令发送完成后后面跟着的是点的颜色数据。
五、运行效果:
|