Developing in C for the ATmega328P: Introduction

4 minute read

Where I begin the process of developing in Standard C (and only C) on the Arduino Uno (also known as the ATmega328P).

Sources

Introduction

This series is designed to help someone who has been using the Arduino framework, however, desires to use only the C Language. It covers adding the toolchain to your system of choice, executing the toolchain on examples and (possibly) advancing the ability to debug using a hardware debugging tool. Or stated more formally:

  1. (OPTIONAL) Install the tool chain necessary to cross-compile C and link code for the ATmega328P on the platform of choice. OR use the same tools as the Arduino IDE, however, use them via the command line.
  2. Test #1 using an Arduino Uno (without the Arduino software framework!) using some examples from the web as well as my own.
  3. (TBD) Advance the ability to debug by adding a hardware debugger in the form of a hardware debugger.

For each of the platforms, there is a level of complexity to accomplish each step. Its important to understand that the tools that exist to accomplish cross-compiling and linking C for an embedded microcontroller are designed for Linux. Therefore, the closer a system “looks like Linux”, the easier it is to follow the three steps. This isn’t a problem. It is simply something to understand and account for. Specifically, the level of difficulty of the three steps in each operating system is:

Linux (or Raspberry Pi, the recommend approach)

  1. Intall Toolchain Easy.
  2. Develop C code Easy
  3. Hardware Debug Easy

macOS

  1. Install Toolchain Easy.
  2. Develop C code Easy
  3. Hardware Debug TBD

Windows

  1. Install Toolchain Intermediate, requires a different approach than usual in Windows.
  2. Develop C code Easy.
  3. Hardware Debug TBD

Why It Is Worth It

You might ask yourself, if the Pico/RP2040, is a far more advanced microcontroller and less expensive microcontroller, why would I pursue this? Because while the Pico is a great bargain, and having a 32-bit microcontroller with hardware debugger for only $15 is a steal. The RP2040 is a powerful and extremely complex microcontroller even with all of the great documentation. I wanted to explore performing the same task (developing in C) on a far less complex microcontroller such as the ATmega328P.

The AVR series of processors is incredibly broad, absurdly inexpensive and very easy to use. One can purchase an ATtiny13 for about $1 which, “combines 1 KB ISP Flash memory, 64B SRAM, 64B EEPROM, a 32B register file, and a 4-channel 10-bit A/D converter. The device supports a throughput of 20 MIPS at 20 MHz and operates between 2.7-5.5 volts.” In an easy to integrate 8-pin DIP package.

I found this passage In Elliott William’s book, enlightening as to why to learn how to do this without the Arduino:

“The price paid for the Arduino pin/timer abstraction is that what we [can] do in one or two lines of code, and two or three cycles of CPU time, the Arduino does in 50+ clock cycles. It tests if you want the pin fully on or off, and it uses a whole switch() expression and memory lookup to figure out which timer registers to write to—and all of this just so that you don’t have to look up the pin in the datasheet.”

“If you’re calling analogWrite() infrequently in your code, this will probably work OK, and you’ll never notice the speed penalty. If you’re setting the OCR bits frequently, as we will be in Chapter 13, this extra code overhead means the difference between the possible and impossible. If you’re coming from the Arduino world, you’ll probably be annoyed by how much detail about the chip you’re required to learn, but once you learn how to actually use the hardware peripherals as they’re intended, you’ll be surprised by how much more is possible. And once you know how the internal peripherals work, it’s not much harder to configure them yourself.”

–Excerpt From: Elliot Williams. “Make: AVR Programming.” Apple Books. "

OK, now what?

Get started!

Be aware that there might be some difficulty in the initial steps of installing and confirming the operation of the tool chain required. Understand that you might need to download and install a fair number of software applications. And that some of those applications might use commands and a language that is difficult to understand.

That said, if developing code on an embedded microcontroller is important to you, the steps you take to accomplish these tasks are very important. And learning what it takes to implement and use the tool chain will be very useful.

One Last Thing

For years, I’ve been adamant that a superior development approach was to use a code editor along with a command-line toolchain to develop code. I believed this approach would help someone understand how the tools interacted, how to use the tools and therefore would develop a well-grounded approach to embedded computing.

While I still believe this, I also acknowledge the ubiquity and flexiblity of VS Code is quite powerful. Being able to use VS Code in a remote shell environment along with VS Code being platform-agnostic, enables a stream-lined approach to tooling. For that reason, I agree and support using VS Code as the code editor and use it for some tool automation. I’ll also add, its incessent offer to “install yet another extension” is not-only annoying, it is typically counter-productive. Less is more.

And please keep AI to helping solve problems, not develop your code. The former is great for learning, that latter means you haven’t learned.

Next entry: Developing in C for the ATmega328P: Setup Introduction

Comments powered by Talkyard.