Developing in C for the ATmega328P: Example Code

4 minute read

Where I list all of the examples found in the examples folder for easy reference.

Introduction

When using a specific function or while attempting to understand a C programming concept, it is helpful to see a working example. The examples below, (except where noted), have all been tested and work on an Arduino Uno.

Examples

AVR C Code on Github

analogRead

Demonstrates analogRead() which reads the 10bit Analog-Digital Converter (ADC).

analogRead_16

Currently not a working example, a AVR program which demonstrates how to over-sample the ADC to derive a 16bit result.

analogWrite

Demonstrates analogWrite(), which configures one of the 6 PWM pins (those marked with "~" to deliver a PWM signal with a specific duty cycle.

Classic blink routine, use to confirm everything is working.

A blink example using native bit-setting commands, use to understand how set bits for higher speeds.

Demonstrates how to use digitalWrite(pin, TOG) to flip a bit, as compared to a read-modify-write sequence, required by the Arduino framework.

Demonstrates blink_wo_delay using a struct to initialize and control the blinking.

Adafruit example, blink_wo_delay in C from Multi-tasking the Arduino Part 1

button

Demonstrates how to use an ISR-based approach to debounce a set of buttons.

calculations

Demonstrates the impact of data types on math calculations.

constrain

Demonstrates how to write a constrain function, which could be used as the Arduino constrain function.

d_analogRead

Demonstrates a dummy version of analogRead() using a random number generator rand(). Remaining logic, is identical to analogRead() example.

digitalRead

Demonstrates how to use digitalRead()

durationTest

A very simple linear test to demonstrate tone(), plays “Shave and Haircut…”.

flashwithoutdelay

Adafruit State Machine example, flashwithoutdelay in C from Multi-tasking the Arduino Part 1

float_perf

Demonstrates integer vs. float performance when performing calculations, also demonstrates integer math errors of large numbers.

four_states

A multi-file demonstration of a state machine with each state using both a header and code file.

map

Demonstrates both types of mapping, one using shifts to move from 10bits to 8bits of a range and the other using a map function.

melody_array

Similar to durationTest, plays “Shave and a Haircut”, however, uses arrays for the notes and durations. Demonstrates a more similar to the way music would be constructed as compared to hard-coded in durationTest. See: Play a Melody using the tone function

micros - see ticks

millis

Demonstrates how to use millis(), how to set it up and how to use it.

min_max

A min_max example similar to the Arduino functions min() and max(), neither are implemented in AVR_C due to the proliferation of types, which would be needed. Use these examples as a guide to making your own.

multi_Ard

Very, very simple multi-tasking example using an Arduino digitalWrite(), which makes it pretty slow.

multi_struct

Demonstrates using a struct instead of using a class in implementing Adafruit’s multi-tasking example, “A Classy Approach”.

multi_struct_usec

A more precise (microseconds) demonstration using a struct instead of using a class in implementing Adafruit’s multi-tasking example, “A Classy Approach”.

multifunction

A very simple multi-tasking example which used function pointers to move between tasks. Each function can be a very different task with a simple round-robin scheduling technique. Uses AVR native commands for highest possible speed.

oneline_Ard and oneline_avr

Comparison of using the oneline multi-tasking concept comparing using native AVR commands vs. Arduino-style commands. Used to compare speed of execution.

powers

Calculates and prints the powers of 2 for each data type in C for the ATmega328P. Does not demonstrate data types for 64bits or long long as AVR-LIBC documentation for vprintf() states “But the ll length modifier will to [sic] abort the output, as this realization does not operate long long arguments.”

progmem

Demonstrates three methods of printing strings from PROGMEM. You use PROGMEM to store strings (or static data) in Flash to reduce the consumption of RAM.

rand_test

Simple test of the C language pseudo-random number generator (PRNG) rand().

serialio_char

Very simple serial test to demonstrate the setup required and how to puts() and printf().

serialio_string

Simple string I/O example to demonstrate the setup required and how to scanf() and printf().

serialRead

Demonstrates replacing analogRead() with the serialRead() function so that it is easier to debug a program. The function serialRead() provides the capability of using the serial port instead of an analog device.

ticks

Demonstrates how to use ticks() and ticks_ro() for measuring execution times. The Uno runs at 16MHz resulting in a 62.5nsec clock cycle, and ticks() rolls over every 65,535 times or 4.096ms. This example allows you to check it’s accuracy. Note: ticks() and ticks_ro() replaces micros(). Dividing the number of ticks by 16 gets you 1 microsecond, which is the same as shifting to the right, 4 times. So n usecs = (ticks() ยป 4).

tinymt

Demonstrates using Tiny Mersenne Twister pseudo-random number generator (PRNG), instead of the typical function, rand(). The Tiny Mersenne Twister is thought to be of much higher quality randomness than the rand() function.

Comments powered by Talkyard.