Electronic – Issue with _delay_ms and PORT

atmegaavrdudeport

Whenever I try to set a certain port to high, then wait some time, then set some other port to high, I get an issue where only the last value would be set.
The code I import via USBASP and avrdude is:

#define F_CPU 1000000UL
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>

int main(void)
{
    DDRD = 0b11111111;
    PORTD = 0b11111110;
    _delay_ms(1000);
    PORTD = 0b11111101;

    while(1)
    {
        ;
    }
}

And that just sets all pin on PORTD to high except PD0, ignoring the second instruction. Moreover, if I replace that piece of code with something like this:

_delay_ms(1000);
DDRD = 0b11111111;
PORTD = 0b11111110;

It does light all the pins on the port with a one second delay. I'm using an ATmega8A and also utterly lost.

Best Answer

why don't you use while loop ? as I know the compiler work like that it run the while loop and then it set ports to default value and then run while loops again you code will work like thus : it will first set PORTD = 0b11111110; then after your delay PORTD = 0b11111101; and after that nothing will change. I suggest you write same code inside while loop