Electrical – AVR ATMEGA328p LED Example

atmegaavrc

I have the following code for testing an LED:

configGlobal.h

#define F_CPU 1000000UL
#define setBit(sfr, bit)     (_SFR_BYTE(sfr) |= (1 << bit))
#define clearBit(sfr, bit)   (_SFR_BYTE(sfr) &= ~(1 << bit))
#define toggleBit(sfr, bit)  (_SFR_BYTE(sfr) ^= (1 << bit))

main.c

#include "configGlobal.h"
#include <avr/io.h>
#include <util/delay.h>

#define LED PB0
#define LED_DDR DDRB
#define LED_PORT PORTB

int main(void) {

  // -------- Inits --------- //
  DDRB |= 0b00000001;            /* Data Direction Register B:
                                   writing a one to the bit
                                   enables output. */

  // ------ Event loop ------ //
  while (1) {

    PORTB = 0b00000001;          /* Turn on first LED bit/pin in PORTB */
    _delay_ms(1000);                                           /* wait */

    PORTB = 0b00000000;          /* Turn off all B pins, including LED */
    _delay_ms(1000);                                           /* wait */

  }                                                  /* End event loop */
  return 0;                            /* This line is never reached */
}

Using avrdude, the code gets loaded on the chip with the following verbose output. The LED does not light up as expected, but shortly flickers when the code gets loaded on the chip.

AVR Development Stack
Get input main.c file...
Compile code and return elf file...
Convert elf file to hex...
Uploading data to microcontroller...

avrdude: Version 6.3, compiled on Sep 18 2017 at 20:31:51
         Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
         Copyright (c) 2007-2014 Joerg Wunsch

         System wide configuration file is "/usr/local/Cellar/avrdude/6.3/etc/avrdude.conf"
         User configuration file is "/Users/sebastianscharf/.avrduderc"
         User configuration file does not exist or is not a regular file, skipping

         Using Port                    : usb
         Using Programmer              : usbtiny
         Overriding Baud Rate          : 9600
avrdude: usbdev_open(): Found USBtinyISP, bus:device: 020:008
         AVR Part                      : ATmega328P
         Chip Erase delay              : 9000 us
         PAGEL                         : PD7
         BS2                           : PC2
         RESET disposition             : dedicated
         RETRY pulse                   : SCK
         serial program mode           : yes
         parallel program mode         : yes
         Timeout                       : 200
         StabDelay                     : 100
         CmdexeDelay                   : 25
         SyncLoops                     : 32
         ByteDelay                     : 0
         PollIndex                     : 3
         PollValue                     : 0x53
         Memory Detail                 :

                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           eeprom        65    20     4    0 no       1024    4      0  3600  3600 0xff 0xff
                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           flash         65     6   128    0 yes     32768  128    256  4500  4500 0xff 0xff
                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           lfuse          0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           hfuse          0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           efuse          0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           lock           0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           calibration    0     0     0    0 no          1    0      0     0     0 0x00 0x00
                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           signature      0     0     0    0 no          3    0      0     0     0 0x00 0x00

         Programmer Type : USBtiny
         Description     : USBtiny simple USB programmer, http://www.ladyada.net/make/usbtinyisp/
avrdude: programmer operation not supported

avrdude: Using SCK period of 10 usec
CMD: [ac 53 00 00] [00 00 53 00]
avrdude: AVR device initialized and ready to accept instructions

Reading |                                                    | 0% 0.00sCMD: [30 00 00 00] [00 30 00 1e]
CMD: [30 00 01 00] [00 30 00 95]
Reading | #################                                  | 33% 0.00sCMD: [30 00 02 00] [00 30 00 0f]
Reading | ################################################## | 100% 0.00s

avrdude: Device signature = 0x1e950f (probably m328p)
CMD: [50 00 00 00] [00 50 00 62]
avrdude: safemode read 1, lfuse value: 62
CMD: [50 00 00 00] [00 50 00 62]
avrdude: safemode read 2, lfuse value: 62
CMD: [50 00 00 00] [00 50 00 62]
avrdude: safemode read 3, lfuse value: 62
avrdude: safemode: lfuse reads as 62
CMD: [58 08 00 00] [00 58 08 d9]
avrdude: safemode read 1, hfuse value: d9
CMD: [58 08 00 00] [00 58 08 d9]
avrdude: safemode read 2, hfuse value: d9
CMD: [58 08 00 00] [00 58 08 d9]
avrdude: safemode read 3, hfuse value: d9
avrdude: safemode: hfuse reads as D9
CMD: [50 08 00 00] [00 50 08 ff]
avrdude: safemode read 1, efuse value: ff
CMD: [50 08 00 00] [00 50 08 ff]
avrdude: safemode read 2, efuse value: ff
CMD: [50 08 00 00] [00 50 08 ff]
avrdude: safemode read 3, efuse value: ff
avrdude: safemode: efuse reads as FF

CMD: [50 00 00 00] [00 50 00 62]
avrdude: safemode read 1, lfuse value: 62
CMD: [50 00 00 00] [00 50 00 62]
avrdude: safemode read 2, lfuse value: 62
CMD: [50 00 00 00] [00 50 00 62]
avrdude: safemode read 3, lfuse value: 62
avrdude: safemode: lfuse reads as 62
CMD: [58 08 00 00] [00 58 08 d9]
avrdude: safemode read 1, hfuse value: d9
CMD: [58 08 00 00] [00 58 08 d9]
avrdude: safemode read 2, hfuse value: d9
CMD: [58 08 00 00] [00 58 08 d9]
avrdude: safemode read 3, hfuse value: d9
avrdude: safemode: hfuse reads as D9
CMD: [50 08 00 00] [00 50 08 ff]
avrdude: safemode read 1, efuse value: ff
CMD: [50 08 00 00] [00 50 08 ff]
avrdude: safemode read 2, efuse value: ff
CMD: [50 08 00 00] [00 50 08 ff]
avrdude: safemode read 3, efuse value: ff
avrdude: safemode: efuse reads as FF
avrdude: safemode: Fuses OK (E:FF, H:D9, L:62)

avrdude done.  Thank you.

Best Answer

If your LED is flickering when the code is getting loaded, it's likely that you put it on the wrong pin. You are toggling PB0 - pin number 14. The ISP uses the SPI pins (PB1 - PB3) which are chip pin numbers 17 - 19 (for the DIP package).

ATmega328 Pins


You have some nice defines, but aren't using them. Although, I'd get rid of those junk macros in configGlobal.h. Try this:

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

#define LED _BV(PB0)     // = (1 << PB0)
#define LED_DDR DDRB
#define LED_PORT PORTB

int main (void) {   
  LED_DDR = LED;   // Set the LED pin as an output

  for(;;) {
    LED_PORT ^= LED;   // Toggles just the LED pin
    _delay_ms(1000);
  }
}

I verified this code on a virgin ATmega328p chip compiled with AVR-GCC and programmed using AVRDUDE. MCU is powered from 5V with an LED connected to Pin PB0 (chip pin 14) with a 1k series resistor.

schematic

simulate this circuit – Schematic created using CircuitLab