目录
前言
因本人备赛蓝桥杯嵌入式省赛,故编写此学习笔记进行学习上的记录。
上文我们实现了ADC程序设计,本文我们进行TIM程序设计。本节的内容较多,由基本定时器、PWM输入捕获、PWM输出、方波输出。
1. 定时器介绍
STM32G431共有十个定时器
2个基本定时器(TIM6和TIM7)。
3个通用定时器(TIM2~TIM4):全功能通用定时器。
3个通用定时器(TIM15~TIM17):只有1个或者2个通道。
2个高级控制定时器(TIM1和TIM8)。
1.1 不同定时器的功能区别
- 基本定时器有的功能通用定时器都有
- 通用定时器有的功能高级控制定时器都有
1.2 功能详述
- ADC和DAC开始转换触发
- 输入捕获:脉冲计数、上升沿或下降沿时间检测、PWM输入检测
- 输出比较:脉冲输出、步进电机控制
- 脉冲宽度调节PWM:电压输出控制、直流减速电机控制、直流无刷电机控制
- 单脉冲模式输出
- 编码器接口、霍尔传感器接口
2. 使用CubeMx进行源工程配置
2.1 基本定时器配置
在【Pinout&Configuration】中【Timers】选择【Tim6】
设置【Mode】为Activated,将【Parameter Settings】中的【Prescaler】设置为7999,【Counter Period】设置为999。
将中断使能打开
将中断优先级设置为3
2.2 PWM输入捕获配置
在【Pinout view】中设置PA15为TIM2_CH1。
【Pinout&Configuration】
-【Timers】
-【Tim2】
-【Mode】
-设置【Slave Mode】为Reset Mode
-设置【Trigger Source】为TI1FP1
-设置【Clock Sorce】为Internel Clock
-设置【Channel1】为Input Capture direct mode
-设置【Channel2】为Input Capture indirect mode
-【Configuration】
-【Parameter Settings】
-设置【Prescaler】为79
-设置【Counter Period】为65535
-Input Capture Channel 1
-设置Polarity Selection为Rising Edge
-Input Capture Channel 2
-设置Polarity Selection为Falling Edge
设置中断使能
设置中断优先级为3
2.3 PWM输出配置
在【Pinout view】中设置PA6为TIM3_CH1,设置PA7为TIM17_CH1。
【Pinout&Configuration】
-【Timers】
-【Tim3】
-【Mode】
-设置【Clock Sorce】为Internel Clock
-设置【Channel1】为PWM Generation CH1
-【Configuration】
-【Parameter Settings】
-【Counter Settings】
-设置【Prescaler】为79
-设置【Counter Period】为999
-【PWM Generation Channel 1】
-设置【Pulse】为300
-设置【CH Polarity】为High
其中TIM3和TIM17均如此设置,其中TIM17为通用定时器,
【Mode】
-设置【Clock Sorce】为Internel Clock
-设置【Channel1】为PWM Generation CH1
其余均一致
2.4 方波输出配置
在【Pinout view】中设置PA2为TIM15_CH1。
【Pinout&Configuration】
-【Timers】
-【Tim15】
-【Mode】
-勾选Internel Clock
-设置【Channel1】为Output Compare CH1
-【Configuration】
-【Parameter Settings】
-【Counter Settings】
-设置【Prescaler】为79
-设置【anto-reload preload】为Enable
-【Output Compare Channel】
-设置【Mode】为Toggle on match
-设置【Pulse】为100
设置中断使能
设置中断优先级为3
至此,我们完成了CubeMx配置源工程。我们总结一下如下表所示。
定时器功能 | 引脚 | 定时器编号 | 是否需要中断 |
基本定时器 | |||
PWM输入捕获 | PA15 | TIM2 | 是 |
PWM输出 | PA6 | TIM3 | 否 |
PWM输出 | PA7 | TIM17 | 否 |
方波输出 | PA2 | TIM15 | 是 |
3. 代码编程
3.1 准备工作
接下来我们在Test_Project工程里的Src文件夹创建BSP\TIM\bsp_tim.c,同理,在Inc文件夹创建BSP\TIM\bsp_tim.h。这就是我们后面要编写的中间层代码文件。
打开Test_Project工程,进行文件Group的添加
在bsp_tim.c中添加依赖文件
#include "TIM/bsp_tim.h"
检查是否已添加stm32g4xx_hal_tim.c和stm32g4xx_hal_tim_ex.c驱动文件
在stm32g4xx_hal_conf.h中去掉#define HAL_ADC_MODULE_ENABLED 的注释
#define HAL_TIM_MODULE_ENABLED
3.2 进行bsp_tim.h编写
通过Source工程生成的tim模块进行剪裁修改,h文件如下
/* Includes ------------------------------------------------------------------*/
#include "main.h"
extern TIM_HandleTypeDef htim2;
extern TIM_HandleTypeDef htim3;
extern TIM_HandleTypeDef htim6;
extern TIM_HandleTypeDef htim15;
extern TIM_HandleTypeDef htim17;
void PWMIN_TIM2_Init(void);
void PWMOUT_TIM3_Init(void);
void BASE_TIM6_Init(void);
void SQU_TIM15_Init(void);
void PWMOUT_TIM17_Init(void);
void HAL_TIM_MspPostInit(TIM_HandleTypeDef *htim);
3.3 进行bsp_tim.c编写
通过Source工程生成的tim模块进行剪裁修改,c文件如下
/* Includes ------------------------------------------------------------------*/
#include "TIM/bsp_tim.h"
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
TIM_HandleTypeDef htim2;
TIM_HandleTypeDef htim3;
TIM_HandleTypeDef htim6;
TIM_HandleTypeDef htim15;
TIM_HandleTypeDef htim17;
/* TIM2 init function */
void PWMIN_TIM2_Init(void)
{
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
TIM_SlaveConfigTypeDef sSlaveConfig = {0};
TIM_MasterConfigTypeDef sMasterConfig = {0};
TIM_IC_InitTypeDef sConfigIC = {0};
htim2.Instance = TIM2;
htim2.Init.Prescaler = 79;
htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
htim2.Init.Period = 65535;
htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
if (HAL_TIM_Base_Init(&htim2) != HAL_OK)
{
Error_Handler();
}
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK)
{
Error_Handler();
}
if (HAL_TIM_IC_Init(&htim2) != HAL_OK)
{
Error_Handler();
}
sSlaveConfig.SlaveMode = TIM_SLAVEMODE_RESET;
sSlaveConfig.InputTrigger = TIM_TS_TI1FP1;
sSlaveConfig.TriggerPolarity = TIM_INPUTCHANNELPOLARITY_RISING;
sSlaveConfig.TriggerFilter = 0;
if (HAL_TIM_SlaveConfigSynchro(&htim2, &sSlaveConfig) != HAL_OK)
{
Error_Handler();
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK)
{
Error_Handler();
}
sConfigIC.ICPolarity = TIM_INPUTCHANNELPOLARITY_RISING;
sConfigIC.ICSelection = TIM_ICSELECTION_DIRECTTI;
sConfigIC.ICPrescaler = TIM_ICPSC_DIV1;
sConfigIC.ICFilter = 0;
if (HAL_TIM_IC_ConfigChannel(&htim2, &sConfigIC, TIM_CHANNEL_1) != HAL_OK)
{
Error_Handler();
}
sConfigIC.ICPolarity = TIM_INPUTCHANNELPOLARITY_FALLING;
sConfigIC.ICSelection = TIM_ICSELECTION_INDIRECTTI;
if (HAL_TIM_IC_ConfigChannel(&htim2, &sConfigIC, TIM_CHANNEL_2) != HAL_OK)
{
Error_Handler();
}
}
void PWMOUT_TIM3_Init(void)
{
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
TIM_MasterConfigTypeDef sMasterConfig = {0};
TIM_OC_InitTypeDef sConfigOC = {0};
htim3.Instance = TIM3;
htim3.Init.Prescaler = 79;
htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
htim3.Init.Period = 999;
htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
if (HAL_TIM_Base_Init(&htim3) != HAL_OK)
{
Error_Handler();
}
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
if (HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig) != HAL_OK)
{
Error_Handler();
}
if (HAL_TIM_PWM_Init(&htim3) != HAL_OK)
{
Error_Handler();
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK)
{
Error_Handler();
}
sConfigOC.OCMode = TIM_OCMODE_PWM1;
sConfigOC.Pulse = 300;
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
if (HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
{
Error_Handler();
}
HAL_TIM_MspPostInit(&htim3);
}
/* TIM6 init function */
void BASE_TIM6_Init(void)
{
TIM_MasterConfigTypeDef sMasterConfig = {0};
htim6.Instance = TIM6;
htim6.Init.Prescaler = 7999;
htim6.Init.CounterMode = TIM_COUNTERMODE_UP;
htim6.Init.Period = 999;
htim6.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
if (HAL_TIM_Base_Init(&htim6) != HAL_OK)
{
Error_Handler();
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim6, &sMasterConfig) != HAL_OK)
{
Error_Handler();
}
}
/* TIM15 init function */
void SQU_TIM15_Init(void)
{
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
TIM_MasterConfigTypeDef sMasterConfig = {0};
TIM_OC_InitTypeDef sConfigOC = {0};
TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig = {0};
htim15.Instance = TIM15;
htim15.Init.Prescaler = 79;
htim15.Init.CounterMode = TIM_COUNTERMODE_UP;
htim15.Init.Period = 65535;
htim15.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim15.Init.RepetitionCounter = 0;
htim15.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
if (HAL_TIM_Base_Init(&htim15) != HAL_OK)
{
Error_Handler();
}
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
if (HAL_TIM_ConfigClockSource(&htim15, &sClockSourceConfig) != HAL_OK)
{
Error_Handler();
}
if (HAL_TIM_OC_Init(&htim15) != HAL_OK)
{
Error_Handler();
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim15, &sMasterConfig) != HAL_OK)
{
Error_Handler();
}
sConfigOC.OCMode = TIM_OCMODE_TOGGLE;
sConfigOC.Pulse = 100;
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCNPolarity = TIM_OCNPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET;
sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_RESET;
if (HAL_TIM_OC_ConfigChannel(&htim15, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
{
Error_Handler();
}
sBreakDeadTimeConfig.OffStateRunMode = TIM_OSSR_DISABLE;
sBreakDeadTimeConfig.OffStateIDLEMode = TIM_OSSI_DISABLE;
sBreakDeadTimeConfig.LockLevel = TIM_LOCKLEVEL_OFF;
sBreakDeadTimeConfig.DeadTime = 0;
sBreakDeadTimeConfig.BreakState = TIM_BREAK_DISABLE;
sBreakDeadTimeConfig.BreakPolarity = TIM_BREAKPOLARITY_HIGH;
sBreakDeadTimeConfig.BreakFilter = 0;
sBreakDeadTimeConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE;
if (HAL_TIMEx_ConfigBreakDeadTime(&htim15, &sBreakDeadTimeConfig) != HAL_OK)
{
Error_Handler();
}
HAL_TIM_MspPostInit(&htim15);
}
/* TIM17 init function */
void PWMOUT_TIM17_Init(void)
{
TIM_OC_InitTypeDef sConfigOC = {0};
TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig = {0};
htim17.Instance = TIM17;
htim17.Init.Prescaler = 79;
htim17.Init.CounterMode = TIM_COUNTERMODE_UP;
htim17.Init.Period = 999;
htim17.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim17.Init.RepetitionCounter = 0;
htim17.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
if (HAL_TIM_Base_Init(&htim17) != HAL_OK)
{
Error_Handler();
}
if (HAL_TIM_PWM_Init(&htim17) != HAL_OK)
{
Error_Handler();
}
sConfigOC.OCMode = TIM_OCMODE_PWM1;
sConfigOC.Pulse = 300;
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCNPolarity = TIM_OCNPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET;
sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_RESET;
if (HAL_TIM_PWM_ConfigChannel(&htim17, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
{
Error_Handler();
}
sBreakDeadTimeConfig.OffStateRunMode = TIM_OSSR_DISABLE;
sBreakDeadTimeConfig.OffStateIDLEMode = TIM_OSSI_DISABLE;
sBreakDeadTimeConfig.LockLevel = TIM_LOCKLEVEL_OFF;
sBreakDeadTimeConfig.DeadTime = 0;
sBreakDeadTimeConfig.BreakState = TIM_BREAK_DISABLE;
sBreakDeadTimeConfig.BreakPolarity = TIM_BREAKPOLARITY_HIGH;
sBreakDeadTimeConfig.BreakFilter = 0;
sBreakDeadTimeConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE;
if (HAL_TIMEx_ConfigBreakDeadTime(&htim17, &sBreakDeadTimeConfig) != HAL_OK)
{
Error_Handler();
}
HAL_TIM_MspPostInit(&htim17);
}
void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* tim_baseHandle)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
if(tim_baseHandle->Instance==TIM2)
{
/* TIM2 clock enable */
__HAL_RCC_TIM2_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
/**TIM2 GPIO Configuration
PA15 ------> TIM2_CH1
*/
GPIO_InitStruct.Pin = GPIO_PIN_15;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Alternate = GPIO_AF1_TIM2;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/* TIM2 interrupt Init */
HAL_NVIC_SetPriority(TIM2_IRQn, 3, 0);
HAL_NVIC_EnableIRQ(TIM2_IRQn);
}
else if(tim_baseHandle->Instance==TIM3)
{
/* TIM3 clock enable */
__HAL_RCC_TIM3_CLK_ENABLE();
}
else if(tim_baseHandle->Instance==TIM6)
{
/* TIM6 clock enable */
__HAL_RCC_TIM6_CLK_ENABLE();
/* TIM6 interrupt Init */
HAL_NVIC_SetPriority(TIM6_DAC_IRQn, 3, 0);
HAL_NVIC_EnableIRQ(TIM6_DAC_IRQn);
}
else if(tim_baseHandle->Instance==TIM15)
{
/* TIM15 clock enable */
__HAL_RCC_TIM15_CLK_ENABLE();
/* TIM15 interrupt Init */
HAL_NVIC_SetPriority(TIM1_BRK_TIM15_IRQn, 3, 0);
HAL_NVIC_EnableIRQ(TIM1_BRK_TIM15_IRQn);
}
else if(tim_baseHandle->Instance==TIM17)
{
/* TIM17 clock enable */
__HAL_RCC_TIM17_CLK_ENABLE();
}
}
void HAL_TIM_MspPostInit(TIM_HandleTypeDef* timHandle)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
if(timHandle->Instance==TIM3)
{
__HAL_RCC_GPIOA_CLK_ENABLE();
/**TIM3 GPIO Configuration
PA6 ------> TIM3_CH1
*/
GPIO_InitStruct.Pin = GPIO_PIN_6;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Alternate = GPIO_AF2_TIM3;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}
else if(timHandle->Instance==TIM15)
{
__HAL_RCC_GPIOA_CLK_ENABLE();
/**TIM15 GPIO Configuration
PA2 ------> TIM15_CH1
*/
GPIO_InitStruct.Pin = GPIO_PIN_2;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Alternate = GPIO_AF9_TIM15;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}
else if(timHandle->Instance==TIM17)
{
__HAL_RCC_GPIOA_CLK_ENABLE();
/**TIM17 GPIO Configuration
PA7 ------> TIM17_CH1
*/
GPIO_InitStruct.Pin = GPIO_PIN_7;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Alternate = GPIO_AF1_TIM17;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}
}
void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef* tim_baseHandle)
{
if(tim_baseHandle->Instance==TIM2)
{
/* Peripheral clock disable */
__HAL_RCC_TIM2_CLK_DISABLE();
/**TIM2 GPIO Configuration
PA15 ------> TIM2_CH1
*/
HAL_GPIO_DeInit(GPIOA, GPIO_PIN_15);
/* TIM2 interrupt Deinit */
HAL_NVIC_DisableIRQ(TIM2_IRQn);
}
else if(tim_baseHandle->Instance==TIM3)
{
/* Peripheral clock disable */
__HAL_RCC_TIM3_CLK_DISABLE();
}
else if(tim_baseHandle->Instance==TIM6)
{
/* Peripheral clock disable */
__HAL_RCC_TIM6_CLK_DISABLE();
/* TIM6 interrupt Deinit */
HAL_NVIC_DisableIRQ(TIM6_DAC_IRQn);
}
else if(tim_baseHandle->Instance==TIM15)
{
/* Peripheral clock disable */
__HAL_RCC_TIM15_CLK_DISABLE();
/* TIM15 interrupt Deinit */
HAL_NVIC_DisableIRQ(TIM1_BRK_TIM15_IRQn);
}
else if(tim_baseHandle->Instance==TIM17)
{
/* Peripheral clock disable */
__HAL_RCC_TIM17_CLK_DISABLE();
}
}
3.5 中断文件编写
添加头文件,这里主要是为了把bsp_tim.h里的全局结构体变量添加进去
#include "TIM\bsp_tim.h"
添加中断函数
void TIM1_BRK_TIM15_IRQHandler(void)
{
/* USER CODE BEGIN TIM1_BRK_TIM15_IRQn 0 */
/* USER CODE END TIM1_BRK_TIM15_IRQn 0 */
HAL_TIM_IRQHandler(&htim15);
/* USER CODE BEGIN TIM1_BRK_TIM15_IRQn 1 */
/* USER CODE END TIM1_BRK_TIM15_IRQn 1 */
}
/**
* @brief This function handles TIM2 global interrupt.
*/
void TIM2_IRQHandler(void)
{
/* USER CODE BEGIN TIM2_IRQn 0 */
/* USER CODE END TIM2_IRQn 0 */
HAL_TIM_IRQHandler(&htim2);
/* USER CODE BEGIN TIM2_IRQn 1 */
/* USER CODE END TIM2_IRQn 1 */
}
/**
* @brief This function handles TIM6 global interrupt, DAC1 and DAC3 channel underrun error interrupts.
*/
void TIM6_DAC_IRQHandler(void)
{
/* USER CODE BEGIN TIM6_DAC_IRQn 0 */
/* USER CODE END TIM6_DAC_IRQn 0 */
HAL_TIM_IRQHandler(&htim6);
/* USER CODE BEGIN TIM6_DAC_IRQn 1 */
/* USER CODE END TIM6_DAC_IRQn 1 */
}
3.4 main.c编写
3.4.1 头文件引用
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "LCD\bsp_lcd.h"
#include "TIM\bsp_tim.h"
3.4.2 变量声明
//变量声明
//*LCD显示专用变量
unsigned char Lcd_Disp_String[22];
//时钟变量
int time = 0;
int PWM_T_Count = 0;
int PWM_D_Count = 0;
float PWM_Duty = 0;
3.4.3 子函数声明
//***子函数声明区
void SystemClock_Config(void);
void Lcd_Proc(void);
3.4.4 函数定义
3.4.4.1 LCD函数定义
void Lcd_Proc(void)
{
if((uwTick - uwTick_Lcd_Set_Point)<200)
return;
uwTick_Lcd_Set_Point = uwTick;
sprintf((char*)Lcd_Disp_String,"BASE_Time:%dS",time/10);
LCD_DisplayStringLine(Line4,Lcd_Disp_String);
sprintf((char*)Lcd_Disp_String,"PWM:%dHz ,%f%%\n",1000000/PWM_T_Count,PWM_Duty);
LCD_DisplayStringLine(Line5,Lcd_Disp_String);
}
3.4.4.2 系统时钟函数定义
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
/** Configure the main internal regulator output voltage
*/
HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1);
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = RCC_PLLM_DIV3;
RCC_OscInitStruct.PLL.PLLN = 20;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
{
Error_Handler();
}
/** Initializes the peripherals clocks
*/
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART1|RCC_PERIPHCLK_ADC12;
PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK2;
PeriphClkInit.Adc12ClockSelection = RCC_ADC12CLKSOURCE_PLL;
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
{
Error_Handler();
}
}
3.4.4.3 中断函数定义
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
if(htim->Instance == TIM6)
{
time++;
}
}
void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
{
if(htim->Instance == TIM2)
{
if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1)
{
PWM_T_Count = HAL_TIM_ReadCapturedValue(htim,TIM_CHANNEL_1)+1;
PWM_Duty = (float)PWM_D_Count/PWM_T_Count;
}
else if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_2)
{
PWM_D_Count = HAL_TIM_ReadCapturedValue(htim,TIM_CHANNEL_2)+1;
}
}
}
void HAL_TIM_OC_DelayElapsedCallback(TIM_HandleTypeDef *htim)
{
if(htim->Instance == TIM15)
{
if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1)
{
__HAL_TIM_SET_COMPARE(htim, TIM_CHANNEL_1, __HAL_TIM_GET_COUNTER(htim)+500);
}
}
}
3.4.5 main函数编写
int main(void)
{
HAL_Init();
SystemClock_Config();
LCD_Init();
LCD_Clear(White);
LCD_SetBackColor(White);
LCD_SetTextColor(Blue);
//PWM输入
PWMIN_TIM2_Init();
//pwm输出
PWMOUT_TIM3_Init();
PWMOUT_TIM17_Init();
//方波输出
SQU_TIM15_Init();
//基本定时器
BASE_TIM6_Init();
//打开定时器
HAL_TIM_Base_Start_IT(&htim6);//没100ms触发一次中断
//*输入捕获启动
HAL_TIM_Base_Start_IT(&htim2);/*启动*/
HAL_TIM_IC_Start_IT(&htim2,TIM_CHANNEL_1);/*捕获*/
HAL_TIM_IC_Start_IT(&htim2,TIM_CHANNEL_2);
//输出方波PA2引脚
HAL_TIM_OC_Start_IT(&htim15,TIM_CHANNEL_1);
//输出PWM
HAL_TIM_PWM_Start(&htim3,TIM_CHANNEL_1);//PA6
HAL_TIM_PWM_Start(&htim17,TIM_CHANNEL_1);//PA7
while (1)
{
Lcd_Proc();
}
}
4. 代码实验
将代码进行编译并下载到开发板上。效果如下图所示。
也可以使用PWM捕获定时器去采集PWM输出以及方波信号。
5. 总结
本文介绍了STM32G431的定时器资源并使用CubeMx配置了基本定时器、PWM捕获、PWM输出、方波输出等配置,并编写代码进行基本操作的实现。