Mastering AVR Microcontrollers: A Hands-On Guide to Project Development

AVR microcontrollers boast distinct advantages compared to similar microcontrollers, offering higher MIPS and a more user-friendly architecture. This article will delve into mastering the AVR Integrated Development Environment (IDE), guiding readers through the process of creating an executable project. Additionally, it will cover building a fundamental AVR circuit and programming it effectively.

Schematic

How to Build the Circuit

Constructing the circuit proves straightforward and adaptable to various construction techniques like breadboard, stripboard, veroboard, or PCB assembly.

The project schematic illustrates a basic power regulator circuit (utilizing the 7805) to supply the ATMEGA device with 5V. However, note that the USBASP programmer supplies around 3.3V. Despite this, employing an external power source is advisable to prevent the USBASP from potentially drawing excessive current from any USB port.

The USBASP programmer I acquired includes a converter to switch the 10-pin program header to a more user-friendly 6-pin format. However, this header, featuring a two-row 2.54mm pitch spacing, isn’t directly compatible with a breadboard. To address this issue, I resolved it by linking the header to wires that reach and connect to the different programming pins on the breadboard.

How to Install AVR Studio

AVR devices are programmed using a customized version of Visual Studio 2015 known as AVR Studio 7, available for download from the AVR website. To initiate the programming process for AVR devices, start by downloading the web installer, a compact application responsible for acquiring the necessary files. After the download completes, proceed to execute the installer.

During the initial installation, you’ll encounter prompts regarding license agreements and the option to send anonymous information. Personally, I advise against sending anonymous data for various reasons (primarily due to extreme caution).

The initial installer page.

The following page prompts you to choose the architecture for installation. Given that these tutorials exclusively focus on devices within the AVR 8-bit MCU range, this will be the sole selection. Nonetheless, opting for all options could be advantageous in the long run, especially if you have a decent download speed (mine is only 4mbps).

Device selection page

After completing the device support selection, the subsequent page prompts you about the Atmel Software Framework and example projects. At this point, make sure to have this option selected or checked.

The following page performs a computer check to identify potential issues, such as pending restarts due to updates or system compatibility concerns that might hinder the IDE’s operation. Once all checks are verified, proceed by clicking “Next.”

If a previous version of the AVR IDE is already installed, you might encounter the following window. If this occurs, simply click on the “Install” button.

Now it is all about patience, waiting for the IDE to download all the needed components and install.

After completion, a prompt will appear giving you the option to launch Atmel Studio 7.0. Make sure to check this option before closing the installation window.

How to Install WINAVR

Although Atmel Studio 7 serves well for code writing, it might not be the optimal choice for programming AVR devices through open-source tools. To streamline the compilation and programming phase, we’ll utilize WINAVR, bundled with AVRDUDE, facilitating code uploading to the Atmega device. To acquire WINAVR for Windows, click on this link and choose the exe file for download.

After the download completes, initiate the installer and navigate through the provided options until prompted for an installation destination.

The recommended installation directory for WINAVR is C:\WinAVR-20100110. When prompted for PATH environment options, ensure that all the checkboxes (as illustrated below) are selected.

Once the install button is clicked, the installer will do the rest of the work, and once done, the installer will close.

How to Connect USBASP and Install Driver for Windows

If you’re using Windows, changing the USB driver for the USBASP device can be accomplished effortlessly using a tool called Zadig. Here are the steps: Firstly, download Zadig from the provided link, connect your USBASP device to your PC, and wait for Windows to automatically install USBASP.

Next, launch Zadig, locate the USBASP device from the dropdown menu, and choose the driver labeled “libusb-win32 (v1.2.6.0).” After selecting this driver, click on “Replace Driver” or “Reinstall Driver.”

Zadig will then proceed to automatically install the necessary driver required to enable USBASP functionality on Windows.

How to Configure Atmel Studio 7 and WinAVR

Atmel Studio 7 does not inherently support WINAVR or AVRDUDE, necessitating configuration adjustments. Despite appearing daunting, the setup is straightforward. Only one instruction line for AVRDUDE is necessary to program the device, along with defining a single directory. The instructions provided in this article should be easily applicable to any ATMEGA168-based project by simply copying and pasting the provided code.
Therefore, the initial action involves informing Atmel Studio 7 about the preferred compiler to utilize. To accomplish this, open Atmel Studio 7 (if not already open) and navigate to Tools > Options.
Within the opened window, utilize the left-hand list to access Toolchain > Package Configuration. In the now-displayed options on the right, choose Atmel AVR 8-bit (C language) from the drop-down menu, then proceed to click on “Add Flavor” (or “Flavour” as per US spelling).
If WINAVR is installed in its default location (on the C drive) and matches the version mentioned in this article, the displayed window can be completed as indicated below. After filling in the details, click on “Add”. Upon returning to the previous window, finalize the process by clicking “OK”.

Atmel Studio 7 is now capable of compiling your AVR programs using WINAVR. However, to enable device programming, an external tool must be added and configured for the ATMEGA168 device.

To begin, go to Tools > External tools. This window is essential for configuring the USBASP programmer. Below is a window displaying most of the necessary details that require filling in.

The arguments box doesn’t display all necessary information, so here’s what was input:

