Shift register using ATMEGA328p

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

Shift register using ATMEGA328p

Postby nanawhite107 » Tue Jun 27, 2017 12:48 am

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() {
PORTB |= (LOW << LATCH);
shiftOut(DATA, CLOCK, LSBFIRST, leds);
PORTB |= (HIGH << LATCH);
}

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


int main(void)
{
setupRegisters();
leds = 1;
updateShiftRegister();
_delay_ms(2000);
leds = 0;
updateShiftRegister();

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.

Return to “Other Projects”

Who is online

Users browsing this forum: No registered users and 0 guests