Developing in C on the AVR ATmega328P

5 minute read

Introduction

While the Arduino tool set, (both the Arduino IDE and Arduino software framework) are outstanding for quickly developing a working prototype, they do so with a combination of a graphical-user-interface (GUI), the C++ language and Arduino-only classes. This in itself, isn’t a problem, one can be quite efficient and productive using this approach.

This approach is an issue if you are at a university or job which expects a standards-based C language proficiency. It is also an issue, if your class or job, requires understanding how to use command-line-interface (CLI)-based tools. The C language, and not C++, is the basis of a significant number of embedded systems development.

This set of entries are written to help you learn how develop embedded ANSI C programs for the AVR family and specifically the ATmega328P (Arduino Uno R3). While doing so, it will also use a set of tools which can serve as the basis of an intermediate-level software programmer.

There are two approaches to doing this, the tools differ slightly in how they are installed:

  • Beginner - for someone with little to no experience in programming, we are going to use the tool chain found in the Arduino IDE. We won’t use the GUI, we will use the CLI, as most programmers do, to provide a near-professional experience. The advantage of using the Arduino tool chain is that it simplifies your computer setup. The work required is changing two lines in a configuration file as compared to a hour’s worth of work installing a new set of tools.
  • Advanced - for someone who already fills comfortable with the CLI and wishes to install and maintain tools of the latest revision. In this case, we’ll install and use the GNU tool chain. The disadvantage to using the Arduino tool chain is that it is quite out-dated. Practically speaking there isn’t a signficant difference when compiling for the Uno R3. There will be issues when attempting to compile for new AVR microcontrollers. The upload program avrdude has also seen upgrades from version 6.3 to 7.2. Most of the improvements have been in enabling newer AVR microcontrollers as well.

(A timeline of the GNU GCC tool chain can be found here. The Arduino version is 7.3, while most recent installations are 12.3.)

Most of the introductory pages has been updated to reflect using the Arduino tool chain instead of installing a GNU-based tool chain (as in Developing in C for the ATmega328P: Linux/macOS/Windows Setup). Ultimately, the decision is yours. If you wish to keep things simple, go forward with using the Arduino tool chain (not the IDE) in a commmand-line environment. If you want to use new AVR microcontrollers and want more control over how you compile/link/upload, I’d recommend installing the newer GNU version.

Pages of Significant Interest

Course Material

Developing in C for the ATmega328P

Beginning tutorial using Labs_10C_Class content to help someone replace the Arduino C++ software framework with a Standard C approach.

A broader-based approach using the AVR_C repository is recommended for a detailed approach to learning C for the AVR-family of microcontrollers.

  1. Developing in C for the ATmega328P: Getting Started
  2. Developing in C for the ATmega328P: Setup Introduction
    1. Developing in C for the ATmega328P: Linux Setup (Advanced)
    2. Developing in C for the ATmega328P: macOS Setup (Advanced)
    3. Developing in C for the ATmega328P: Windows/WSL Setup (Deprecated)
    4. Developing in C for the ATmega328P: Windows 10 Setup (Advanced)
  3. Developing in C for the ATmega328P: Using git
  4. Developing in C for the ATmega328P: Edit
  5. Developing in C for the ATmega328P: Build
  6. Developing in C for the ATmega328P: Upload
  7. Developing in C for the ATmega328P: AVR_C Library Functions
  8. Developing in C for the ATmega328P: Using a Serial Monitor
  9. Developing in C for the ATmega328P: Functions - Serial input/output
  10. Developing in C for the ATmega328P: Function - analogWrite()
  11. Developing in C for the ATmega328P: Functions - Serial input/output
  12. Developing in C for the ATmega328P: Function - digitalWrite()
  13. Developing in C for the ATmega328P: Function - pinMode()
  14. Developing in C for the ATmega328P: Function - digitalRead()
  15. Developing in C for the ATmega328P: Struct - buttons[]
  16. Developing in C for the ATmega328P: Function - analogRead()
  17. Developing in C for the ATmega328P: Hints
  18. Developing in C for the ATmega328P: Mapping Values
  19. Developing in C for the ATmega328P: Using Data Types and Math
  20. Developing in C for the ATmega328P: Marking Time and Measuring Time
  21. Developing in C for the ATmega328P: User Functions
  22. Developing in C for the ATmega328P: Multi-tasking the Arduino - Part 1
  23. Developing in C for the ATmega328P: Function - tone()
  24. Developing in C for the ATmega328P: Using PROGMEM
  25. Developing in C for the ATmega328P: structures
  26. Developing in C for the ATmega328P: Code vs. Cost
  27. Developing in C for the ATmega328P: Pointers
  28. Developing in C for the ATmega328P: Servos
  29. Developing in C for the ATmega328P: I/O Ports
  30. Developing in C for the ATmega328P: Make, Makefile and env.make
  31. Developing in C for the ATmega328P: Better Serial Input
  32. Developing in C for the ATmega328P: Using the GNU Linker to Enhance printf()
  33. Serial Applications for Embedded Development

AVR Datasheet Notes

Technical tutorial using specific capabilities of the ATmega328P chip. This content is intended to help somone understand how to use the datasheet to implement a specific function using the ATmega328P.

  1. AVR Datasheet Note: Blink
  2. AVR Datasheet Note: PWM
  3. AVR Datasheet Note: analogWrite

Programming Examples

Intermediate tutorials focused on using the AVR-gcc library or examples from the library.

  1. Developing in C for the ATmega328P: Comparison of Languages
  2. AVR-gcc Note: Simple Project
  3. Using the Standard C Framework
  4. Writing Multitasking Programs
  5. Using Makefiles to Automate Development

Hardware Debugging

Technical tutorials on using Bloom, gdb, debugWIRE, a hardware debugger such as the Microchip SNAP or the one on the ATmega328PB Xplained Mini board to debug programs on the microcontroller.

  1. Hardware Debug: Using gdb to debug
  2. Hardware Debug: Setup Bloom and gdb to Debug
  3. Hardware Debug: Examples of Bloom with gdb to Debug
  4. Hardware Debug: Hints on Solving Issues Bloom and gdb
  5. Hardware Debug: Exploring the ATmega328PB Xplained Mini
  6. Hardware Debug: Command and Configuration Reference for Bloom and gdb

Reference Content on C

Reference books which are important for understanding C

  1. The C Programming Language Kernighan and Ritchie - available in PDF and paper copy
  2. Barr Group: Embedded C Coding Standard
  3. SEI CERT C Coding Standard
  4. Arduino Resources page
  5. comp.lang.c FAQ

Comments powered by Talkyard.