Wellys Dev

  • Home
  • Search/Topics
  • Writings
  • About
  • 2022-08-24
    Developing in C for the ATmega328P: Functions - Serial input/output

    Where I describe the functions for serial input/out - puts(), getchar() and printf().

    Introduction

    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:

    • getchar(): reads the next character from the standard input stream and returns it as type int
    • putchar(char): writes a character to the standard output stream
    • puts(string): print a null-terminated string to the standard output stream, ending with a newline character
    • printf(format, variable list): writes to the standard output stream, the list of variables using the formatting string specified. There is considerable detail as to how to use printf below.
    • readLine(*buffer, buffersize) reads a line of text from serial input, either for buffersize number of characters or until CR has been entered. Returns the number of characters read.

    Baud Setting

    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:

  • 2022-08-22
    Developing in C for the ATmega328P: Function - analogWrite()

    Where I describe the function - analogWrite().

    Introduction

    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.

    Pulse Width Modulation (PWM)

    Lets start with explaining pulse-width modulation. Here is a video where a pulse is being modulated:

  • 2022-08-19
    Developing in C for the ATmega328P: AVR_C Library Functions

    Where I describe how to use the Library functions in AVR_C.

    Introduction

    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:

    1. The Standard C Library also known as avr-libc
    2. The AVR_C Library, a GitHub repository developed to mirror the functionality of the Arduino Language

    Standard C Library

    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.

  • 2022-08-18
    Developing in C on the AVR ATmega328P: Frequently Asked Questions (FAQ)

    Where I attempt to direct all of the questions, pertaining to Developing in C for the Arduino Uno.

    How do I find my Serial port?

    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

    1. Open the Arduino IDE and go to Tools -> Port and write down the port which has (Arduino Uno) after it. If you aren’t using an Uno, it might not identify itself. Which means you might have to guess, or unplug your board and see which one goes away.
    2. Open CoolTerm, it will provide a drop-down of devices which can be connected to by, CoolTerm.

    Why is learning C important for the Arduino Uno?

    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.

  • 2022-07-23
    Developing in C for the ATmega328P: Build

    Where I describe the process of developing code for the Arduino Uno(AVR ATmega328P), specifically, the build process.

    Introduction (from Edit)

    The middle three steps compile/link/locate are typically called the build process, which can simplify the five steps to three:

    1. Edit
    2. Build
    3. Upload

    For a detailed and very worthwhile description of the three steps in Build, I highly recommend this page, Compiling, Link and Locating: Barr Group.

    2. Build (compile/link/locate)

    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.

  • 2022-07-23
    Developing in C for the ATmega328P: Edit

    Where I describe the process of developing code for the Arduino Uno(AVR ATmega328P), specifically, editing the code.

    Keyboard Commands Cheatsheet (in all applications)

    Description Windows Keys macOS Keys
    Copy selection Ctrl - c Cmd - c
    Paste clipboard Ctrl - v Cmd - v
    Select all Ctrl - a Cmd - a

    VS Code Keyboard Commands Cheatsheet

    Description Windows Keys macOS Keys
    Tool Palette Ctrl-Shift-p Cmd-Shift-p
    Build Task Ctrl-Shift-b Cmd-Shift-b

    Terminal Commands Cheatsheet

    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

    Setup Raspberry Pi Development Screen

    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.

  • 2022-07-23
    Developing in C for the ATmega328P: Upload

    Where I describe the process of developing code for the Arduino Uno(AVR ATmega328P), specifically, uploading code to the Uno.

    Introduction (from Edit)

    The middle three steps compile/link/locate are typically called the build process, which can simplify the five steps to three:

    1. Edit
    2. Build
    3. Upload

    Upload

    Determine the port

    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:

  • 2022-07-07
    Developing in C for the ATmega328P: Using git

    Where I discuss how to implement and use git to develop code in C for the Arduino Uno.

    Sources

    • Beginner’s Guide to Git and GitHub

    Introduction

    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…

  • 2022-06-23
    GCC 12 AVR array subscript 0 is outside array bounds

    Where I describe a new error caused by a bug in GCC 12 as it relates to the AVR microcontrollers.

    Sources:

    • GCC Bug report
    • Another bread crumb

    Description

    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.

  • 2022-05-29
    Flashforth: Using the Three Values of Forth

    Where I demonstrate the three values of Forth; speed, extensibility and interactive, to develop a better understanding of the ATmega328P.

    Sources

    • Flashforth Example on Github

    Introduction

    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.

Page 7 of 15
Copyright © 2025 Lief Koepsel
  • Home
  • Search/Topics
  • Writings
  • About