I have been having problems with my ATMEGA8 program memory getting corrupted when I cycle the power on my experimental digital clock board:
http://cosinekitty.com/digitalclock
I am using a 9V wall adapter, and apparently what was happening was that when I unplug it, it has a capacitor that gradually loses its charge, so the voltage it supplies to the voltage regulator gradually (meaning many, many milliseconds!) falls to zero. I confirmed in the ATMEGA8 data sheet, and elsewhere, that this can cause the ATMEGA8 to execute instructions incorrectly, and thereby overwrite program memory.
I also found a fix: for the ATMEGA8 only, you can change the low fuse byte from 0xdf to 0x1f. This will NOT work on ATMEGA168; I have not yet read that microcontroller's data sheet to figure out the proper value. Quit out of Arduino if you are running it, then edit the file hardware/boards.txt in your Arduino install and add the following lines at the front of the file:
##############################################################
atmega8bod.name=ATMEGA8 with brownout detection
atmega8bod.upload.protocol=stk500
atmega8bod.upload.maximum_size=7168
atmega8bod.upload.speed=19200
atmega8bod.bootloader.low_fuses=0x1f
atmega8bod.bootloader.high_fuses=0xca
atmega8bod.bootloader.path=atmega8
atmega8bod.bootloader.file=ATmegaBOOT.hex
atmega8bod.bootloader.unlock_bits=0x3F
atmega8bod.bootloader.lock_bits=0x0F
atmega8bod.build.mcu=atmega8
atmega8bod.build.f_cpu=16000000L
atmega8bod.build.core=arduino
Note that I just copied the "Arduino NG or older w/ ATmega8" configuration, renamed all the "atmega8" prefixes to "atmega8bod" (bod = Brown Out Detection), changed low_fuses from 0xdf to 0x1f, and the "name" field.
After saving boards.txt, run Arduino, hook up your bootloader hardware (I use the do-it-yourself parallel port), and select Tools/Board. Choose the "Custom ATMEGA8 with brownout detection" option (the one we just added to boards.txt). Then choose Tools/Burn Bootloader, and then the appropriate hardware connection.
Afterward, you will need to hook up the serial port and reload whatever software you want to run on the chip, because the bootloader burn will have erased anything in there.
So far this seems to be working... my program memory is no longer corrupted, no matter how many times I unplug and replug my wall adapter.
My request to the Arduino developers is, can you provide options for all of these in the standard Arduino install? If it would help, I can try to research what all the fuse bits should be for the other microcontrollers. I will need somebody else to help me test in all the hardware configurations I do not own.
Thanks,
Don