SPI problem with led matrix

Hello.

I just got my serial led matrix ( http://www.sparkfun.com/commerce/product_info.php?products_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.

#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 !

No ideas ??

Can’t you try a simpler test, like addressing the same LED again and again, or using the demo code on the sparkfun site ?

At this time of the day I’m having a hard time to understand what your nested for loops are doing. And there seem to be a few {} missing too. Even if this “for (int i=0;i<8;i++) for (int j=0;j<8;j++) { … }” should be valid code, it looks strange.

Are you sure that 0 and 255 are valid pixel colors at all? I think I remember that the sparkfun spi backpack used quite strange numbers for the 7 colors.

Thanks for your answer !

Can’t you try a simpler test, like addressing the same LED again and again, or using the demo code on the sparkfun site ?

The problem occurs only when doing some kind of animation, static display works fine.
The problem is when updating frames, some are dropped.

Here is the code from sparkfun, just modified to get animation :

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

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

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

  digitalWrite(SLAVESELECT,HIGH); 
} 

void loop() {
  delay(50);
  
  digitalWrite(SLAVESELECT, LOW);
  delayMicroseconds(500);
  for(int LED=0; LED<64; LED++){
    spi_transfer(char(random(100,110)));
  }
  delayMicroseconds(500);
  digitalWrite(SLAVESELECT, HIGH);

  delay(50);
} 

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 ( frames are dropped )

At this time of the day I’m having a hard time to understand what your nested for loops are doing. And there seem to be a few {} missing too. Even if this “for (int i=0;i<8;i++) for (int j=0;j<8;j++) { … }” should be valid code, it looks strange.

Changed it. It just a 0 … 63 loop that sets pixels to random colors ( so that I get some kind of animation ).

Are you sure that 0 and 255 are valid pixel colors at all? I think I remember that the sparkfun spi backpack used quite strange numbers for the 7 colors.

The actual version has colors coded on 8 bits ( 3 r 3 g 2 b ).
I successfully displayed all 256 colors, and a version with just black and white don’t work better.

Thanks for any other idea you may have.