Ads

Get STM32 tutorial using HAL at $10 for a limited time!

Sunday, July 19, 2015

STM32F4 Discovery Tutorial 7 - ADC

In this tutorial, I will share how to use ADC on STM32F4 Discovery to read analog voltage. ADC is stands for Analog to Digital Converter. Microcontrollers are digital component, so they only understand discrete/digital signals. Therefore if you want to read analog voltage that can be from various sensors, you need an ADC. STM32F407 has 3 ADC that can work independently. Every ADC have 18 channels. 16 channels are external and connected to GPIO pin. 2 channels are internal that connected to internal temperature sensor and ADC voltage reference.

When you use ADC, you can choose which ADC and its channel from this table:


Friday, July 17, 2015

Arduino Tutorial - Make Delay with Timer 0 (CTC Mode, Pooling)

In my another tutorial Make Delay with Timer 0 (CTC Mode, Pooling), I have made a delay function using timer 0 in normal mode. This time I will modify the timer mode to use CTC mode. In normal mode, timer count will count up from loaded value to overflow value. In CTC mode, timer count will count up from zero to compare match value. Compare match value is stored on compare match register (OCRx). To calculate compare match value for delay 10ms, we can use this formula OCR0A = (0.01s * 16000000Hz)/1024 = 156 = 0x9C.

After we get OCR0A value, we can use that in delay function:
// Delay 10ms with timer 0 CTC mode, pooling
void T0Delay()
{
    // Load initial count value
    TCNT0 = 0;
    // Load compare match value
    OCR0A = 0x9C;
    // Init timer mode and prescaler
    TCCR0A = (1 << WGM01);
    TCCR0B = (1 << CS02) | ( 1 << CS00);
    // Wait until timer 0 compare match
    while ((TIFR0 & (1 << OCF0A)) == 0);
    // Stop timer 0
    TCCR0B = 0;
    // Clear compare math flag
    TIFR0 = (1 << OCF0A);
}

Arduino Tutorial - Make Delay with Timer 0 (Normal Mode, Pooling)

In this tutorial, I will show you how to make a delay function with timer 0 on Arduino Uno. The delay function will generate a delay of 10ms. The settings that I used for this timer is normal mode. In this mode, timer will count from a loaded value up to timer overflow value. Loaded value for timer 0 (TCNT0) can be calculated with this formula TCNT0 = (1 + 0xFF) - (T * CLK)/N. T is the period of this timer will be overflow (0.01s), N is the prescaler, and CLK is the crystal frequency (16 MHz). The prescaler that I use is 1024. Therefore the TCNT0 value is (1 + 0xFF) - (0.01s * 16000000Hz)/1024 = 100 = 0x64.

This is the function for generating 10ms delay using timer 0:
// Delay 10ms with timer 0 normal mode, pooling
void T0Delay()
{
    // Load initial count value
    TCNT0 = 0x64;
    // Init timer mode and prescaler
    TCCR0A = 0;
    TCCR0B = (1 << CS02) | (1 << CS00);
    // Wait until timer 0 overflow
    while ((TIFR0 & (1 << TOV0)) == 0);
    // Stop timer 0
    TCCR0B = 0;
    // Clear time 0 overflow flag
    TIFR0 = (1 << TOV0);
}
In this function there is a while loop for waiting until timer 0 is overflow. Using while loop for waiting something happen is usually called pooling method. When use pooling method, CPU is not do anything and can't do other task, just wait until something happen.

Sunday, July 12, 2015

AVR ATmega Tutorial - Millis Function on AVR Microcontrollers

In this tutorial, I will make tutorial for making millis() function that will return number of millisecond since AVR microcontrollers began executing program. This function is like millis() function on Arduino. I will use ATmega16 and CodeVisionAVR for this tutorial. First, you have to make a variable that will hold the value of millisecond. I declared this variable as volatile because the value of this variable will be changed inside ISR.
// Store value of millisecond since uC started
volatile unsigned long millis_value;
Next, Setup timer 0 (or another timer if you wish) to generate interrupt every 1 ms. I am using timer 0 on CTC mode to count up from 0 to the value of OCR0 register. The prescaler that I use is 64 with 16 MHz CPU clock. To calculate the OCR0 register for 1ms compare match, we can use this formula OCRx = ((T x Fcpu) / N) - 1. Where T is the desired compare match value in second (1ms = 0.001s), Fcpu in Hz is 16,000,000 and N is prescaler (64). The OCR0 value will be 0xF9. To generate code for this settings we will use CodeWizardAVR. This is the settings for timer 0: