0
\$\begingroup\$

So I have begun with projects using AVR microcontrollers. A few months ago, I got one of these to simply flash an LED but I accidentally cooked that one with a bad power supply. Now I got some new ones and I believe that I'm doing the same thing before but the LED isn't flashing!

This is my current code:

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

int main(void){

    DDRB |= 1<<PB0;
    while(1){
        PORTB &= -(1<<PB0);
        _delay_ms(1000);
        PORTB |= 1<<PB0;
        _delay_ms(1000);
    }
    return 0;
}

I'm able to successfully program the device but when I test it, nothing happens. The device is an ATmega168A. This is my current pin connections:

Pin 1(Reset): 5V

Pin 7(Vcc): 5V

Pin 8(GND): GND

Pin 14(B0): LED OUT

Pin 20(AVCC): 5V

Pin 22(GND): GND

Is there something obvious that I'm just missing?

\$\endgroup\$
  • \$\begingroup\$ You must have a current limiting resistor in series with the LED or you may damage your controller. The LED will be heavily forward biased and draw more current than is safely available from PB0. \$\endgroup\$ – sherrellbc Mar 24 '15 at 2:14
4
\$\begingroup\$

PORTB &= -(1<<PB0); should be PORTB &= ~(1<<PB0);.

~(1<<PB0) is 0xFE, which turns off bit 0. -(1<<PB0) is 0xFF, which works out to a no-op, so the pin is never getting turned off.

\$\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.