Pages: [1] 2 3   Go Down
Author Topic: SPI: Arduino and Sparkfun RGB Matrix Backpack  (Read 3087 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 5
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi everyone, just thought I'd share this with you:
I bought an RGB matrix & backpack from Sparkfun which acts as an SPI slave device.  I wrote a chunk of arduino code that speaks to it the right way.

Here it is, so enjoy!

~Daniel~

Code:
// Simple program to test SPI with the RGB Matrix & Backpack from Sparkfun.
// Code is a combination of Heather Dewey-Hagborg, Arduino Forum user: Little-Scale, and Daniel Hirschmann
// Enjoy!

// The Backpack requires 125Khz SPI -> which is the slowest rate at which the Arduino's hardware SPI bus can communicate at.
// We need to send SPI to the backpack in the following steps:
// 1) Activate ChipSelect;
// 2) Wait 500microseconds;
// 3) Transfer 64bytes @ 125KHz (1 byte for each RGB LED in the matrix);
// 4) De-activate ChipSelect;
// 5) Wait 500microseconds
// Repeat however often you like!


#define CHIPSELECT 10//ss
#define SPICLOCK  13//sck
#define DATAOUT 11//MOSI
#define DATAIN 12//MISO

char spi_transfer(volatile char data)
{
  SPDR = data;                    // Start the transmission
  while (!(SPSR & (1<<SPIF)))     // Wait the end of the transmission
  {
  };
}

void setup()
{
  byte clr;
  pinMode(DATAOUT,OUTPUT);
  pinMode(SPICLOCK,OUTPUT);
  pinMode(CHIPSELECT,OUTPUT);
  digitalWrite(CHIPSELECT,HIGH); //disable device

  SPCR = B01010001;             //SPI Registers
  SPSR = SPSR & B11111110;      //make sure the speed is 125KHz

  /*
  SPCR bits:
   7: SPIEE - enables SPI interrupt when high
   6: SPE - enable SPI bus when high
   5: DORD - LSB first when high, MSB first when low
   4: MSTR - arduino is in master mode when high, slave when low
   3: CPOL - data clock idle when high if 1, idle when low if 0
   2: CPHA - data on falling edge of clock when high, rising edge when low
   1: SPR1 - set speed of SPI bus
   0: SPR0 - set speed of SPI bus (00 is fastest @ 4MHz, 11 is slowest @ 250KHz)
   */

  clr=SPSR;
  clr=SPDR;
  delay(10);
}

void loop()            
{
    delay(100);                  
    digitalWrite(CHIPSELECT,LOW); // enable the ChipSelect on the backpack
    delayMicroseconds(500);
    for (int i=0;i<8;i++) for (int j=0;j<8;j++)
    {
      spi_transfer(i);          //There are only 8 colours available to the matrix with the backpack, so this will present 1 colour per column on the matrix
    }
    digitalWrite(CHIPSELECT,HIGH); // disable the ChipSelect on the backpack
    delayMicroseconds(500);
}


Logged

Daniel
Guest
 Bigger Bigger  Smaller Smaller  Reset Reset

nice

I added it to the Playground, here

D
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I just bought the RG Matrix (http://www.sparkfun.com/commerce/product_info.php?products_id=759) and I'm trying out this code.  For testing purposes I changed the spi_transfer to spi_transfer(1); which should make the entire matrix go red, but when I run the program I get random red dots flashing all over the place.  Any ideas?
Logged

Chicago &#124; IL
Offline Offline
Newbie
*
Karma: 0
Posts: 23
Arduino Nerd
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I've had the same issue with this - The RG Backpack works - and I get the quick test - but then it goes to random flashes of LEDs.

Is there a different way to int and reg the RG display over the RGB?

Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 23
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

 smiley i have two of these rgb back packsa and can get them to scan red to blue or any of the seven colours i choose but would like to know how to display characters e.g, the letter A.

Any help would be much appreciated smiley
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 23
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi Just found this :

// Simple program to test using the Arduino with the RGB Matrix
// & Backpack from Sparkfun. Code is a combination of Heather Dewey-Hagborg,
// Arduino Forum user: Little-Scale, and // Daniel Hirschmann. Enjoy!
//
// The Backpack requires 125Khz SPI, which is the slowest rate
// at which the Arduino's hardware SPI bus can communicate at.
//
// We need to send SPI to the backpack in the following steps:
// 1) Activate ChipSelect;
// 2) Wait 500microseconds;
// 3) Transfer 64bytes @ 125KHz (1 byte for each RGB LED in the matrix);
// 4) De-activate ChipSelect;
// 5) Wait 500microseconds
// Repeat however often you like!


#define CHIPSELECT 10//ss
#define SPICLOCK  13//sck
#define DATAOUT 11//MOSI / DI
#define DATAIN 12//MISO / DO

int data[] =
{0,0,0,0,0,0,0,0,
0,0,1,1,0,1,1,0,
0,1,0,0,1,0,0,1,
0,1,0,0,0,0,0,1,
0,0,1,0,0,0,1,0,
0,0,0,1,0,1,0,0,
0,0,0,0,1,0,0,0,
0,0,0,0,0,0,0,0
}[glow]
char spi_transfer(volatile char data)[/glow]
{
  SPDR = data;                    // Start the transmission
  while (!(SPSR & (1<<SPIF)))     // Wait the end of the transmission
  {
  };
}

void setup()
{
  byte clr;
  pinMode(DATAOUT,OUTPUT);
  pinMode(SPICLOCK,OUTPUT);
  pinMode(CHIPSELECT,OUTPUT);
  digitalWrite(CHIPSELECT,HIGH); //disable device

  SPCR = B01010001;             //SPI Registers
  SPSR = SPSR & B11111110;      //make sure the speed is 125KHz

  /*
  SPCR bits:
   7: SPIEE - enables SPI interrupt when high
   6: SPE - enable SPI bus when high
   5: DORD - LSB first when high, MSB first when low
   4: MSTR - arduino is in master mode when high, slave when low
   3: CPOL - data clock idle when high if 1, idle when low if 0
   2: CPHA - data on falling edge of clock when high, rising edge when low
   1: SPR1 - set speed of SPI bus
   0: SPR0 - set speed of SPI bus (00 is fastest @ 4MHz, 11 is slowest @ 250KHz)
   */

  clr=SPSR;
  clr=SPDR;
  delay(10);
}

void loop()            
{
    delay(100);
    int index = 0;                  
    digitalWrite(CHIPSELECT,LOW); // enable the ChipSelect on the backpack
    delayMicroseconds(500);
    for (int i=0;i<8;i++) for (int j=0;j<8;j++)
    {
      spi_transfer(data[index]);
        index++;                   
// There are only 8 colours available to the matrix with the
// backpack, so this will present 1 colour per column on the matrix
    }
    digitalWrite(CHIPSELECT,HIGH); // disable the ChipSelect on the backpack
    delayMicroseconds(500);
}

i copy it into my arduino but it comes up with :
error: expected ',' or ';' before 'char'

Can some one help smiley

Thanks
Logged

0
Offline Offline
Faraday Member
**
Karma: 7
Posts: 2526
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Well, you could try sticking a ; before the char - specifically, after the int data[] = {....}

-j

Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 23
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

yea that worked thanks alot smiley
Logged

Paris
Offline Offline
Newbie
*
Karma: 0
Posts: 10
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,

I have an arduino Duemilanove with atmega328 and two of the backkpacks from sparkfun. I started to play with it.
As cubtastic wrote, the piece of code in the Arduino playground is not working correctly as I get some random display.
I think this random display also changed the daisy chain config of my test backpack.

I modified the existing code of the test program and here is a piece of code working for me.

For info pin 12 is not used and pin 10 is to connect to cs on the backpack.

I'm french so sorry for the mistakes. And I'm not a great programmer so comments are welcome.

Code:
// Simple program to test using the Arduino with the RGB Matrix
// & Backpack from Sparkfun. Code is a combination of Heather Dewey-Hagborg,
// Arduino Forum user: Little-Scale, and // Daniel Hirschmann. Enjoy!
//
// The Backpack requires 125Khz SPI, which is the slowest rate
// at which the Arduino's hardware SPI bus can communicate at.
//
// We need to send SPI to the backpack in the following steps:
// 1) Activate ChipSelect;
// 2) Wait 500microseconds;
// 3) Transfer 64bytes @ 125KHz (1 byte for each RGB LED in the matrix);
// 4) De-activate ChipSelect;
// 5) Wait 500microseconds
// Repeat however often you like!

#define CHIPSELECT 10//ss
#define SPICLOCK  13//sck
#define DATAOUT 11//MOSI
#define DATAIN 12//MISO

#define RED      0xE0 // For code readability.
#define GREEN      0x1C // Only 8 colors available
#define BLUE      0x07 // but variation possible with
#define ORANGE  0xFC // brightness (see Sparkfun doc).
#define MAGENTA      0xE3
#define TEAL      0x1F
#define WHITE      0xFF
#define BLACK      0x00

byte color_tst_tbl[] = {0xE0,0x1C,0x07,0xFC,0xE3,0x1F,0xFF,0xE0}; // Table of color codes, one color duplicated to get 8 entries for test purpose.

byte pattern[] = { // Test pattern to be displayed on the matrix, entries in binary for learning purpose.
B11111111,         // So you can see which led is on (1) or off (0=BLACK) on your pattern.
B00000000,         // Just change it to test your own patterns.
B11111111,         // We can also use hex notation: B11111111 is 0xFF in hexa
B00000000,
B11111111,
B00000000,
B10101010,
B01010101  
};

char spi_transfer(volatile char data)
{
  SPDR = data;                    // Start the transmission
  while (!(SPSR & (1<<SPIF)))     // Wait the end of the transmission
  {
  };
}

void setup()
{
  byte clr;
  pinMode(DATAOUT,OUTPUT);
  pinMode(SPICLOCK,OUTPUT);
  pinMode(CHIPSELECT,OUTPUT);
  digitalWrite(CHIPSELECT,HIGH); //disable device

  SPCR = B01010001;             //SPI Registers
  SPSR = SPSR & B11111110;      //make sure the speed is 125KHz

  /*
  SPCR bits:
   7: SPIEE - enables SPI interrupt when high
   6: SPE - enable SPI bus when high
   5: DORD - LSB first when high, MSB first when low
   4: MSTR - arduino is in master mode when high, slave when low
   3: CPOL - data clock idle when high if 1, idle when low if 0
   2: CPHA - data on falling edge of clock when high, rising edge when low
   1: SPR1 - set speed of SPI bus
   0: SPR0 - set speed of SPI bus (00 is fastest @ 4MHz, 11 is slowest @ 250KHz)
   */

  clr=SPSR;
  clr=SPDR;
  delay(10);
}

void loop()            
{
   delay(100);
   int idx = 0;
   digitalWrite(CHIPSELECT,LOW); // enable the ChipSelect on the backpack
   delayMicroseconds(500);
   for (int i=0;i<8;i++)      
    {
      for (int j=0;j<8;j++)
      {
        if (bitRead(pattern[i],j) == 0) //bitRead funcion is used to get each 8 bit of the 8 bytes pattern table (64 bits in total).
          { spi_transfer(BLACK);}       // bit is 0 -> Transfer Black color.
        else
          { spi_transfer(color_tst_tbl[j]);} // bit is 1 -> Transfer a color choosen in the color test table.
        idx++;
      }                      
   }   // 64 bytes should have been transfered to the RGB matrix.
   digitalWrite(CHIPSELECT,HIGH); // disable the ChipSelect on the backpack
   // What you've send is displayed on the matrix.
   delayMicroseconds(500);
}

I also checked the backpack documentation for the daisy chain feature and amended the program to easily change their daisy chain config.
Each backpack must be configured individually (not chained to others) !
I encourage you to check the backpack doc, there is a pdf at Sparkfun (see link on the playground).

Code:
// Simple program to configure daisy chain for the Sparkfun RGB Matrix using the Arduino
// Each backpack must be configured separately (not physically chained) !! (See Sparkfun backpack doc)
// Code is a combination of Heather Dewey-Hagborg,
// Arduino Forum user: Little-Scale, and // Daniel Hirschmann. Enjoy!
//
// The Backpack requires 125Khz SPI, which is the slowest rate
// at which the Arduino's hardware SPI bus can communicate at.
//
// We need to send SPI to the backpack in the following steps:
// 1) Activate ChipSelect;
// 2) Wait 500microseconds;
// 3) Transfer 2 bytes: char % (0x25 in hex) and a configuration number: 1 to 8 (1 means standalone backpack, 2 means daisy chain of two backpacks)
//    Plesae change it accordingly in this program.
// 4) De-activate ChipSelect;
// 5) Wait 500microseconds

#define CHIPSELECT 10//ss
#define SPICLOCK  13//sck
#define DATAOUT 11//MOSI
#define DATAIN 12//MISO

char spi_transfer(volatile char data)
{
  SPDR = data;                    // Start the transmission
  while (!(SPSR & (1<<SPIF)))     // Wait the end of the transmission
  {
  };
}

void setup()
{
  byte clr;
  pinMode(DATAOUT,OUTPUT);
  pinMode(SPICLOCK,OUTPUT);
  pinMode(CHIPSELECT,OUTPUT);
  digitalWrite(CHIPSELECT,HIGH); //disable device

  SPCR = B01010001;             //SPI Registers
  SPSR = SPSR & B11111110;      //make sure the speed is 125KHz

  /*
  SPCR bits:
   7: SPIEE - enables SPI interrupt when high
   6: SPE - enable SPI bus when high
   5: DORD - LSB first when high, MSB first when low
   4: MSTR - arduino is in master mode when high, slave when low
   3: CPOL - data clock idle when high if 1, idle when low if 0
   2: CPHA - data on falling edge of clock when high, rising edge when low
   1: SPR1 - set speed of SPI bus
   0: SPR0 - set speed of SPI bus (00 is fastest @ 4MHz, 11 is slowest @ 250KHz)
   */

  clr=SPSR;
  clr=SPDR;
  delay(10);
}

void loop()            
{
   delay(100);
   int idx = 0;  
   digitalWrite(CHIPSELECT,LOW); // enable the ChipSelect on the backpack
   delayMicroseconds(500);
  
   spi_transfer(0x25); // transfer the escape code %
   spi_transfer(1); // transfer the number of matrix in the daisy chaine (1 for standalone, 2 to 8 for a daisy chain)

   digitalWrite(CHIPSELECT,HIGH); // disable the ChipSelect on the backpack
   delayMicroseconds(500);
}
Logged

France
Offline Offline
Sr. Member
****
Karma: 0
Posts: 262
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Has anyone tried to bootload the onboard ATmega168 with the Arduino bootloader to run a program standalone?

(As the original firmware is not available, I don't suggest to do it, but wanted to know if it's possible or already done...)
« Last Edit: August 27, 2009, 10:05:03 am by tep » Logged

Paris
Offline Offline
Newbie
*
Karma: 0
Posts: 10
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@tep: Hello tep, perhaps you should post in the hardware/development section of this forum. I cannot reply to your question as I'm not a "PCB maker". Perhaps be more precise in describing what you want to do too.

Concerning my previous post:
Seems Sparkfun modified its RGB bakpack and the 2 I bought are version 2(?). They have two 6pin connectors. (SPI input and SPI output). [See the product comments at Sparkfun website]

I managed to drive one standalone with my Arduino Duemilanove but did not manage to make the daisy chained feature to work correctly.

Following the procedure in the dcumentation don't seem to work. I found a post saying you have to send the escape commmand before every 64 byte frame when using daisy chain and tested it. It was better but one frame (last of the chain) is not correctly transmitted.
(there is a shift and some noise too)
I will try to contact Sparkfun support.

I saw several backpacks can be controlled separately by using a separate CS wire for each. Will try to test it.
Logged

France
Offline Offline
Sr. Member
****
Karma: 0
Posts: 262
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@LaurentR: No problem, that was only to know if somebody did this!
Actually, looking at the schematic, nothing new, it should be fairly simple!
That should allow some simple electret based VU-meter or such.
Hm, have to think over it when I finish my current projects!
Logged

Paris
Offline Offline
Newbie
*
Karma: 0
Posts: 10
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello again,
Another addition concerning coding for 8x8 displays.
Richard Prinz wrote a cool program to rapidly get font description in your code and design new symbols. This program is intended to people playing with displays on embedded systems.
With this program called "8x8 Pixel ROM Font Editor", you can export the font in .asm text file and get an 8 bytes table description for each caracter. (Note the program comes with a library of classic fonts).

Here is the link (with the permission of the author), the program licence type is Creative Commons (see website).

http://www.min.at/prinz/software/pixelfont/

Here is an example of an 8 bytes description for one character:

Code:
byte happy_face[]={0x00, 0x66, 0x66, 0x00, 0x00, 0x42, 0x3C, 0x00};

Don't hesitate to post on the author site the new fonts or symbol libraries you create !
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 13
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

I tried to chain four of the 8x8 LED Matrix with backpacks. After configuring each of them (not even sure if I did right), does anyone have some simple example code how to send data to four of them?

Thanks!

Elim
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 13
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This what I did for configuration for each of the backpack, please point out if there's anything wrong. Becuase after upload the code from arduino to the backpack, nothing happened. (is this correct?)

I copy the code from previous post

// Simple program to configure daisy chain for the Sparkfun RGB Matrix using the Arduino
// Each backpack must be configured separately (not physically chained) !! (See Sparkfun backpack doc)
// Code is a combination of Heather Dewey-Hagborg,
// Arduino Forum user: Little-Scale, and // Daniel Hirschmann. Enjoy!
//
// The Backpack requires 125Khz SPI, which is the slowest rate
// at which the Arduino's hardware SPI bus can communicate at.
//
// We need to send SPI to the backpack in the following steps:
// 1) Activate ChipSelect;
// 2) Wait 500microseconds;
// 3) Transfer 2 bytes: char % (0x25 in hex) and a configuration number: 1 to 8 (1 means standalone backpack, 2 means daisy chain of two backpacks)
//    Plesae change it accordingly in this program.
// 4) De-activate ChipSelect;
// 5) Wait 500microseconds

#define CHIPSELECT 10//ss
#define SPICLOCK  13//sck
#define DATAOUT 11//MOSI
#define DATAIN 12//MISO

char spi_transfer(volatile char data)
{
  SPDR = data;                    // Start the transmission
  while (!(SPSR & (1<<SPIF)))     // Wait the end of the transmission
  {
  };
}

void setup()
{
  byte clr;
  pinMode(DATAOUT,OUTPUT);
  pinMode(SPICLOCK,OUTPUT);
  pinMode(CHIPSELECT,OUTPUT);
  digitalWrite(CHIPSELECT,HIGH); //disable device

  SPCR = B01010001;             //SPI Registers
  SPSR = SPSR & B11111110;      //make sure the speed is 125KHz

  /*
  SPCR bits:
   7: SPIEE - enables SPI interrupt when high
   6: SPE - enable SPI bus when high
   5: DORD - LSB first when high, MSB first when low
   4: MSTR - arduino is in master mode when high, slave when low
   3: CPOL - data clock idle when high if 1, idle when low if 0
   2: CPHA - data on falling edge of clock when high, rising edge when low
   1: SPR1 - set speed of SPI bus
   0: SPR0 - set speed of SPI bus (00 is fastest @ 4MHz, 11 is slowest @ 250KHz)
   */

  clr=SPSR;
  clr=SPDR;
  delay(10);
}

void loop()            
{
   delay(100);
   int idx = 0;  
   digitalWrite(CHIPSELECT,LOW); // enable the ChipSelect on the backpack
   delayMicroseconds(500);
  
   spi_transfer(0x25); // transfer the escape code %
   spi_transfer(1); // transfer the number of matrix in the daisy chaine (1 for standalone, 2 to 8 for a daisy chain)

   digitalWrite(CHIPSELECT,HIGH); // disable the ChipSelect on the backpack
   delayMicroseconds(500);
}
 



And chenged the number 1 to number 4 for all of the LED because I want to chain 4 LED backpacks:
spi_transfer(1);


How do I know if I configure successful or not?

Thanks!

Elim



Logged

Pages: [1] 2 3   Go Up
Jump to: