STM32底层LL库TIM应用LL篇7

Posted:   2020-08-21

Status:   Completed

Tags :   STM32 CUBE LL TIM

Categories :   STM32 CUBE LL TIM

Previous:   STM32底层LL库ADC使用DMA多通道采集LL篇6

Next:   STM32底层LL库高级定时器PWM输出LL篇8


配置TIM

  • 和HAL库配置一样
  • 在高级设置里为TIM6选择LL
  • 生成代码

TIM函数

TIM结构体分析

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/**
  * @brief  TIM Time Base configuration structure definition.
  */
typedef struct
{
  uint16_t Prescaler;         /*!< Specifies the prescaler value used to divide the TIM clock.
                                   This parameter can be a number between Min_Data=0x0000 and Max_Data=0xFFFF.

                                   This feature can be modified afterwards using unitary function @ref LL_TIM_SetPrescaler().*/

  uint32_t CounterMode;       /*!< Specifies the counter mode.
                                   This parameter can be a value of @ref TIM_LL_EC_COUNTERMODE.

                                   This feature can be modified afterwards using unitary function @ref LL_TIM_SetCounterMode().*/

  uint32_t Autoreload;        /*!< Specifies the auto reload value to be loaded into the active
                                   Auto-Reload Register at the next update event.
                                   This parameter must be a number between Min_Data=0x0000 and Max_Data=0xFFFF.
                                   Some timer instances may support 32 bits counters. In that case this parameter must be a number between 0x0000 and 0xFFFFFFFF.

                                   This feature can be modified afterwards using unitary function @ref LL_TIM_SetAutoReload().*/

  uint32_t ClockDivision;     /*!< Specifies the clock division.
                                   This parameter can be a value of @ref TIM_LL_EC_CLOCKDIVISION.

                                   This feature can be modified afterwards using unitary function @ref LL_TIM_SetClockDivision().*/

  uint8_t RepetitionCounter;  /*!< Specifies the repetition counter value. Each time the RCR downcounter
                                   reaches zero, an update event is generated and counting restarts
                                   from the RCR value (N).
                                   This means in PWM mode that (N+1) corresponds to:
                                      - the number of PWM periods in edge-aligned mode
                                      - the number of half PWM period in center-aligned mode
                                   This parameter must be a number between 0x00 and 0xFF.

                                   This feature can be modified afterwards using unitary function @ref LL_TIM_SetRepetitionCounter().*/
} LL_TIM_InitTypeDef;
  • 预分频因子Prescaler
  • 计数模式CounterMode
1
2
3
4
5
6
7
8
/** @defgroup TIM_LL_EC_COUNTERMODE Counter Mode
  * @{
  */
#define LL_TIM_COUNTERMODE_UP                  0x00000000U          /*!<Counter used as upcounter */
#define LL_TIM_COUNTERMODE_DOWN                TIM_CR1_DIR          /*!< Counter used as downcounter */
#define LL_TIM_COUNTERMODE_CENTER_UP           TIM_CR1_CMS_0        /*!< The counter counts up and down alternatively. Output compare interrupt flags of output channels  are set only when the counter is counting down. */
#define LL_TIM_COUNTERMODE_CENTER_DOWN         TIM_CR1_CMS_1        /*!<The counter counts up and down alternatively. Output compare interrupt flags of output channels  are set only when the counter is counting up */
#define LL_TIM_COUNTERMODE_CENTER_UP_DOWN      TIM_CR1_CMS          /*!< The counter counts up and down alternatively. Output compare interrupt flags of output channels  are set only when the counter is counting up or down. */
  • 定时器周期Autoreload
  • 时钟分频ClockDivision
1
2
3
4
5
6
/** @defgroup TIM_LL_EC_CLOCKDIVISION Clock Division
  * @{
  */
#define LL_TIM_CLOCKDIVISION_DIV1              0x00000000U          /*!< tDTS=tCK_INT */
#define LL_TIM_CLOCKDIVISION_DIV2              TIM_CR1_CKD_0        /*!< tDTS=2*tCK_INT */
#define LL_TIM_CLOCKDIVISION_DIV4              TIM_CR1_CKD_1        /*!< tDTS=4*tCK_INT */
  • 重复计数器RepetitionCounter

