Philipp Reist

Quadrature Decoding with MCUXpresso SDK2.2 on FRDM-K66F

Discussion created by Philipp Reist on May 6, 2017
Latest reply on Jun 16, 2017 by 旭升 李

Just figured this out, might be helpful to someone else. The following assumes that you mux'd the pins correctly, so that the quadrature encoder is hooked up to the FTM's phase A and B inputs.

I am setting the counter to 30000 so that the motor can move in either direction without an overflow.

 

// config FTM 2 for quadrature decoding

// Init FTM2
ftm_config_t flex_config;
FTM_GetDefaultConfig(&flex_config);
FTM_Init(FTM2, &flex_config);

// set MOD, which is overflow value
FTM2->MOD = 60000U;

// set CNTIN, set counter to CNTIN
FTM2->CNTIN = 30000U;
FTM2->CNT = 1U; // specific value doesn't matter

// then set cntin to 0, so that counter can decrease below value set above
FTM2->CNTIN = 0U;

// And init quadrature decoding, did not experiment with filter value too much
ftm_phase_params_t ftm_phase_par;
ftm_phase_par.enablePhaseFilter = true;
// signal needs to be consistent for phaseFilterVal * 4 * sys_tick
ftm_phase_par.phaseFilterVal = 8;
ftm_phase_par.phasePolarity = kFTM_QuadPhaseNormal;

// function "starts" timer, too.
FTM_SetupQuadDecode(FTM2, &ftm_phase_par, &ftm_phase_par, kFTM_QuadPhaseEncode);

Note that if you want to set the counter value as above, you must first stop the quad decoding. E.g. as follows:

// stop quad decoding
FTM2->QDCTRL &= (~1U);

// set CNTIN and set counter to cntin
FTM2->CNTIN = 30000U;
FTM2->CNT = 1U; // specific value doesn't matter

// then set cntin to 0, so that counter can decrease below value set above
FTM2->CNTIN = 0U;

// start quad decoding
FTM2->QDCTRL |= 1U;

Feedback welcome, of course!

Outcomes