TIM Exported Functions

group TIM_Exported_Functions

Functions

void TIM_DeInit(TIM_TypeDef *TIMx)

Deinitialize the specified TIMx registers to their default reset values.

Example usage

void timer_demo(void)
{
    TIM_DeInit(TIM6);
}

参数:

TIMx -- [in] Select the TIM peripheral. Refer to TIM Declaration.

void TIM_TimeBaseInit(TIM_TypeDef *TIMx, TIM_TimeBaseInitTypeDef *TIM_TimeBaseInitStruct)

Initialize the specified TIMx according to the specified parameters in TIM_TimeBaseInitStruct.

Example usage

void driver_timer_init(void)
{
    RCC_PeriphClockCmd(APBPeriph_TIMER, APBPeriph_TIMER_CLOCK, ENABLE);

    TIM_TimeBaseInitTypeDef TIM_InitStruct;
    TIM_StructInit(&TIM_InitStruct);
    TIM_InitStruct.TIM_PWM_En = DISABLE;
    TIM_InitStruct.TIM_Period = 1000000 - 1 ;
    TIM_InitStruct.TIM_Mode = TIM_Mode_UserDefine;
    TIM_TimeBaseInit(TIM6, &TIM_InitStruct);
}

参数:
void TIM_StructInit(TIM_TimeBaseInitTypeDef *TIM_TimeBaseInitStruct)

Fill each TIM_InitStruct member with its default value.

Example usage

void driver_timer_init(void)
{
    RCC_PeriphClockCmd(APBPeriph_TIMER, APBPeriph_TIMER_CLOCK, ENABLE);

    TIM_TimeBaseInitTypeDef TIM_InitStruct;
    TIM_StructInit(&TIM_InitStruct);
    TIM_InitStruct.TIM_PWM_En = DISABLE;
    TIM_InitStruct.TIM_Period = 1000000 - 1;
    TIM_InitStruct.TIM_Mode = TIM_Mode_UserDefine;
    TIM_TimeBaseInit(TIM6, &TIM_InitStruct);
}

备注

The default settings for the TIM_InitStruct member are shown in the following table:

TIM_InitStruct member

Default value

TIM_ClockSrc

TIM_CLOCK_SRC_40M

TIM_Mode

TIM_Mode_UserDefine

TIM_Period

0xfff

TIM_SOURCE_DIV

TIM_CLOCK_DIVIDER_1

TIM_SOURCE_DIV_En

DISABLE

TIM_PWM_En

DISABLE

PWM_Stop_State_P

PWM_STOP_AT_LOW

PWM_Stop_State_N

PWM_STOP_AT_HIGH

PWMDeadZone_En

DISABLE

PWM_Deadzone_DIV

TIM_CLOCK_DIVIDER_1

PWM_Deazone_ClockSrc

PWM_CK_32K_TIMER

PWM_Deazone_Size

10

参数:

TIM_TimeBaseInitStruct -- [in] Pointer to a TIM_TimeBaseInitTypeDef structure which will be initialized.

void TIM_Cmd(TIM_TypeDef *TIMx, FunctionalState NewState)

Enable or disable the specified TIMx peripheral.

Example usage

void driver_timer_init(void)
{
    RCC_PeriphClockCmd(APBPeriph_TIMER, APBPeriph_TIMER_CLOCK, ENABLE);

    TIM_TimeBaseInitTypeDef TIM_InitStruct;
    TIM_StructInit(&TIM_InitStruct);
    TIM_InitStruct.TIM_PWM_En = DISABLE;
    TIM_InitStruct.TIM_Period = 1000000 - 1;
    TIM_InitStruct.TIM_Mode = TIM_Mode_UserDefine;
    TIM_TimeBaseInit(TIM6, &TIM_InitStruct);
    TIM_Cmd(TIM6, ENABLE);
}

参数:
  • TIMx -- [in] Select the TIM peripheral. Refer to TIM Declaration.

  • NewState -- [in] New state of the TIMx peripheral. This parameter can be one of the following values:

    • ENABLE: Count start.

    • DISABLE: Count stop and clear count value.

void TIM_INTConfig(TIM_TypeDef *TIMx, FunctionalState NewState)

Enable or disable the specified TIMx interrupt.

Example usage

void driver_timer_init(void)
{
    RCC_PeriphClockCmd(APBPeriph_TIMER, APBPeriph_TIMER_CLOCK, ENABLE);

    TIM_TimeBaseInitTypeDef TIM_InitStruct;
    TIM_StructInit(&TIM_InitStruct);
    TIM_InitStruct.TIM_PWM_En = DISABLE;
    TIM_InitStruct.TIM_Period = 1000000 - 1;
    TIM_InitStruct.TIM_Mode = TIM_Mode_UserDefine;
    TIM_TimeBaseInit(TIM6, &TIM_InitStruct);
    TIM_ClearINT(TIM6);
    TIM_INTConfig(TIM6, ENABLE);
}

