I have two ATMEGA328's and I'm experiencing a very strange problem. I have a very simple code to blink a LED:
#define F_CPU 8000000UL
#include <avr/io.h>
#include <util/delay.h>
#include <stdlib.h>
void delayms( uint16_t millis )
{
while ( millis )
{
_delay_ms( 1 );
millis--;
}
}
int main( void )
{
DDRD = 0b11111111;
int interval = 1000;
while ( 1 )
{
PORTD = 0b11111111;
delayms( interval );
PORTD = 0b00000000;
delayms( interval );
}
return 0;
}
And here is the Makefile:
CC=/usr/bin/avr-gcc
MEGA=328p
CFLAGS=-g -Os -Wall -mcall-prologues -mmcu=atmega$(MEGA)
OBJ2HEX=/usr/bin/avr-objcopy
PROG=/usr/bin/avrdude
TARGET=blink
program : $(TARGET).hex
$(PROG) -c avrispv2 -p m$(MEGA) -P /dev/ttyACM2 -e
$(PROG) -c avrispv2 -p m$(MEGA) -P /dev/ttyACM2 -U flash:w:$(TARGET).hex
%.obj : %.o
$(CC) $(CFLAGS) $< -o $@
%.hex : %.obj
$(OBJ2HEX) -R .eeprom -O ihex $< $@
clean :
rm -f *.hex *.obj *.o
The code works perfectly, but here's the strange part:
ATMEGA #1:
- Works as expected when connected straight to the 5V source. Meaning it executes the last code, that was written to it.
- Can't be programmed. avrdude: stk500v2_command(): command failed. Can't connect.
ATMEGA #2:
- Works only through Arduino case. When connected straight to the 5V source, PORTD outputs nothing.
- Can be programmed.
This mystery has happened so many times before. All I can think of is that it has something to do with the fuse bits or possibly the frequency is 'hardcoded' for one chip at 8Mhz and 16Mhz for the other one. I was expecting that F_CPU defines that and avrdude uses that definition to set the fuses correctly.
I don't want to use an external oscillator. Any hints on what's going on?
UPDATE: Here's the output for one of the ATMEGAs that failed to program.
/usr/bin/avrdude -c avrispv2 -p m328p -P /dev/ttyACM0 -e -U lfuse:w:0xe2:m -B 250.0 -U hfuse:w:0xd9:m
avrdude: AVR device initialized and ready to accept instructions
Reading | ################################################## | 100% 0.07s
avrdude: Device signature = 0x1e950f
avrdude: erasing chip
avrdude: reading input file "0xe2"
avrdude: writing lfuse (1 bytes):
Writing | ################################################## | 100% 0.07s
avrdude: 1 bytes of lfuse written
avrdude: verifying lfuse memory against 0xe2:
avrdude: load data lfuse data from input file 0xe2:
avrdude: input file 0xe2 contains 1 bytes
avrdude: reading on-chip lfuse data:
Reading | ################################################## | 100% 0.02s
avrdude: verifying ...
avrdude: 1 bytes of lfuse verified
avrdude: reading input file "0xd9"
avrdude: writing hfuse (1 bytes):
Writing | ################################################## | 100% 0.02s
avrdude: 1 bytes of hfuse written
avrdude: verifying hfuse memory against 0xd9:
avrdude: load data hfuse data from input file 0xd9:
avrdude: input file 0xd9 contains 1 bytes
avrdude: reading on-chip hfuse data:
Reading | ################################################## | 100% 0.02s
avrdude: verifying ...
avrdude: 1 bytes of hfuse verified
avrdude: safemode: Fuses OK (E:07, H:D9, L:E2)
avrdude done. Thank you.
/usr/bin/avrdude -c avrispv2 -p m328p -P /dev/ttyACM0 -U flash:w:serial.hex
avrdude: AVR device initialized and ready to accept instructions
Reading | ################################################## | 100% 0.07s
avrdude: Device signature = 0x1e950f
avrdude: NOTE: "flash" memory has been specified, an erase cycle will be performed
To disable this feature, specify the -D option.
avrdude: erasing chip
avrdude: reading input file "serial.hex"
avrdude: input file serial.hex auto detected as Intel Hex
avrdude: writing flash (912 bytes):
Writing | | 0% 0.00s
avrdude: stk500v2_ReceiveMessage(): timeout
Writing | ###### | 12% 2.84s
avrdude: stk500v2_ReceiveMessage(): timeout
Writing | ############# | 25% 5.68s
avrdude: stk500v2_ReceiveMessage(): timeout
Writing | ################### | 37% 8.52s
avrdude: stk500v2_ReceiveMessage(): timeout
Writing | ############################### | 62% 14.20s
avrdude: stk500v2_ReceiveMessage(): timeout
Writing | ###################################### | 75% 17.04s
avrdude: stk500v2_ReceiveMessage(): timeout
Writing | ############################################ | 87% 19.89s
avrdude: stk500v2_ReceiveMessage(): timeout
Writing | ################################################## | 100% 22.73s
avrdude: 912 bytes of flash written
avrdude: verifying flash memory against serial.hex:
avrdude: load data flash data from input file serial.hex:
avrdude: input file serial.hex auto detected as Intel Hex
avrdude: input file serial.hex contains 912 bytes
avrdude: reading on-chip flash data:
Reading | | 0% 0.00s
avrdude: stk500v2_ReceiveMessage(): timeout
Reading | ###### | 12% 2.80s
avrdude: stk500v2_ReceiveMessage(): timeout
Reading | ################### | 37% 8.39s
avrdude: stk500v2_ReceiveMessage(): timeout
Reading | ######################### | 50% 11.18s
avrdude: stk500v2_ReceiveMessage(): timeout
Reading | ############################### | 62% 13.98s
avrdude: stk500v2_ReceiveMessage(): timeout
Reading | ############################################ | 87% 19.57s
avrdude: stk500v2_ReceiveMessage(): timeout
Reading | ################################################## | 100% 22.37s
avrdude: verifying ...
avrdude: verification error, first mismatch at byte 0x0000
0x80 != 0x0c
avrdude: verification error; content mismatch
avrdude: safemode: Fuses OK (E:07, H:D9, L:E2)
avrdude done. Thank you.
Makefile:9: recipe for target 'program' failed
make: *** [program] Error 1
-v
). e.g.avrdude -c avrispv2 -p m328p -P /dev/ttyACM2 -v
and post what you get. It should cast some light on what fails. \$\endgroup\$