5
Start up programming AVR microcontrollers
Still have a PC with a parallel port laying around? In this AVR tutorial I will describe how to setup a minimalist system to develop AVR programs, using the C programming language, under Windows. This comprises of a really cheap hardware programmer, a test circuit and PC software. You will end following this tutorial with an AVR programmed by you blinking a LED on your bench . I’ll not explain in detail what all commands do; this tutorial focuses on setting up the development environment, leaving the applications development for another tutorial.
Portuguese speakers: Podem ver uma versão portuguesa deste artigo aqui.
“AVR” is the name of a family of 8-bit microcontrollers developed by ATMEL. The architecture was designed by two Norwegian PhD students in 1992, and then proposed to ATMEL for commercialization. A small video about the AVR can be seen on AVRTV: AVR-TV Special
AVR consists, just like a PIC and other microcontroller families, in: a core processor, volatile and non-volatile memories and peripherals. Contrary to PIC, the AVR core was well thought-off from the beginning, and the core used in the chips designed today is still the same core from the 1st AVRs 10 years ago, same (although augmented) instruction set even at the binary level.
In a quick way we can define AVR’s architecture in a few items:
- Consists of a processing core (processor), program memory (non-volatile, FLASH), volatile memory (static RAM, SRAM), persistent data memory (non-volatile, EEPROM) and fuse/lock bits (configuration of special features).
- Harvard memory architecture (program and data in separate memory spaces/buses).
- Continuous addressing of the SRAM.
- Most instructions are 16-bit sized, which is the size of a program memory (FLASH) word.
- Most instructions execute in 1 system clock cycle.
- The processor has 32 8-bit registers, with little limitations on what can be done with each.
- Both the processor and peripheral configuration registers are mapped into SRAM addressing space.
- There’s a unique interrupt vector for each interrupt source.
- The processor supports instructions with complex addressing calculation.
- The instruction set was developed to be well adapted to code generation from C source.
Although there are many AVR models, varying in the sizes of the several memories and number/type of peripherals, I’ll center the tutorial in one specific AVR model: ATtiny26 (ATtiny261/ATtiny461/ATtiny861 can also be used without any change). This AVR model, although far from being one of the big ones, has, in my opinion, an excellent price/functionality balance; has ADC, counters/timer, hardware to ease the implementation of SPI/UART/I2C interfaces, PWM, up to 16 MHz system clock (up to 20 MHz for ATtiny261; a PIC would need to have a 64 MHz clock to approach the 16 MHz clock AVR), and more. I’ll also use C language, since it’s much easier to start with than the hardcore assembly language.
Development Tools
You need to get the WinAVR package and install it. WinAVR is an Open Source development tools set for the AVR family, based on a GCC port. The package contains a code editor (Programmer’s Notepad), the C/C++ toolchain (compiler, linker, other tools), AVR simulator and programming software. We’ll use toolchain and the programming software. You can download it here: WinAVR (23MB).
After install, you need to add a description of our programmer hardware to avrdude’s configuration file. avrdude is the name of the AVR programming application that is included in the WinAVR package. So, open the file <your-install-directory>\WinAVR\bin\avrdude.conf and search for the following text comment:
# Parallel port programmers.
This comment marks the beginning of the parallel port (LPT) programmers configuration area of the file. At that point, insert the following text, which is a description of how to interface with our programmer’s hardware:
# AVR Tutorial programmer at http://EmbeddedDreams.com/ programmer id = "avrpt"; desc = "AVR Tutorial at EmbeddedDreams.com/"; reset = 6; sck = 8; mosi = 7; miso = 10; type = par; ;
avrdude is a very flexible program which accepts descriptions of hardware programmers typed in its avrdude.conf configuration file. That very same file stores the parameters of every AVR model, instead of them being hardwired in avrdude’s binary code. This way, new programmers and AVR models can be supported by avrdude just by adding a few text lines to a file; smart, hum ?
The AVR programmer hardware that is described in this tutorial uses a PC’s parallel port (printer port), which means that you must have or have access to a PC with a parallel port. There are USB<->LPT converters on the market, but these usually do not work, because they don’t respect times between operations. You also need to have administration privileges on the PC in order to be able to install a parallel port Windows driver.
Next we look a the hardware.
Hardware
Programmer Device
We need a programmer device to save (program) our programs into the AVR. The AVR has 2 programming modes: parallel/high-voltage mode and serial/low-voltage mode. These modes allow read and write access to the AVR non-volatile memories and fuse/lock bits. The circuit below is a serial mode programmer, which I’ll use in this tutorial.
This circuit must be assembled inside the parallel port connector (DB25), and it is essential that it stays as close as possible to the PC connector (that’s why it needs to be inside the connector’s box!). You should not use a parallel port extension cable and should try to keep the strip of flat cables short.
Test Circuit
Assemble the circuit below in a solderless breadboard. This is a minimal circuit to have the AVR running and prove to us that it is in fact running :). It can be powered by any voltage between 4.5 V and 5.5 V, typically 5V. However, most AVRs will run at voltages starting from 3V so, since in this simple circuit the AVR will run at low speed and to make things easier, you can use a pair of AA or AAA 1,5 V batteries, or even a “coin” 3 V lithium cell (ATtinyx61 versions run from 2.7V to 5.5V). If the circuit doesn’t work, like giving you programming “verify errors”, then power it with a higher voltage, like with 3 series AA or AAA bateries (givin a total of 4.5 V).
In this circuit we have only 1 LED as an “actuator”, but after this tutorial you will certainly get creative and connect many other things to the AVR . I use a double AA battery support to power the circuit:
If you have ever tried to put a male header row in a solderless breadboard, you have noticed that the pins aren’t long enough to stick to the board. To solve that problem pick a pliers and push the pins slowly 1 or 2 mm, using the black plastic base as a support for one of the pliers “fingers”, like this:
Now the header will hold in the breadboard.
I used these cheap male headers, but you can also use the “male header connector” that mates with the white female connector I use for the programmer’s cable.
Hardware test
Well, before starting to make programs to run in the AVR, you need to check if the hardware (programmer device and test circuit) is working. You can do it by checking if the programmer software, avrdude, is able to recognize and “speak” to your AVR.
Start by installing the parallel port driver in your PC, which will grant parallel port access to avrdude. Go to Windows Start menu and then Execute/Run, and type the command
runas /user:administrator cmd
where administrator is the name of a user on your PC that has administration privileges. This command opens a Command Line where the commands issued will be issued as if user administrator had issue them. If the user you currently use already has administration privileges, then it’s enough to directly open a Command Line.
So, on the opened Command Line, go to (cd) directory …\WinAVR\bin\ and run install_giveio.bat to install the driver, as in the session below (I have WinAVR installed under c:\devtools\):
Check if the driver install was successful; you should see the same messages as above. In theory you should have to do this only once, because the driver will install itself as a windows service and will startup automatically on a PC reboot. You can now close your “administration” command line and open a new, normal one (Start -> Execute/Run -> type “cmd” and press ENTER).
Make a working directory for this tutorial, for example c:\tutorial and move (cd …) there. Next, connect the programmer to the PC’s parallel port and to the test circuit.
Pay attention to the white connector orientation. Then connect the power source to the test circuit and check if avrdude is able to “speak” to the AVR, issuing the command:
C:\tutorial> avrdude -c avrpt -p t26 -i 50
avrdude: AVR device initialized and ready to accept instructions
Reading | ################################################## | 100% 0.01s
avrdude: Device signature = 0×1e9109
avrdude: safemode: Fuses OK
avrdude done. Thank you.
C:\tutorial>
The orange (it won’t be orange, I painted it :)!) is a good indication of a successful conversation, since avrdude was able to get the AVR’s signature, a code which identifies the AVR model. In case of failure, you will clearly understand from the messages… and then you’ll have to check if the programming device is correctly assembled, if the connections are correct, if the circuit is correctly assembled, if you powered up the circuit, and so on. You will only be able to program an AVR if the test above succeeds and you can see the device’s signature.
If everything went just right, then continue to the next tutorial section where we’ll look into software to run on the AVR.
Software
If you got the guts to reach this point in the tutorial, then you should be eager to program your 1st AVR :). Let’s create a simple program and then burn it in the AVR! “To burn” is an expression meaning “to save in non-volatile memory, nothing will be really burnt, don’t worry :).
The program you’ll do is the dream program of any beginner, a blinking LED . Copy the following text into a file and save it as led_blink.c.
---- cut here --------------------------------------------- #include <avr/io.h> #include <util/delay.h> int main (void) { DDRA = 0x01; // Configure pin PA0 as output do { // loop start PORTA = 0x01; // Turn LED on _delay_ms(250); // Wait 250ms... PORTA = 0x00; // Turn LED off _delay_ms(250); // Wait 250ms... } while (1); // Jump to loop start } ---- cut here ---------------------------------------------
Then compile it with the following commands:
C:\tutorial> avr-gcc -mmcu=attiny26 -DF_CPU=1000000UL -g -O1 -o led_blink.elf led_blink.c
C:\tutorial> avr-objcopy -j .text -j .data -O binary led_blink.elf led_blink.bin
C:\tutorial>
Finally, a last command will program the AVR while you observe it in delight :
C:\tutorial> avrdude -c avrpt -p t26 -i 50 -U flash:w:led_blink.bin:r
avrdude: AVR device initialized and ready to accept instructions
Reading | ################################################## | 100% 0.01s
avrdude: Device signature = 0×1e9109
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 “led_blink.bin”
avrdude: writing flash (108 bytes):
Writing | ################################################## | 100% 0.38s
avrdude: 108 bytes of flash written
avrdude: verifying flash memory against led_blink.bin:
avrdude: load data flash data from input file led_blink.bin:
avrdude: input file led_blink.bin contains 108 bytes
avrdude: reading on-chip flash data:
Reading | ################################################## | 100% 0.35s
avrdude: verifying …
avrdude: 108 bytes of flash verified
avrdude: safemode: Fuses OK
avrdude done. Thank you.
C:\tutorial>
As the AVR programming ends, and if it is successful just like in the example above, you’ll have a blinking LED before your eyes. Congratulations, you’ve just program your first AVR !
Nuno
Links
http://avrfreaks.net - AVR-centric forum
http://avrbeginners.net - a good place for beginners
Did you like this article? Have questions, critics, suggestions?
I want your feedback!
Hi,
Nice tutorial. I have also written some tutorials for AVR MCUs you can see them at
http://extremeelectronics.co.in/category/avr-tutorials/
Gr8 tutorial….
Maybe I’ll check it out….