AVR ATMEGA328p LED Ejemplo

1

Tengo el siguiente código para probar un 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 */
}

Usando avrdude, el código se carga en el chip con la siguiente salida detallada. El LED no se enciende como se esperaba, pero parpadea brevemente cuando el código se carga en el 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.
    
pregunta sesc360

1 respuesta

1

Si su LED parpadea cuando el código se está cargando, es probable que lo coloque en el pin incorrecto. Está alternando PB0 - número de pin 14. El ISP usa los pines SPI (PB1 - PB3) que son los números de pin de chip 17 - 19 (para el paquete DIP).

Tienesalgunasdefinicionesagradables,peronolasestásutilizando.Aunque,medesharíadeesasmacrosnodeseadasenconfigGlobal.h.Pruebaesto:

#defineF_CPU1000000UL#include<avr/io.h>#include<util/delay.h>#defineLED_BV(PB0)//=(1<<PB0)#defineLED_DDRDDRB#defineLED_PORTPORTBintmain(void){LED_DDR=LED;//SettheLEDpinasanoutputfor(;;){LED_PORT^=LED;//TogglesjusttheLEDpin_delay_ms(1000);}}

VerifiquéestecódigoenunchipvirgenATmega328pcompiladoconAVR-GCCyprogramadousandoAVRDUDE.LaMCUsealimentadesde5VconunLEDconectadoalPinPB0(chippin14)conunaresistenciadelaserie1k.

simular este circuito : esquema creado usando CircuitLab

    
respondido por el Kurt E. Clothier

Lea otras preguntas en las etiquetas