Wellys Dev

  • Home
  • Search/Topics
  • Writings
  • About
  • 2022-09-27
    Developing in C for the ATmega328P: Code vs. Cost

    Where I demonstrate the value of coding efficiency to reduce the cost of hardware.

    Video

    Introduction

    In the another entry, I discussed code size, program space and RAM requirements and why they matter. Here are my thoughts from that page:

    Why Size Matters

    (From Developing in C for the ATmega328P: structures)

  • 2022-09-26
    Developing in C for the ATmega328P: struct

    Where I demonstrate how to use the C Language data type struct and how to use it to simplify a program.

    Introduction

    As programs become more complicated, its desirable to group common elements into a “block”, then debug that block and have the block serve as a single debugged element. For example, a function can serve as a block of code which performs a set of commands commonly used together. Once debugged, the function can be called instead of the individual commands, simplifying debugging and code management.

  • 2022-09-24
    Developing in C for the ATmega328P: Example Code

    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).

  • 2022-09-24
    Developing in C for the ATmega328: Using PROGMEM

    Where I discuss how to use PROGMEM (storing values in Flash memory) for storage.

    Introduction

    Storing message strings in AVR RAM can become prohibitively expensive, very quickly. With only 2KB of RAM, the Uno needs as much RAM available for dynamic storage, while message strings are best stored in Flash memory. Due to the AVR microcontroller’s different architecture, you can’t simply read the Flash memory as reading RAM. You also can’t simply “embed” strings in Flash, by making them a constant, there are very specific commands to do both of these tasks.

  • 2022-09-24
    Developing in C for the ATmega328: Marking Time and Measuring Time

    Where I discuss marking time and measuring program execution time.

    Introduction

    The Arduino has two time routines, millis() and micros(). The former provides the ability to measure time in milliseconds for a long period of time (50 days) and the latter provides the ability to measure in microseconds (kinda) for a far shorter period of time, 70 minutes. The kinda arises from this comment from the arduino reference on micros() “On 16 MHz Arduino boards (e.g. Duemilanove and Nano), this function has a resolution of four microseconds (i.e. the value returned is always a multiple of four). On 8 MHz Arduino boards (e.g. the LilyPad), this function has a resolution of eight microseconds.”

  • 2022-09-22
    Developing in C for the ATmega328P: Function - tone()

    Where I describe the function - tone() as well as noTone() and how to play music on the Uno.

    Introduction

    Using the function tone() allows us to play musical notes via the Uno and a speaker. Using tone() is more difficult than understanding the command. It requires having a understanding of music, songs and melodies to use tone() to its fullest.

    We are also somewhat spoiled in this era of polyphonic speakers, that is speakers which are able to play multiple notes at once. The Uno and a simple cone speaker is only capable of playing a single tone, which limits the melodies and fidelity of the music played.

  • 2022-09-22
    Developing in C for the ATmega328P: Multi-tasking the Arduino - Part 1

    Where I develop the C version of multi-tasking, similar to the example provided by Adafruit.

    Introduction

    An informative and valuable lesson on the Adafruit site is Multi-tasking the Arduino - Part 1. In this explanation, the author lays out how to “Make your Arduino walk and chew gum at the same time.” While it is phenomenal instructional content, the ultimate solution is a class approach which is not allowed in C. This entry will follow the content of the original, however, it will provide C-based solution.

  • 2022-09-20
    Developing in C for the ATmega328P: User Functions

    Where I describe how to develop user functions in AVR_C.

    Introduction

    While much of the content in “Developing in C for the ATmega328P” is focused on the Library functions, developing your own functions is an important aspect of programming in C. Functions help you structure your code into smaller, less complex modules. This allows you to debug each module, then as you develop your code and you will feel more confident that the program will work, if all modules are error-free.

  • 2022-09-18
    Developing in C for the ATmega328P: Using Data Types and Math

    Where I describe the impact of data types on math on an AVR microcontroller (ATmega328P) and pitfalls to avoid.

    Introduction

    While working on the mapping entry I ran into a problem with the mapping function. The function wasn’t returning correct values. I solved it by changing the data types and examining the results, after a couple of attempts, it was clear the mapping function requires a long data type for all of its calculations. This issue got me thinking about the impact of data types on complex math functions using an embedded microcontroller.

  • 2022-09-15
    Developing in C for the ATmega328P: Mapping Values

    Where I describe how to map values in one domain to another domain.

    Introduction

    Mapping is the concept of translating a range of numbers to a second range of numbers, where the second range might be smaller (typically) or larger than the first range. For example, if you had a 10-bit analog-to-digital converter (ADC) and wanted to use its values for the duty cycle of an 8-bit pulse-width modulator (PWM), you could “divide by 4” to achieve the desired results. That is, if, the range of the ADC values were 0-1024 and the desired range of the PWM values are 0-255. If not, we’ll need to map the values of the first range into the desired values of the second range. We’ll talk about both approaches in this entry.

Page 3 of 8
Copyright © 2025 Lief Koepsel
  • Home
  • Search/Topics
  • Writings
  • About