avrdude -c usbasp -p atmega168 -U lfuse:w:0x26:m -U flash:w:$(ProjectDir)Debug$(TargetName).hex:i

While most of this argument’s details might not concern you, two specific parts are crucial. Firstly, “-p atmega168” signifies to AVRDUDE that the programming is for an ATMEGA168. If your project involves a different chip, replace “atmega168” with your specific device, like “atmega88.”

The second parameter, “-U lfuse:w:0x26:m,” is ATMEGA168-specific. This command configures the device to use an external crystal. Once programmed, the device will only operate when connected to a crystal circuit (refer to the schematic).

Keep in mind that this also means the device necessitates a crystal during programming. Importantly, this argument solely applies to the ATMEGA168! Attempting to use it with other devices may result in damaging those devices (rendering them unusable). To simplify, consider acquiring an ATMEGA168.

Creating Your First Project

The subsequent step is to initiate an AVR project to verify the circuit, compiler, and programmer.

Begin by going to File > New > Project. In the ensuing window, opt for “GCC C Executable Project” and designate a name for the project in the provided text box.

 
The subsequent window you should encounter is the device selection window. From this list, choose the Atmega168. As far as I am aware, this window seems unnecessary since we manually pass the device name to AVRDUDE. Thus far, I haven’t discovered a way to make Atmel Studio 7 automatically transmit the device name to AVRDUDE through arguments.
The outcome should be a main.c file housing our program code for the AVR to execute. However, the generated code remains non-functional. Therefore, replace the entire content of the main.c file with the following program (ensure to save the file after entering the new code).

Now, let’s proceed to compile and upload the code to the AVR device.

Begin by confirming that our project utilizes the WINAVR compiler. Right-click on the project within the solution window and access the “Advanced” section in the properties window. Within this “Advanced” window, verify that WINAVR is chosen in the Toolchain Flavour box.

Save the project and compile it by clicking Build > Build Solution (or pressing F7).

If everything progresses as expected, you should encounter the following message in the output window:

Build succeeded.

========== Build: 1 succeeded or up-to-date, 0 failed, 0 skipped ==========

This indicates that our project has compiled successfully and is now prepared for transfer to our chip.

To program the device, ensure that the USBASP is connected to both the PC and the Atmega circuit, the circuit is powered, and if the IC has been configured to use an external crystal, ensure the chip has a crystal connected.

Once these steps are completed, navigate to Tools > USBASP, and from there, the process should proceed automatically.

If everything goes smoothly, the LED on your circuit should begin blinking! Here’s the output from AVRDUDE in Atmel Studio 7, displaying what a successful program execution looks like.

avrdude.exe: warning: cannot set sck period. please check for usbasp firmware update.
avrdude.exe: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.01s

avrdude.exe: Device signature = 0x1e9406
avrdude.exe: NOTE: FLASH memory has been specified, an erase cycle will be performed
To disable this feature, specify the -D option.
avrdude.exe: erasing chip
avrdude.exe: warning: cannot set sck period. please check for usbasp firmware update.
avrdude.exe: reading input file “0x26”
avrdude.exe: writing lfuse (1 bytes):

Writing | ################################################## | 100% 0.00s

avrdude.exe: 1 bytes of lfuse written
avrdude.exe: verifying lfuse memory against 0x26:
avrdude.exe: load data lfuse data from input file 0x26:
avrdude.exe: input file 0x26 contains 1 bytes
avrdude.exe: reading on-chip lfuse data:

Reading | ################################################## | 100% 0.00s

avrdude.exe: verifying …
avrdude.exe: 1 bytes of lfuse verified
avrdude.exe: reading input file “c:\users\robinlaptop\Documents\Atmel Studio\7.0\OurFirstAVR\OurFirstAVR\Debug\OurFirstAVR.hex”
avrdude.exe: writing flash (184 bytes):

Writing | ################################################## | 100% 0.11s

avrdude.exe: 184 bytes of flash written
avrdude.exe: verifying flash memory against c:\users\robinlaptop\Documents\Atmel Studio\7.0\OurFirstAVR\OurFirstAVR\Debug\OurFirstAVR.hex:
avrdude.exe: load data flash data from input file c:\users\robinlaptop\Documents\Atmel Studio\7.0\OurFirstAVR\OurFirstAVR\Debug\OurFirstAVR.hex:
avrdude.exe: input file c:\users\robinlaptop\Documents\Atmel Studio\7.0\OurFirstAVR\OurFirstAVR\Debug\OurFirstAVR.hex contains 184 bytes
avrdude.exe: reading on-chip flash data:

Reading | ################################################## | 100% 0.10s

avrdude.exe: verifying …
avrdude.exe: 184 bytes of flash verified

avrdude.exe: safemode: Fuses OK

avrdude.exe done. Thank you.

Conclusion

Commencing with AVR devices might pose challenges, particularly if an official programmer isn’t readily compatible with Atmel Studio 7. Nevertheless, setting up just one command line within the programming tool—defining the device type, fuse settings, etc.—allows all subsequent projects to utilize the same tool and compiler, needing configuration only once.

Therefore, the upcoming ATMEGA168 project can employ the same compiler and external tool demonstrated in this project without necessitating alterations. Naturally, more complex projects might require modifications to fuse bits. However, this tutorial should serve as a starting point to initiate your journey with AVR devices.


About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top