STM32CubeMX
配置过程演示Clock Source(时钟源)选择Internal Clock(内部时钟源),这里选择CH1(对应的输出引脚是PA8)作为PWM通道。
定时时间为 1ms,可设置Prescaler = 64-1;Counter = 1000 - 1;(TIM1时钟频率设置为64MHz),则PWM频率为1KHz。
PWM频率和Counter Period(AutoReload Register)参数关系
1000 -1 ------>f= 1KHz
2000 -1 ------>f= 500Hz
500 -1 ------->f= 2KHz
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_TIM1_Init();
/* USER CODE BEGIN 2 */
HAL_TIM_PWM_Start(&htim1,TIM_CHANNEL_1);//加入定时器启动函数
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
/* 500与设置的Counter Period计数值有关,值为1000,则下面的500表示占空比为50% */
__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1,500);//设置PWM占空比
}
/* USER CODE END 3 */
}
链接:https://pan.baidu.com/s/1C5Cyer53LGJ61Jki0e3Mjg
提取码:8vw0