Keil Logo

Timer

Timers are peripherals that generate periodic events in a microcontroller. A timer can be used as a PWM generator, or a trigger for a motor, or to control an ADC. The main parts of a timer are: a counter-, a reload-, and a match-register.

To generate periodic events, the timer must be configured with a value and a match event or an over-/underflow event. Timers are implemented differently across various microcontrollers.

The general steps to configure a timer are:

  • Enable the clock signal for the timer.
  • Setup the timer prescaler.
  • Setup the timer reload value.
  • Configure the timer as up- or down counter.
  • Configure the timer match.
  • Configure the interrupt.

For this example the following steps are required:

  1. Create a new project.
  2. Select the STM32F107VC microcontroller from the Device Database.
  3. Include the files:
    • startup_stm32f10x_cl.s
    • system_stm32f10x_cl.c
    • core_cm3.c
    • Serial.c
    • Retarget.c
  4. Create and add a new file, Timer.c, to the project.
  5. Use the code below as a reference.
    /*-----------------------------------------------------------------------------
     * Name:    Timer.c
     * Purpose: Timer example. Prints a sting to UART2
     * Notes:
    *----------------------------------------------------------------------------*/
    #include <stdio.h>
    #include <stm32f10x_cl.h>
    
    volatile unsigned long TimeTick;            // Counts 10ms timeTicks
    unsigned char clock_1s;                     // Flag activated each second
    
    extern void init_serial (void);             // Function defined in Serial.c
    
    /******************************************************************************
      Initialise the TIM3 for 1ms @ 72MHz
    ******************************************************************************/
    void TIM3_Init (void)  {
    
      RCC->APB1ENR |= (1<<1);                   // enable clock for TIM3
    
      TIM3->PSC = 3;                            // set prescaler
      TIM3->ARR = (18000000UL / 1000UL) - 1UL;  // set auto-reload
      TIM3->CR1 = 0;                            // reset command register 1
      TIM3->CR2 = 0;                            // reset command register 2
      TIM3->DIER = (1<<0);                      // Update interrupt enabled
      TIM3->CR1 |= (1<<0);                      // Enable Timer
    
      NVIC_EnableIRQ (TIM3_IRQn);               // Enable TIM3 interrupt
    }
    
    /*-----------------------------------------------------------------------------
      Timer Counter 3 interrupt service function
      executes each 1ms @ 25 MHz Crystal Clock
    *----------------------------------------------------------------------------*/
    void TIM3_IRQHandler (void)  {
    
      if (TIM3->SR & (1<<0))  {                 // UIF interrupt?
        if (TimeTick++ >= 999)  {               // Set clock_1s to 1 every 1 second
          TimeTick    = 0;
          clock_1s = 1;
      }
        TIM3->SR &= ~(1<<0);                    // clear UIF flag
      }
    }
    
    /*-----------------------------------------------------------------------------
      MAIN function
    *----------------------------------------------------------------------------*/
    int main (void)  {
    
      SystemInit();                             // Initialize system functions
      init_serial();                            // Initialize serial interface
      TIM3_Init();
    
      while (1)  {                              // Loop forever
        if (clock_1s)  {
          clock_1s = 0;
          printf ("TimeTick\r\n");
        }
      }
    }
    
  6. Build the project.
  7. Connect a Flash programming utility to the PC and to the board.
  8. Configure µVision for Flash downloading.
  9. Download to Flash and debug the application.
  Arm logo
Important information

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies.

Change Settings

Privacy Policy Update

Arm’s Privacy Policy has been updated. By continuing to use our site, you consent to Arm’s Privacy Policy. Please review our Privacy Policy to learn more about our collection, use and transfers
of your data.