资深版主
- 积分
- 5136
- 金钱
- 5136
- 注册时间
- 2014-8-26
- 在线时间
- 1316 小时
|
[mw_shl_code=c,true]TIM_HandleTypeDef TIM1_Handler; //定时器句柄
TIM_OC_InitTypeDef TIM1_CHxHandler; //定时器1比较输出句柄
__IO uint32_t uhCCR1_Val = 500-1; //计数500翻转,频率就是1000Hz
__IO uint32_t uhCCR2_Val = 1000-1; //计数1000翻转,频率就是500Hz
__IO uint32_t uhCCR3_Val = 2000-1; //计数2000翻转,频率就是250Hz
__IO uint32_t uhCCR4_Val = 4000-1; //计数4000翻转,频率就是125Hz
//TIM1比较输出初始化
void TIM1_PWM_Init(void)
{
TIM1_Handler.Instance = TIM1;
TIM1_Handler.Init.Period = 65535;
TIM1_Handler.Init.Prescaler = 199; //定时器计数频率1Mhz
TIM1_Handler.Init.ClockDivision = 0;
TIM1_Handler.Init.CounterMode = TIM_COUNTERMODE_UP;
HAL_TIM_OC_Init(&TIM1_Handler);
TIM1_CHxHandler.OCMode=TIM_OCMODE_TOGGLE; //模式选择TOGGLE
TIM1_CHxHandler.OCPolarity=TIM_OCPOLARITY_LOW; //输出比较极性为低
TIM1_CHxHandler.Pulse=uhCCR1_Val;
HAL_TIM_OC_ConfigChannel(&TIM1_Handler, &TIM1_CHxHandler, TIM_CHANNEL_1);
TIM1_CHxHandler.Pulse=uhCCR2_Val;
HAL_TIM_OC_ConfigChannel(&TIM1_Handler, &TIM1_CHxHandler, TIM_CHANNEL_2);
TIM1_CHxHandler.Pulse=uhCCR3_Val;
HAL_TIM_OC_ConfigChannel(&TIM1_Handler, &TIM1_CHxHandler, TIM_CHANNEL_3);
TIM1_CHxHandler.Pulse=uhCCR4_Val;
HAL_TIM_OC_ConfigChannel(&TIM1_Handler, &TIM1_CHxHandler, TIM_CHANNEL_4);
HAL_TIM_OC_Start_IT(&TIM1_Handler, TIM_CHANNEL_1);
HAL_TIM_OC_Start_IT(&TIM1_Handler, TIM_CHANNEL_2);
HAL_TIM_OC_Start_IT(&TIM1_Handler, TIM_CHANNEL_3);
HAL_TIM_OC_Start_IT(&TIM1_Handler, TIM_CHANNEL_4);
}
void HAL_TIM_OC_MspInit(TIM_HandleTypeDef *htim)
{
GPIO_InitTypeDef GPIO_InitStruct;
__HAL_RCC_TIM1_CLK_ENABLE();
__HAL_RCC_GPIOE_CLK_ENABLE();;
//PE9、PE11、PE13、PE14分别对应定时器1的通道1、2、3、4
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF1_TIM1;
GPIO_InitStruct.Pin = GPIO_PIN_9;
HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);
GPIO_InitStruct.Alternate = GPIO_AF1_TIM1;
GPIO_InitStruct.Pin = GPIO_PIN_11;
HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);
GPIO_InitStruct.Alternate = GPIO_AF1_TIM1;
GPIO_InitStruct.Pin = GPIO_PIN_13;
HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);
GPIO_InitStruct.Alternate = GPIO_AF1_TIM1;
GPIO_InitStruct.Pin = GPIO_PIN_14;
HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);
//设置定时器1的比较输出中断优先级
HAL_NVIC_SetPriority(TIM1_CC_IRQn, 0, 1);
HAL_NVIC_EnableIRQ(TIM1_CC_IRQn);
}
void HAL_TIM_OC_DelayElapsedCallback(TIM_HandleTypeDef *htim)
{
uint32_t uhCapture = 0;
//TIM1_CH1频率1000Hz
if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1)
{
uhCapture = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_1);
//设置通道1的比较值
__HAL_TIM_SET_COMPARE(&TIM1_Handler, TIM_CHANNEL_1, (uhCapture + uhCCR1_Val));
}
//TIM1_CH2频率500Hz
if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_2)
{
uhCapture = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_2);
//设置通道2的比较值
__HAL_TIM_SET_COMPARE(&TIM1_Handler, TIM_CHANNEL_2, (uhCapture + uhCCR2_Val));
}
//TIM1_CH3频率250Hz
if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_3)
{
uhCapture = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_3);
//设置通道3的比较值
__HAL_TIM_SET_COMPARE(&TIM1_Handler, TIM_CHANNEL_3, (uhCapture + uhCCR3_Val));
}
//TIM1_CH3频率125Hz
if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_4)
{
uhCapture = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_4);
//设置通道4的比较值
__HAL_TIM_SET_COMPARE(&TIM1_Handler, TIM_CHANNEL_4, (uhCapture + uhCCR4_Val));
}
}
void TIM1_CC_IRQHandler(void)
{
HAL_TIM_IRQHandler(&TIM1_Handler);
}[/mw_shl_code]
实测效果
|
|