Sometimes in your projects you simply do not have enough I/O lines available, take for example a lot of the multiple LED examples, these use 8 outputs to control 8 LEDs via your PIC, that can restrict the amount of outputs you would have available to drive other devices. Instead of this we can use a shift register, in this case a 74HC595 and using 3 I/O pins we can control 8 LED’s, that’s a saving of 5 I/O pins for other uses.
You can use it to control 8 outputs at a time while only taking up a few pins on your microcontroller. You can link multiple registers together to extend your output even more. The 74HC595 has an 8 bit storage register and an 8 bit shift register. Data is written to the shift register serially, then latched onto the storage register. The storage register then controls 8 output lines
Lets look at the 74HC595
PINS 1-7, 15 | Q0 to Q7 | Output Pins |
PIN 8 | GND | Ground, Vss |
PIN 9 | Q7″ | Serial Out |
PIN 10 | MR | Master Reclear, active low |
PIN 11 | SH_CP | Shift register clock pin |
PIN 12 | ST_CP | Storage register clock pin (latch pin) |
PIN 13 | OE | Output enable, active low |
PIN 14 | DS | Serial data input |
PIN 16 | Vcc | Positive supply voltage usually 5v |
This is a link to the 74HC595 datasheet
Schematic
In the schematic below we have only shown 3 sets of resistors and LEDs but you could easily populate all 8
Code
This was written in mikroC PRO for PIC compiler, no libraries were used
<pre>#define SHIFT_CLOCK PORTB.F2 #define SHIFT_LATCH PORTB.F1 #define SHIFT_DATA PORTB.F0 void shiftdata595(unsigned char _shiftdata) { unsigned int i; unsigned char temp; temp = _shiftdata; i=8; while (i>0) { if (temp.F7==0) { SHIFT_DATA = 0; } else { SHIFT_DATA = 1; } temp = temp<<1; SHIFT_CLOCK = 1; Delay_us(1); SHIFT_CLOCK = 0; i--; } } void latch595() { SHIFT_LATCH = 1; Delay_us(1); SHIFT_LATCH = 0; } void main() { PORTB=0; TRISB=0; while(1) { shiftdata595(1); latch595(); delay_ms(2000); shiftdata595(0x02); latch595(); delay_ms(2000); shiftdata595(0b00000100); latch595(); delay_ms(2000); } }</pre>
Links
10PCS SN74HC595N
30pcs SN74HC595N 8 Bit Shift Register