Programming AVR Microcontrollers with Atmel Studio 7

Содержание

Inexpensive microcontrollers have seen a resurgence thanks to the internet of things (IoT) and wearable devices. These end-uses depend on parts that have low costs rather than record-breaking speed. Because of this, devices like the Atmel AVR series have enjoyed decades of popularity.

Any software we develop for these devices eventually needs to go from being an abstract idea, to being software stored inside the device’s flash memory. This walkthrough will do exactly that using a program called Atmel Studio 7.

Notice of Non-Affiliation and Disclaimer: As of the publication date, we are not affiliated with, associated with, authorized with, endorsed by, compensated by, or in any way officially connected with Microchip Technology Inc., Arduino, or Microsoft, or their owners, subsidiaries or affiliates.

The names Microchip Technology Inc., Arduino, and Microsoft, as well as related names, marks, emblems, and images are trademarks of their respective owners.

External Links: Links to external web pages have been provided as a convenience and for informational purposes only. Unboxing Tomorrow and Voxidyne Media bear no responsibility for the accuracy, legality or content of the external site or for that of subsequent links. Contact the external site for answers to questions regarding its content.

Objectives

The circuit board in Figure 1 below is the custom part of a machine vision side-project going on here at Unboxing Tomorrow. On the circuit board is an ATTiny2313-20SU microcontroller. As part of the AVR family, the ATTiny2313 is a low-cost, 8-bit microcontroller unit (MCU) made by the Atmel Corporation and its parent company: Microchip Technology.

A circuit board Description automatically generated

Figure 1: A Closeup of the Chip we will Program Today

Because the chip arrives in a blank state, we need to download the Atmel Studio 7 application to a laptop, and then write a program in C to blink a test LED on the circuit board. Additionally, we will use a hardware programming device to perform In-System Programming (ISP) on the chip.

For now, today’s objectives are simple:

  1. Download Atmel Studio 7
  2. Write a C program to blink a test LED on the custom circuit board
  3. Use the ISP Programmer to upload the C program from a laptop to the actual chip

This schematic below is a simplified version of the custom circuit board. Our code will be uploaded through a 6-pin ISP connector, where it will eventually blink an LED attached to port B pin 4 (PB4).

Figure 2: A Simplified Drawing of the Custom Board

Material Requirements

As usual, I recommend doing any assembly work at an anti-static workstation if you can.

  • Windows 10 Computer
  • ATTiny2313 (or other AVR microcontroller)
  • Microchip/Atmel AVRISP mkII or similar ISP programmer with matching USB cable

ISP Programmer Device

This tutorial will focus on the Atmel/Microchip AVRISP mkII (also called AVRISP “mark 2”). But I should note that this is an older device that is difficult to find as of the year 2020. But you can find newer ISP devices from Microchip that work very much like the AVRISP mkII.

Software Requirements

We will also be installing a program from Microchip called Atmel Studio 7. While there is other software available for programming AVR microcontrollers, Atmel Studio 7 is the official integrated development environment (IDE).

  • (We will install) Atmel Studio 7
  • (Recommended) Anti-malware software

Back up your Data

You should back up any important data on your laptop or desktop before you start.

The Microcontroller will be Erased

This procedure will erase and replace any programs or bootloaders already stored on your Atmel AVR microcontroller. Replacing these things is a difficult process.

  • Do not perform the procedure below if your Atmel microcontroller is part of a development kit such as an Arduino. If you do, you will erase the bootloader and your kit will not work.

If you bought your Atmel microcontroller from a distributor, it is probably already blank and you should be okay to proceed.

Downloading Atmel Studio

The first step is to visit Microchip Technology’s official website for the Atmel Studio 7 IDE. As of this writing, the direct link is https://www.microchip.com/mplab/avr-support/atmel-studio-7.

I will download the web installer from the official website…

Figure 3: At the Official Atmel Studio Website, Select either the Web Installer, or Alternately: the Offline Installer

As with anything you download off the internet, scan it with your favorite anti-malware software. If it looks okay, run the application.

A close up of a logo Description automatically generated

Figure 4: The Atmel Studio “Online” Installer

Prior to installation, you should review the end-user license agreement and the installation directory. After you have reviewed them, click Next.

Next, you will be asked to select among the following architectures:

  • AVR 8-bit MCU
  • AVR 32-bit MCU
  • SMART ARM MCU

This article only depends on the AVR 8-bit MCU, so at minimum, you should count on installing AVR 8-bit MCU. But I will install all three today…

A screenshot of a cell phone Description automatically generated

Figure 5: At Minimum, Select the “AVR 8-bit MCU” Option to Complete this Tutorial

