/** * @brief Conversion complete callback in non blocking mode * @param htim: TIM handle * @retval None */ void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim) { if (htim->Channel == HAL_TIM_ACTIVE_CHANNEL_2) { if(uhCaptureIndex == 0) { /* Get the 1st Input Capture value */ uwIC2Value1 = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_2); uhCaptureIndex = 1; } else if(uhCaptureIndex == 1) { /* Get the 2nd Input Capture value */ uwIC2Value2 = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_2); /* Capture computation */ if (uwIC2Value2 > uwIC2Value1) { uwDiffCapture = (uwIC2Value2 - uwIC2Value1); } else /* (uwIC2Value2 <= uwIC2Value1) */ { uwDiffCapture = ((0xFFFF - uwIC2Value1) + uwIC2Value2); } /* Frequency computation: for this example TIMx (TIM1) is clocked by 2xAPB2Clk */ uwFrequency = (2*HAL_RCC_GetPCLK2Freq()) / uwDiffCapture; uhCaptureIndex = 0; } } }
/** * @brief Conversion complete callback in non blocking mode * @param htim : htim handle * @retval None */ void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim) { if (htim->Channel == HAL_TIM_ACTIVE_CHANNEL_4) { if(uhCaptureIndex == 0) { /* Get the 1st Input Capture value */ uwIC2Value1 = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_4); uhCaptureIndex = 1; } else if(uhCaptureIndex == 1) { /* Get the 2nd Input Capture value */ uwIC2Value2 = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_4); /* Capture computation */ if (uwIC2Value2 > uwIC2Value1) { uwDiffCapture = (uwIC2Value2 - uwIC2Value1); } else if (uwIC2Value2 < uwIC2Value1) { uwDiffCapture = ((0xFFFF - uwIC2Value1) + uwIC2Value2); } else { uwDiffCapture = 0; } /* Compute the pulse width in us */ uwMeasuredPulse = (uint32_t)(((uint64_t) uwDiffCapture * 1000000) / HAL_RCC_GetPCLK1Freq()); uhCaptureIndex = 0; } } }
/** * @brief Output Compare callback in non blocking mode * @param htim : TIM OC handle * @retval None */ void HAL_TIM_OC_DelayElapsedCallback(TIM_HandleTypeDef *htim) { /* TIM3_CH1 toggling with frequency = 195 Hz */ if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1) { uhCapture = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_1); /* Set the Capture Compare Register value */ __HAL_TIM_SET_COMPARE(&TimHandle, TIM_CHANNEL_1, (uhCapture + uhCCR1_Val)); } /* TIM3_CH2 toggling with frequency = 390 Hz */ if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_2) { uhCapture = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_2); /* Set the Capture Compare Register value */ __HAL_TIM_SET_COMPARE(&TimHandle, TIM_CHANNEL_2, (uhCapture + uhCCR2_Val)); } /* TIM3_CH3 toggling with frequency = 780 Hz */ if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_3) { uhCapture = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_3); /* Set the Capture Compare Register value */ __HAL_TIM_SET_COMPARE(&TimHandle, TIM_CHANNEL_3, (uhCapture + uhCCR3_Val)); } /* TIM3_CH4 toggling with frequency = 1560 Hz */ if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_4) { uhCapture = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_4); /* Set the Capture Compare Register value */ __HAL_TIM_SET_COMPARE(&TimHandle, TIM_CHANNEL_4, (uhCapture + uhCCR4_Val)); } }
/** * @brief 获得两次时间间隔中的编码器读数差. * @param None * @retval None */ void Get_Speed(void){ uint32_t TempL,TempR; TempL=HAL_TIM_ReadCapturedValue(&TimHandleT9, TIM_CHANNEL_1);//编码器读取 Speed_L=TempL; /* Speed_L=TempL-Pre_Speed_L; Pre_Speed_L=TempL; if(Speed_L<-20000){ Speed_L+=65535;} else if(Speed_L>20000){ Speed_L-=65535;} */ TempR=HAL_TIM_ReadCapturedValue(&TimHandleT5, TIM_CHANNEL_1);//编码器读取 Speed_R=TempR; /* Speed_R=TempR-Pre_Speed_R; Pre_Speed_R=TempR; if(Speed_R<-20000){ Speed_R+=65535;} else if(Speed_R>20000){ Speed_R-=65535;} //一阶低通滤波 Speed_A_Last=(Speed_L-Speed_R)/2; Speed_A*=0.7; Speed_A+=Speed_A_Last*0.3; */ }
/** * @brief Conversion complete callback in non blocking mode * @param htim: TIM handle * @retval None */ void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim) { uint16_t captureVal; if (htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1) { captureVal = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_1); motorHallTimoutCnt[0] = 0; motorHallPeriode[0] = captureVal - motorHallLastCapVal[0]; motorHallLastCapVal[0] = captureVal; } if (htim->Channel == HAL_TIM_ACTIVE_CHANNEL_2) { captureVal = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_2); motorHallTimoutCnt[0] = 0; motorHallPeriode[0] = captureVal - motorHallLastCapVal[1]; motorHallLastCapVal[1] = captureVal; } if (htim->Channel == HAL_TIM_ACTIVE_CHANNEL_3) { captureVal = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_3); motorHallTimoutCnt[1] = 0; motorHallPeriode[1] = captureVal - motorHallLastCapVal[2]; motorHallLastCapVal[2] = captureVal; } if (htim->Channel == HAL_TIM_ACTIVE_CHANNEL_4) { captureVal = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_4); motorHallTimoutCnt[1] = 0; motorHallPeriode[1] = captureVal - motorHallLastCapVal[3]; motorHallLastCapVal[3] = captureVal; } }
void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim) { if(edge == TIM_INPUTCHANNELPOLARITY_RISING) { edge = TIM_INPUTCHANNELPOLARITY_FALLING; capture_value_raising = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_1); } else if(edge == TIM_INPUTCHANNELPOLARITY_FALLING) { capture_value_falling = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_1); float capture_value = capture_value_falling - capture_value_raising; distance = ((sonic_velocity * (capture_value / tim_clock)) / 2.0) * 10000; // mm USBD_CUSTOM_HID_SendReport(&hUsbDeviceFS, (uint8_t*)&distance, CUSTOM_HID_EPOUT_SIZE); } }
// Callback for sonar echo void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim) { int idx; uint32_t ic_val = 0; // >>>>> Which sonar has received the echo? if (htim->Instance == TIM2) { idx = SONAR_0; elapsed_tick[idx] = HAL_GetTick() - trigger_tick[COUPLE_0_2]; // Update Input Capture times ic_val = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_2); } else if (htim->Instance == TIM3) { idx = SONAR_1; elapsed_tick[idx] = HAL_GetTick() - trigger_tick[COUPLE_0_2]; // Update Input Capture times ic_val = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_2); } else if (htim->Instance == TIM4) { idx = SONAR_2; elapsed_tick[idx] = HAL_GetTick() - trigger_tick[COUPLE_1_3]; // Update Input Capture times ic_val = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_2); } else if (htim->Instance == TIM5) { idx = SONAR_3; elapsed_tick[idx] = HAL_GetTick() - trigger_tick[COUPLE_1_3]; // Update Input Capture times ic_val = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_1); } else return; // Should never be here!!! // <<<<< Which sonar has received the echo? // >>>>> Durations update if (ic_val > MAX_DURATION_10_USEC) distValid[idx] = 0; else distValid[idx] = 1; echo_duration[idx] = ic_val; // <<<<< Durations update }
void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim) { uint32_t CCRx_tn=0, CCRx_tn_1=0; if (htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1) { CCRx_tn = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_1); if(htim->Instance == TIM3x) { RC2_tn = CCRx_tn; intflag2 = 0; } else if(htim->Instance == TIM4x) { RC3_tn = CCRx_tn; intflag3 = 0; } } else if (htim->Channel == HAL_TIM_ACTIVE_CHANNEL_2) { CCRx_tn_1 = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_2); // Input capture value if(htim->Instance == TIM3x) { RC2_tn_1 = CCRx_tn_1; intflag2 = 1; } else if(htim->Instance == TIM4x) { RC3_tn_1 = CCRx_tn_1; intflag3 = 1; } } else if (htim->Channel == HAL_TIM_ACTIVE_CHANNEL_3) { CCRx_tn = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_3); if(htim->Instance == TIM3x) { RC1_tn = CCRx_tn; intflag1 = 0; } else if(htim->Instance == TIM4x) { RC4_tn = CCRx_tn; intflag4 = 0; } } else if (htim->Channel == HAL_TIM_ACTIVE_CHANNEL_4) { CCRx_tn_1 = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_4); // Input capture value if(htim->Instance == TIM3x) { RC1_tn_1 = CCRx_tn_1; intflag1 = 1; } else if(htim->Instance == TIM4x) { RC4_tn_1 = CCRx_tn_1; intflag4 = 1; } // ExpLedToggle(GREEN_LED); } }
// Timer 17 update and capture compare interrupt void TIM1_TRG_COM_TIM17_IRQHandler(void) { if(__HAL_TIM_GET_FLAG(&TimHandle[0], TIM_FLAG_CC1) != RESET) { if(__HAL_TIM_GET_ITSTATUS(&TimHandle[0], TIM_IT_CC1) !=RESET) { { __HAL_TIM_CLEAR_IT(&TimHandle[0], TIM_IT_CC1); encoderSpeed[0] = HAL_TIM_ReadCapturedValue(&TimHandle[0], TIM_CHANNEL_1); TIM17->CNT = 0; } } } if(__HAL_TIM_GET_FLAG(&TimHandle[0], TIM_FLAG_UPDATE) != RESET) { if(__HAL_TIM_GET_ITSTATUS(&TimHandle[0], TIM_IT_UPDATE) !=RESET) { __HAL_TIM_CLEAR_IT(&TimHandle[0], TIM_IT_UPDATE); encoderSpeed[0] = PERIOD; // If timer expires we have got no pulse during measurment period, set max time } } }
/* USER CODE BEGIN 0 */ void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim) { uint8_t i; uint16_t temp; temp = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_1); if ((temp > 5000) && (!data_ready)) { pointer = 0; for (i = 0; i < 4; i++) { if (captured_value[i] < 1000) captured_value[i] = 1000; else if (captured_value[i] > 2000) captured_value[i] = 2000; rc_data[i] = captured_value[i]-1000; }; rc_data[4] = 0; if (captured_value[4] > 1500) rc_data[4] |= (1<<4); if (captured_value[5] > 1500) rc_data[4] |= (1<<5); if (captured_value[6] > 1500) rc_data[4] |= (1<<6); if (captured_value[7] > 1500) rc_data[4] |= (1<<7); data_ready = 1; } else { captured_value[pointer] = temp; pointer++; }; if (pointer == 8) pointer = 0; };
/** * @brief Output Compare callback in non blocking mode * @param htim: TIM OC handle * @retval None */ void HAL_TIM_OC_DelayElapsedCallback(TIM_HandleTypeDef *htim) { uint32_t capture = 0; /* Get the TIM4 Input Capture 1 value */ capture = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_1); /* Set the TIM4 Capture Compare1 Register value */ __HAL_TIM_SET_COMPARE(htim, TIM_CHANNEL_1, (CCR1Val + capture)); }
void detect(DETECTORSTATUS *st, TIM_HandleTypeDef *htim) { int8_t shift; int16_t diff; uint16_t lastDelta; uint16_t capturedValue; if (htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1) { capturedValue = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_1); if(st->initialized) { lastDelta = capturedValue - st->prevCapture; st->accumulator += lastDelta; //st->debug[st->counter] = lastDelta; if(++st->counter == g_config.mccount) { //HAL_GPIO_TogglePin(ACTIVE1_GPIO_Port, ACTIVE1_Pin); st->lastMeasured = st->accumulator; st->accumulator = 0; st->counter = 0; if(!st->sum) st->sum = (uint32_t)st->lastMeasured << g_config.sumshift; else { st->avg = st->sum >> g_config.sumshift; st->threshold = st->avg / g_config.thdiv; st->diff = (int32_t)st->lastMeasured - st->avg; st->tolerance = st->threshold >> SHIFT_TOLERANCE; diff = -st->diff; if (diff < 0) st->state = st->tolerance + diff < 0 ? BELOW:BASE; else if(diff < st->tolerance) st->state = BASE; else if(diff < st->threshold) st->state = ABOVE; else if(st->state < ACTIVE) { //!ACTIVE && !TIMEOUT st->activeStart = HAL_GetTick(); st->state = ACTIVE; } else if(HAL_GetTick() - st->activeStart > g_config.tlimit * 1000) st->state = TIMEOUT; shift = g_config.shifts[st->state]; if(shift != SCHAR_MIN) { st->correction = (shift >= 0) ? (((int32_t)st->diff) << shift) : (((int32_t)st->diff) >> -shift); st->sum += st->correction; } else
/** * @brief Input Capture callback in non blocking mode * @param htim : TIM IC handle * @retval None */ void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim) { /* Get the Input Capture value */ tmpCC4[uwCaptureNumber++] = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_4); if (uwCaptureNumber >= 2) { /* Compute the period length */ uwPeriodValue = (uint16_t)(0xFFFF - tmpCC4[0] + tmpCC4[1] + 1); uwMeasurementDone = 1; uwCaptureNumber = 0; } }
/** * @brief Conversion complete callback in non blocking mode * @param htim : hadc handle * @retval None */ void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim) { if (htim->Channel == HAL_TIM_ACTIVE_CHANNEL_2) { if(uhCaptureIndex == 0) { /* Get the 1st Input Capture value */ uwIC2Value1 = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_2); uhCaptureIndex = 1; } else if(uhCaptureIndex == 1) { /* Get the 2nd Input Capture value */ uwIC2Value2 = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_2); /* Capture computation */ if (uwIC2Value2 > uwIC2Value1) { uwDiffCapture = (uwIC2Value2 - uwIC2Value1); } else if (uwIC2Value2 < uwIC2Value1) { /* 0xFFFF is max TIM3_CCRx value */ uwDiffCapture = ((0xFFFF - uwIC2Value1) + uwIC2Value2) + 1; } else { /* If capture values are equal, we have reached the limit of frequency measures */ Error_Handler(); } /* Frequency computation: for this example TIMx (TIM3) is clocked by 2xAPB1Clk */ uwFrequency = (2*HAL_RCC_GetPCLK1Freq()) / uwDiffCapture; uhCaptureIndex = 0; } } }
/** * Callback function implementation. * @param htim timer handle which trigger the callback */ void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim) { static uint8_t steerRise = 0; if (htim->Instance == TIM4) { if (htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1) { /* TIM4 Channel 1 (Motor input) */ uwIC1Value = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_1); } else if (htim->Channel == HAL_TIM_ACTIVE_CHANNEL_3) { /* TIM4 Channel 3 (Steer input) */ uwIC2Value = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_3) - uwIC1Value; steerRise = 1; if (_signalDetected == RESET) { _signalDetected = SET; if (_servo == ENABLE) HAL_TIM_PWM_Start_IT(&htim4, TIM_CHANNEL_4); } /* Connect to servo output */ if (_connected == ENABLE) { BSP_Radio_SetSteer(uwIC2Value - 1500); } } else if (htim->Channel == HAL_TIM_ACTIVE_CHANNEL_2) { /* TIM4 Channel 2 (Motor input, fall edge) */ if (!steerRise && _signalDetected == SET) { _signalDetected = RESET; HAL_TIM_PWM_Stop(&htim4, TIM_CHANNEL_4); } else if (steerRise) { steerRise = 0; } } } }
/** * @brief Input Capture callback in non blocking mode * @param htim : TIM IC handle * @retval None */ void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim) { if (htim->Channel == HAL_TIM_ACTIVE_CHANNEL_2) { /* Get the Input Capture value */ uwIC2Value = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_2); if (uwIC2Value != 0) { /* Duty cycle computation */ uwDutyCycle = ((HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_1)) * 100) / uwIC2Value; /* uwFrequency computation TIM4 counter clock = (RCC_Clocks.HCLK_Frequency)/2 */ uwFrequency = (HAL_RCC_GetHCLKFreq())/2 / uwIC2Value; } else { uwDutyCycle = 0; uwFrequency = 0; } } }
/** * @brief Input Capture callback in non blocking mode * @param htim : TIM IC handle * @retval None */ void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim) { /* Get the Input Capture value */ tmpCC4[uwCaptureNumber++] = HAL_TIM_ReadCapturedValue(&Input_Handle, TIM_CHANNEL_1); if (uwCaptureNumber >= 2) { /* Compute the period length */ uwPeriodValue = (uint16_t)(0xFFFF - tmpCC4[0] + tmpCC4[1] + 1); /* Frequency computation */ uwLsiFreq = (uint32_t) SystemCoreClock / uwPeriodValue; uwLsiFreq *= 8; } }
/** * @brief This function handles TIM5 global interrupt request. * @param None * @retval None */ void TIM5_IRQHandler(void) { HAL_TIM_IRQHandler(&TimInputCaptureHandle); { /* Get the Input Capture value */ tmpCC4[uwCaptureNumber++] = HAL_TIM_ReadCapturedValue(&TimInputCaptureHandle, TIM_CHANNEL_4); if (uwCaptureNumber >= 2) { /* Compute the period length */ uwPeriodValue = (uint16_t)(0xFFFF - tmpCC4[0] + tmpCC4[1] + 1); uwMeasurementDone = 1; uwCaptureNumber = 0; } } }
// Timer 1 capture compare interrupt void TIM1_CC_IRQHandler(void) { if(__HAL_TIM_GET_FLAG(&TimHandle[1], TIM_FLAG_CC2) != RESET) { if(__HAL_TIM_GET_ITSTATUS(&TimHandle[1], TIM_IT_CC2) !=RESET) { { __HAL_TIM_CLEAR_IT(&TimHandle[1], TIM_IT_CC2); encoderSpeed[1] = HAL_TIM_ReadCapturedValue(&TimHandle[1], TIM_CHANNEL_2); TIM1->CNT = 0; } } } }
/** * @brief This function handles TIM16 global interrupt request. * @param None * @retval None */ void TIM14_IRQHandler(void) { HAL_TIM_IRQHandler(&Input_Handle); { /* Get the Input Capture value */ tmpCC4[uwCaptureNumber++] = HAL_TIM_ReadCapturedValue(&Input_Handle, TIM_CHANNEL_1); if (uwCaptureNumber >= 2) { /* Compute the period length */ uwPeriodValue = (uint16_t)(0xFFFF - tmpCC4[0] + tmpCC4[1] + 1); /* Frequency computation */ uwLsiFreq = (uint32_t) SystemCoreClock / uwPeriodValue; uwLsiFreq *= 8; } } }
/** * @brief Output Compare callback in non blocking mode * @param htim: TIM OC handle * @retval None */ void HAL_TIM_OC_DelayElapsedCallback(TIM_HandleTypeDef *htim) { uint32_t capture = 0; if(LEDsState == LED3_TOGGLE) { /* Toggling LED3 */ BSP_LED_Toggle(LED3); BSP_LED_Off(LED6); BSP_LED_Off(LED4); } else if(LEDsState == LED4_TOGGLE) { /* Toggling LED4 */ BSP_LED_Toggle(LED4); BSP_LED_Off(LED6); BSP_LED_Off(LED3); } else if(LEDsState == LED6_TOGGLE) { /* Toggling LED6 */ BSP_LED_Off(LED3); BSP_LED_Off(LED4); BSP_LED_Toggle(LED6); } else if(LEDsState == STOP_TOGGLE) { /* Turn ON LED6 */ BSP_LED_On(LED6); } else if(LEDsState == LEDS_OFF) { /* Turn OFF all LEDs */ BSP_LED_Off(LED3); BSP_LED_Off(LED4); BSP_LED_Off(LED5); BSP_LED_Off(LED6); } /* Get the TIM4 Input Capture 1 value */ capture = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_1); /* Set the TIM4 Capture Compare1 Register value */ __HAL_TIM_SET_COMPARE(htim, TIM_CHANNEL_1, (CCR1Val + capture)); }
/** * @brief Input Capture callback in non blocking mode * @param htim : TIM4 IC handle * @retval None */ void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim) { /* Get the converted value */ ICReadValue = HAL_TIM_ReadCapturedValue(htim,TIM_CHANNEL_2); /* Increment the counter */ ICCounter++; /* sum up the samples */ SumICReadValue += ICReadValue; if(ICCounter > 255) { /* Compute the average value for 256 samples */ AvrgICReadValue = SumICReadValue/256; ICCounter = 0; SumICReadValue = 0; } }
int main(void) { HAL_Init(); Nucleo_BSP_Init(); MX_TIM1_Init(); HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_1); /* Connect a LED to PA8 pin to see the fading effect */ uint16_t dutyCycle = HAL_TIM_ReadCapturedValue(&htim1, TIM_CHANNEL_1); while(1) { while(dutyCycle < __HAL_TIM_GET_AUTORELOAD(&htim1)) { __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, ++dutyCycle); HAL_Delay(1); } while(dutyCycle > 0) { __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, --dutyCycle); HAL_Delay(1); } } }
// Output Compare callback in non blocking mode void HAL_TIM_OC_DelayElapsedCallback(TIM_HandleTypeDef *htim) { if (ledState == LED3_TOGGLE) { BSP_LED_Toggle(LED3); BSP_LED_Off(LED6); BSP_LED_Off(LED4); } else if (ledState == LED4_TOGGLE) { BSP_LED_Toggle(LED4); BSP_LED_Off(LED6); BSP_LED_Off(LED3); } else if (ledState == LED6_TOGGLE) { BSP_LED_Off(LED3); BSP_LED_Off(LED4); BSP_LED_Toggle(LED6); } else if (ledState == STOP_TOGGLE) { BSP_LED_On(LED6); } else if (ledState == LEDS_OFF) { led_all_off(); } /* Get the TIM4 Input Capture 1 value */ uint32_t capture = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_1); /* Set the TIM4 Capture Compare1 Register value */ __HAL_TIM_SET_COMPARE(htim, TIM_CHANNEL_1, (CCR1Val + capture)); }
/** * @brief Timer Capture Callback function for Frequency Calculations */ void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim) { __HAL_TIM_DISABLE(htim); if(htim->Instance == TIM9 || htim->Instance == TIM12) { /* Get the Input Capture value */ // if(tim9_12_flag == ENABLE) uwIC2Value = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_1); // else // uwIC2Value = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_2); if (uwIC2Value != 0) { /* Duty cycle computation */ // if(tim9_12_flag == ENABLE) uwDutyCycle = ((HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_2)) * 100) / uwIC2Value; // else // uwDutyCycle = ((HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_1)) * 100) / uwIC2Value; /* uwFrequency computation TIM4 counter clock = (RCC_Clocks.HCLK_Frequency)/2 */ if(htim->Instance == TIM9) uwFrequency1 = (168000000UL)/(TimCapPsc+1) / uwIC2Value; else if(htim->Instance == TIM12) uwFrequency1 = (168000000UL)/(2*(TimCapPsc+1)) / uwIC2Value; } else { uwDutyCycle = 0; uwFrequency1 = 0; } } else if(htim->Instance == TIM10 || htim->Instance == TIM11 || htim->Instance == TIM14 || htim->Instance == TIM13) { if(uhCaptureIndex == 0) { /* Get the 1st Input Capture value */ uwIC2Value1 = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_1); uhCaptureIndex = 1; } else if(uhCaptureIndex == 1) { /* Get the 2nd Input Capture value */ uwIC2Value2 = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_1); /* Capture computation */ if (uwIC2Value2 > uwIC2Value1) { uwDiffCapture = (uwIC2Value2 - uwIC2Value1); } else /* (uwIC2Value2 <= uwIC2Value1) */ { uwDiffCapture = ((TimCapPeriod - uwIC2Value1) + uwIC2Value2); } /* Frequency computation: for this example TIMx (TIM1) is clocked by 2xAPB2Clk */ uwFrequency = (2*HAL_RCC_GetPCLK2Freq()) / uwDiffCapture; if(htim->Instance == TIM10 || htim->Instance == TIM11) uwFrequency = uwFrequency/(TimCapPsc+1); else uwFrequency = uwFrequency/(2*(TimCapPsc+1)); uhCaptureIndex = 0; } } __HAL_TIM_ENABLE(htim); }
void DHT22_InterruptHandler(DHT22_HandleTypeDef* handle) { uint16_t val = HAL_TIM_ReadCapturedValue(&handle->timHandle, handle->timChannel); uint32_t freq = HAL_RCC_GetPCLK2Freq(); uint16_t val2; if (val > handle->lastVal) val2 = val - handle->lastVal; else val2 = 65535 + val - handle->lastVal; handle->lastVal = val; float time = 1000000.0 * val2 / freq; if (handle->bitPos < 0) { if (time > 155.0 && time < 165.0) { handle->bitPos = 0; } } else if (handle->bitPos >= 0 && handle->bitPos < 40) { if (time > 78.0 && time < 97.0) { handle->bitsRX[handle->bitPos / 8] &= ~(1 << (7 - handle->bitPos % 8)); handle->bitPos++; } else if (time > 120.0 && time < 145.0) { handle->bitsRX[handle->bitPos / 8] |= 1 << (7 - handle->bitPos % 8); handle->bitPos++; } else { handle->bitPos = -1; HAL_TIM_IC_Stop_IT(&handle->timHandle, handle->timChannel); handle->state = DHT22_READY; } } if(handle->bitPos==40){ handle->bitPos = -1; //TODO This was changed (commented out) recently i'm not sure why HAL_TIM_IC_Stop_IT(&handle->timHandle, handle->timChannel); uint8_t sum = 0; for (int i = 0; i < 4; i++) { sum += handle->bitsRX[i]; } if (sum == handle->bitsRX[4]) { handle->crcErrorFlag = 0; int16_t temp10 = 0; if ((handle->bitsRX[2] & 0x80) == 0x80) { temp10 |= (handle->bitsRX[2] & 0x7F) << 8; temp10 |= handle->bitsRX[3]; temp10 *= -1; } else { temp10 |= handle->bitsRX[2] << 8; temp10 |= handle->bitsRX[3]; } handle->temp = 0.1 * temp10; int16_t hum10 = 0; if ((handle->bitsRX[0] & 0x80) == 0x80) { hum10 |= (handle->bitsRX[0] & 0x7F) << 8; hum10 |= handle->bitsRX[1]; hum10 *= -1; } else { hum10 |= handle->bitsRX[0] << 8; hum10 |= handle->bitsRX[1]; } handle->hum = 0.1 * hum10; } else { handle->crcErrorFlag = 1; } handle->state = DHT22_RECEIVED; } }