常用函数

  • 计算预分频值__LL_TIM_CALC_PSC(__TIMCLK__, __CNTCLK__)
  • 计算ARR值__LL_TIM_CALC_ARR(__TIMCLK__, __PSC__, __FREQ__)
  • 计算比较初值__LL_TIM_CALC_DELAY(__TIMCLK__, __PSC__, __DELAY__)
  • 使能计时器void LL_TIM_EnableCounter(TIM_TypeDef *TIMx)
  • 使能ARR缓冲void LL_TIM_EnableARRPreload(TIM_TypeDef *TIMx)
  • 设置计数器值void LL_TIM_SetCounter(TIM_TypeDef *TIMx, uint32_t Counter)
  • 读取计数器值uint32_t LL_TIM_GetCounter(TIM_TypeDef *TIMx)
  • 得到预分频值uint32_t LL_TIM_GetPrescaler(TIM_TypeDef *TIMx)
  • 设置ARR值void LL_TIM_SetAutoReload(TIM_TypeDef *TIMx, uint32_t AutoReload)
  • 使能通道void LL_TIM_CC_EnableChannel(TIM_TypeDef *TIMx, uint32_t Channels)
  • 使能CCR缓冲void LL_TIM_OC_EnablePreload(TIM_TypeDef *TIMx, uint32_t Channel)
  • 设置比较CCR1void LL_TIM_OC_SetCompareCH1(TIM_TypeDef *TIMx, uint32_t CompareValue)
  • 设置比较CCR2void LL_TIM_OC_SetCompareCH2(TIM_TypeDef *TIMx, uint32_t CompareValue)
  • 设置比较CCR3void LL_TIM_OC_SetCompareCH3(TIM_TypeDef *TIMx, uint32_t CompareValue)
  • 设置比较CCR4void LL_TIM_OC_SetCompareCH4(TIM_TypeDef *TIMx, uint32_t CompareValue)
  • 设置捕获极性void LL_TIM_IC_SetPolarity(TIM_TypeDef *TIMx, uint32_t Channel, uint32_t ICPolarity)
  • 获取捕获CCR1uint32_t LL_TIM_IC_GetCaptureCH1(TIM_TypeDef *TIMx)
  • 获取捕获CCR2uint32_t LL_TIM_IC_GetCaptureCH2(TIM_TypeDef *TIMx)
  • 获取捕获CCR3uint32_t LL_TIM_IC_GetCaptureCH3(TIM_TypeDef *TIMx)
  • 获取捕获CCR4uint32_t LL_TIM_IC_GetCaptureCH4(TIM_TypeDef *TIMx)
  • 主输出使能void LL_TIM_EnableAllOutputs(TIM_TypeDef *TIMx)
  • 清除更新中断标志位void LL_TIM_ClearFlag_UPDATE(TIM_TypeDef *TIMx)
  • 读取更新中断标志位uint32_t LL_TIM_IsActiveFlag_UPDATE(TIM_TypeDef *TIMx)
  • 清除通道1中断标志位void LL_TIM_ClearFlag_CC1(TIM_TypeDef *TIMx)
  • 读取通道1中断标志位uint32_t LL_TIM_IsActiveFlag_CC1(TIM_TypeDef *TIMx)
  • 清除通道2中断标志位void LL_TIM_ClearFlag_CC2(TIM_TypeDef *TIMx)
  • 读取通道2中断标志位uint32_t LL_TIM_IsActiveFlag_CC2(TIM_TypeDef *TIMx)
  • 清除通道3中断标志位void LL_TIM_ClearFlag_CC3(TIM_TypeDef *TIMx)
  • 读取通道3中断标志位uint32_t LL_TIM_IsActiveFlag_CC3(TIM_TypeDef *TIMx)
  • 清除通道4中断标志位void LL_TIM_ClearFlag_CC4(TIM_TypeDef *TIMx)
  • 读取通道4中断标志位uint32_t LL_TIM_IsActiveFlag_CC4(TIM_TypeDef *TIMx)
  • 使能更新中断void LL_TIM_EnableIT_UPDATE(TIM_TypeDef *TIMx)
  • 使能通道1中断void LL_TIM_EnableIT_CC1(TIM_TypeDef *TIMx)
  • 使能通道2中断void LL_TIM_EnableIT_CC2(TIM_TypeDef *TIMx)
  • 使能通道3中断void LL_TIM_EnableIT_CC3(TIM_TypeDef *TIMx)
  • 使能通道4中断void LL_TIM_EnableIT_CC4(TIM_TypeDef *TIMx)

代码移植

修改定时器中断

  • stm32f10x_it.c文件添加外部变量
1
extern __IO uint32_t time;
  • 修改中断
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/**
  * @brief This function handles TIM6 global interrupt.
  */
void TIM6_IRQHandler(void)
{
    /* USER CODE BEGIN TIM6_IRQn 0 */
    if(LL_TIM_IsActiveFlag_UPDATE(TIM6))
    {
        LL_TIM_ClearFlag_UPDATE(TIM6);
        time++;
    }
    /* USER CODE END TIM6_IRQn 0 */
    /* USER CODE BEGIN TIM6_IRQn 1 */

    /* USER CODE END TIM6_IRQn 1 */
}

时间应用

  • main.c文件添加变量
1
__IO uint32_t time;
  • 在主函数初始化添加定时器使能和中断使能
1
2
    LL_TIM_EnableCounter(TIM6);//使能定时器
    LL_TIM_EnableIT_UPDATE(TIM6);//使能更新中断
  • 主循环添加时间处理
1
2
3
4
5
6
7
8
9
        if(time>=1000)
        {
            time=0;
            LED1_TOGGLE;
            LED2_TOGGLE;
            printf( "\r\n The IC current tem= %.2fC\r\n", temp);
            printf( "\r\n The IC current VDDA= %.2fV\r\n", VREF);
            printf( "\r\n The IC current ADC= %.2fV\r\n", adcValue);
        }

下载调试

  • 编译之后下载到开发板
  • 连接开发板串口
  • 打开串口助手
  • 可以看到串口每1秒接收到数据,开发板上LED在1s间隔闪烁
  • 和HAL库结果一致