Saturday, November 15, 2008

Adding a shift register to the mix

So I hooked up a shift register (SN74HC595N) (8 bits output) to the ATMega168 (avr).

I wrote code. It wouldn't work. I couldn't figure out why. It sometimes worked, sometimes didn't. The lower 4 bits would work, sometimes...

Turns out, I forgot to set the 'you allowed to send information/output stuff' (SRCLR (overriding clear)) pin to high (tie it to vcc), so it was floating. This made it work sometimes, and most of the time it didn't work.

Once I put +5v on pin 10 (SRCLR) everything worked out great. I cleaned up all of the crazy code that I wrote trying to get something to work (I thought it was a code problem, not a "I missed hooking up a pin"). I had LED's hooked up to the 8 outputs of the shift register. It just lights the last led, then the next up(with the first), and etc until it lights all of them.

oh, for the connecting of stuffs, I used this diagram

from (note: On that page, he mentions adding power to the shift register as its not on the diagram. I didn't read that until making this post, but just remembered to do it from the data sheet on the shift register. Sucks that I didn't remember to hookup pin 10!) I put the LEDS on QA -QH.

the code (blinkin.c):

#include <avr/io.h>
#include <util/delay.h>

#define nop() __asm__ __volatile__ ("nop")
#define F_CPU 20000000UL //My chip is 20 Mhz
//make sure F_CPU is after #include <util/delay.h>

send_byte_to_shift_register(int8_t bite);


j = 1;



j = j*2;
( j == 512)
j = 1;}


send_byte_to_shift_register(int8_t bite)
//should be able to use SPI instead of this way...

DDRB = 0xFF;

i = 0;

out = 0;


out = (bite >> i) & 0x01; //extract 0 or 1
//0x01 for last bit only, and >> to get location i
if (out == 1) {PORTB = (1 << PINB3); }
{PORTB = (1 >> PINB3); }
PORTB = (1 << PINB1); //clock;
PORTB = (1<<PINB2); //latch the shift register


yeah, so my sister yells out "STELLLLA" at the end of the video. Think of it as a bonus.

No comments: