• Home
  • pcDuino
  • WiKi
  • Store
  • Distributors
  • Home
  • pcDuino
  • WiKi
  • Store
  • Distributors
HomepcDuinoArduino-ish ProgramGPIOUse 8x8x8 RGB LED Cube on pcDuino
Previous Next

Use 8x8x8 RGB LED Cube on pcDuino

Posted by: admin , March 22, 2014

[vc_row][vc_column][vc_column_text]QQ图片20140308000344[/vc_column_text][vc_tour][vc_tab title=”Introduction” tab_id=”1395023110-1-12″][vc_column_text]8x8x8 RGB LED cube is one of the stunning RGB LED Cube designed  by LinkSprite. It has Arduino compatible interface and uses the SPI interface to communicate with Arduino/pcDuino.

WP_20140306_004[/vc_column_text][/vc_tab][vc_tab title=”Wiring Diagram” tab_id=”1395023110-2-2″][vc_column_text]WP_20140307_006

The 8x8x8 LED Cube can be directly installed on pcDuino. However, we do recommend to use a T Board to translate the voltage.[/vc_column_text][/vc_tab][vc_tab title=”Test code” tab_id=”1395024761307-2-8″][vc_column_text]

#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);

}

[/vc_column_text][/vc_tab][vc_tab title=”Descriptions of Commands” tab_id=”1395024762137-3-1″][vc_column_text]8x8x8 RGB LED cube supports  four kinds of commands. The format of the commands is below: [cmd] [x] [y] [z] [r] [g] [b] [rsv]

In the command sequence:

cmd is the command byte.

x, y, z, represents the three axis coordinate values.

r, g, b represents the three color of red, blue, green value of  corresponding coordinate , rsv is a reserved char.

Command 0x01: Set the whole LED cube into a certain color. X/Y/Z is invalid.

Command 0x02: Set a demo mode, X means demo mode X. Y and Z are invalid. t/h/b/rsv means the repeat number. There are currently three demo mode.

Command 0x03:  Set the color of a certain LED.  x, y, z is the coordinate. r, g, b is the color.

Command 0x04:  Send the data starting from (0,0,0).  x, y, z, r, g, b, rsv are invalid.  The data after the command is the data of color.

[/vc_column_text][/vc_tab][vc_tab title=”Test result” tab_id=”1395026470746-4-0″][vc_column_text]QQ图片20140308000344[/vc_column_text][/vc_tab][/vc_tour][/vc_column][/vc_row]

Tags: GPIO

Share!
Tweet

admin

About the author

Leave a Reply Cancel reply

You must be logged in to post a comment.

Category

  • Home
  • pcDuino
  • WiKi
  • Store
  • Distributors