Friday, August 15, 2014

LED matrix

I decided to play a little bit with LED matrix, so i will write what i found out.

Led matrix is just a bunch of LED's which represents a table.
To light chosen LED we need to apply + on R2 and connect C3 with GND. In that way we can light any LED or group of LED's in 1 row or column. If we try to light LED's that are in different rows and columns then we light other LED's. e.g. if we light bottom left LED R6 - C0  at the same time as our chosen one, then we will unintentionally light another 2 R6 C3 и R2 C0.

But there are other methods of connecting LED matrixes, which use less MC pins, for example Charlieplexing by Charlie Allen

As we can see with 3 pins we are able to control 6 LED, with 10 == 90. With  N pins N^2 - N LED's.

Principle is simple, we need to  turn on and off every row or column consistently, lighting there needed LED's, our eyes will see only static image.


                               

AVR pin can't provide/consume too much current, i burned PORTD on ATmega32 when accidently lighted entire row. ATmega32 is able to hold about 40 mA on pin and 200 mA on all MC, to avoid it people usually use external shift registers. I didn't have them so i decided to light only 1 LED at a time.

i  tried different time values and lighing LED for 0.3 ms seemed normal (I didn't see any blinking).

I builded LED matrix on a breadboard, this is what i got:


#include <avr/io.h>
#include <util/delay.h>

int main()
{
 DDRD=0xFF; 
 DDRA=0xFF; 

 int fps = 8;
 int pic = 0;
 int frames = 0;

 PORTA = 0b11111011; // GND
 PORTD = 0b00000001;

 int matrix[8][60] = {
 {0,0,0,0,0,0,0,0,1,1,0,0,1,1,0,0,0,0,1,1,0,0,0,0,1,1,0,0,1,1,0,1,1,1,1,1,0,1,1,0,0,0,1,1},
 {0,0,0,0,0,0,0,0,1,1,0,0,1,1,0,0,0,1,1,1,1,0,0,0,1,1,0,0,1,1,0,0,1,1,1,0,0,1,1,0,0,1,1,0},
 {0,0,0,0,0,0,0,0,1,1,1,1,1,1,0,0,1,1,0,0,1,1,0,0,1,1,1,1,1,1,0,0,0,1,0,0,0,1,1,0,1,1,0,0},
 {0,0,0,0,0,0,0,0,1,1,1,1,1,1,0,1,1,0,0,0,0,1,1,0,1,1,1,1,1,1,0,0,0,1,0,0,0,1,1,1,1,0,0,0},
 {0,0,0,0,0,0,0,0,0,0,0,0,1,1,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0,0,0,1,1,1,0,0,0,0,0,1,1,0,1,1,1,1,1,0,1,1,0,0,0,1,1},
 };

 while(1)
 {
  pic++;
  if(pic >= fps) 
  {
   pic = 0;
   frames++;
   
   for(int n = 0; n < 8; n++)
   {
    for(int k = 0; k < 59; k++)
    {
     if(k != 58) matrix[n][k] = matrix[n][k+1];
     else matrix[n][k] = 0;
    }
   }
  }

  if(frames >= 60)
  {
   for(int n = 0; n < 8; n++)
   {
    for(int k = 0; k < 59; k++)
    {
     matrix[n][k] = matrix2[n][k];
     frames = 0;
    }
   }
  }

  for(int row = 0; row < 8; row++)
  {
   PORTD = (1 << row); // so PORTC = 0,1,2,4 VCC

   for(int col = 0; col < 8; col++)
   {         
    PORTA ^= (matrix[row][col] << col);
    _delay_ms(0.3);
    PORTA = 0xFF;
   }
  }
 } 
}

It's not perfect, but it works, so i have an array where i keep my string, after some time i shift string to left, when i get to the end i read  this string again from another array and all repeats.

Info i got from here:
easyelectronics.ru
poprobotics

No comments :

Post a Comment