16x32 Matrix flashing, coding help

Good morning all,

I am writing some code for a 16x32 matrix I have, it works of a 16bit shift register for the column bits data, and a 4 to 16 line decoder to set the row to write the data to.

I am somewhere near with it, but when I try and display just the number 2 ( which I have defined as a bit array, ) it just flashes the matrix at a high rate, I know the routine is a bit rough, and I am sure its just the position of my update, latch or something but I would really appreciate some advice

many thanks

Rich

byte number2[] ={
  B00000000,
  B01111110,
  B01111110,
  B00000110,
  B00000110,
  B00000110,
  B00000110,
  B01111110,
  B01111110,
  B01100000,
  B01100000,
  B01100000,
  B01100000,
  B01111110,
  B01111110};
  
int clockPin = 2;
int reddataPin = 3;


void setup() {
  // put your setup code here, to run once:
pinMode(4,OUTPUT);   //  Sets latch pin as output
pinMode(5,OUTPUT);  // Sets OE pin as output

pinMode(clockPin, OUTPUT);
pinMode(reddataPin, OUTPUT);

pinMode(34,OUTPUT);  // Sets Address row pins as outputs
pinMode(35,OUTPUT);
pinMode(36,OUTPUT);
pinMode(37,OUTPUT);

 shiftOut(reddataPin, clockPin, MSBFIRST, 0);  // Clears the display upon boot up
  shiftOut(reddataPin, clockPin, MSBFIRST, 0);
  shiftOut(reddataPin, clockPin, MSBFIRST, 0);
  shiftOut(reddataPin, clockPin, MSBFIRST, 0);
  #define pinport PIOC                          // defines the address pins on PORT C
}

void loop() {
  // put your main code here, to run repeatedly: 


 
for (int j=0; j<15; j++){                   //Loop For getting new row data


       for (int i = 0; i<16; i++) {              // Loop for new column address
    
   
       pinport -> PIO_SODR = i << 2 ;   // set row Address eg, B1010    
 
      digitalWrite(4,LOW);    // Set Latch Low
 
 
      shiftOut(reddataPin, clockPin, MSBFIRST, 0);    // Shift BYTE 1 onto register
      shiftOut(reddataPin, clockPin, MSBFIRST, 0);    // Shift BYTE 2 onto register
      shiftOut(reddataPin, clockPin, MSBFIRST, 0);    // Shift BYTE 3 onto register
      shiftOut(reddataPin, clockPin, MSBFIRST, number2[j]);    // Shift BYTE 4 onto register
  
  
  
      digitalWrite(5,HIGH);   // Set Output Enable HIGH ( Turn off the display while new data is latched into it)
      digitalWrite(4,HIGH);      // Set Latch HIGH to latch the new data from the register to the display
      digitalWrite(5,LOW);    // Set Output Enable LOW to show the new data
 
      pinport -> PIO_CODR = i << 2 ;  // clear pin    // Return Row address Pins to B0000

 
      }
 
  }

}

I think output can be enabled at all the time. This should remove the flickering.
If you want to speed up the latching , use “bitSet” and “bitClear”

Rich_P:
it works of a 16bit shift register for the column bits data, and a 4 to 16 line decoder to set the row to write the data to.

Hi Rich, did you mean 32-bit shift register?

Rich_P:
it just flashes the matrix at a high rate

Do you mean all 512 leds flash together? Or just a block of 6 wide by 16 high? Are they all equally bright or are some brighter than others? I suspect the middle 2 columns of the 6 will be less bright. Am I right?

Rich_P:
I know the routine is a bit rough, and I am sure its just the position of my update, latch or something but I would really appreciate some advice

Your sketch won’t even compile for me, I get

sketch_aug15a.ino: In function ‘void loop()’:
sketch_aug15a.ino:39:19: error: ‘PIOC’ was not declared in this scope
sketch_aug15a.ino:53:8: note: in expansion of macro ‘pinport’

Reading the code, it seems to me you have one too many for() loops. You need one loop to select each row of the display in turn. Not sure the second loop is wanted at all. At the moment, I think as a result of the 2 loops, it is displaying the same bit pattern on all 16 rows. First time, this is the top row of the “2” pattern, then the second row and so on.

