Developing in C for the ATmega328P: Using Windows, Advanced

9 minute read

Where I setup the advanced, Standard C tool chain (avr-gcc) for the ATmega328P on Windows.

If you are looking to install the AVR toolchain for C using Windows Subsystem for Linux, don’t bother.

Background

Note: This tutorial hasn’t been strictly tested by me on Windows 11 system, that said, it has been performed on numerous other Windows 11 systems without issues.

Windows has two different operating environments, Windows (the Graphical User Interface or GUI) and the command line interface (CLI). The former is the typical method of using Windows, particularly when using applications such as Word, Excel and Powerpoint. The latter is used for program automation and Windows program development and it is typically entered by typing cmd in the Windows search bar. We’ll introduce a new and improved method, shortly.

We want to use the command line environment to develop C software for the Uno. Unfortunately, the GCC compiler for C is intended for use in a command line environment and the environment is expected to be Linux and not Windows CLI.

To resolve this issue we’ll do two things, we’ll install a command line application called git bash to interface with the CLI and a special set of applications that were compiled to run under Windows.

1. Start by testing with the Arduino IDE!

Install Arduino Legacy IDE (1.8.19)

After performing these steps on multiple machines, I’ve found it best to install and test using the Arduino IDE before going forward with the installation instructions below. This will reduce the errors to something more manageable and having the Arduino IDE is handy for its Serial Monitor as well it provides an easy method to identify the serial port the Uno is connected.

If you haven’t installed the Arduino Legacy IDE (currently 1.8.19 and not the IDE 2.0), I would recommend installing from the download from the Arduino site, instructions are here. We will use the Legacy IDE (as it is a more simple IDE) for testing. Once installed, connect your Uno to your computer and compile and upload the File -> Examples -> Basic -> blink sketch. Don’t continue until you’ve been successful, otherwise the remaining instructions will be far too difficult to complete.

If you are new to the Arduino, I highly recommend walking through the Adafruit Blink example. It will show you how to load a program onto your Uno and will explain a little bit about the example. This will make your life much easier, when we begin to program the Uno using the Standard C Language.

You will need to write down the port from the Arduino IDE so you may use it later. Click on Tools in the Arduino IDE, and write down the port name following Port:.

Click on Tools to identify the Port

Click on Tools to identify the Port

Large Version to see detail

2. Install git and the git bash terminal

As we said above, we will need a command line environment (CLI) to use the new tools. If we were to attempt to use the tools created for Linux, they wouldn’t work in the Windows operating system. Instead we will do two things:

  1. Install an environment which looks like Linux called git bash
  2. Install a special version of the tool set (next section)

This operation will install both git (a version control program) and git-bash (a command-line environment). To install, go to this site. Click on the 64-bit Git for Windows Setup to download it. Once it is downloaded, go to your download folder and double click on it. Once the Installer has started, click through all of the options using the offered defaults.

3. Install and Confirm Tool Chain

Download the avr-gcc toolchain

We can’t use the Linux-based version of the tool chain, as it won’t run in the Windows CLI. We’ll need to download and install a special set of applications, which have been created specifically to run in Windows CLI. This site (Zak’s Electronics Blog) creates specific builds for Windows to perform the tasks we need to do.

Click on link to download avr-gcc-12.1.0-x64-windows.zip

Install the avr-gcc folder

To install:

  1. Extract the contents of the zip file by right clicking on the file and select “Extract All…”.
  2. Have it to extract to the Downloads directory (current directory) by clicking Next.
  3. Open another Explorer window and in that window click on Local Disk (C:).
  4. Select the folder “Program Files(x86)” then drag and drop the “avr-gcc 12.10…” directory into the “Program Files(x86)” directory.
Drag Folder into Program Files(x86)

Drag Folder into Program Files(x86)

Large Version to see detail

Add Folder to Execution Path

Once we have it where we want to run it, we need to add that location to our execution path.

To do this, go to the Search bar and begin to enter environment, this will bring up the “Edit the System Environment Variables Control Panel” in the results window, click on it.

Search bar for environment

Search bar for environment

Large Version to see detail

This will open a window called System Properties, click on Environment Variables… at the bottom

Click on Environment Variables...

Click on Environment Variables...

Large Version to see detail

In the next window, select Path, in the top window and click on Edit… under the top window.

select Path...click on Edit...

select Path...click on Edit...

Large Version to see detail

In the Edit environment variable window,

  1. IMPORTANT! Click on New (If you don’t, you will lose the path to Windows Applications.)
  2. Click Browse.
    Click first on New then second on Browse

    Click first on New then second on Browse

    Large Version to see detail

Then broswe to the location where you stored avr-gcc…. As you can see from the image below:

  1. Click on This PC
  2. Click on Local Disk (C:)
  3. Click on Program Files(x86…)
  4. Click on avr-gcc…
  5. Select the bin directory
  6. Press OK.

Follow the numbers to select the correct folder

Follow the numbers to select the correct folder

Large Version to see detail

Please confirm your Path Environmental Variable looks like the image below, before you click on OK on each window which has opened to back out of the exercise.

Confirm this image!

Confirm this image!

Large Version to see detail

Confirm Tool Chain

