Wellys Dev

  • Home
  • Search/Topics
  • Writings
  • About
  • 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.

  • 2022-09-13
    Developing in C for the ATmega328P: Comparison of Languages

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

  • 2022-09-13
    Developing in C for the ATmega328P: Hints

    This page provides a broad level of short hints and comments as to C programming and AVR programming.

    C Best Practices

    From Embedded C Coding Standard Barr Group

    While the entire book is outstanding, I want to call out some specific practices which I strongly recommend following:

    Braces

    b. Each left brace ({) shall appear by itself on the line below the start of the block it opens. The corresponding right brace (}) shall appear by itself in the same position the appropriate number of lines later in the file.

  • 2022-09-12
    Developing in C for the ATmega328P: Function - analogRead()

    Where I describe the function - analogRead().

    Introduction

    The function analogRead() is similar to digitalRead(), in the sense, it takes a pin number as a parameter and returns the pin’s value. In the case of analogRead, the value will be a number between 0 and 1024. With the range of numbers corresponding to the range of voltages on the pin, typically between 0V and 5V.

    Digital vs Analog

    In the microcontroller world, there are two methods of measurement, digital and analog. The former identifies a binary value, HIGH or LOW. The latter describes a linear range of values, based on the voltage which appears on the pin and the number of bits of the analog-to-digital controller (ADC) on the microcontroller. For example, the Uno has a 10-bit ADC, which means the value may range from 0-1024. One more element defining the value is the analog reference (AREF) voltage, which defines the highest voltage, the value 1024 will represent. Typically, (and in our case) this value will be 5V. (There are ways to define it as 1.1V or to an external voltage on a specific pin.) Therefore, the range of voltages which can be returned by analogRead() are 0V to 5V, and each step is 5V/1024 or .00488V or 4.88mV.

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