[MCU framework][BSP layer][cx32l003][bsp_tim] TIM timer configuration and use

Publisher:xinyi9008Latest update time:2022-09-21 Source: csdn Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Introduction to TIM1

The Advanced Control Timer (TIM1) consists of a 16-bit auto-reload counter driven by a programmable prescaler. It is suitable for a variety of purposes, including measuring the pulse width of an input signal (input capture), or generating output waveforms (output compare, PWM, complementary PWM with embedded dead time, etc.).


Using the timer prescaler and RCC clock control prescaler, the pulse width and waveform period can be adjusted from a few microseconds to a few milliseconds.


TIM1 Key Features

The functions of TIM1 timer include:

⚫ 16-bit up, down, up/down auto-load counter

⚫ 16-bit programmable (can be modified in real time) prescaler, the counter clock frequency division factor is any value between 1 and 65535

Up to 4 independent channels:


Input Capture

Output Compare

PWM generation (edge ​​or center aligned mode)

Single pulse mode output

⚫ Complementary outputs with programmable dead time

⚫ Use external signals to control timers and synchronize timer interconnection circuits

⚫ A repeating counter that allows updating of the timer register after a specified number of counter cycles

⚫ The brake input signal can put the timer output signal into a reset state or a known state

⚫ An interrupt is generated when the following events occur:


Update: counter overflow/underflow, counter initialization (by software or internal/external trigger)

Trigger event (counter starts, stops, initializes, or counts by internal/external trigger)

Input Capture

Output Compare

Brake signal input

⚫ Supports incremental (quadrature) encoder and Hall sensor circuits for positioning

⚫ Trigger input as external clock or cycle-by-cycle current management

/********************************************************************************

* @file bsp_tim.c

* @author jianqiang.xue

* @Version V1.0.0

* @Date 2021-04-19

* @brief   

************************************************************************************/

/* Private includes ----------------------------------------------------------*/

#include

#include


#include "cx32l003_hal.h"


#include "bsp_gpio.h"

#include "bsp_tim.h"


#include "business_gpio.h"

#include "business_function.h"


/* Private define ------------------------------------------------------------*/

#define TIM1_EN BS_TIM1_EN

#define TIM1_PRESCALER BS_TIM1_PRESCALER

#define TIM1_PERIOD BS_TIM1_PERIOD


#define TIM1_CH1_PIN BS_TIM1_CH1_PIN

#define TIM1_CH1_GPIO_PORT BS_TIM1_CH1_GPIO_PORT

#define TIM1_CH1_GPIO_CLK_ENABLE() BS_TIM1_CH1_GPIO_CLK_ENABLE()

#define TIM1_CH1_GPIO_CLK_DISABLE() BS_TIM1_CH1_GPIO_CLK_DISABLE()


#define TIM1_CH2_PIN BS_TIM1_CH2_PIN

#define TIM1_CH2_GPIO_PORT BS_TIM1_CH2_GPIO_PORT

#define TIM1_CH2_GPIO_CLK_ENABLE() BS_TIM1_CH2_GPIO_CLK_ENABLE()

#define TIM1_CH2_GPIO_CLK_DISABLE() BS_TIM1_CH2_GPIO_CLK_DISABLE()


#define TIM1_CH3_PIN BS_TIM1_CH3_PIN

#define TIM1_CH3_GPIO_PORT BS_TIM1_CH3_GPIO_PORT

#define TIM1_CH3_GPIO_CLK_ENABLE() BS_TIM1_CH3_GPIO_CLK_ENABLE()

#define TIM1_CH3_GPIO_CLK_DISABLE() BS_TIM1_CH3_GPIO_CLK_DISABLE()


#define TIM1_CH4_PIN BS_TIM1_CH4_PIN

#define TIM1_CH4_GPIO_PORT BS_TIM1_CH4_GPIO_PORT

#define TIM1_CH4_GPIO_CLK_ENABLE() BS_TIM1_CH4_GPIO_CLK_ENABLE()

#define TIM1_CH4_GPIO_CLK_DISABLE() BS_TIM1_CH4_GPIO_CLK_DISABLE()


#define PWM0_EN BS_PWM0_EN

#define PWM1_EN BS_PWM1_EN

#define PWM2_EN BS_PWM2_EN

#define PWM3_EN BS_PWM3_EN


#if TIM1_EN

TIM_HandleTypeDef tim1_handle_t;

TIM_OC_InitTypeDef tim1_oc_init_handle_t;

#endif


/* Private typedef -----------------------------------------------------------*/

void bsp_tim_pwm_init(void)

