Wednesday, February 12, 2014

LED matrix with transistor switches.

I just connected the teensy up to the LED matrix and wrote a script to run a pattern on it. When I first tried it out nothing happened at all... I spent a really long time wondering if at 5V I wasn't getting enough current to switch the transistor on so I tried to learn more about transistors - couldn't find a problem in the circuit and in the end it was just a stupid mistake in my code (using numbers 1,2,3 instead of 1<<1,1<<2,1<<3).

The main thing here is that the transistors are being used as switches, the setup for this is:


The switches and ground represent pins of the microcontroller.


#include 
#include 
#include 

#define CPU_PRESCALE(n) (CLKPR = 0x80, CLKPR = (n))
#define DIT 160  /* unit time for morse code */

#define L(x,y) ((1<<(2+(x))) | (1<<(5+(y))))

int main(void) {
  // set for 16 MHz clock, and make sure the LED is off
  CPU_PRESCALE(0); 
  DDRC |= 0xF;
  PORTC = 0;
 
  while (1) {
    cw();
    cw();
    cw();
    cw();
    cw();
    fig8();
    cw();
    cw();
    cw();
    strip();
    strip();
  }
}

#define P(x,y) {PORTC = L(x,y); _delay_ms(DIT);}

void cw(void) {
  P(0,0); P(1,0); P(2,0);
  P(2,1);
  P(2,2);
  P(1,2);
  P(0,2);
  P(0,1);
}

void strip(void) {

  P(0,0);
  P(0,1);
  P(0,2);

  P(1,0);
  P(1,1);
  P(1,2);

  P(2,0);
  P(2,1);
  P(2,2);
}

void fig8(void) {
  P(0,0);

  P(1,0);
  P(1,1);
  P(1,2);

  P(2,2);
  P(2,1);
  P(2,0);

  P(1,0);
  P(1,1);
  P(1,2);
  
  P(0,2);
  P(1,2);
}

No comments:

Post a Comment