NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Microcontroller Programming » Needed : Shift Register Sample Code with LED Array

October 11, 2010
by lavared
lavared's Avatar

Hi,

I need a sample code for adding/controlling a shift register to the LED Array using Atmega168 chip. The shift register i'm using is Phillips 54HC595 with 3-state output. I plan to light up 8 leds using the Atmega168 chip with 3 pins using a shift register. I have looked at the arduino shift register information on their website but it seems to use some pre-defined functions (applicable for the arduino development board). I'm not sure whether the same functions are supported in AVR library too or if there's a way to export the library. I'm planning to use the Atmega168 chip on the LED array and with the programming/setup background of LED array kit:

Could somebody please help me.

Thanks.

October 11, 2010
by Rick_S
Rick_S's Avatar

Look in the photography club post on the forum. HERE IS THE LINK

Part way down, you'll see a post I made with code using 4 74HC595's to drive 4 seven segment displays. The code should easily port to whatever project you have in mind.

Rick

October 24, 2010
by lavared
lavared's Avatar

Rick,

I have a question in the code that you had written for Shift Register (lines 57 onwards).

the part of the code is given below.

for(;;) { for(i=0;i<15;i++){ cnt=1; while(cnt) { cnt=cnt<<1; PORTD=SPI_WriteRead(cnt); //_delay_ms(100); delay_ms(25); }

cnt=0x80;
while(cnt) {
  cnt=cnt>>1;
  PORTD=SPI_WriteRead(cnt);
  //_delay_ms(100);
  delay_ms(25);
}
}
for(i=0;i<15;i++){
PORTD=SPI_WriteRead(0b10101010);
delay_ms(125);
PORTD=SPI_WriteRead(0b01010101);
delay_ms(125);
}

}

return 0; }

In this why are you using Port D of the Atmega168 chip, when Pins 2,3,4,5 from port B are connected to the shift register ? Also what is the use of this code 'PORTD=SPI_WriteRead(0b01010101)' ? Also could you explain briefly the counting sequence used in words.

Could you please reply to me.

Thanks Lavan.

October 24, 2010
by Rick_S
Rick_S's Avatar

Lavan,

That program was one of my 1st attempts using SPI for a shift register. I had pieced together some code and got it working. The use of PORTD was part of my old testing that I had never removed. The post a few down from that shows a little better implementation using 7 segment displays (however, I still had that PORTD init in there :D ).

However to answer your question. In that program, PORTD did not need to be initialized as it was. Also, the PORTD=SPI_WriteRead in that program was a modified version of the original that I found on the web. This version of the function was doing nothing other than writing the bytes to the shift register not reading from it. You'll notice in the funtion in the program below that, I made the function type void instead of char and just called it.

The 0b01010101 was one of the bytes sent to the shift register (every other light on).

Hope that helped clarify a bit :D

Rick

October 24, 2010
by lavared
lavared's Avatar

Thanks Rick,

I thought that Port D is redundant here but wasn't sure if there was any reason behind in having it in the code.

Btw do you have any code snippet of PWM implementation on Atmega168 ?

Thanks Lavan.

October 25, 2010
by Rick_S
Rick_S's Avatar

Check out the servo squirter code on the tutorial page. In that example, the NK guys use PWM to control the position of a small hobby servo. The code could be used / modified to dim LED's or control motor speed through a transistor.

Rick

Post a Reply

Please log in to post a reply.

Did you know that NerdKits also has extra parts available for its customers? Learn more...