Developing in C for the ATmega328P: Notes

3 minute read

Where I comment on the differences between developing code using Standard C vs using the Arduino Framework.

Introduction

The goal of the AVR C content is to demonstrate how you are able to develop code in C without the Arduino Framework. This note is intended to show specifically, the differences in code development between the two. I don’t believe one is intrinsically better than the other as I believe, each one has strong positive traits related to its goals.

Embedded Language Overview

Its best to understand the different language/development environments is directly related to embedded development. In this comparison, I’ll talk about 4 distinct languages/environments in the development of applications on an embedded microcontroller.:

  1. Python(MicroPython/CircuitPython): Interpreted language with a Read-Eval-Print-Loop (REPL) which provides for extremely fast prototyping with a comparatively slow execution speed as well as significant memory requirements. Python is a friendly language, which means it is easy to read, easy to write and ubiquitous. It does have significant memory requirements so one can’t use it on the ATmega328P for example. I would recommend it for someone that is more interested in getting a larger project accomplished where the microcontroller is a means to the end.
  2. Arduino Framework: This framework brings in aspects of C/C++/Custom code to create a friendly environment for programming. Because of the support provided by the Arduino people and their supporters, programming via the Arduino framework can be as friendly as Python. Its memory requirements are much less than that of Python, however more than native C. This isn’t an issue on the ATmega328P as much as it is an issue on any of the ATtiny processors. I would recommend it to be viewed in the same way as Python. Not a standards-based approach, which causes issues in university-level instruction.
  3. C Language using avr-gcc/avrdude tool chain: Far less friendly than the previous two. It requires understanding the command line regardless of platform (Linux, macOS, or Windows) and a bit more searching to find the answers. This approach will be identical to the approach software developers use for developing commercial products, so there are little technical restraints to how or where it can be used. I would recommend it to someone that wants to understand the microcontroller as much as they want to accomplish a project. If someone is early in their career and wishes to become an engineer developing products with microcontrollers, this is the way to go.
  4. Forth Language: I’ll include this one as it might appeal to a select few that are looking for the functionality of a REPL with the speed and memory requirements of C. What it doesn’t have is the support framework of any of the above 3 approaches, which make it a tough slog at times. While this approach is esoteric, it can be the most rewarding for a technical-minded person.

Arduino IDE to Standard C Translation

Here is a table, where I attempt to show the differences and similarities between using the Arduino Framework and the Standard C approach.

Approach Arduino Standard C
Interface GUI Command line
Language C/C++/Arduino Classes ANSI C (C99)
Tool Chain All self-contained in Arduino IDE Must be installed separately, easy in Linux, can be difficult in Windows.
Compile Command Checkmark in GUI, Verify make
Upload Right Arrow in GUI, Upload make flash
Program Development Called a sketch and can be multi-file. Is file-based, can be multi-file
Files filename must ends with .ino filename must end with .c
Folder files must be in current folder or Library files must be in current folder or Library
Libraries Uses Libraries for specific functionality Uses Libraries for specific functionality
Library Content Broad and diverse set of Libraries, rich with 3rd party devices Narrow and well-defined Library aimed at Standard C
Principal Library Arduino Board AVR-libc
Library Documentation Language Reference avr-libc
Arduino Compatibility N/A already Arduino AVR_C

File Template

Arduino

filename.ino

void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:

}

Standard C

main.c

/* Document what this program does, include req'd hardware changes
*/

/* Put your #includes here */
#include <stdio.h>
#include <stdlib.h>


/* Put your global variables here */

/* Main - is the function which C will look for to run at initialization*/ 
int main() {
/* initialize code goes here, similar to setup in the Arduino*/

// while(1) loop routine runs over and over again forever:
    while(1) {
        // this is the primary code section to execute
    }
    return0; /* Required for strict C implementation
}

Comments powered by Talkyard.