Warning
This page is located in archive. Go to the latest version of this course pages.

T03 - Stopwatch

Write a program for Nucleo STM32F401RE and Experimental shield, that performs a 1s precision stopwatch (or better).

Instructions:

  • Use LED display on the Experimental shield
  • Use button S1 to start/stop/clear stopwatch

How timer / counter works

Timer TIM2 (pooling)

#include "stm32f4xx.h"
 
#define setbit(reg, bit)      ((reg) |= (1U << (bit)))
#define clearbit(reg, bit)    ((reg) &= (~(1U << (bit))))
#define togglebit(reg, bit)   ((reg) ^= (1U << (bit)))
#define getbit(reg, bit)      (((reg) & (1U << (bit))) >> (bit))
 
void initLED(void) {
    setbit(RCC->AHB1ENR, 0);
    setbit(GPIOA->MODER, 10);
}
 
void initTIM2(void)
{
    /*
     * TIM2 je pripojen ke sbernici APB1, ktera bezi na 16MHz
     * frekvence pred TIM2 je zvojnasobena PLL
     */
    setbit(RCC->APB1ENR, 0);  /* enable Clock for TIM2 */
    TIM2->PSC = ?;                /* prescaler - doplnte podle potreby */
    TIM2->ARR = ?;                /* autoreload - doplnte podle potreby */
    setbit(TIM2->CR1, 0);        /* enable TIM2 */
    clearbit(TIM2->SR, 0);       /* status TIM2 */
}
 
int main(void)
{
    initLED();
    initTIM2();
 
    while(1)
    {
        /* test na UEV (update event)    */
        if(getbit(TIM2->SR, 0))
        {
            togglebit(GPIOA->ODR, 5);
            /* vraceni status bitu na log 0 */
            clearbit(TIM2->SR, 0);
        }
    }
}

courses/be2m37mam/tasks/task03.txt · Last modified: 2019/11/14 13:47 by viteks