I want to learn more about the shiftout function used with Arduinos and used the implementation with my ATMEGA328p.
The Setup
This is the original shiftout
function used in Arduino Programming, except that I replaced 1 with HIGH.
#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
void shiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t val)
{
uint8_t i;
for (i = 0; i < 8; ++i) {
if (bitOrder == LSBFIRST) {
PORTD |= (!!(val & (HIGH << i)) << DATA);
} else {
PORTD |= (!!(val & (HIGH << (7 - i))) << DATA);
}
PORTB |= (HIGH << clockPin);
PORTB &= ~(HIGH << clockPin);
}
}
Main
int main(void)
{
initUSART();
DDRD |= (HIGH << DATA);
DDRB |= (HIGH << LATCH) && (HIGH << CLOCK);
PORTB |= (HIGH << LATCH);
shiftOut(DATA, CLOCK, LSBFIRST, 1);
printBinaryByte(PIND);
PORTB ^= (HIGH << LATCH);
/* Replace with your application code */
while (1)
{
}
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.
Szenario
PORTD |= (!!(val & (HIGH << 0)) << DATA);
This is what I expect for val = 1
(HIGH << 0)
=00000001
val & (HIGH << 0)
=00000001 & 00000001
!!(val & (HIGH << 0))
=1
PORTD |= (1 << DATA)
As PORTD
is the data pin on the shift register, it should get 00000001
.
When I print the binary number within PORTD
I get: 10000111
and my LED Array connected to the 74HC595 displays: 10101111
I am really lost.... I checked the connections to the LEDs and they are all good. Can you explain to me why I receive this awkward result?