Open a git bash terminal window by typing “git bash” in the Windows search bar and hitting return. In the window which opens, enter the following commands: (You can click the copy button in the upper right hand corner then execute them all at once.)

avr-gcc --version
make --version
git --version
avrdude

Confirm each component is installed

Confirm each component is installed

Large Version to see detail

The application avrdude will show more detail, however, on the last line of the printout, the version number needs to be 6.4 or 7.0.

Congratulations, you are now ready to code in C for the Arduino Uno! Let’s start!

4. Install Code Editor

Before you start, you will need one more application, a code editor. A code editor is similar to a word processor such as Microsoft Word, however, it provides important capabilities like text-only (no special characters) and it will highlight the syntax of the code being written. A very popular and free version for Windows is Notepad++. I will use it for the following examples. I recommend you install Notepad++ or use one that is to your liking.

  1. Go to the Notepad++ site
  2. Click on Download in the left column
  3. Click on the topmost version (as of this writing, Notepad++ v8.4.7)
  4. Go to Downloads in an Explorer window, and double-click on the npp.n.n.n.installer.x64 to install (in the case of the above version, the filename is npp.8.4.7.installer.x64)
  5. Once installed, you can start the application by going to the Windows search bar and typing “note”, use the arrow keys to highlight “Notepad++” then hit return to start the application.
  6. To make it easier to find, once started, right-click on the icon in the taskbar and select “Pin to taskbar”

5. Create a C program file

Create a test folder

In this step, we’ll setup a specific folder for developing C. I called it test. We’ll add a file called main.c, the we’ll compile/link the file on to the Uno.

  1. In an Explorer window, navigate to your Documents folder. Right-click in the window and select New Folder, then name the folder “test”.
  2. Align your git bash window below your Explorer window, and type “cd “ (the letters “c” “d” and a space) at the command line.
  3. Drag the test folder icon onto the window, and it will fill-in the folder path following the “cd “. Make sure you have a space between the cd and the file path!

When you have completed the three steps above, you will see an image similar to this:

Confirm your image matches this one

Confirm your image matches this one

Large Version to see detail

Keep the git bash window open, as you will use it when you compile/link/load your file.

Code for file

You will want to select the code in the box below, copy it then you will paste it into Notepad++ editor window.

#include <avr/io.h>
#include <util/delay.h>
 
#define BLINK_DELAY_MS 100
 
int main (void)
{
 /* set pin 5 of PORTB for output*/
 DDRB |= _BV(DDB5);
 
 while(1) {
  /* set pin 5 high to turn led on */
  PORTB |= _BV(PORTB5);
  _delay_ms(BLINK_DELAY_MS);
 
  /* set pin 5 low to turn led off */
  PORTB &= ~_BV(PORTB5);
  _delay_ms(BLINK_DELAY_MS);
 }
}

Steps to Create file

I’m using Notepad++ as my editor to keep this simple, this will be a copy and paste from code above into a file on your system.

  1. Open Notepad++, click on File -> New then paste the contents of the box above. Confirm the text is the same then click on File -> Save.
  2. In the Save dialog, navigate to Documents/test then save the file and call it main.c in the Notepad++ editor.
  3. Once you have saved the file, it will look like this:
    main.c in the Notepad++ editor

    main.c in the Notepad++ editor

Large Version to see detail

6. Use Tool Chain to Compile/Link/Load main.c

Go back to the git bash window and enter the commands below to compile/link your file. I recommend using copy and paste, attempting to type them in by hand might be difficult. As we did with testing the versions of the software, copy each command and paste it into the git bash window, then hit return. (You can copy the each line in its entirety, the text after the “#” is a comment, describing the step. It will be ignored.)

avr-gcc -Os -DF_CPU=16000000UL -mmcu=atmega328p -c -o main.o main.c # Compile
avr-gcc -mmcu=atmega328p main.o -o main # Link
avr-objcopy -O ihex -R .eeprom main main.hex # Load

Your window will now look similar to this, note that the compile, link and load operations won’t have output (unless you have errors.):

main.c in the Notepad++ editor

main.c in the Notepad++ editor

Large Version to see detail

The steps Compile/Link/Load are the three steps to create an executable file appropriate for uploading to the Uno. These are the same three steps which are executed when you click the checkmark labeled Verify in the Arduino IDE. We will simplify these steps later in the course.

7. Upload to the Uno

Our last step is to upload the executable file, main.hex file to the Uno. We will use the program avrdude via the following command:

avrdude -F -V -c arduino -p ATMEGA328P -P COM3 -b 115200 -U flash:w:main.hex # Upload

Notice the “COM3”? You will want to change the “COM3” to the port name that you wrote down in the Arduino IDE port identification in the earlier step (1. Start by testing with the Arduino IDE!). When you run the avrdude command above successfully, you will see the following:

Successful avrdude upload

Successful avrdude upload

Large Version to see detail

AND your Uno will be blinking at a much faster rate!

If you see this instead:

Typical avrdude failure

Typical avrdude failure

Large Version to see detail

  • Is the Arduino IDE installed, have you tested it, and it worked?
  • Have you closed the Arduino IDE?
  • Are you sure you are using the right port? Double check the number using the Arduino IDE and make sure the port name matches the port name on your avrdude command.

Next Steps

  1. Developing in C for the ATmega328P: Using Git

Comments powered by Talkyard.