LPTMR as Pulse Counter Problem

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

LPTMR as Pulse Counter Problem

跳至解决方案
1,622 次查看
yar
Contributor III

Hi, I've got a custom MKL15Z128VFT4 board and trying to use LPTMR0 as pulse counter. Here is my code:

  unsigned int compare_value = 1;

  serial.baud(115200);

  /* Clock the timer */

    SIM->SCGC5 |= SIM_SCGC5_LPTMR_MASK;

  /* reset the counter */

  LPTMR0->CSR = 0;

  /* turn on portc */

  SIM->SCGC5 |= SIM_SCGC5_PORTC_MASK; //Turn on PORTC clock

    PORTC->PCR[5] = PORT_PCR_MUX(0x4); //Use ALT2 on PTC5

  LPTMR0->PSR = LPTMR_PSR_PCS(0x1) | LPTMR_PSR_PBYP_MASK; //Use LPO clock but bypass glitch filter

  LPTMR0->CMR = LPTMR_CMR_COMPARE(compare_value);  //Set compare value

  /* set pulse mode */

  LPTMR0->CSR = LPTMR_CSR_TPS(0x2) | LPTMR_CSR_TMS_MASK; //Set LPT to use the pin selected, and put in pulse count mode, on rising edge (default)

  /* start pulse counter */

  LPTMR0->CSR |= LPTMR_CSR_TEN_MASK;

  while(1) {

       if(LPTMR0->CNR != 0)

            printf("Current value of pulse count register CNR is %d\r\n", LPTMR0->CNR);

  }

I followed an example here http://peninsula.googlecode.com/svn/trunk/Kinetis512/kinetis-sc/src/projects/lptmr/lptmr_pulse.c

also tried to use PORT_PCR_MUX(0x3) and even PORT_PCR_MUX(0x2) with no luck - the value of LPTMR0_CNR is always 0.

Did I miss something? Will appreciate any help.

标签 (2)
标记 (2)
0 项奖励
1 解答
875 次查看
yar
Contributor III

Thanks to Kan_Li, but my problem got a different solution. According to reference manual, to read CNR register, you need to write something first where. What's why my value while printing was always zero.

在原帖中查看解决方案

0 项奖励
4 回复数
876 次查看
yar
Contributor III

Thanks to Kan_Li, but my problem got a different solution. According to reference manual, to read CNR register, you need to write something first where. What's why my value while printing was always zero.

0 项奖励
875 次查看
Kan_Li
NXP TechSupport
NXP TechSupport

Hi Yar,

Have you enabled the interrupt and clear TCF flag in the ISR? Because on overflow event, CNR is reset whenever TCF is set.

Hope that helps,


Have a great day,
Kan

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

0 项奖励
875 次查看
yar
Contributor III

Hi Kan,

Thanks for your reply, you were right, I didn't enable the interrupt and didn't clear the TFC flag. You ment TFC, not TCF, yeah? Because, according to reference manual, TFC flag is responsible for CNR reset, not TCF.

But there is still no luck and CNR is still remains zero =(

Here is the full code:

  unsigned int compare_value = 1000;

  serial.baud(115200);

  __enable_irq();

  NVIC_EnableIRQ(LPTimer_IRQn);

  /* Clock the timer */

    SIM->SCGC5 |= SIM_SCGC5_LPTMR_MASK;

  /* reset the counter */

  LPTMR0->CSR &= ~LPTMR_CSR_TEN_MASK;

  /* turn on portc */

  SIM->SCGC5 |= SIM_SCGC5_PORTC_MASK; //Turn on PORTC clock

  PORTC->PCR[5] = PORT_PCR_MUX(0x4); //Use ALT2 on PTC5

  LPTMR0->PSR = LPTMR_PSR_PCS(3) | LPTMR_PSR_PBYP_MASK; //Use LPO clock but bypass glitch filter

  LPTMR0->CMR = LPTMR_CMR_COMPARE(compare_value);  //Set compare value

  /* set pulse mode */

  LPTMR0->CSR = LPTMR_CSR_TPS(0x2) | LPTMR_CSR_TMS_MASK; //Set LPT to use the pin selected, and put in pulse count mode, on rising edge (default)

  /* disable timer compare */

  LPTMR0->CSR &= ~LPTMR_CSR_TFC_MASK;

  /* Enable interrupt */

  LPTMR0->CSR |= LPTMR_CSR_TIE_MASK;

  /* start pulse counter */

  LPTMR0->CSR |= LPTMR_CSR_TEN_MASK;

  while(1) {

  if(LPTMR0->CNR != 0)

       printf("Current value of pulse count register CNR is %d\r\n", LPTMR0->CNR);

  }

Didn't I miss something?

0 项奖励
875 次查看
Kan_Li
NXP TechSupport
NXP TechSupport

Hi Yar,

The flag to be cleared in ISR is TCF bit, TFC bit is for selecting the case when CNR should be reset, you may fetch details from the RM like the following:

1.png

2.png

so you should clear TFC flag when initialize LPTMR, and in the ISR write one to TCF flag to let CNR continue to count instead of resetting.

Hope that helps,


Have a great day,
Kan

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

0 项奖励