//TIM3 PWM2¿·Ö3õê¼»ˉ
//PWMêä3ö3õê¼»ˉ
//arr£o×Ô¶ˉÖØ×°Öμ
//psc£oê±ÖóÔ¤·ÖÆμêy
void TIM3_Base_Init(u16 arr,u16 psc)
{
TIM3_Handler.Instance=TIM3; //¶¨ê±Æ÷3
TIM3_Handler.Init.Prescaler=psc; //¶¨ê±Æ÷·ÖÆμ
TIM3_Handler.Init.CounterMode=TIM_COUNTERMODE_UP;//ÏòéϼÆêyÄ£ê½
TIM3_Handler.Init.Period=arr; //×Ô¶ˉÖØ×°ÔØÖμ
TIM3_Handler.Init.ClockDivision=TIM_CLOCKDIVISION_DIV1;
HAL_TIM_Base_Init(&TIM3_Handler); //3õê¼»ˉTIM3
TIM_ClockConfigStrue.ClockSource=TIM_CLOCKSOURCE_ETRMODE2;
TIM_ClockConfigStrue.ClockFilter=TIM_CLOCKPRESCALER_DIV1;
TIM_ClockConfigStrue.ClockPolarity=TIM_CLOCKPOLARITY_NONINVERTED;
TIM_ClockConfigStrue.ClockPrescaler=0;
HAL_TIM_ConfigClockSource(&TIM3_Handler,&TIM_ClockConfigStrue);
__HAL_TIM_CLEAR_IT(&TIM3_Handler, TIM_IT_TRIGGER);
__HAL_TIM_SET_COUNTER(&TIM3_Handler,0);
__HAL_TIM_ENABLE(&TIM3_Handler);
}
//¶¨ê±Æ÷μ×2ãÇy¶ˉ£¬ê±Öóê1Äü£¬òy½ÅÅäÖÃ
//′Ëoˉêy»á±»HAL_TIM_PWM_Init()μ÷óÃ
//htim:¶¨ê±Æ÷¾ä±ú
void HAL_TIM_Base_MspInit(TIM_HandleTypeDef *htim)
{
GPIO_InitTypeDef GPIO_Initure;
__HAL_RCC_TIM3_CLK_ENABLE(); //ê1Äü¶¨ê±Æ÷3
__HAL_RCC_GPIOD_CLK_ENABLE(); //¿aÆôGPIODê±Öó
GPIO_Initure.Pin=GPIO_PIN_2; //PD2
GPIO_Initure.Mode=GPIO_MODE_INPUT; //¸¡¿Õêäèë
GPIO_Initure.Pull=GPIO_PULLDOWN; //
GPIO_Initure.Speed=GPIO_SPEED_HIGH; //¸ßËù
GPIO_Initure.Alternate= GPIO_AF2_TIM3; //PD2¸′óÃÎaTIM3_ETR
HAL_GPIO_Init(GPIOD,&GPIO_Initure);
HAL_NVIC_SetPriority(TIM3_IRQn,2,0); //éèÖÃÖD¶ÏóÅÏ輶£¬ÇàÕ¼óÅÏ輶2£¬×óóÅÏ輶0
HAL_NVIC_EnableIRQ(TIM3_IRQn); //¿aÆôITM3ÖD¶Ïí¨μà
}
//¶¨ê±Æ÷3ÖD¶Ï·tÎñoˉêy
void TIM3_IRQHandler(void)
{
HAL_TIM_IRQHandler(&TIM3_Handler);//¶¨ê±Æ÷12óÃ′|àíoˉêy
}
u8 triggercounter=0;
//¶¨ê±Æ÷′¥·¢ÖD¶Ï′|àí»Øμ÷oˉêy
void HAL_TIM_TriggerCallback(TIM_HandleTypeDef *htim)
{
triggercounter++;
__HAL_TIM_CLEAR_IT(htim, TIM_IT_TRIGGER);
printf("
%d
",triggercounter);
}
一周热门 更多>