Timer Interrupt

This sample uses the TIM to generate a timeout interrupt every second.

To facilitate observation, connect the P0_0 pin to LED0 on the EVB board.

When the timer reaches 1 second, an interrupt will be triggered. In the interrupt handler function, the program will toggle the level of the P0_0 pin, changing the GPIO output polarity, thereby making LED0 blink once per second.

Here should be enhtimer timeout.

Timer timeout diagram

Users can modify pin information, output frequency, and whether to dynamically change the frequency through different macro configurations. For specific macro configurations, refer to Configurations.

Requirements

For requirements, please refer to the Requirements.

Wiring

Connect P0_0 and LED0.

The LED driver circuit is shown below.

Here should be a picture of the LED driver circuit

LED Driver Circuit Diagram

Configurations

  1. The following macro can be configured to modify the pin definition.

    #define OUTPUT_PIN          P0_0
    #define GPIO_PIN            GPIO_GetPin(OUTPUT_PIN)
    #define GPIO_PORT           GPIO_GetPort(OUTPUT_PIN)
    
  2. The following macro can be configured to modify the timing.

    #define TIME_UINT            40                            /*< 40M source clock is divided to get unit: 1us */
    #define TIME_TIMING          1000000                       /*< Set this macro to 1000000, with a timer duration of 1s. The unit for this macro is in microseconds (us). */
    #define TIMER_PERIOD         (TIME_TIMING * TIME_UINT - 1)
    

Building and Downloading

For building and downloading, please refer to the Building and Downloading.

Experimental Verification

  1. When the EVB starts, observe the following log within the Debug Analyzer.

    Start timer interrupt test!
    
  2. After enabling TIM, each time the count reaches 1 second, a TIM interrupt will be triggered. Within the interrupt function, the output level of P0_0 is toggled, allowing LED0 to be observed flashing once every second.

Code Overview

This section mainly introduces the code and process description for initialization and corresponding function implementation in the sample.

Source Code Directory

The directory for project file and source code are as follows:

  • Project directory: sdk\samples\peripheral\timer\timer_interrupt\proj

  • Source code directory: sdk\samples\peripheral\timer\timer_interrupt\src

Initialization

The initialization flow for peripherals can refer to Initialization Flow in General Introduction.

  1. Call Pad_Config() and Pinmux_Config() to configure the PAD and PINMUX of the corresponding pins.

    void board_gpio_init(void)
    {
        Pad_Config(OUTPUT_PIN, PAD_PINMUX_MODE, PAD_IS_PWRON, PAD_PULL_NONE, PAD_OUT_ENABLE, PAD_OUT_HIGH);
    
        Pinmux_Config(OUTPUT_PIN, DWGPIO);
    }
    
  2. Call RCC_PeriphClockCmd() to enable the GPIO clock.

  3. Initialize the GPIO peripheral:

    1. Define the GPIO_InitTypeDef type GPIO_InitStruct, and call GPIO_StructInit() to pre-fill GPIO_InitStruct with default values.

    2. Modify the GPIO_InitStruct parameters as needed. The initialization parameter configurations for GPIO are shown in the table below. Call GPIO_Init() to initialize the GPIO peripheral.

GPIO Initialization Parameters

GPIO Hardware Parameters

Setting in the GPIO_InitStruct

GPIO

GPIO pin

GPIO_InitTypeDef::GPIO_Pin

GPIO_PIN

GPIO direction

GPIO_InitTypeDef::GPIO_Dir

GPIO_DIR_OUT

  1. Call RCC_PeriphClockCmd() to enable the TIM clock.

  2. Initialize the TIM peripheral:

    1. Define the TIM_TimeBaseInitTypeDef type TIM_InitStruct, and call TIM_StructInit() to pre-fill TIM_InitStruct with default values.

    2. Modify the TIM_InitStruct parameters as needed. The initialization parameter configurations for TIM are shown in the table below. Call TIM_TimeBaseInit() to initialize the TIM peripheral.

    3. Call NVIC_Init() to configure the NVIC. For NVIC-related configurations, refer to Interrupt Configuration.

    4. Call TIM_ClearINT() and TIM_INTConfig() to clear the TIM interrupt and enable the TIM interrupt.

TIM Initialization Parameters

TIM Hardware Parameters

Setting in the TIM_InitStruct

TIM

Counter mode

TIM_TimeBaseInitTypeDef::TIM_Mode

TIM_Mode_UserDefine

PWM mode

TIM_TimeBaseInitTypeDef::TIM_PWM_En

DISABLE

Count value

TIM_TimeBaseInitTypeDef::TIM_Period

TIMER_PERIOD

  1. Call TIM_Cmd() to enable the TIM peripheral.

Functional Implementation

After enabling the TIM peripheral, the TIM begins timing. When the timing period ends, an interrupt is triggered, entering the interrupt handler function Timer2_Handler. When the timer of TIM2 reaches the specified timing, it triggers an interrupt and enters the interrupt handling function called Timer2_Handler.

  1. Clear the interrupt flag of TIM2 and disable TIM2.

  2. Determine the current LED status and call GPIO_WriteBit() to toggle the output level of P0_0.

  3. Enable TIM2 again.

void TIMER_Handler(void)
{
    TIM_ClearINT(TIMER_NUM);
    TIM_Cmd(TIMER_NUM, DISABLE);
    if (!LED_Status)
    {
        GPIO_WriteBit(GPIO_PORT, GPIO_PIN, (BitAction)(1));
        LED_Status = 1;
    }
    else
    {
        GPIO_WriteBit(GPIO_PORT, GPIO_PIN, (BitAction)(0));
        LED_Status = 0;
    }
    //Add user code here
    TIM_Cmd(TIMER_NUM, ENABLE);
}