title: HALPWM配置占空比频率
tags:
- STM32
- Clion
- Hal
控制pwm开关
//启动定时器
HAL_StatusTypeDef HAL_TIM_Base_Start(TIM_HandleTypeDef *htim);
//关闭定时器
HAL_StatusTypeDef HAL_TIM_Base_Stop(TIM_HandleTypeDef *htim);
//启动指定通道PWM
HAL_StatusTypeDef HAL_TIM_PWM_Start(TIM_HandleTypeDef *htim, uint32_t Channel);
//停止指定通道PWM
HAL_StatusTypeDef HAL_TIM_PWM_Stop(TIM_HandleTypeDef *htim, uint32_t Channel);
//设置指定通道占空比
void __HAL_TIM_SetCompare(TIM_HandleTypeDef *htim, uint32_t Channel,uint16_t Value);
//设置定时器计数器值
coid __HAL_TIM_SetCompare(TIM_HandleTypeDef *htim, uint16_t Value)
if (sta==0){
HAL_TIM_PWM_Stop(&htim2, TIM_CHANNEL_2);
} else if(sta==1){
// D=5%
HAL_TIM_PWM_Stop(&htim2, TIM_CHANNEL_2);
D=5; HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_2);
} else if (sta==2){
// 10%≤D≤85%
HAL_TIM_PWM_Stop(&htim2, TIM_CHANNEL_2);
D=50; HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_2);
} else if (sta==3){
// 85%<D≤90%
HAL_TIM_PWM_Stop(&htim2, TIM_CHANNEL_2);
D=88;HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_2); /// 这里的Start 已经不起作用了
}
** HAL_TIM_PWM_Stop(&htim2, TIM_CHANNEL_2); 相当于把这个中断使能关闭了需要到外部开启 **
/ 按键中断
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
if(GPIO_Pin==SW1_Pin)
{
HAL_GPIO_WritePin(GPIOB, LED_G_Pin|LED_B_Pin|LED_R_Pin, GPIO_PIN_SET);
HAL_GPIO_TogglePin(LED_G_GPIO_Port,LED_G_Pin);
HAL_UART_Transmit(&huart1,"G", sizeof("G")-1, 100);
sta--;
if(sta==4){
HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_2);
}
} else if (GPIO_Pin==SW3_Pin){
HAL_GPIO_WritePin(GPIOB, LED_G_Pin|LED_B_Pin|LED_R_Pin, GPIO_PIN_SET);
HAL_GPIO_TogglePin(LED_R_GPIO_Port,LED_R_Pin);
HAL_UART_Transmit(&huart1,"R", sizeof("R")-1, 100);
sta++;
if(sta==4){
HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_2);
}
}
}
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
if(sta==1){
set_pwm_param(htim2, TIM_CHANNEL_2, 1000, 50);
}
else if(sta==2)
{
set_pwm_param(htim2, TIM_CHANNEL_2, 1000, 20);
}
else if(sta==3){
//
HAL_TIM_PWM_Stop(&htim2, TIM_CHANNEL_2);
HAL_UART_Transmit(&huart1,"3", sizeof("3")-1, 100);
}
else if(sta==4){
//
set_pwm_param(htim2, TIM_CHANNEL_2, 1000, 40);
}
}
大概就是这么个意思也没仔细看HAL开发的文档 就虾摸我豆