TIMER3的的Channel1输出,
在TIM3的HAL_TIM_PeriodElapsedCallback中断中GPIO7翻转,在TIM3的HAL_TIM_OC_DelayElapsedCallback 中选取TIM3的一般周期翻转GPIO8
或者可以通过sConfigOC.OCMode = TIM_OCMODE_TOGGLE直接翻转TIM3的比较输出映射GPIO6
void MX_TIM3_Init(void)
{
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
TIM_MasterConfigTypeDef sMasterConfig = {0};
TIM_OC_InitTypeDef sConfigOC = {0};
htim3.Instance = TIM3;
htim3.Init.Prescaler = 100;
htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
htim3.Init.Period = 10000;
htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV2;
htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
if (HAL_TIM_Base_Init(&htim3) != HAL_OK)
{
Error_Handler();
}
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
if (HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig) != HAL_OK)
{
Error_Handler();
}
if (HAL_TIM_OC_Init(&htim3) != HAL_OK)
{
Error_Handler();
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK)
{
Error_Handler();
}
// 此处如果使用TIM_OCMODE_TOGGLE且初始化HAL_TIM_MspPostInit(&htim3);则会在time_channel1的GPIO翻转电平,但是如果配置为TIM_OCMODE_TIMING则time_channel的GPIO不会有电平变化
sConfigOC.OCMode = TIM_OCMODE_TOGGLE;//TIM_OCMODE_TIMING;
sConfigOC.Pulse = 5000;
sConfigOC.OCPolarity = TIM_OCPOLARITY_LOW;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
if (HAL_TIM_OC_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
{
Error_Handler();
}
HAL_TIM_MspPostInit(&htim3);
HAL_TIM_Base_Start_IT(&htim3);
HAL_TIM_OC_Start_IT(&htim3, TIM_CHANNEL_1);
}
void HAL_TIM_MspPostInit(TIM_HandleTypeDef* timHandle)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
if(timHandle->Instance==TIM3)
{
/* USER CODE BEGIN TIM3_MspPostInit 0 */
/* USER CODE END TIM3_MspPostInit 0 */
__HAL_RCC_GPIOC_CLK_ENABLE();
/**TIM3 GPIO Configuration
PC6 ------> TIM3_CH1
*/
GPIO_InitStruct.Pin = GPIO_PIN_6;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Alternate = GPIO_AF2_TIM3;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
/* USER CODE BEGIN TIM3_MspPostInit 1 */
/* USER CODE END TIM3_MspPostInit 1 */
}
}
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
if(htim->Instance == TIM1)
{
time1_cnt++;
}
else if(htim->Instance == TIM2)
{
time2_cnt++;
}
else if(htim->Instance == TIM3)
{
time3_cnt++;
TestPinToggle();
}
}
此中断就是比较输出的中断
void HAL_TIM_OC_DelayElapsedCallback(TIM_HandleTypeDef *htim)
{
if(htim->Instance == TIM3)
{
Test8PinToggle();
}
}
版权声明:本文为CSDN博主「攻城狮之路人甲」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/forlifefor/article/details/121397177
暂无评论