Serial Bootloader for AVR ATMega8
One great thing about AVR microcontroller is that they can “program theirselves”. This is not unusual for nowadays microcontrollers but for a legacy like device like an AVR it is really nice to have.
The idea is: When the AVR wakes up from reset a special program is started, called the boot loader. This program usually waits for some kind of action like a pulled up pin or data reception via the serial port. If non of these events occur in a specified time, the boot loader hands the control on to the main program.
For many applications it is nice to be able to change the program running on the microcontroller without the need of a special programming hardware. The boot loader presented in this post will enable you to do so.
- Check out my repository using mercurial ( hg clone https://bitbucket.org/befi/remote-controlled-socket )
- The folder uc/avrprog_boot_v0_85 contains the boot loader
- Type make to build the bootloader
- Insert the ATMega8 into your AVR-MK2 programmer and type make program and the boot loader will be flashed and the fuses of the AVR are set accordingly (clock is set to 8MHz)
The makefile uses avrdude to flash the chip. If you use a different programmer than the MK2 you can change the parameters in the Makefile.
Ok, now we have a AVR with a serial boot loader installed. What next? Take a look at the folder uc/analog2usb in my repository. You’ll find an example program which reads values from the ADC and outputs them via the serial port.
What is interesting about that example is the Makefile. You can flash the program over the serial port into the microcontroller without using a programmer.
- Attach a Serial-To-USB converter to your microcontroller. This should give you a /dev/ttyUSB0 device unter Linux
- Reset the microcontroller and within 2 seconds execute make program from the analog2usb example
- Voilà, your AVR is now programmed via serial
To explain what has happened here: After reset the AVR waits for two seconds for the programming byte “S” via the serial line at 19200 baud. If the “S” character is received within two seconds, the boot loader emulates a AVR109 serial programming dongle. After that avrdude is called to flash the main program into the chip.