Quadrature Decoding with MCUXpresso SDK2.2 on FRDM-K66F

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Quadrature Decoding with MCUXpresso SDK2.2 on FRDM-K66F

1,257 Views
philipp_zh
Contributor II

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!

1 Reply

857 Views
richishion
Contributor I

so, I do not consider you can compile completly,If you  find demo project of Quadrature Decoder soft ,it will save much more time,Good luckly!