Where I describe the functions for serial input/out - puts(), getchar() and printf().
The serial input/output capabilities of C are vastly superior to those in the Arduino framework. In order to communicate via the USB serial cable, you must run a serial monitor program such as the Arduino Serial Monitor, CoolTerm, or moserial. See this page for more details.
Currently available are:
buffersize
number of characters or until CR
has been entered. Returns the number of characters read.While the serial I/O capabilities are polling and not interrupt-based, I’ve found the baud rate can still be set quite high. This is of value when you are attempting to transmit a signficant amount of information such as in examples/pointers. I have successfully set the baud rate to the following speeds in env.make:
Where I describe the function - analogWrite().
While analogWrite can be an easy function to use, its not easy to initially understand. This stems from a couple of reasons, first, its mis-named in that the function is not doing anything from an analog perspective and second, it uses a concept called pulse-width modulation (PWM), which in itself is difficult to understand.
Lets start with explaining pulse-width modulation. Here is a video where a pulse is being modulated:
Where I describe how to use the Library functions in AVR_C.
This page describes where to find specific functions for programming an AVR microcontroller in Standard C using functions similar to the Arduino Language Reference. In this case, the AVR_C framework consists of:
The Standard C Library is provided by AVR Libc. This library is incredibly important as it provides the ability to develop code in Standard C for the AVR family of microcontrollers. It has all of the functionality you would expect if your were using the a Standard C book such as The C Programming Language Kernighan and Ritchie (available in PDF and paper copy). I recommend having a link to the online manual open while developing code.
Where I attempt to direct all of the questions, pertaining to Developing in C for the Arduino Uno.
A: There is more than one way!
CLI methods
# identify using device
ls /dev/tty* | grep -E 'ACM|USB'
# identify using tio
tio -l
# if you have multiple devices, using tio will provide more information
GUI methods
A: C is the ideal combination of a common language and a language which is “close” to the hardware. For example, the AVR assembly language allows writing directly to the hardware (the best example of “close”), except it is not commonly known or easily understood. On the other hand, Python is widely known and easily understood, however, due to it’s requirement to be compiled on-board, it is too large to fit into the memory of the ATmega328P (the microcontroller used by the Arduino Uno). The Arduino approach is to use C++, however, C++ is a little more abstract and requires more memory than C. It’s a good compromise for teaching, however, it is not the best language for embedded development.
Where I describe the process of developing code for the Arduino Uno(AVR ATmega328P), specifically, the build process.
The middle three steps compile/link/locate are typically called the build process, which can simplify the five steps to three:
For a detailed and very worthwhile description of the three steps in Build, I highly recommend this page, Compiling, Link and Locating: Barr Group.
The image is a typical start-up build sequence, I need to cd three times, to get into the specific folder. Once there, I use make clean to ensure I have a clean start (source code and makefile only) then I execute a make flash.
Where I describe the process of developing code for the Arduino Uno(AVR ATmega328P), specifically, editing the code.
Description | Windows Keys | macOS Keys |
---|---|---|
Copy selection | Ctrl - c | Cmd - c |
Paste clipboard | Ctrl - v | Cmd - v |
Select all | Ctrl - a | Cmd - a |
Description | Windows Keys | macOS Keys |
---|---|---|
Tool Palette | Ctrl-Shift-p | Cmd-Shift-p |
Build Task | Ctrl-Shift-b | Cmd-Shift-b |
Description | Command/key | Comments |
---|---|---|
Change directories | cd | Use to change folders |
Expand current text to a folder name | Tab | Type first few letters then hit Tab |
Present working directory | pwd | What folder are you in? |
List contents of directory | ls | Show what is in the folder |
List folder in tree format | tree | Show what is in the folder |
Previous command | Up arrow | display previous command |
Open serial monitor | tio [ acm | usb ] | Pick one of the two options based on your controller board serial port |
This is a a very simple configuration. The left-half of your window is VS Code and the right half of your window is your CLI (terminal application.) Use tabs on both to view multiple files or instances.
Where I describe the process of developing code for the Arduino Uno(AVR ATmega328P), specifically, uploading code to the Uno.
The middle three steps compile/link/locate are typically called the build process, which can simplify the five steps to three:
The most important step in uploading to the Uno is to ensure your env.make file is using the correct serial port. The easiest method for this is to plug-in your Uno and use the Arduino IDE and Tools -> Port to identify the port used by the Uno. Here is a screenshot of this process on the Mac:
Where I discuss how to implement and use git to develop code in C for the Arduino Uno.
In this post, I’ll describe how to use git to add the Lab content. I’ll also offer some guidance as to how to use git in your own projects. I am not a git expert and I’ll try not to stray too far from status->add->commit-push…
Where I describe a new error caused by a bug in GCC 12 as it relates to the AVR microcontrollers.
While debugging a setup script, I ran across this error on my Linux system, however, it didn’t show up on my macOS or Windows computers.
$ make flash
avr-gcc -Os -mcall-prologues -g3 -std=gnu99 -Wall -Werror -Wundef -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -ffunction-sections -fdata-sections -DF_CPU=16000000UL -DBAUD=9600UL -DSOFT_RESET=0 -I. -I../../Library -mmcu=atmega328p -c -o main.o main.c
main.c: In function 'main':
main.c:33:9: error: array subscript 0 is outside array bounds of 'volatile uint8_t[0]' {aka 'volatile unsigned char[]'} [-Werror=array-bounds]
33 | PORTB &= ~_BV(PORTB5);
| ^~
cc1: all warnings being treated as errors
make: *** [<builtin>: main.o] Error 1
$
It does seem rather odd as 0 is certainly not “outside array bounds”, therefor I had no idea as to how to remediate it.
Where I demonstrate the three values of Forth; speed, extensibility and interactive, to develop a better understanding of the ATmega328P.
In describing Forth to others, I typically use what I call the “Three Values of Forth”. They are speed, both in execution and development, extensibility, the capability to easily add to the language and interactive, the ability to easily interact with Forth using the serial terminal. It is these three values which make Forth, a great language to use for programming microcontroller boards.