Next, you will be asked about the Atmel Software Framework (ASF) and example projects. In this tutorial, the ASF is optional, but I will install it anyway.

A screenshot of a cell phone Description automatically generated

Figure 6: The Atmel Software Framework (ASF) is an Optional Installation for this Guide

Before it attempts to install anything, the installer will check for anything that may interfere. This includes any Windows Updates already in progress. If you see anything other than checkmarks, you will need to resolve the problem before you can proceed. Follow any on-screen instructions on how to do this.

If things look okay, click Next.

Figure 7: System Conditions Check

Next, the installer will show any special tips that may help your installation.

Atmel Studio installation tips

Figure 8: A Special Notice from the Atmel Studio Team

Click Next, and the installation will begin. Acknowledge any security alerts from Windows unless they look suspicious.

Figure 9: The Installation Process

The system will automatically transition from installing Atmel Studio to installing the Microsoft Visual Studio isolated shell. But stick around to monitor it in case you are asked to approve anything.

Figure 10: A Sample of the Visual Studio Isolated Shell Installation Process

After several minutes, the installation should complete. Make sure “Launch Atmel Studio 7.0” is checked (☑) so you can start it immediately.

Figure 11: Installation Complete

Building a C Program

Now that Atmel Studio 7 is installed, launch it.

Create a New Project

First, create a new project by clicking the New Project label on the Start Page. If the start page isn’t visible, you can instead navigate to File New Project.

Figure 12: Select “New Project”

Next, it is time to select the programming language. This project will use the C coding language, so select GNU Compiler Collection C (GCC C). Select C/C++ in the top-left corner first. Then select “GCC C Executable Project.”

Figure 13: Selecting the Project Type, Language, Compiler, and Directory

Before you click “OK,” you may want to rename your project or change the location. Today, I will accept the default name of “GccApplication1.”

I also recommend making sure “Create directory for solution” is checked (☑). When you are ready, click “OK.”

Next, you will be asked to select your specific Atmel device. It’s a long list, so you may want to use the search filter on the top-right corner to enter a portion of the device part number.

I am using the ATtiny2313, so that’s what I will select.

Figure 14: Selecting the Device by Part Number

Coding Main.c

Atmel Studio 7 will populate the editor field with a very basic main() function…

Figure 15: Atmel Studio will Automatically Prepare a Simple Program Structure for You

Now would be a good time to review the simplified schematic…

Figure 16: A Simplified Schematic of the Custom Board

Only 3 things matter for now:

  • Compiling a test program to blink the LED (named OUTPUT_RUN_ID)
  • Connecting to the Programming Connector
  • Making sure Atmel Studio 7 can communicate with the ATTiny2313
  • Uploading the program

We’ll start by coding main.c…

Code Explanation

Any text located to the right of a “//” is a comment. Comments are optional, but good for our own documentation.

#define OUTPUT_RUN_ID (4)

…Because the LED is connected to the 4 th bit of Port B, this line assigns a constant called “OUTPUT_RUN_ID” a decimal value of 4.

#define F_CPU 1000000UL // Clock frequency (as unsigned long)

…This line defines the clock frequency of the ATTiny2313 in megahertz (MHz). By default, the ATTiny2313 contains an internal 8 MHz clock source. That 8 MHz signal will be internally divided by 8 for a final value of 1 MHz. You can disable the divide-by-8 feature if you want, but I will keep it for now.

The “UL” part is short for “Unsigned Long” integer.

#include avr/io.h // Recognizes pins, ports, registers, etc.

#include util/delay.h // Required for _delay_ms() function

…These are precompiler directives that reference header files that are included with Atmel Studio 7.

DDRB = (1OUTPUT_RUN_ID); // Set the pin for LED to output mode

… This line of code will set the 4 th pin of Port B from being an input to being an output. Here, DDRB is short for “Data Direction Register B.” Because OUTPUT_RUN_ID was set to equal 4 earlier, the “” operation causes only the 4 th bit of DDRB to be set to 1. If you’re wondering where I got the name “DDRB,” it came from Microchip’s official datasheet for the ATTiny2313.

PORTB |= (1OUTPUT_RUN_ID); // LED on

…This command will set pin 4 of Port B to +5V (logic = 1). The “|=” operation does this using a bitwise OR of Port B’s previous state.

_delay_ms(500); // Hold for approx. 500 ms

…This line will pause the program for about 500 milliseconds (ms).

(1OUTPUT_RUN_ID); // LED off

…This line will clear pin 4 of Port B to 0 volts (logic = 0). The “=” operation does this using a bitwise AND of Port B’s previous state. The “

