查看: 3527|回复: 3

在pcDuino上使用8*8*8 RGB Led cube

[复制链接]

该用户从未签到

发表于 2014-3-12 10:33:11 | 显示全部楼层 |阅读模式
分享到:
一、模块介绍:
8*8*8 RGB Led cube 是我们刚设计的一款超炫RGB三色光立方模块,具有arduino兼容接口,控制接口采用SPI接口。内置几种演示模式。
二、接线图:
1.jpg
三、演示代码:
#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无效。命令发送完成后后面跟着的是点的颜色数据。

五、运行效果:
2.jpg



回复

使用道具 举报

该用户从未签到

发表于 2014-3-14 15:03:41 | 显示全部楼层

这个不错,支持一下。
回复 支持 反对

使用道具 举报

  • TA的每日心情
    奋斗
    2015-4-1 14:41
  • 签到天数: 135 天

    连续签到: 1 天

    [LV.7]常住居民III

    发表于 2014-3-17 11:01:01 | 显示全部楼层
    有没有演示视频....超炫!
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    开心
    2021-11-27 21:43
  • 签到天数: 10 天

    连续签到: 1 天

    [LV.3]偶尔看看II

    发表于 2014-3-20 14:17:31 | 显示全部楼层
    很好的idea啊~~~~~
    回复 支持 反对

    使用道具 举报

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

    本版积分规则

    关闭

    站长推荐上一条 /5 下一条

    手机版|小黑屋|与非网

    GMT+8, 2024-12-21 20:49 , Processed in 0.137453 second(s), 21 queries , MemCache On.

    ICP经营许可证 苏B2-20140176  苏ICP备14012660号-2   苏州灵动帧格网络科技有限公司 版权所有.

    苏公网安备 32059002001037号

    Powered by Discuz! X3.4

    Copyright © 2001-2024, Tencent Cloud.