2
\$\begingroup\$

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.

\$\endgroup\$
  • 1
    \$\begingroup\$ Have you checked if the fuse bits are set for internal oscillator? Also make sure that you compile for the correct target (for avr-gcc -mmcu=atmega8 parameter should be passed). \$\endgroup\$ – Ashton H. Jun 28 '15 at 2:25
  • \$\begingroup\$ Can you please re-post this question on SE Embedded System site. I think this question belongs to that site. \$\endgroup\$ – prasad Aug 10 '15 at 15:48
  • \$\begingroup\$ The post is kinda old, and I cannot find the Embedded System site. Also my question is pretty closely related to EE \$\endgroup\$ – Nopony Aug 10 '15 at 18:02
1
\$\begingroup\$

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

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.