TIM1_CH1
TIM1_CH1N
TIM1_CH2
TIM1_CH2N
TIM1_CH3
TIM1_CH3N
TIM1_CH4
TIM1的通道1、2、3输出同频率(20KHz)的PWM波形(占空比50%)
TIM1的通道1输出100Hz的PWM波形(占空比50%)
#include "tim.h"
/* USER CODE BEGIN 0 */
uint16_t f1 = 100; //168000000/84/100 = 20khz
uint16_t f2 = 100; //168000000/84/100 = 20khz
uint16_t f3 = 100; //168000000/84/100 = 20khz
uint16_t f4 = 20000; //168000000/84/40000 = 100hz
float Duty_Cycle1 = 0.5f;
float Duty_Cycle2 = 0.5f;
float Duty_Cycle3 = 0.5f;
float Duty_Cycle4 = 0.5f;
/* USER CODE END 0 */
TIM_HandleTypeDef htim1;
/* TIM1 init function */
void MX_TIM1_Init(void)
{
/* USER CODE BEGIN TIM1_Init 0 */
/* USER CODE END TIM1_Init 0 */
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
TIM_MasterConfigTypeDef sMasterConfig = {0};
TIM_OC_InitTypeDef sConfigOC = {0};
TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig = {0};
/* USER CODE BEGIN TIM1_Init 1 */
/* USER CODE END TIM1_Init 1 */
htim1.Instance = TIM1;
htim1.Init.Prescaler = 83;
htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
htim1.Init.Period = 65535;
htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim1.Init.RepetitionCounter = 0;
htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
if (HAL_TIM_Base_Init(&htim1) != HAL_OK)
{
Error_Handler();
}
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
if (HAL_TIM_ConfigClockSource(&htim1, &sClockSourceConfig) != HAL_OK)
{
Error_Handler();
}
if (HAL_TIM_OC_Init(&htim1) != HAL_OK)
{
Error_Handler();
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK)
{
Error_Handler();
}
sConfigOC.OCMode = TIM_OCMODE_TOGGLE;
sConfigOC.Pulse = 0;
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(&htim1, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
{
Error_Handler();
}
if (HAL_TIM_OC_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_2) != HAL_OK)
{
Error_Handler();
}
if (HAL_TIM_OC_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_3) != HAL_OK)
{
Error_Handler();
}
if (HAL_TIM_OC_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_4) != HAL_OK)
{
Error_Handler();
}
sBreakDeadTimeConfig.OffStateRunMode = TIM_OSSR_DISABLE;
sBreakDeadTimeConfig.OffStateIDLEMode = TIM_OSSI_DISABLE;
sBreakDeadTimeConfig.LockLevel = TIM_LOCKLEVEL_OFF;
sBreakDeadTimeConfig.DeadTime = 8; // 8/168M=47.6ns
sBreakDeadTimeConfig.BreakState = TIM_BREAK_DISABLE;
sBreakDeadTimeConfig.BreakPolarity = TIM_BREAKPOLARITY_HIGH;
sBreakDeadTimeConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE;
if (HAL_TIMEx_ConfigBreakDeadTime(&htim1, &sBreakDeadTimeConfig) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN TIM1_Init 2 */
// __HAL_TIM_DISABLE_OCxPRELOAD(&htim1,TIM_CHANNEL_1);
// __HAL_TIM_DISABLE_OCxPRELOAD(&htim1,TIM_CHANNEL_2);
// __HAL_TIM_DISABLE_OCxPRELOAD(&htim1,TIM_CHANNEL_3);
// __HAL_TIM_DISABLE_OCxPRELOAD(&htim1,TIM_CHANNEL_4);
HAL_TIM_OC_Start_IT(&htim1,TIM_CHANNEL_1);
HAL_TIMEx_OCN_Start(&htim1,TIM_CHANNEL_1);
HAL_TIM_OC_Start_IT(&htim1,TIM_CHANNEL_2);
HAL_TIMEx_OCN_Start(&htim1,TIM_CHANNEL_2);
HAL_TIM_OC_Start_IT(&htim1,TIM_CHANNEL_3);
HAL_TIMEx_OCN_Start(&htim1,TIM_CHANNEL_3);
HAL_TIM_OC_Start_IT(&htim1,TIM_CHANNEL_4);
/* USER CODE END TIM1_Init 2 */
HAL_TIM_MspPostInit(&htim1);
}
void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* tim_baseHandle)
{
if(tim_baseHandle->Instance==TIM1)
{
/* USER CODE BEGIN TIM1_MspInit 0 */
/* USER CODE END TIM1_MspInit 0 */
/* TIM1 clock enable */
__HAL_RCC_TIM1_CLK_ENABLE();
/* TIM1 interrupt Init */
HAL_NVIC_SetPriority(TIM1_CC_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(TIM1_CC_IRQn);
/* USER CODE BEGIN TIM1_MspInit 1 */
/* USER CODE END TIM1_MspInit 1 */
}
}
void HAL_TIM_MspPostInit(TIM_HandleTypeDef* timHandle)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
if(timHandle->Instance==TIM1)
{
/* USER CODE BEGIN TIM1_MspPostInit 0 */
/* USER CODE END TIM1_MspPostInit 0 */
__HAL_RCC_GPIOB_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
/**TIM1 GPIO Configuration
PB13 ------> TIM1_CH1N
PB14 ------> TIM1_CH2N
PB15 ------> TIM1_CH3N
PA8 ------> TIM1_CH1
PA9 ------> TIM1_CH2
PA10 ------> TIM1_CH3
PA11 ------> TIM1_CH4
*/
GPIO_InitStruct.Pin = GPIO_PIN_13|GPIO_PIN_14|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_TIM1;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10|GPIO_PIN_11;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Alternate = GPIO_AF1_TIM1;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/* USER CODE BEGIN TIM1_MspPostInit 1 */
/* USER CODE END TIM1_MspPostInit 1 */
}
}
void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef* tim_baseHandle)
{
if(tim_baseHandle->Instance==TIM1)
{
/* USER CODE BEGIN TIM1_MspDeInit 0 */
/* USER CODE END TIM1_MspDeInit 0 */
/* Peripheral clock disable */
__HAL_RCC_TIM1_CLK_DISABLE();
/* TIM1 interrupt Deinit */
HAL_NVIC_DisableIRQ(TIM1_CC_IRQn);
/* USER CODE BEGIN TIM1_MspDeInit 1 */
/* USER CODE END TIM1_MspDeInit 1 */
}
}
/* USER CODE BEGIN 1 */
void HAL_TIM_OC_DelayElapsedCallback(TIM_HandleTypeDef *htim)
{
uint16_t OC_Count = 0;
if(htim->Instance == TIM1)
{
if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1)
{
OC_Count=__HAL_TIM_GET_COMPARE(&htim1,TIM_CHANNEL_1);//获取定时器计数值
if(GPIO_PIN_RESET == HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_8))
{
__HAL_TIM_SET_COMPARE(&htim1,TIM_CHANNEL_1,OC_Count + f1 * Duty_Cycle1);
}
else
{
__HAL_TIM_SET_COMPARE(&htim1,TIM_CHANNEL_1,OC_Count + f1 * (1 - Duty_Cycle1));
}
}
else
if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_2)
{
OC_Count=__HAL_TIM_GET_COMPARE(&htim1,TIM_CHANNEL_2);//获取定时器计数值
if(GPIO_PIN_RESET == HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_9))
{
__HAL_TIM_SET_COMPARE(&htim1,TIM_CHANNEL_2,OC_Count + f2 * Duty_Cycle2);
}
else
{
__HAL_TIM_SET_COMPARE(&htim1,TIM_CHANNEL_2,OC_Count + f2 * (1 - Duty_Cycle2));
}
}
else
if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_3)
{
OC_Count=__HAL_TIM_GET_COMPARE(&htim1,TIM_CHANNEL_3);//获取定时器计数值
if(GPIO_PIN_RESET == HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_10))
{
__HAL_TIM_SET_COMPARE(&htim1,TIM_CHANNEL_3,OC_Count + f3 * Duty_Cycle3);
}
else
{
__HAL_TIM_SET_COMPARE(&htim1,TIM_CHANNEL_3,OC_Count + f3 * (1 - Duty_Cycle3));
}
}
else
if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_4)
{
OC_Count=__HAL_TIM_GET_COMPARE(&htim1,TIM_CHANNEL_4);//获取定时器计数值
if(GPIO_PIN_RESET == HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_11))
{
__HAL_TIM_SET_COMPARE(&htim1,TIM_CHANNEL_4,OC_Count + f4 * Duty_Cycle4);
}
else
{
__HAL_TIM_SET_COMPARE(&htim1,TIM_CHANNEL_4,OC_Count + f4 * (1 - Duty_Cycle4));
}
}
}
}
/* USER CODE END 1 */
(包括增加死区时间效果)