ATmega8 LED blinking project not working properly


I am making a simple project using ATmega8 microchip. I uploaded this code to the AVR:

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

int main(void) {
    DDRD = 0b10000000;
    DDRD = 0b01000000;

    while(1) {
        PORTD = 0b10000000;
        PORTD = 0b00000000;
        PORTD = 0b01000000;
        PORTD = 0b00000000;
    return 1;

It basically turns on and off two different LED's which are connected to pin (AIN0) PD6 and (AIN1) PD7.

ATmega8 datasheet

When the system is working, the AVR can deliver power to just one of the LED's properly. Other LED has a significantly low brightness. When I measure the voltage delivered to the bright LED which is connected to PD6, it takes approximately 2.3 volts, and the dimmer one which is connected to PD7 takes approximately 1.2 volts.

Why I am having this issue? Is it about the code that I wrote or is it about the chip itself? On the other hand when I light just one of the LED's on either PD6 or PD7 with a simple code it works just fine. So there is no problem with pins, connections or LED's I use. (I also could not copy and paste the package names here correctly! So it is correct too.)

Best Answer

You are overwriting your initialisation. First you set D7 as an output and all other pins on port D as a input. In your next statement you set D6 as an output and all other pins including D7 as an input.

To fix this use this instead of both initialisations:

DDRD = 0b11000000;

you can also use this:

DDRD = 0b10000000; DDRD |= 0b01000000;

if you want to setup more outputs use |= instead of =.