SparkFun Forums 

Where electronics enthusiasts find answers.

Your source for all things Atmel.
By arthurwolf
#76590
Hello.

I just got my serial led matrix ( http://www.sparkfun.com/commerce/produc ... cts_id=760 ) and I'm trying to get it to work.

I successfully connected it and displayed stuff on it, but when it comes to animation/regular-display-updating, something strange happens.

Here is the code, it's an example that I just modified to see the problem better.
Code: Select all
#define CHIPSELECT 10//ss
#define SPICLOCK  13//sck
#define DATAOUT 11//MOSI
#define DATAIN 12//MISO

int num = 1;

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
  clr=SPSR;
  clr=SPDR;
}

void loop()		
{ 
    digitalWrite(CHIPSELECT,LOW); 
    delayMicroseconds(500);
    
    num++;
    if(num > 64){num = 0;}
    for (int i=0;i<8;i++) for (int j=0;j<8;j++)
    {
       if( num > j+(i*8) ){
         spi_transfer(char(255));
       }else{
         spi_transfer(char(0));
       }

    }
    
    digitalWrite(CHIPSELECT,HIGH); 
    delayMicroseconds(500);

     delay(1000);		
}

So the problem is : normally this code should light up one more led on the display every second.

That's not what happens, I get 4 more leds every 4 seconds ( or 8 more every 8 seconds, delay seems to add with uptime ).
Depending on the value of the delay this number changes too, but I never get the display to change EVERY time I tell it to.
It always jumps a few spi transmitions.


pin 13, used as SPIclock in the code, is also a led on the arduino, and I see it blinking every 1 second, so the arduino is doing it's job. ( but maybe not well )

I have read all forum threads about this device and have found no similar problem, or clue.

Does someone have any idea what the problem might be ?

Thanks a lot !
By arthurwolf
#76614
billroy wrote:It looks from here like you may have selected /16 instead of /128 for the SPI rate.

-br
Thanks for your answer !

I tried different values, none solved the problem, this is just the last one I tried.
To erase any error I may have done while debugging, here is the example from the sparkfun site with just two lines of code modified to have animation.


Code: Select all

#define BLACK  0
#define RED  0xE0
#define GREEN  0x1C
#define BLUE  0x03
#define ORANGE  RED|GREEN
#define MAGENTA  RED|BLUE
#define TEAL  BLUE|GREEN
#define WHITE (RED|GREEN|BLUE)-0xA0


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

char color_buffer [64]; 


void setup() {  

  SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR1);	//Enable SPI HW, Master Mode, divide clock by 16    //SPI Bus setup

  pinMode(DATAOUT, OUTPUT);
  pinMode(DATAIN, INPUT);
  pinMode(SPICLOCK,OUTPUT);
  pinMode(SLAVESELECT,OUTPUT);

  digitalWrite(SLAVESELECT,HIGH); 

} 

 

void loop() {

  color_buffer[0]=BLACK;
  color_buffer[1]=RED;
  color_buffer[2]=GREEN;
  color_buffer[3]=BLUE;
  color_buffer[4]=ORANGE;
  color_buffer[5]=MAGENTA;
  color_buffer[6]=TEAL;
  color_buffer[7]=WHITE;

  digitalWrite(SLAVESELECT, LOW);
  for(int LED=0; LED<64; LED++){
    spi_transfer(char(random(255)));
  }
  digitalWrite(SLAVESELECT, HIGH);
  
  delay(100); // allow some time for the Serial data to be sent 

} 

char spi_transfer(volatile char data){

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

  return SPDR;                    // return the received byte
}
This still does not work.

Maybe I'm trying something impossible, maybe it is not possible to update the led matrix backpack 10 times a second ?
But it sometimes works ... so I'm totally lost.

Thanks !
By billroy
#76617
I don't believe it's the delay between frames that's the problem. I have working code here that delays 50 ms between frames.

The second example you posted also has /16. It needs to be /128 to meet the 125 kHz SPI timing spec. See page 170 of the '168 data sheet and use those values for SPR1 and SPR0.

I see in my code that I delay 1 ms after pulling SS low, and also again after sending 64 SPI bytes just _before_ setting SS back high. IIRC there is stern wording in the data sheet about waiting 500 microseconds after clearing / before setting this bit; worth a double check. (Second edit: I see you have the delay in your code, but after setting SS - have you tried it before setting SS?)

Violating either timing spec will lead to unpredictable behavior, which it sounds like you have.

Hope that helps. Keep at it, when the lights start flashing you're not far from done.

-br
By arthurwolf
#76642
Tried to answer but looks like I use a forbidden word ...
Waiting for moderation.