参数:
  • TIMx -- [in] Select the TIM peripheral. Refer to TIM Declaration.

  • NewState -- [in] New state of the TIMx interrupt. This parameter can be one of the following values:

    • ENABLE: Enable the timeout interrupt.

    • DISABLE: Disable the timeout interrupt.

void TIM_ChangePeriod(TIM_TypeDef *TIMx, uint32_t period)

Change the specified TIMx period value.

Example usage

void timer_demo(void)
{
    uint32_t new_period = 1000000 - 1;
    TIM_Cmd(TIM6, DISABLE);
    TIM_ChangePeriod(TIM6, new_period);
}

参数:
  • TIMx -- [in] Select the TIM peripheral. Refer to TIM Declaration.

  • period -- [in] Period value to be changed. This parameter must range from 0x0 to 0xFFFFFFFF.

void TIM_PWMChangeFreqAndDuty(TIM_TypeDef *TIMx, uint32_t high_count, uint32_t low_count)

Change the PWM frequency and duty cycle of the specified TIMx according to high_count and low_count.

Example usage

#define TIM_DEMO TIMB_6

void timer_demo(void)
{
    uint32_t high_count = 1000000 - 1;
    uint32_t low_count = 1000000 - 1;
    TIM_Cmd(TIM_DEMO, DISABLE);
    TIM_ChangePeriod(TIM_DEMO, high_count, low_count);
}

参数:
  • TIMx -- [in] Select the TIM peripheral. Refer to TIM Declaration.

  • high_count -- [in] This parameter must range from 0~0xFFFFFFFF.

  • low_count -- [in] This parameter must range from be 0~0xFFFFFFFF.

uint32_t TIM_GetCurrentValue(TIM_TypeDef *TIMx)

Get the specified TIMx current value when timer is running.

Example usage

void timer_demo(void)
{
    uint32_t cur_value = TIM_GetCurrentValue(TIM6);
}

参数:

TIMx -- [in] Select the TIM peripheral. Refer to TIM Declaration.

返回:

The current counter value.

ITStatus TIM_GetINTStatus(TIM_TypeDef *TIMx)

Get the specified TIMx interrupt status.

Example usage

void timer_demo(void)
{
    ITStatus int_status = TIM_GetINTStatus(TIM6);
}

参数:

TIMx -- [in] Select the TIM peripheral. Refer to TIM Declaration.

返回:

The NewState of the timer interrupt status.

  • SET: The TIM interrupt has occurred.

  • RESET: The TIM interrupt has not occurred.

ITStatus TIM_GetOperationStatus(TIM_TypeDef *TIMx)

Get the specified TIMx operation status.

Example usage

void timer_demo(void)
{
    ITStatus intstatus = TIM_GetOperationStatus(TIM6);
}

参数:

TIMx -- [in] Select the TIM peripheral. Refer to TIM Declaration.

返回:

The NewState of the timer operation status.

  • SET: The timer is running.

  • RESET: The timer is not running.

uint32_t TIM_GetElapsedValue(TIM_TypeDef *TIMx)

Get the specified TIMx elapsed value when timer is running.

Example usage

void timer_demo(void)
{
    uint32_t value = TIM_GetElapsedValue(TIM6);
}

参数:

TIMx -- [in] Select the TIM peripheral. Refer to TIM Declaration.

返回:

The elapsed counter value.

void TIM_ClearINT(TIM_TypeDef *TIMx)

Clear the specified TIMx interrupt.

Example usage

void timer_demo(void)
{
    TIM_ClearINT(TIM6);
}

参数:

TIMx -- [in] Select the TIM peripheral. Refer to TIM Declaration.

void TIM_PWMComplOutputEMCmd(PWM_TypeDef *PWMx, FunctionalState NewState)

Enable or disable the emergency stop for PWM complementary output function.

Example usage

void pwm_demo(void)
{
   board_pwm_init();
   driver_pwm_init();
   TIM_PWMComplOutputEMCmd(PWM2,ENABLE);
}

备注

When using PWM functions, it is crucial to pay attention to the corresponding relationships: PWM2 corresponds to TIM2, and PWM3 corresponds to TIM3.

参数:
  • PWMx -- [in] Select the PWM peripheral. Refer to PWM Declaration.

  • NewState -- [in] NewState of complementary output.

    • ENABLE: Enable the emergency stop output function.

    • DISABLE: Disable the emergency stop output function.

