FRDM-MCXC242 TPM0 Timer OverFlow Capture

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 

FRDM-MCXC242 TPM0 Timer OverFlow Capture

1,348 次查看
SvenAsbach
Contributor II

I am not able to bring the TPM0 overflow flag set.
TOIE is set in register, Interrupts are active and capturing works fine.

When I for example 2Hz I get a capture value with 62900, when I reduce the frequency to 1.8Hz I get 4100 counts and the TOF will not be set.
When raising the frequency everything works fine and the capture is correct.

******************* Code initialisation ****************

tpm_config_t tpmInfo;

uint32_t helper;

 

helper = SIM->SCGC6; //enable TPM0 clock

helper |= 0x1000000;

SIM->SCGC6 = helper;

 

TPM_StopTimer(TPM0_PERIPHERAL);

 

TPM_GetDefaultConfig(&tpmInfo);

tpmInfo.useGlobalTimeBase = false;

tpmInfo.prescale = kTPM_Prescale_Divide_4;

tpmInfo.syncGlobalTimeBase = false;

tpmInfo.triggerSelect = kTPM_Trigger_Select_0;

tpmInfo.triggerSource = kTPM_TriggerSource_External;

tpmInfo.extTriggerPolarity = kTPM_ExtTrigger_Active_High;

tpmInfo.chnlPolarity = 0U;

 

TPM_Init(TPM0_PERIPHERAL, &tpmInfo);

 

/* Setup input capture on a TPM channel */

TPM_SetupInputCapture(TPM0_PERIPHERAL, kTPM_Chnl_2, kTPM_RiseAndFallEdge);

 

/* Set the timer to be in free-running mode */

TPM_SetTimerPeriod(TPM0_PERIPHERAL, TPM_MAX_COUNTER_VALUE(TPM0_PERIPHERAL));

 

/* Enable channel interrupt  */

TPM_EnableInterrupts(TPM0_PERIPHERAL, kTPM_Chnl2InterruptEnable | kTPM_TimeOverflowInterruptEnable);

 

/* Enable at the NVIC */

EnableIRQ(TPM0_IRQn);

 

TPM_StartTimer(TPM0_PERIPHERAL, kTPM_SystemClock);

******************************************

标签 (1)
0 项奖励
回复
5 回复数

1,326 次查看
xiangjun_rong
NXP TechSupport
NXP TechSupport

Hi,

Regarding your question, I suppose that you do not need to set the TOIE and have the TOF fire an interrupt based on your application. You just need to enable the capture event to fire an interrupt.

In the TPM0 ISR, you can read the capture value in the corresponding TPM0_CnV, then check the TOF bit, if the TOF bit is set, then you can compute the time.

time=TPM0_CnV+MOD register-TPM0_CnV_Previous.

then clear the TOF bit by setting the bit.

If the TOF is not set,

time=TPM0_CnV-TPM0_CnV_Previous.

Hope it can help you

BR

XiangJun Rong

xiangjun_rong_0-1727231321744.png

 

0 项奖励
回复

1,314 次查看
SvenAsbach
Contributor II

nice idea, but as I wrote, TOF will never be set.
How should I check TOF when this flag will never be set?

The TOF must be checked to be sure the result is correct, an overflow result is by sure wrong.

 

Best regards

SvenAsbach

0 项奖励
回复

1,310 次查看
xiangjun_rong
NXP TechSupport
NXP TechSupport

Hi,

For TPM0 module, there is only one interrupt vector, so all the interrupt sources of TPM0 share the same ISR. In the ISR, you can check the TOF, if it is 1, you can write a flag. Then check if the flag is set or not.

BR

XiangJun Rong

 

0 项奖励
回复

1,306 次查看
SvenAsbach
Contributor II

In the register is never written TOF to "1"!

As you can see in the picture, TOF is not set even when an timer overflow occurs.

SvenAsbach_0-1727251284366.png

 

0 项奖励
回复

1,289 次查看
SvenAsbach
Contributor II

I have changed the clocksource from MCGIRCLK to MCGPCLK and it seems to work.
This clock is faster then I wanted.

0 项奖励
回复