Wellys Dev

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

  • 2022-09-08
    Developing in C for the ATmega328P: Struct - buttons[]

    Where I describe the struct, buttons[] and how to use it to debounce and check for button presses.

    Introduction

    Describing buttons[] is slightly different than describing a function such as analogWrite() and digitalRead() as buttons[] is a struct. The goal of this entry is to provide sufficient instruction as to how to use buttons[]. If you wish to understand more as to how to debounce a button, please review the links mentioned at the end of this entry.

  • 2022-08-29
    Developing in C for the ATmega328P: Function - digitalRead()

    Where I describe the function - digitalRead().

    Introduction

    The function digitalRead() is analogous to digitalWrite(), in the sense, it takes a pin number as a parameter and as compared to setting its value, it returns the pin’s value. The value will either be a HIGH, indicating the value of the pin was approximately 5V or it will be LOW, indicating the value is approximately 0V or GND.

    Background

    Like digitalWrite(), it also requires using pinMode(), however there are two choices to use, INPUT or INPUT_PULLUP. For the most part, its best to use INPUT_PULLUP as it will have the least chance for error. What this means, is that when a pin is in INPUT_PULLUP, its value will always be HIGH unless it is specifically pulled low. For example, pressing a button connected to ground would pull the pin to GND or LOW. When using INPUT, you will need to ensure the device connected to the pin, will either be at 5V or GND.

  • 2022-08-28
    Developing in C for the ATmega328P: Function - pinMode()

    Where I describe the function - pinMode().

    Introduction

    The function, pinMode() is one of the most important and one of the most forgotten of all of the Arduino framework. It is important, as it sets the function of the pin in question. And when its forgotten, hours of debugging may occur before the programmer realizes “the program works, I forgot to setup the pin”.

    The Arduino Reference: Digital Pins also has a good description as to how pins and pinMode() works.

  • 2022-08-28
    Developing in C for the ATmega328P: Function - digitalWrite()

    Where I describe the function - digitalWrite().

    Introduction

    The digitalWrite() function is a basic building block of using a microcontroller. The function will either raise the value of an output pin to 5V or it will lower it to GND (0V), and in doing so will provide current to drive an LED, a motor or any device which requires current.

    The Arduino Reference: Digital Pins also has a good description as to how pins and pinMode() works.

  • 2022-08-25
    Developing in C on the AVR ATmega328P: Frequently Found Errors (FFE)

    Where I attempt to gather in one place, all of the errors found while building (compile/link/locate) and uploading code to the Uno.

    1. Everytime, I run make, I get an error from Makefile like “Makefile:65: *** missing separator. Stop.” or “Makefile:125….”

    A: Make and tabs

    2. I just downloaded the Windows AVR bundle from Zak and I’m getting this wierd error “error: array subscript 0 is outside array bounds”!

    A: GCC 12 AVR array subscript 0 is outside array bounds

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