setting up counter of the quadrature decoder in software

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

setting up counter of the quadrature decoder in software

902 Views
alexeyusoltsev
Contributor IV

Hi guys!

I've got an issue. I want to set up CNT value in the code. And now I can't get the CNT of QD to be certain value  after initialization. If i write something to it - nothing changes. I've tryed to set CNINT value and after initialization change it. It leads to immediate change to CNTINT after i synchronize CNTINT buffer.

FTM_HAL_SetCounterInitVal(FTM1_BASE_PTR,2550); (previously set to 4000)

FTM_HAL_SetSoftwareTriggerCmd(FTM1,true);

(synchronization by software is setted up in the initialisation block)

The problem is that CNT value rules my PWM on the other channel and it is not safe to start from CNTINT and need to be started more like from MOD value.

Any ideas?

Best regards,

Alexey.

0 Kudos
6 Replies

627 Views
xiangjun_rong
NXP TechSupport
NXP TechSupport

Hi, Alexey,

I think the FTMx_CNT can not be written by any value. Pls refer to the following description from RM:

BR

XiangJun Rong

"

26.3.4 Counter (FTMx_CNT)

The CNT register contains the FTM counter value.

Reset clears the CNT register. Writing any value to COUNT updates the counter with its

initial value, CNTIN.

When Debug is active, the FTM counter is frozen. This is the value that you may read.

"

627 Views
alexeyusoltsev
Contributor IV

Hi again!

According to RefMan yes but I had a hope that there is a way. So i did try to trick with CNINIT update. It's very sad since it makes QD part of FTM useless for HMI rotary encoders.

So MOD=1 and process my variable in the interrupt is my only option? Or are there any other idea?

Thx for responses!

0 Kudos

627 Views
xiangjun_rong
NXP TechSupport
NXP TechSupport

Hi, Alexey,

It is sure that user is not allowed to write the COUNT directly.

As the RM says "Writing any value to COUNT updates the counter with its initial value CNTIN.", I think you can have a workaround. Firstly, you can write the CNTIN register, then using   FTM_HAL_SetSyncMode(FTM1,kFtmUseSoftwareTrig); to synchronize the CNTINIT register, then write any value to COUNT register so that the COUNT is initialized by the new CNTIN value. afterwords, you can rewrite the CNTIN register with original value and synchronize it again so that the CNTIN can restore it default value.

Hope it can help you.

BR

XiangJun Rong

627 Views
alexeyusoltsev
Contributor IV

Hi,XiangJun Rong,

Had the same think but it had not worked. After re-write to CNTINIT it account that action as attempt to write something to COUNT so it restarts COUNT to new CNTININT immediately:(

627 Views
xiangjun_rong
NXP TechSupport
NXP TechSupport

Hi, Alexey,

Frankly speaking, I am not clear about what you would like to do with FTM.

If you want to use the FTM to decode the encoder signal:PhaseA and PhaseB, which have 90 degree phase shift, you have to check which pin is the encoder signal input pins in Signal Multiplexing and Signal Descriptions in the reference manual, for example, FTMx_QD_PHB, FTMx_QD_PHA.  You have to connect the PhaseA and PhaseB signals of encoder to the two pins (FTM2_QD_PHB, FTM2_QD_PHA) or (FTM1_QD_PHB, FTM1_QD_PHA), after you configure the FTM in quadrature  decoder mode, the FTM counter will count the rising/falling edges of both phaseA and phaseB signals, you can set the FTM_MOD and enable FTM overflow interrupt so that you can switch stator in the ISR of FTM overflow interrupt after a defined number of edges has reached. In the case, it is unnecessary to change the CNINIT register.

I suggest you refer to an5142.pdf from the website, it has the code and explanation:

http://tinyurl.com/ogvhlny

Hope it can help you.

BR

XiangJun Rong

0 Kudos

627 Views
alexeyusoltsev
Contributor IV

Hi XiangJun Rong,

Thank you for your response! I got no problems with initialisation and work with QD exept i Cant write to FTM CNT register a value. I am using encoder with push button as a HMI. My INI-code. When timer overflows it goes to CNINiT and when underflow it goes to MOD-it is as predicted. But i want to have an FTM_CNT at a defined by software value at some moments, particulary in the beggining.

My code is:

//init QD on FTM1

  SIM_SCGC6|=SIM_SCGC6_FTM1_MASK;

  FTM1_MODE |= FTM_MODE_WPDIS_MASK;

  FTM1_MODE |= FTM_MODE_FTMEN_MASK;

  FTM1_CONF |= FTM_CONF_BDMMODE(3);

  FTM1_MOD = 4500;

  FTM_HAL_SetCounterInitVal(FTM1_BASE_PTR,2550);

  FTM_HAL_SetSyncMode(FTM1,kFtmUseSoftwareTrig);

  FTM1_QDCTRL |= FTM_QDCTRL_QUADEN_MASK;

  FTM1_SC |= FTM_SC_CLKS(3);

  FTM_HAL_SetClockPs(FTM1_BASE_PTR,kFtmDividedBy1);

  FTM_HAL_SetQuadPhaseAPolarity(FTM1_BASE_PTR,1);

  FTM_HAL_SetQuadPhaseBPolarity(FTM1_BASE_PTR,1);

And i had a hope that that code can do it:

FTM_HAL_SetCounter(FTM1,4000);

FTM_HAL_SetSoftwareTriggerCmd(FTM1,true);

Ofc. I can set up MOD=1 and CNINiT=0 and use interrupts to count my value using TOF and TOFDIR bits but it is not attractive solution since it uses my CPU more often.

0 Kudos