atmega168 corrupted after upload an HEX file?

SOLVED, VIEW THE FIX IN THE NEXT REPLY

Hi all.

After reading a bunch of post I couldnt found any fix for my problem:

Playing with my Arduino-NG I followed this topic about how to implement TV Video-Out with Arduino. One step, here, required to upload an HEX file to arduino using avrdude.

Here the hex file:
Here the c source

I uploaded but didn't work at all. The problem came once I wanted to upload a different sketch using the Arduino application. It was impossible and I got this error message:

Couldn't determine program size:    text         data          bss          dec          hex      filename

avrdude: verification error, first mismatch at byte 0x0002
         0x61 != 0x5c
avrdude: verification error; content mismatch

It happens with any sample sketches.

I found some posts about problems uploading code to Arduino and one possible reason is that the bootloader is corrupted, so I pushed the reset button and I got the three blink led light that's is supposed to get if you have the bootloader burned in the ATMEGA168.

Anyway, I wondered that maybe the bootloader is there, but corrupted, so I tried to load it again using avrdude. That's what I got:

./avrdude -p m168 -c stk500v1 -P /dev/ttyUSB0 -b 19200 -U flash:w:../bootloaders/atmega/ATmegaBOOT_168_ng.hex 

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.02s

avrdude: Device signature = 0x1e9406
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 "../bootloaders/atmega/ATmegaBOOT_168_ng.hex"
avrdude: input file ../bootloaders/atmega/ATmegaBOOT_168_ng.hex auto detected as Intel Hex
avrdude: writing flash (16038 bytes):

Writing | ################################################## | 100% 1.30s

avrdude: 16038 bytes of flash written
avrdude: verifying flash memory against ../bootloaders/atmega/ATmegaBOOT_168_ng.hex:
avrdude: load data flash data from input file ../bootloaders/atmega/ATmegaBOOT_168_ng.hex:
avrdude: input file ../bootloaders/atmega/ATmegaBOOT_168_ng.hex auto detected as Intel Hex
avrdude: input file ../bootloaders/atmega/ATmegaBOOT_168_ng.hex contains 16038 bytes
avrdude: reading on-chip flash data:

Reading | ################################################## | 100% 12.05s

avrdude: verifying ...
avrdude: verification error, first mismatch at byte 0x0000
         0xff != 0x0c
avrdude: verification error; content mismatch

avrdude: safemode: Fuses OK

avrdude done.  Thank you.

Reading more post I found the people use the AVRISP mkII to program the ATMEGA168, so I bought one with the aim to fix the problem with the same devices about I was reading.

No way.

I got the same error type:

avrdude: verification error; content mismatch

So, at this point I don't really know what I'm doing (but anyway, I'm learning a lot)

I don't have big skills on coding, so I feel a little bit lost.

Maybe one way to fix it is erasing completely the flash memory and reload the bootloader... but I don't really know.

I would really thank any suggestion or useful documentation to fix this problem.

I'm using both Ubuntu linux and OSX 10.5, Arduino 11 and Arduino 15 and I get the same errors in both platforms.

Thanks to all in advance.

Rez

Ok. Solved.

After read a similar post that I just found, I've fixed it setting up the ATMEGA168 from scracth (as if it was a blanck chip).

  1. Connect the AVRISP mkII to the USB port
  2. Connect the Arduino board to the AVRISP mk II
  3. Plug an external power source to Arduino and change the power selection jumper
  4. Then:
sudo avrdude -c avrispmkII -p m168 -P usb: -B 6 -v -U lock:w:0x3f:m -U lfuse:w:0xff:m -U hfuse:w:0xdf:m -U efuse:w:0x0:m
  1. And then:
sudo avrdude -c avrispmkII -p m168 -P usb: -B 6 -v -U flash:w:ATmegaBOOT_168_diecimila.hex -U lock:w:0x0f:m

Tried to upload a sketch with the Arduino IDE and IT WORKED!

The KEY was the -B parameter, "bit clock period". Before I didn't used, and as I see, it's essential.

:wink: