2023-09-05 02:52 AM
I want to measure the time period between each hall state
I configured timer 3 in XORmode so that every change in hall state interrupt will occur, based on this i implemented this code but the period measurement is incorrect.
comment for discussion, please feel free to ask questions to solve issue
code for period measurement
void TIM3_IRQHandler(void)
{
/* USER CODE BEGIN TIM3_IRQn 0 */
/* USER CODE END TIM3_IRQn 0 */
/* USER CODE BEGIN TIM3_IRQn 1 */
if (0U == LL_TIM_IsActiveFlag_UPDATE(TIM3))
{
}
else
{
uint8_t ResetHall=((uint8_t)
((LL_GPIO_IsInputPinSet( HALL_3_GPIO_Port, HALL_3_Pin ) << 2)
|(LL_GPIO_IsInputPinSet( HALL_2_GPIO_Port, HALL_2_Pin ) << 1)
| LL_GPIO_IsInputPinSet( HALL_1_GPIO_Port, HALL_1_Pin ) ));
LL_TIM_ClearFlag_UPDATE(TIM3);
reset_hall(resetHall);
OverFlowCounter +=1;
}
if (LL_TIM_IsActiveFlag_CC1 (TIM3)!=0U)
{
uint8_t updateHall=((uint8_t)
((LL_GPIO_IsInputPinSet( HALL_3_GPIO_Port, HALL_3_Pin ) << 2)
|(LL_GPIO_IsInputPinSet( HALL_2_GPIO_Port, HALL_2_Pin ) << 1)
| LL_GPIO_IsInputPinSet( HALL_1_GPIO_Port, HALL_1_Pin ) ));
update_hall(updateHall);
LL_TIM_ClearFlag_CC1(TIM3);
if (startCapture==0)
{
PastValue=LL_TIM_IC_GetCaptureCH1(TIM3);
startCapture=1;
}
else if(startCapture==1)
{
currentValue=LL_TIM_IC_GetCaptureCH1(TIM3);
if (currentValue>PastValue)
{
temp1=(currentValue-PastValue);
}
else if (currentValue<PastValue){
temp1=(0xFFFF-PastValue)+currentValue;
// temp1=PastValue-currentValue;
}
float ref_clock=(timerFrequency/(timerPrescaler));
temp2=ref_clock/temp1;
periodBuffer[bufferIndex] = temp2;
bufferIndex = (bufferIndex + 1) % NUM_SAMPLES;
float periodAverage = 0;
for (uint8_t i = 0; i < NUM_SAMPLES; i++) {
periodAverage += periodBuffer[i];}
periodAverage /=NUM_SAMPLES;
temp3= 1/periodAverage;
startCapture=0;
}
}
else
{
//Nothing to do
}
/* USER CODE END TIM3_IRQn 1 */
}