Electronic – Basic timings with an STM32

armclockstm32

I am looking to make very basic timings on an STM32. For example, I would like to program my STM32 to output bytes on the UART for 1 minute. What clock/timer should I use?

Looking through the reference manual, a lot of clocks are available. It seems that some of the most appropriate clocks would be the Real Time Clock (RTC), a General-Purpose Timer or an Advanced-Control Timer. What clock would be the easiest to use for making basic fixed timings?

Best Answer

For periodic tasks, I would recommend using a SysTick interrupt. Here's an example application:

void SysTick_Handler(void) {
    static uint8_t tick   = 0;
    static uint8_t second = 0;
    static uint8_t minute = 0;

    switch (tick++) {
        case 0:
            // task 0 here
            break;
        case 1:
            // task 1 here
            break;
        // and so on
        case 99:
            tick = 0;
            second = (second < 59) ? (second + 1) : 0;
            minute = (second == 0) ? ((minute < 59) ? (minute + 1) : 0) : minute;
            break;  
    }
}

int main(void)
{
    // interrupt at 100Hz
    SysTick_Config(SystemCoreClock/100);

    while (1);
}