Home TutorialsmegaAVR Atmega16 and 74HC595 shift register example

Atmega16 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 in more detail

595 pin diagram

595 pin diagram

Pins

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

 

atmega16 and 74hc595

atmega16 and 74hc595

Code

 

[codesyntax lang=”c”]

#define SHIFT_CLOCK PORTD.F2
#define SHIFT_LATCH PORTD.F1
#define SHIFT_DATA PORTD.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()
{
DDRD=0xFF;
PORTD=0xFF;

while(1)
{
shiftdata595(1);
latch595();
delay_ms(200);

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

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

[/codesyntax]

 

Links

10PCS SN74HC595N

30pcs SN74HC595N 8 Bit Shift Register

You may also like