最近小编在调试STM32的hal库,TIM1波形刹车控制在此做以下几点总结,如有问题在相互交流谢谢!
1、用cube配置TIM1定时配置PWM输出,使能定时器自动重载,刹车自动恢复:
2、程序:
uint16_t dutyCycle = 1;
uint16_t test_value[5];
uint16_t pwmStartUp =0;
/* USER CODE BEGIN 2 */
HAL_TIM_Base_Start(&htim1); //使能TIM1
HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_1); //使能PWM通道1
HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_2); //使能PWM通道2
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
if(test_value[0] != dutyCycle) //修改移相值
{
test_value[0] = dutyCycle;
test_value[1] ++;
__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, dutyCycle); //通道移相
__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_2, dutyCycle); //通道移相
}
else if(pwmStartUp != 0) //启动PWM输出
{
pwmStartUp = 0;
dutyCycle = 1; //设置最小值
__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, dutyCycle); //通道移相
__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_2, dutyCycle); //通道移相
HAL_TIM_Base_Start(&htim1); //使能TIM1
HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_1); //使能PWM通道1
HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_2); //使能PWM通道2
}
test_value[4] ++;
HAL_Delay(200);
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
3、仿真波形
最近小编在调试STM32的hal库,TIM1波形刹车控制在此做以下几点总结,如有问题在相互交流谢谢!
1、用cube配置TIM1定时配置PWM输出,使能定时器自动重载,刹车自动恢复:
2、程序:
uint16_t dutyCycle = 1;
uint16_t test_value[5];
uint16_t pwmStartUp =0;
/* USER CODE BEGIN 2 */
HAL_TIM_Base_Start(&htim1); //使能TIM1
HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_1); //使能PWM通道1
HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_2); //使能PWM通道2
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
if(test_value[0] != dutyCycle) //修改移相值
{
test_value[0] = dutyCycle;
test_value[1] ++;
__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, dutyCycle); //通道移相
__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_2, dutyCycle); //通道移相
}
else if(pwmStartUp != 0) //启动PWM输出
{
pwmStartUp = 0;
dutyCycle = 1; //设置最小值
__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, dutyCycle); //通道移相
__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_2, dutyCycle); //通道移相
HAL_TIM_Base_Start(&htim1); //使能TIM1
HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_1); //使能PWM通道1
HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_2); //使能PWM通道2
}
test_value[4] ++;
HAL_Delay(200);
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
3、仿真波形
举报