芯片为STM32F412VETx,使用的是高级定时器TIM1的PWM互补PWM输出,看一下STM32CubeMX配置,
再看时钟的配置,TIM1用的是APB2的时钟,。
再看引脚的配置
产生的PWM频率为:Fpwm = 96MHz / ((95+1)(9999+1)) =100Hz,占空比为50%
然后生成代码,看下代码中的FMX_TIM1_Init函数初始化
/* TIM1 init function */
static void FMX_TIM1_Init(void)
{
TIM_ClockConfigTypeDef sClockSourceConfig;
TIM_MasterConfigTypeDef sMasterConfig;
TIM_OC_InitTypeDef sConfigOC;
TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig;
htim1.Instance = TIM1;
htim1.Init.Prescaler = 95;
htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
htim1.Init.Period = 9999;
htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim1.Init.RepetitionCounter = 0;
if (HAL_TIM_Base_Init(&htim1) != HAL_OK) //使能TIM1时钟
{
_Error_Handler(__FILE__, __LINE__);
}
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
if (HAL_TIM_ConfigClockSource(&htim1, &sClockSourceConfig) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
if (HAL_TIM_PWM_Init(&htim1) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
sConfigOC.OCMode = TIM_OCMODE_PWM1;
sConfigOC.Pulse = 5000;
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(&htim1, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
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.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE;
if (HAL_TIMEx_ConfigBreakDeadTime(&htim1, &sBreakDeadTimeConfig) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
HAL_TIM_MspPostInit(&htim1); //引脚配置
}
好了到此TIM1的PWM互补已经配置完成,接下来就是要是要使能PWM输出了,接下在再main函数中使能输出就OK了。
HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_1); //starts PWM on CH1 pin
HAL_TIMEx_PWMN_Start(&htim1, TIM_CHANNEL_1); //starts PWM on CH1N pin
最好用示波器看下效果图
上一篇:STM32高级定时器TIM1、TIM15输出PWM
下一篇:STM32F103ZET定时器TIM1输出六路带死区互补PWM波形
推荐阅读最新更新时间:2024-11-07 18:36
设计资源 培训 开发板 精华推荐
- LTC3633AEUFD-2 双通道 3A、20V 单片式同步降压型稳压器的典型应用电路
- ST2S06B 双路同步整流带复位或抑制、0.5 A、1.5 MHz 可调降压开关稳压器的典型应用电路
- NCV2001SQ2T2G 0.9V单电源运放音带滤波器典型应用电路
- 用于低功率应变计电路的 REF193 低压差参考电压的典型应用电路
- LF33CV 3.3V 低压降稳压器的典型应用
- DER-270 - 使用HiperLCS LCS701HG设计的125 W LLC DC-DC谐振转换器
- 【征集令】oneTesla开源数字灭弧修改版
- 振动网状雾化器参考设计
- 家门电子值守(敲门检测、提醒)
- 会眨眼的小流氓兔