Showing posts with label theory. Show all posts
Showing posts with label theory. Show all posts

Tuesday, March 11, 2014

Theory: shift registers, and the test jig code explained!

Ahh... Shift registers. The heart of most LED cubes.

So, what is a shift register and what does it do? Basically it's a serial input, parallel output IC; you send a stream of ones and zeroes to it on one pin and this makes a bunch of output pins go high or low. Usually you have two more pins: a clock pin and a latch pin. The clock pin is used to tell the shift register when to read a new bit on its input pin. The latch pin is used to tell it when to make the read bits appear on the output pins.

In my cube I use the 74HC595 shift register which is an 8 bit shift register. It simply means that it has 8 outputs. You can find these for almost a dime a dozen on eBay. If you need the smaller SMD type, search for 74HC595D.
The 595 (as I'll refer to it from now on) has two internal registers: the shift register and the storage register. We'll start at the beginning - the input pin - and work our way down to the output pins.

When you want to send data to the 595 you start by setting the DS pin (Serial Data Input) high or low, depending on whether you want to write a one or zero. Then you make the SHCP pin (Shift Register Clock Input) go from low to high to low again. The shift register will update whenever that pin goes from low to high. It will store whatever value is at DS and put it in bit 0 of the shift register. All the other bits of the register are shifted to the next one. So bit 1 will get the old value of bit 0, bit 2 the old value of bit 1, etc etc. The cool thing about this is that the old value of bit 7 will appear at a pin called Q7S. If you connect this pin to the DS pin of a second 595, and also connect the SHCP pins of both 595s, then bit 7 of the first 595 will be shifted to bit 0 of the second 595. You can chain a whole bunch of registers like this, effectively forming one large shift register.

Great, now that you've shifted 8 bits into the 595, you'll want to have them appear at the output pins. To do this you pull the STCP (Storage Register Clock Input, otherwise known as Latch) pin from low to high to low again. Whenever this pin goes from low to high all values from the shift register will be copied to the storage register.
This storage register is connected to the output pins, so any value in it will appear on the output pins. But only if the OE (Output Enable) pin is pulled low. If OE is high all output pins will be low.

One thing that's important to know is that when both the STCP and SHCP pins go high simultaneously, the shift will take place after the contents of the shift register have been copied to the storage register. This means that when you have STCP and SHCP connected to eachother, and you shift in a bit, that this bit will not appear on the output directly (since the content of the shift register has been copied to the output register before the new bit was shifted in).

Let's have another look at my pillar testing code. For this I have connected STCP and SHCP to the same pin on the controller (B4) and DS to B3. OE has been connected to ground.


1:  /*  
2:   * TinyPillarTester.c  
3:   *  
4:   * Created: 6-3-2014 21:48:11  
5:   * Author: Daniel  
6:   */   
7:    
8:    
9:  #include <avr/io.h>  
10:  #include <avr/interrupt.h>  
11:    
12:  int main(void)  
13:  {  
14:      PORTB = 7;  
15:      DDRB = _BV(DDB0) | _BV(DDB1) | _BV(DDB2) | _BV(DDB3) | _BV(DDB4);  
16:    
17:      TCNT0 = 0;  
18:      TIMSK0 = (1 << OCIE0A); // interrupt on compare match  
19:      TCCR0A = _BV(WGM01);  
20:      TCCR0B = _BV(CS01); // F_CPU / 8  
21:      OCR0A = 250;  
22:      asm("sei");  
23:    while(1)  
24:    {  
25:        // do nothing!  
26:        asm("sleep");  
27:    }  
28:  }  
29:    
30:  uint8_t cathodeCounter = 4;  
31:  uint8_t anodeCounter = 0;  
32:    
33:  ISR(TIM0_COMPA_vect, ISR_NAKED)  
34:  {  
35:      uint8_t sreg = SREG;  
36:      static uint8_t counter = 0;  
37:      ++counter;  
38:      if ( counter >= 75 )  
39:      {  
40:          counter = 0;  
41:          ++anodeCounter;  
42:          if ( anodeCounter == 8 )  
43:          {  
44:              anodeCounter = 0;  
45:              PORTB |= _BV(3);  
46:          }  
47:          else if ( anodeCounter == 1 )  
48:          {  
49:              PORTB |= cathodeCounter;  
50:              cathodeCounter <<= 1;  
51:              if ( cathodeCounter >= 8 )  
52:              {  
53:                  cathodeCounter = 1;  
54:              }  
55:              PORTB &= ~cathodeCounter;  
56:              PORTB &= ~_BV(3);  
57:          }  
58:          PORTB |= _BV(4);  
59:          PORTB &= ~_BV(4);  
60:      }  
61:      SREG = sreg;  
62:      asm("reti");  
63:  }  
64: 

Let's go through this line by line.

12:  int main(void)  
13:  {  
14:      PORTB = 7;  
15:      DDRB = _BV(DDB0) | _BV(DDB1) | _BV(DDB2) | _BV(DDB3) | _BV(DDB4);  
Line 14: Pins B0,B1 and B2 are set high, since they are connected to the cathodes of the LEDs this will make the LEDs go off. B3 (DS) and B4 (SHCP, STCP) are set low.
Line 15: All pins are configured as output pins (except B5, which is the reset pin)

17:      TCNT0 = 0;  
18:      TIMSK0 = (1 << OCIE0A); // interrupt on compare match  
19:      TCCR0A = _BV(WGM01);  
20:      TCCR0B = _BV(CS01); // F_CPU / 8  
21:      OCR0A = 250;  
Line 17-21: Timer initialization, it's set to generate an interrupt on compare match. The value to match is 250. The counter will reset when the match occurs (line 19) and its speed is 1/8th of the cpu speed (line 20).

22:      asm("sei");  
23:    while(1)  
24:    {  
25:        // do nothing!  
26:        asm("sleep");  
27:    }  
Line 22: Inline assembler, the "sei" command will enable interrupts
Line 23 - 27: Do nothing. The "sleep" command puts the cpu in idle state until there's an interrupt. This also reduces power consumption from 3 mA to 0.5 mA.

33:  ISR(TIM0_COMPA_vect, ISR_NAKED)  
34:  {  
35:      uint8_t sreg = SREG;  
Line 33: Start of interrupt handler for timer0 compare match. ISR_NAKED means the compiler won't put any code at the start and end specific to handling interrupts. One of the things you need do do is store the SREG register and restore it at the end, as you can see on line 35
Let's skip some stuff

61:      SREG = sreg;  
62:      asm("reti");  
63:  }  
64: 
Line 61: Restore SREG, not really necessary since we don't have any code in our main loop. But hey, it's good practice.
Line 62: Return from interrupt. Required special command if you exit an interrupt handler.

36:      static uint8_t counter = 0;  
37:      ++counter;  
38:      if ( counter >= 75 )  
39:      {  
40:          counter = 0;  
Line 36-40: The timer is configured to generate 600 interrupts per second, I only need 8, so this slows it down a bit :)

41:          ++anodeCounter;  
42:          if ( anodeCounter == 8 )  
43:          {  
44:              anodeCounter = 0;  
45:              PORTB |= _BV(3);  
46:          }  
Line 41: A counter to run through all the anodes. When it reaches 8, I reset it to 0. I also set B3 (DS) high (line 45).
47:          else if ( anodeCounter == 1 )  
48:          {
56:              PORTB &= ~_BV(3);  
57:          }  
Line 47: On the first interrupt (or the first one after the anode counter has been reset) I set B3 low again (line 56). It'll stay that way until the counter reaches 8 again. So it's high for 1 update, and low for 7.

58:          PORTB |= _BV(4);  
59:          PORTB &= ~_BV(4);  
Line 58: Set B4 (SHCP and STCP) high, this will cause the contents of the shift register to be copied to the storage register, and then shift a new bit from DS into the shift register.
Line 59: Set B4 low again.

And that's pretty much it. This clocks one bit to the shift register every 8th of a second, one one and seven zeroes. So one pin will be high and the others low, and the high pin will shift to the next every 8th of a second.

Now there's just a small piece of code left:
49:              PORTB |= cathodeCounter;  
50:              cathodeCounter <<= 1;  
51:              if ( cathodeCounter >= 8 )  
52:              {  
53:                  cathodeCounter = 1;  
54:              }  
55:              PORTB &= ~cathodeCounter;  
Line 49-55: This handles switching between red green and blue (cathodes). The cathodeCounter variable is a bit mask, only one bit will be set, which corresponds to one of the output pins B0, B1 or B2. I first apply this mask to the pins, which makes the current active pin go high (line 49) turning the LED off. Then I shift the variable left one bit to move to the next pin (line 50). If it goes past the last pin (line 51) I reset it back to the first pin (line 53). Finally I negate the mask and apply it, setting the new active pin low (line 55) turning the LED on.

The reason this piece of code sits inside the anodeCounter == 1 block is because the actual output of the 595 lags one clock cycle behind the content of the shift register. If I had put this code in the anodeCounter == 8 block then the jig would switch colors while the last LED was on. Since we don't want that, we have to wait one more cycle.

That's it! I hope this gives a better understanding of the 595 and the test jig :)

Tuesday, March 04, 2014

Theory

In this post I want to take some time to explain the schematics. I want this to be as accessible as possible, so we'll start at the very beginning.

Connecting an LED

LEDs arent' like ordinary light bulbs. You cannot (read: should not) connect an LED directly to a battery. The reason for this is that an LED has a very low internal resistance, so you'll effectively be shorting the battery. Sure, it will work if the voltage is right, but you risk damaging the LED. They're also picky about the voltage, so you'll want to provide an LED with just the right voltage.

To accomplish this we put a resistor in series with the LED. To get the right resistor value you need a couple of parameters: the forward voltage of the LED (VF), the input voltage (VI), and the forward current (IF). The forward voltage of an LED depends on its color. Red usually means 2.4V, Green and Blue 3.4V. The input voltage of my cube is 5V. The forward current of an LED is typically 20mA (0.02A). Check your datasheets for your specific LEDs to verify this.
Now we need our input voltage VI to drop to VF, so that's VI - VF Volts. Then we simply apply Ohm's law (I = U/R, or R = U/I) and we get R = (VI - VF) / IF. So for a red LED that's (5 - 2.4) / 0.02 = 130 Ohm. For green and blue it's (5 - 3.4) / 0.02 = 80 Ohm.

Let's wire up a red LED to 5V. 130 Ohm resistors aren't that common, so we'll use the next best thing: 150 Ohm. Remember, LEDs are Light Emitting Diodes, so they'll only let current flow from one lead to the other, specifically from the anode (long lead) to the cathode (short lead, and the side where there's a flat spot).

That was simple enough, now let's try something more interesting.

Connecting an RGB LED

Multicolor LEDs come in two configurations: common Anode and common Cathode. They are basically multiple LEDs in one package with either the anode or cathode lead shared between all LEDs.
Does it matter which one you use? Not really, as long as you know what type you're using/ordering. This also has consequences for where you place the series resistor. You shouldn't connect your resistor to the common lead of your LED, unless you have a bi-color LED where both LEDs have the same forward voltage, or you're using the highest resistor value.

For the rest of this post we'll be using common anode LEDs, since that's what I'm going to put in my cube.

Connecting it is easy, just hook 5V to the common anode, a few resistors to each cathode and the other end of the resistor to ground.

Controlling an RGB LED

So we've got our RGB LED burning, but now we want to control it. We want to be able to turn each color on and off. So we grab our Arduino (or other microcontroller) and hook our LED up. Instead of connecting the resistors to ground, we connect each resistor to a I/O pin of the microcontroller. With an AVR, like the Atmega328 that's on an Arduino Uno/Nano, we can set the pin to OUTPUT mode (low impedance), and by writing a digital LOW we connect the pin to ground (it sinks current), and the LED will turn on. By writing a digital HIGH we connect the pin to 5V (it sources current) and the LED will turn off (since both the anode and cathode are connected to 5V, no current will flow).

While this works fine for one RGB LED, and even for two or three, any more will start to become a problem. Remember how an LED has a forward current of 020mA? If you turn all three colors of an RGB LED on that becomes 60mA. If you connect 4 RGB LEDs and turn them all on you'll be putting 240mA through the microcontroller. Now google the datasheet for the Atmega328 and have a look at chapter 29. Electrical Characteristics, section 1 Absolute Maximum Ratings. It says: DC Current Vcc and GND pins: 200mA. Whoops, we possibly just killed our Arduino. Also note how the DC Current per I/O pin is just 40mA, so don't even think about connecting more than one LED to a pin.

So we'll need to come up with some way to reduce the amount of current going through our microcontroller. Fortunately, there's this thing called a transistor. With just a tiny amount of current you can control a much larger amount of current. A transistor has three leads: Base, Collector and Emitter. The base lead is used to control the amount of current flowing between the collector and emitter. There are two types of transistors: NPN and PNP. In an NPN transistor, a small current entering the base is amplified to produce a large collector and emitter current. (http://en.wikipedia.org/wiki/Bipolar_junction_transistor#NPN)

A PNP transistor works the other way around. When you pull the base low (connect to ground) a larger current will flow from the emitter to the collector (notice how the flow of current between the emitter and collector is reversed compared to a NPN transistor).

We'll be using a NPN transistor. We need a transistor that has a maximum collector current of at least 20mA. Then there's this thing called the gain. This figure indicates how much more current there is going to flow through the collector and emitter, compared to how much flows through the base and emitter. Basically it's your amplification. We need at least 20mA, but we don't want to use that much to control it. So let's use a gain of 10, this way we only need 2mA to control 20mA.

If you have a look at the datasheet of the 2N3904 you can find the following figures:
Collector Current : 200mA
DC current gain (at 1mA IC (input current)) : 70
That's well within specs.

The required base current will be 20mA / 70 = 0.3mA. Now we need to find a resistor to give us at least 0.3mA. First we have another look at the datasheet, we need to find the base-emitter saturation voltage, which happens to be between 0.65 and 0.85V, so 0.75 average. Enter Ohm's law again. (5V - 0.75V) / 0.0003A = 14166 Ohm. So a 10K resistor will do the trick, this will give us (5V - 0.75V) / 10000 = 0.4mA. A lower resistor like 1K will work as well, but it'll increase the power consumption, add extra heat, shorten the life of our components, etc. Don't even think about directly connecting your I/O pin to the transistor base lead since you'll effectively be shorting it to ground and kill your micro controller. If you want to read a bit more, click here.


That's it for now, next time I'll explain the anode driver and shift registers :)