I'm trying to read the quadrature signals generated by motor's encoder.
I implemented a solution similar to the example "ftm_quad_decoder" with the difference that I use a global variable to add the values read to each timer interrupt, and instead of PIT timer I use FTM timer which trips very quickly (every 100 ms) so as not to have overflow problems.
This is my code:
void EncoderTimerInit(MotorStatus* motorStatus) {
hal_timer_handle_t halTimerHandle = (hal_timer_handle_t) (&halTimerHandleBuffer[0]);
hal_timer_config_t halTimerConfig;
halTimerConfig.timeout = 100;
halTimerConfig.srcClock_Hz = CLOCK_GetBusClkFreq();
halTimerConfig.instance = 1;
HAL_TimerInit(halTimerHandle, &halTimerConfig);HAL_TimerInstallCallback(halTimerHandle, &TimerCallBack, (void*) (motorStatus));
HAL_TimerEnable(halTimerHandle);
}void TimerCallBack(void* param)
{
MotorStatus* motorStatus = (MotorStatus*) param;
volatile int16_t value = 0;
value = FTM_GetQuadDecoderCounterValue(QUAD_DECODER_COUNTER);
FTM_ClearQuadDecoderCounterValue(QUAD_DECODER_COUNTER);
encoder_count += value;}
Unfortunately I have seen that in this way the value read is quite far from what is expected.
Could it be that instructions FTM_GetQuadDecoderCounterValue e FTM_ClearQuadDecoderCounterValue take some time so while motors are in movement I lose some values?
Is better to leave timer free running (MOD = 0xFFFF) and use overlap callback?
In this case how a can how can I understand the verse of the overflow? I saw in the "ftm_quad_decoder" example are defined several variables (dir_when_overflow, counter_overflow_flag, counter_overflow_count) that are defined but now used, therefore not showing this type of use...
Thank you very much!
Hello Fabio,
Could you please let me know which device are you working on?
Regards,
Felipe