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
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
- Naxin Micro and Xinxian jointly launched the NS800RT series of real-time control MCUs
- How to learn embedded systems based on ARM platform
- Summary of jffs2_scan_eraseblock issues
- Application of SPCOMM Control in Serial Communication of Delphi7.0
- Using TComm component to realize serial communication in Delphi environment
- Bar chart code for embedded development practices
- Embedded Development Learning (10)
- Embedded Development Learning (8)
- Embedded Development Learning (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Intel promotes AI with multi-dimensional efforts in technology, application, and ecology
- ChinaJoy Qualcomm Snapdragon Theme Pavilion takes you to experience the new changes in digital entertainment in the 5G era
- Infineon's latest generation IGBT technology platform enables precise control of speed and position
- Two test methods for LED lighting life
- Don't Let Lightning Induced Surges Scare You
- Application of brushless motor controller ML4425/4426
- Easy identification of LED power supply quality
- World's first integrated photovoltaic solar system completed in Israel
- Sliding window mean filter for avr microcontroller AD conversion
- What does call mean in the detailed explanation of ABB robot programming instructions?
- STMicroelectronics discloses its 2027-2028 financial model and path to achieve its 2030 goals
- 2024 China Automotive Charging and Battery Swapping Ecosystem Conference held in Taiyuan
- State-owned enterprises team up to invest in solid-state battery giant
- The evolution of electronic and electrical architecture is accelerating
- The first! National Automotive Chip Quality Inspection Center established
- BYD releases self-developed automotive chip using 4nm process, with a running score of up to 1.15 million
- GEODNET launches GEO-PULSE, a car GPS navigation device
- Should Chinese car companies develop their own high-computing chips?
- Infineon and Siemens combine embedded automotive software platform with microcontrollers to provide the necessary functions for next-generation SDVs
- Continental launches invisible biometric sensor display to monitor passengers' vital signs
- STM32H743 development board transplants micropython and expands 32M SQPI flash and 32M SDRAM
- How big is our misunderstanding of green oil? Let’s analyze it with real cases!
- Application and design overview of power line communication analog front end AFE031
- How much do you know about impedance matching?
- DLP Dynamic Floor Projection Technology for Automotive Exterior Lighting
- 16 PCB welding defects! What are their hazards?
- Remote Lighting Control Box System Based on SST89C58 Single Chip Microcomputer
- I saw a post next door and it seems okay since I work there every day.
- Freescale introduces 3G mobile phone RF subsystem, reducing board space by 70%
- How much do you know about power supply? (2)