STM32:PWM-README
pwm,脉冲宽度调制输出,即产生方波信号
频率 = 定时器时钟 / (Prescaler 预分频 + 1)/ (Counter Period 计数值 + 1)Hz
占空比 = Pulse ( 对比值) / (C ounter Period 计数值)%
占空比高就是高电平多,灯越亮
STMF103RCT6只有TIM1CH1通道(PA8)可以用作PWM
选择PA8作为PWN脚:
时钟树设置为72MKZ TIM设置: 设置内部时钟,选择TIM1CH1通道,Prescaler 预分频 (72-1),counter(计数值)为(1000-1=999),Pulse ( 对比值)为(500-1=499),占空比为50%:(后面调占空比调的就是Pulse ( 对比值))
关键函数:
HAL_TIM_PWM_Start(&htimx,TIM_CHANNEL_y);
__HAL_TIM_SET_COMPARE(&htimx, TIM_CHANNEL_y, z);
main里while外:
uint16_t pwmVal=0;
HAL_TIM_PWM_Start(&htim1,TIM_CHANNEL_1);
while函数里:
while (1)
{
while( pwmVal < 1000)
{
pwmVal++;
__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, pwmVal);
HAL_Delay(1);
}
while(pwmVal)
{
pwmVal--;
__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, pwmVal);
HAL_Delay(1);
}
}
}
|