PIC16F877 and 74HC595 shift register example

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, thats 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

595 pin diagram

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

PIC16F877 and 595 shift register_schem

Code

This was written in mikroC PRO for PIC compiler, no libraries were used

[c]

#define SHIFT_CLOCK PORTC.F2
#define SHIFT_LATCH PORTC.F1
#define SHIFT_DATA PORTC.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()
{
PORTC=0;
TRISC=0;
while(1)
{
shiftdata595(1);
latch595();
delay_ms(200);

shiftdata595(0x02);
latch595();
delay_ms(200);

shiftdata595(0b00000100);
latch595();
delay_ms(200);
}
}

[/c]

Links
10PCS SN74HC595N

30pcs SN74HC595N 8 Bit Shift Register

Related posts

PIC16F877 and LDR example

PIC16f877 and push button example

PIC16F877A and RGB LED example