” symbol performs a bitwise inversion of the binary value for 4.

Building the Code

We can build the code by navigating to: Build à Build Solution.

Figure 17: Watch for the “Build succeeded” Message or any Errors in the “Outputs” section in Atmel Studio

Connecting the ATTiny2313 to the ISP Programmer

You can think of the ISP Programmer as a bridge between the Laptop and the ATTiny2313 microcontroller…

Figure 18: A Simplified Cable Diagram

Here is how it all looks on a workbench…

Figure 19: ISP Programmer and Attached Components and Cables

To upload a program, your ISP Programmer will need 6 wires connected to your ATTiny2313…

  • VTG (usually this is +2.7 volts to +5 volts)
  • GND (ground)
  • MOSI
  • MISO
  • SCK
  • RST

You can review how these wires are connected to the ATTiny2313 microcontroller in Figure 16 above.

Figure 20: A Closeup of the 6-Pin Programming Connector. The ISP Programmer will Connect here.

From Figure 20 above, you can see I have a 6-pin connector. This is where the ISP Programmer will connect to the ATTiny2313. This 6-pin layout is standard with Atmel’s ISP Programmers. If you have trouble figuring out which pin is #1, Atmel/Microchip will often mark pin #1 on their connectors with a triangle or a red wire.

Figure 21: Pin 1 on the ISP Programmer Connector may be Marked by a Triangle or a Red Wire

With all 6 programming signals connected to the ATTiny2313, all we have to do now is:

  1. Connect the ISP Programmer’s USB port to the laptop/desktop
  2. Power on the custom circuit board (which will power on the ATTiny2313)
  3. Upload our C program using the ISP Programmer

Uploading the Application

With the coding and the wiring done, it is time to upload the program we just made. Connect the ISP Programmer to your computer’s USB port if you haven’t already, and make sure your ATTiny2313 is receiving power.

Back in Atmel Studio 7, access Device and Debugger Toolbar Options. This was a hammer-shaped icon with the default label of “None on.”

Figure 22: Location of “Device and Debugger” Tools

Clicking this area will launch the Debugger (Programming device) dialog.

Figure 23: Testing and Re-configuring the Wired ISP Connection

First, select your device as the “Selected debugger/programmer.” If you don’t see it listed, save your project, restart Atmel Studio 7, and then try to find it again.

Second, select Interface = ISP.

Finally, try the following settings:

  • ISP Clock = 125 kHz or lower
  • Programming settings = Erase entire chip
  • Preserve EEPROM = Checked ☑
  • Keep timers running in stop mode = Checked ☑
  • Cache all flash memory except = Checked ☑ and blank

When you’re done, click the main.c tab at the top to return to the source code.

Figure 24: Clicking the “main.c” Tab will Return to the Source Code Editor

From here, the easiest way to upload the code is to start debugging by pressing the play button (▶).

Figure 25: The Start/Play Button Location

Within a few seconds, your LED should be blinking at 1 Hz!

Figure 26: Successfully Blinking the Correct LED on the Circuit Board

Troubleshooting your Connection

If your code is not running, navigate to Tools Device Programming. This will launch the Device Programming dialog.

Figure 27: The Device Programming Window will Apply Basic Communications Tests

In the Device Programming window, you should be able to click “Apply,” “Read,” and “Read” again without encountering any errors.

Incorrect wiring (including rotating or mirroring the connector) is a common source of problems here. If you are able to hit the “Apply” button without an error, it means you can at least communicate with the ISP Programmer device, which is half the battle.

As a side note: Windows Device Manager should show your ISP programmer in the “Microchip tools” category.

Figure 28: The “Failed to Launch” Message can Provide Some Signal-Level Clues About Connection Problems

For me, a common mistake is connecting the 6-pin connector backward. Luckily, the AVRISP mkII is forgiving of this sort of mistake and didn’t take any damage. But you should double-check all 6 programming wire connections each time.

Figure 29: A Good Connection will Retrieve the Device Signature and Show an Acceptable Target Voltage

If you can read the device ID after adjusting your wiring, you are ready to retry debugging.

Closing Remarks

The Atmel microcontrollers—including the AVR family—emerged out of the technology boom of the mid 1990’s. These are 8-bit Reduced Instruction Set Computers (RISC). But more importantly, they were one of the first microcontrollers of the modern age to feature on-board flash memory.

Because of this and its low cost, the series remains relevant today, which is why I’ve chosen it here in the year 2020. Hobbyists, professionals, and educators use these devices extensively, even after Atmel was acquired by former competitor Microchip Technology. So you can definitely expect to see more devices like here in the future.


Источник: unboxing-tomorrow.com