Many of my projects use AVR controllers from Atmel. And they need to be "programmed". This means that the software needs to get written into their internal program memory.
For beginners, this can be a major stumbling block in any project. And I do get lots of questions in connection with programming. So I decided to write this tutorial. But my intention is not just to give a list of buttons you have to press. Instead I want you to understand what you are doing.
This tutorial assumes you have a Windows PC and use an USBasp programmer. They are available from your favourite auction website (very cheap, ready made, from China). Alternatively you can support their creator Thomas Fischl and buy his kit.
This tutorial consists of four parts:
The Theory (this post)
Writing Fuses / Troubleshooting
One of the differences between a microcontroller and a microprocessor is that the controller contains its own "non-volatile" memory. This is memory that doesn't lose its contents when the power supply is switched off. AVRs actually have three types of non-volatile memory:
"Program Memory" or "Flash Memory"
This is the type of memory, that you must write to in every project. It contains the executable code, which tells the controller what to do. Depending on the model of AVR, it can be written between 1,000 and 10,000 times. With no contents in this memory, the AVR is like a computer with an empty hard drive.
"Data Memory" or "EEPROM"
This is meant for data. It can not contain executable code for the AVR. You can keep any data in this type of memory. This can be constant or variable data. It can be written 100,000 times. Not all projects need an EEPROM. Some smaller AVRs haven't even got EEPROM.
There are a few extra bits in AVRs, which control the general behaviour of the chip. See the respective data sheets for details.
- Lock bits can control read/write access to the chip. They are meant to protect memory contents from being overwritten or read. They are a kind of copy protection. Lock bits can be erased only with the chip erase command, which also erases all other memories. Advice for beginners: Keep away, can cause unnecessary trouble.
- Fuse bits are there to set some hardware-related properties of the controller. E.g. with fuse bits you can select your system clock source. Without the correct clock source selected, your chip wouldn't even start. So these bits couldn't be set by software. BE CAREFUL! You can disable ISP programming with fuses, which means that wrong fuse settings could leave you with an inaccessible device - at least if you have no access to a high voltage programmer.
- Calibration bytes are there to fine tune the internal clock generator. I guess that this can be useful, but if you need an accurate clock just use an external crystal.
- Signature bytes (read-only) contain a number that is unique to the type of AVR which you have selected. In a production environment this can be used to verify that the correct type of controller is used. It is also useful to test whether the programmer is working, because you can read that known data and check, whether or not you where able to get it right.
For most AVR controllers, there are at least two ways to program them. One is called "High Voltage Parallel Programming". For this mode you stick the controller in a special socket of your programmer. This gets you more or less direct access to the memory, but means you need a lot of pins. The mode is activated by (amongst other things) applying 12 volts to the AVR's reset pin. Also, you have to supply the system clock through the programmer.
With small ATtiny controllers, you simply don't have enough pins for parallel programming, so instead something called "High Voltage Serial Programming" is used. Again, this mode is activated by setting the reset pin to 12 volts. But the data in this case has to be sent in a serial format.
Both "high voltage" modes have one disadvantage: They can usually not be used while the controller is in the target system.
There is a programming mode called ISP (In-System Programming). This mode needs no high voltage. So you can leave the microcontroller in the system, while you program it. It is a serial mode, so all you need are the pins MISO / MOSI / SCK for the serial interface. Plus, you need RESET to switch to this mode. The pins can be shared between normal function and the programming interface. Just make sure that the pins are not driven by external periphery during programming.
Of course, even if this mode can be used while the controller is in the system you can build a little programming board. And I would recommend to do this, if you start out with microcontrollers. It means that you have no kind of interference by any peripheral hardware. So this is just the basic advice of keeping it simple. Here is an example of a small ATTiny13 board (I added an LED on the unused pins for some experiments):
In part 2 we are going to look at the hardware side of things.