void TIM_PWMDZBypassCmd(PWM_TypeDef *PWMx, FunctionalState NewState)

Enable or disable deadzone bypass for the PWM complementary output function. If enabled, PWM_P will become the inverse of PWM_N.

Example usage

void driver_pwm_init(void)
{
    RCC_PeriphClockCmd(APBPeriph_TIMER, APBPeriph_TIMER_CLOCK, ENABLE);

    TIM_TimeBaseInitTypeDef TIM_InitStruct;
    TIM_StructInit(&TIM_InitStruct);
    TIM_InitStruct.TIM_Mode             = TIM_Mode_UserDefine;
    TIM_InitStruct.TIM_PWM_En           = ENABLE;
    TIM_InitStruct.TIM_PWM_High_Count   = PWM_HIGH_COUNT;
    TIM_InitStruct.TIM_PWM_Low_Count    = PWM_LOW_COUNT;
    TIM_InitStruct.PWM_Stop_State_P     = PWM_STOP_AT_HIGH;
    TIM_InitStruct.PWM_Stop_State_N     = PWM_STOP_AT_LOW;
    TIM_InitStruct.PWMDeadZone_En       = ENABLE;  //enable to use pwn p/n output
    TIM_InitStruct.PWM_Deazone_Size     = 255;
    TIM_TimeBaseInit(PWM2, &TIM_InitStruct);

    TIM_Cmd(PWM2, ENABLE);
    TIM_PWMDZBypassCmd(PWM2, ENABLE);
}

备注

When using PWM functions, it is crucial to pay attention to the corresponding relationships: PWM2 corresponds to TIM2, and PWM3 corresponds to TIM3.

参数:
  • PWMx -- [in] Select the PWM peripheral. Refer to PWM Declaration.

  • NewState -- [in] NewState of the PWMx peripheral.

    • ENABLE: Enable bypass deadzone function.

    • DISABLE: Disable bypass deadzone function.

void TIM_PWMChangeDZClockSrc(PWM_TypeDef *PWMx, PWMDZClockSrc_TypeDef PWM_Deazone_ClockSrc)

Change the clock source for the PWM deadzone time.

Example usage

void driver_pwm_init(void)
{
    RCC_PeriphClockCmd(APBPeriph_TIMER, APBPeriph_TIMER_CLOCK, ENABLE);
    //Open 32k clock source.
    RCC_ClockSrc5MCmd();

    TIM_TimeBaseInitTypeDef TIM_InitStruct;

    TIM_StructInit(&TIM_InitStruct);
    TIM_InitStruct.TIM_Mode             = TIM_Mode_UserDefine;
    TIM_InitStruct.TIM_PWM_En           = ENABLE;
    TIM_InitStruct.TIM_PWM_High_Count   = PWM_HIGH_COUNT;
    TIM_InitStruct.TIM_PWM_Low_Count    = PWM_LOW_COUNT;
    TIM_InitStruct.PWM_Stop_State_P     = PWM_STOP_AT_HIGH;
    TIM_InitStruct.PWM_Stop_State_N     = PWM_STOP_AT_LOW;
    TIM_InitStruct.PWMDeadZone_En       = ENABLE;  //enable to use pwn p/n output
    TIM_InitStruct.PWM_Deazone_Size     = PWM_DEAD_ZONE_SIZE;
    TIM_TimeBaseInit(TIM2, &TIM_InitStruct);

    //Use 32k clock source.
    TIM_PWMChangeDZClockSrc(TIM2, PWM_CK_32K_TIMER);

    TIM_Cmd(TIM2, ENABLE);
}

备注

When using PWM functions, it is crucial to pay attention to the corresponding relationships: PWM2 corresponds to TIM2, and PWM3 corresponds to TIM3.

参数:
void TIM_ClkConfig(TIM_TypeDef *TIMx, TIMClockSrc_TypeDef ClockSrc, TIMClockDiv_TypeDef ClockDiv)

Configure the specified TIMx clock.

Example usage

void timer_demo(void)
{
    TIM_ClkConfig(TIM6, TIM_CLOCK_SRC_40M, TIM_CLOCK_DIVIDER_1)
}

参数:
bool TIM_ClkGet(TIM_TypeDef *TIMx, TIMClockSrc_TypeDef *ClockSrc, TIMClockDiv_TypeDef *ClockDiv)

Get the specified TIMx clock configuration.

Example usage

void timer_demo(void)
{
    TIMClockSrc_TypeDef clock_src;
    TIMClockDiv_TypeDef clock_div;
    TIM_ClkGet(TIM6, &clock_src, &clock_div);
}

参数:
返回:

The status of get clock.

  • true: TIM clock is set correctly.

  • false: The TIM clock is set incorrectly.