{

#if TIM1_EN

memset(&tim1_handle_t, 0, sizeof(TIM_HandleTypeDef));

memset(&tim1_oc_init_handle_t, 0, sizeof(TIM_OC_InitTypeDef));

    __HAL_RCC_TIM1_CLK_ENABLE();

    /* Set TIMx instance */

    tim1_handle_t.Instance = TIM1;

    tim1_handle_t.Init.Period = TIM1_PERIOD; // Reload value

    tim1_handle_t.Init.Prescaler = TIM1_PRESCALER; // Prescaler

    tim1_handle_t.Init.ClockDivision = 0; // CKD clock division factor (Clock division)

    tim1_handle_t.Init.CounterMode = TIM_COUNTERMODE_UP; // Edge-aligned mode counter counts up

    tim1_handle_t.Init.RepetitionCounter = 0; // TIM1_RCR repetition counter value

    tim1_handle_t.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; // Disable automatic reloading

    HAL_TIM_PWM_Init(&tim1_handle_t);


#if PWM0_EN

    bsp_gpio_init_tim(TIM1_CH1_GPIO_PORT, TIM1_CH1_PIN, GPIO_AF1_TIM1_CH1);

    tim1_oc_init_handle_t.OCMode = TIM_OCMODE_PWM1; // PWM mode 1 TIM1_CCMR1

    tim1_oc_init_handle_t.Pulse = TIM1_PERIOD; // CCR capture/compare channel

    HAL_TIM_PWM_ConfigChannel(&tim1_handle_t, &tim1_oc_init_handle_t, TIM_CHANNEL_1);

    HAL_TIM_PWM_Start(&tim1_handle_t, TIM_CHANNEL_1);

#endif


#if PWM1_EN

    bsp_gpio_init_tim(TIM1_CH2_GPIO_PORT, TIM1_CH2_PIN, GPIO_AF1_TIM1_CH2);

    tim1_oc_init_handle_t.OCMode = TIM_OCMODE_PWM1; // PWM mode 1 TIM1_CCMR1

    tim1_oc_init_handle_t.Pulse = TIM1_PERIOD; // CCR capture/compare channel

    HAL_TIM_PWM_ConfigChannel(&tim1_handle_t, &tim1_oc_init_handle_t, TIM_CHANNEL_2);

    HAL_TIM_PWM_Start(&tim1_handle_t, TIM_CHANNEL_2);

#endif


#if PWM2_EN

    bsp_gpio_init_tim(TIM1_CH3_GPIO_PORT, TIM1_CH3_PIN, GPIO_AF1_TIM1_CH3);

    tim1_oc_init_handle_t.OCMode = TIM_OCMODE_PWM1; // PWM mode 1 TIM1_CCMR1

    tim1_oc_init_handle_t.Pulse = TIM1_PERIOD; // CCR capture/compare channel

    HAL_TIM_PWM_ConfigChannel(&tim1_handle_t, &tim1_oc_init_handle_t, TIM_CHANNEL_3);

    HAL_TIM_PWM_Start(&tim1_handle_t, TIM_CHANNEL_3);

#endif


#if PWM3_EN

    bsp_gpio_init_tim(TIM1_CH4_GPIO_PORT, TIM1_CH4_PIN, GPIO_AF1_TIM1_CH4);

    tim1_oc_init_handle_t.OCMode = TIM_OCMODE_PWM1; // PWM mode 1 TIM1_CCMR1

    tim1_oc_init_handle_t.Pulse = TIM1_PERIOD; // CCR capture/compare channel

    HAL_TIM_PWM_ConfigChannel(&tim1_handle_t, &tim1_oc_init_handle_t, TIM_CHANNEL_4);

    HAL_TIM_PWM_Start(&tim1_handle_t, TIM_CHANNEL_4);

#endif


#endif

}


void bsp_tim_pwm_deinit(void)

{

#if TIM1_EN

#if PWM0_EN

HAL_TIM_PWM_Stop(&tim1_handle_t, TIM_CHANNEL_1);

bsp_gpio_deinit(TIM1_CH1_GPIO_PORT, TIM1_CH1_PIN);

#endif

#if PWM1_EN

HAL_TIM_PWM_Stop(&tim1_handle_t, TIM_CHANNEL_2);

bsp_gpio_deinit(TIM1_CH2_GPIO_PORT, TIM1_CH2_PIN);

#endif

#if PWM2_EN

HAL_TIM_PWM_Stop(&tim1_handle_t, TIM_CHANNEL_3);

bsp_gpio_deinit(TIM1_CH3_GPIO_PORT, TIM1_CH3_PIN);

#endif

#if PWM3_EN

HAL_TIM_PWM_Stop(&tim1_handle_t, TIM_CHANNEL_4);

bsp_gpio_deinit(TIM1_CH4_GPIO_PORT, TIM1_CH4_PIN);

#endif

HAL_TIM_PWM_DeInit(&tim1_handle_t);

__HAL_RCC_TIM1_CLK_DISABLE();

#endif

}


/********************************************************************************

* @file bsp_tim.h

* @author jianqiang.xue

* @Version V1.0.0

* @Date 2021-04-19

* @brief NULL

************************************************************************************/


#ifndef __BSP_TIM_H

#define __BSP_TIM_H


#include


typedef enum

{

    BSP_TIM_0 = 0,

    BSP_TIM_1

} bsp_tim_t;


void bsp_tim_pwm_init(void);

void bsp_tim_pwm_deinit(void);

#endif

Reference address:[MCU framework][BSP layer][cx32l003][bsp_tim] TIM timer configuration and use

Previous article:[MCU framework][bsp layer][cx32l003][bsp_uart] URAT serial port configuration and use
Next article:[MCU framework][bsp layer][cx32l003][bsp_i2c] I2C/IIC hardware configuration and use

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号