Using Volatile keyword in embedded code

Volatile is probably the least documented keyword in most tutorials and books. This is the primary cause of most misuses and bugs related to it. If you already are programming microcontrollers, you probably know that volatile is always used on global variables that are accessed from interrupt service routines. Otherwise, code won’t work. After few requests, I decided to drop few lines about volatile keywords. This keyword is commonly used to tag memory type. We hear “volatile memory” and “non-volatile memory” when discussing computer hardware. As a quick reminder – “non-volatile memory” is a type of memory that stores its contents even when power is off. Such type of memory is EEPROM, Flash, and FRAM. This is easy from a hardware perspective. But what volatile keyword means in C or C++ code? This is an indicator (called qualifier) to the compiler that tells that this variable may be changed during program flow even if it doesn’t look like it be. This means that compiler must treat this value seriously and keep optimizer away from it.

Continue reading

Software Debouncing of buttons

Connecting a button as an input to a microcontroller is a relatively easy task, but there are some problems. The main problem is that switches bounce, i.e., when you press (or release) a button, it will often change level a couple of times before it settles at the new level. So if you, for example, connect the switch to a pin with an external interrupt enabled, you will get several interrupts when you press the button once. This behavior usually is not wanted. Even if the buttons didn’t bounce (with filtering hardware, for example), we still want to capture the event of a pushed button and take some action once for every button press, so we need to keep track of the state of the switch as well. One technique used in this tutorial to handle this is to check (poll) the button(s) periodically and only decide that a button is pressed if it has been in the pressed state for a couple of subsequent polls.

Continue reading

Accelerometer Interfacing with AVR

ADXL335 size

The article covers how to interface an accelerometer with the atmega32/atmega16. Before proceeding, the user must know the basics of ADC (Analogy to digital converter) of the AVR. An accelerometer is an electromechanical device that measures acceleration forces. These forces may be static, like the constant force of gravity pulling at your feet, or they could be dynamic – caused by moving or vibrating the accelerometer. Accelerometers are of two types Analog and Digital. In this post, we will be discussing Analog accelerometers. They give voltage as output which is proportional to acceleration. The digital one gives the PWM output or direct binary digital data

Continue reading

All you need to know about AVR fuses

avr crystal oscillator

AVR lock bits and fuses are one of the topics that may cause some confusion. If you missed something or set one bit wrong, it may lead to failure – bricking the whole AVR chip. So it is important to understand once and do things right. Even though datasheets give enough information about AVR fuses, many times, we feel somewhat unsure before executing the write command. Let us go through the main features of AVR fuses and lock bits so next time we would feel safe and get the expected results.

Continue reading

Introducing to STM32 ADC programming. Part2

After we had a quick overview of the STM32 ADC peripheral, we can dig deeper into specifics. To understand simple things, let’s go with the simplest case – single conversion mode. In this mode, ADC does one conversion and then stops. After the ADC conversion result is stored into the 16-bit ADC_DR data register (remember that the conversion result is 12-bit), then the End of Conversion (EOC) flag is set. An interrupt is generated if the EOCIE flag is set. The same situation is if the injected channel is converted. The difference is that the result is stored in the corresponding ADC_DRJx register, the JEOC flag is set, an interrupt is generated if the JEOCIE flag is set. In our example, we will measure the internal temperature sensor value and send it using USART. A temperature sensor is internally connected to the ADC1_IN16 channel. The algorithm will start a single conversion and wait for the conversion complete flag EOC. We will then read the ADC value from the ADC_DR register, which will later be used to calculate Celsius’s temperature value…

Continue reading