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).
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 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.
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.