/* Set the TIM state */ htim->State = HAL_TIM_STATE_BUSY;
/* Set the Time Base configuration */ TIM_Base_SetConfig(htim->Instance, &htim->Init);
/* Initialize the DMA burst operation state */ htim->DMABurstState = HAL_DMA_BURST_STATE_READY;
/* Initialize the TIM channels state */ TIM_CHANNEL_STATE_SET_ALL(htim, HAL_TIM_CHANNEL_STATE_READY); TIM_CHANNEL_N_STATE_SET_ALL(htim, HAL_TIM_CHANNEL_STATE_READY);
/* Initialize the TIM state*/ htim->State = HAL_TIM_STATE_READY;
voidliangji_pwm_led(void) { staticuint8_t dir = 1; staticuint16_t ledrpwmval = 0; if (dir) ledrpwmval++; else ledrpwmval--; //switch if (ledrpwmval > 300) dir = 0; if (ledrpwmval == 0) dir = 1;
/* Update IT callback impel in main, toggle led1, and count up ++ HAL_TIM_PeriodElapsedCallback(htim); */
voidHAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) { if (htim->Instance == TIM6){ //LED1_TOGGLE(); } elseif (htim->Instance == TIM3){ //LED1_TOGGLE(); } elseif (htim->Instance == TIM5) { if((g_timxchy_cap_sta & 0X80) == 0){ if(g_timxchy_cap_sta & 0X40){ if ((g_timxchy_cap_sta & 0X3F) == 0X3F){ // wait for falling, over the N max, is too long,think it is success, so re-wait for up, give up wait falling TIM_RESET_CAPTUREPOLARITY(&init_TIM5, TIM_CHANNEL_1); TIM_SET_CAPTUREPOLARITY(&init_TIM5, TIM_CHANNEL_1, TIM_ICPOLARITY_RISING); g_timxchy_cap_sta |= 0x80; g_timxchy_cap_val = 0xffff; //mean: N(max)*arr + N(max) } else { g_timxchy_cap_sta++; //N +1 } } } } }
/* HAL_TIM_IC_CaptureCallback(htim); */ voidHAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim) { //get up cnt //set fall detect if((g_timxchy_cap_sta & 0X80) == 0){ if(g_timxchy_cap_sta & 0X40){ //now 0 1, but now capture falling because when capture up, we detect falling g_timxchy_cap_sta |= 0x80; g_timxchy_cap_val = HAL_TIM_ReadCapturedValue(&init_TIM5, TIM_CHANNEL_1);//get falling cnt t2 (t2<arr) //reset detect up TIM_RESET_CAPTUREPOLARITY(&init_TIM5, TIM_CHANNEL_1); TIM_SET_CAPTUREPOLARITY(&init_TIM5, TIM_CHANNEL_1, TIM_ICPOLARITY_RISING); } else { //now 0 0, wait for up edge, but now capture interupt, so start a new detect g_timxchy_cap_sta = 0; g_timxchy_cap_val = 0; g_timxchy_cap_sta |= 0x40; // set 0 1, we have get up edge //set polo falling detect, no need to record now cnt t1, because t1 + N*arr + t2 = now __HAL_TIM_DISABLE(&init_TIM5); __HAL_TIM_SET_COUNTER(&init_TIM5, 0); TIM_RESET_CAPTUREPOLARITY(&init_TIM5, TIM_CHANNEL_1); TIM_SET_CAPTUREPOLARITY(&init_TIM5, TIM_CHANNEL_1, TIM_ICPOLARITY_RISING); __HAL_TIM_ENABLE(&init_TIM5); } } }