I suggest you try getting rid of the “for (int i=…” loop and replace “i” with “j” in the other lines. Also add a “delay(1);” just before the end of the loop.

Which Arduino pins are the 4 lines for the 4-16 decoder connected to?

Paul

knut_ny: I think output can be enabled at all the time. This should remove the flickering.

I'm not sure I agree, knut_ny. I think if you enable the output full time, you would get "bleed" of the led pattern into the next row. The output needs to be disabled while the new bit pattern gets latched in, and then the new row has been selected. Only once both have been done should the output be enabled again. The shifting can be done while the output is enabled because the new bit pattern won't be visible until it is latched.

I'm not really sure what's causing the flickering, but I don't think it is caused by enabling/disabling the output.

knut_ny: If you want to speed up the latching , use "bitSet" and "bitClear"

Not sure where you are suggesting to use those functions, or how it speeds up anything. Can you elaborate please?

PaulRB:
I’m not sure I agree, knut_ny. I think if you enable the output full time, you would get “bleed” of the led pattern into the next row. The output needs to be disabled while the new bit pattern gets latched in, and then the new row has been selected. Only once both have been done should the output be enabled again. The shifting can be done while the output is enabled because the new bit pattern won’t be visible until it is latched.
I’m not really sure what’s causing the flickering, but I don’t think it is caused by enabling/disabling the output.

Quite right on all counts - the code as posted is thoroughly addled. It does not help that obscure ways of setting the output pins are used. The flickering is substantially caused by a nonsense line of code at the end.

I “pricked up my ears” in regard to the output enable. On previous discussions regarding using shift registers to drive seven segment displays, one per digit, it has been necessary to point out that the output must be permanently enabled, as the latch will update the outputs synchronously. For a matrix display however, the order must be:

Prepare the data to be displayed.  Not time critical
Load the shift register(s).
Disable outputs.
Latch shift register data.
Set row value.
Enable outputs.

So, hopefully functional code:

void loop() {
  // put your main code here, to run repeatedly: 
  for (int j=0; j<15; j++){                   //Loop For getting new row data
    for (int i = 0; i<16; i++) {              // Loop for new column address

      shiftOut(reddataPin, clockPin, MSBFIRST, 0);    // Shift BYTE 1 onto register
      shiftOut(reddataPin, clockPin, MSBFIRST, 0);    // Shift BYTE 2 onto register
      shiftOut(reddataPin, clockPin, MSBFIRST, 0);    // Shift BYTE 3 onto register
      shiftOut(reddataPin, clockPin, MSBFIRST, number2[j]);    // Shift BYTE 4 onto register

      digitalWrite(4,LOW);           // Latch Low
      digitalWrite(5,HIGH);          // ~Output Enable HIGH (disable display while data updated)
      pinport -> PIO_SODR = i << 2 ; // set row Address eg, B1010
      digitalWrite(4,HIGH);          // Latch new data
      digitalWrite(5,LOW);           // ~Output Enable LOW to enable display

//    pinport -> PIO_CODR = i << 2 ;  // clear pin    // Return Row address Pins to B0000 - nonsense!
    }
  }
}

Paul__B, will Rich's sketch compile for you?

I am on Arduino 1.0.5 (Ubuntu 14.04).

PaulRB: Paul__B, will Rich's sketch compile for you?

Err, no, or at least my version does not compile either, but I thought that if it compiles for him then he presumably has something in his system that I do not so it should not be a problem, or the problem. :cold_sweat:

That's why I made the crack about "obscure ways". I have no idea what "PIOC" is, nor it appears, does the compiler.

PaulRB: I am on Arduino 1.0.5 (Ubuntu 14.04).

1.0 - the later versions do not seem to be in the Fedora repository. Tell you what - happened to have my 'doze system set up here which is 1.0.5, same code gives same error:

sketch_aug16a.cpp: In function 'void loop()':
sketch_aug16a.cpp:51:7: error: 'PIOC' was not declared in this scope