AVR Atmega MCUs come with ISP (In-system programming), which enables the programming of the microcontroller through its Three-wire SPI (Serial Peripheral Interface) port.
This interface is used to burn fuses, for programming a bootloader, or the whole chip.
ISP is the way to go if you either:
·Don't plan on using a bootloader.
·Can't communicate with the bootloader.
·Need to burn a bootloader.
·Need to change the fuses.
A bootloader is usually used in order to update the main code of the MCU through an UART port.
In order to start, we need a programmer for Atmega MCUs. A popular one easily found on AliExpress, etc, is USBasp.
Linux may be compatible, but Windows needs drivers for USBasp.
USBasp prorgammer
USBasp pinout
Connecting USBasp to an AVR using ISP
·First check that the voltage setting is correct, if your programmer comes with that option (switch or jumper).
We need 3+1+2 (6) cables for connecting the programmer to the MCU:
SPI interface:
·SCK (Clock)
·MOSI (Master Out Slave In)
·MISO (Master In Slave Out)
Reset:
·Reset (a 10k pullup resistor to reset may be needed)
Power:
·VCC
·GND
Connect everything together, but first, make sure no power is attached at all, and that the programmer isn't connected to the computer.
Having power on any I/O while a power cable is missing can damage the IC. Check the connections before plugging USBasp to the computer.
If SCK, MOSI, or MISO pins have something attached to them, ISP might not work.
Connections
SCK from USBasp goes to SCK on the AVR, MISO to MISO, MOSI to MOSI, Reset to Reset, and the power, VCC to VCC and GND to GND.
Now you're ready to program the AVR MCU using avrdude.
Using Arduino IDE
If you already have your board intalled on Arduino, choose the right board, then 'Tools' -> 'Burn bootloader' will burn the fuses and the bootloader to the board, using the selected programmer.
Burn Bootloader in Arduino IDE
Using avrdude
If you don't have a board installed and are using custom boards or bare MCUs, you'll have to upload using the
avrdude
command using a terminal.
Avrdude changing fuses example:
-U efuse:w:0xfd:m -U hfuse:w:0xde:m -U lfuse:w:0xff:m
Avrdude programming code example:
-U flash:w:/home/luna/MyCode.hex
For the programmer option try
-c usbasp
or
-c usbasp-clone
.
To obtain .hex file on Arduino, click 'Sketch', then 'Export compiled Binary'.
Export compiled Binary in Arduino
Use AVR Fuse Calculator to know the correct value of the fuses for your IC.
Webpage:
http://www.engbedded.com/fusecalc/
USBasp pinouts
Screenshot of Engbedded AVR Fuse Calculator
Digispark pinout
Attiny25/45/85 pinouts
Attiny45/85 and Attiny44/84 pinouts
Atmega328P pinout
Atmega1284P pinout
ATtiny10/9/5/4
Smaller devices such as Attiny10 can also be programmed using an USBasp programmer.
Attiny10 doesn't use SPI for programming, it uses a different programming protocol called TPI (Tiny Programming Interface).
ATtiny10 pinout
Connecting the USBasp programmer to an ATtiny10
To program Attiny10 try
avrdude -p attiny10 -c usbasp-clone -U flash:w:/home/user/MyCode.hex:i