Shift register using ATMEGA328p

Other projects from the blog, flickr, Instructables, etc.
Post Reply
Posts: 1
Joined: Tue Jun 27, 2017 12:45 am

Shift register using ATMEGA328p

Post by nanawhite107 »

I want to learn more about the shiftout function used with Arduinos and used the implementation with my ATMEGA328p.

Here is the date sheet of atmega328p

The Setup

#define F_CPU 1000000UL
#define BAUD 9600UL
#define LSBFIRST 0
#define MSBFIRST 1
#define HIGH 0x1
#define LOW 0x0
#define INPUT 0x0
#define OUTPUT 0x1

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

#define DATA PD7
#define LATCH PB2
#define CLOCK PB0

uint8_t leds = 0;

void shiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t val)
for (uint8_t i = 0; i < 8; ++i) {
if (bitOrder == LSBFIRST) {
PORTD |= (!!(val & (HIGH << i)) << DATA);
} else {
PORTD |= (!!(val & (HIGH << (7 - i))) << DATA);

PORTB |= (HIGH << clockPin);
PORTB |= (LOW << clockPin);

void updateShiftRegister() {
shiftOut(DATA, CLOCK, LSBFIRST, leds);

void setupRegisters() {
DDRD = 0xff;
DDRB = 0xff;

int main(void)
leds = 1;
leds = 0;

return (0);

I created an example to understand more about the function, but the outcome does not add up to what I expected to see.

Given my example, I would expect LED1 to turn on and turn off after 2sec. It turns on, but it does not turn off afterwards. Did I miss anything? I am searching for 2hrs now for the error in my code.
Post Reply