K64F LPTMR as pulse counter not working as expected

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

K64F LPTMR as pulse counter not working as expected

Jump to solution
1,231 Views
arminbego
Contributor II

Hello,

I am trying to implement a pulse counter by using LPTMR0 of MK64FN1M0VLL12 chip. I want to use PTC5 pin for pulse counting. Here is my code:

//enable clock for PORTC

SIM->SCGC5 |= (1UL << 11);

//set PORTC pin 5 as pulse counter pin (PIN ALT3: LPTMR0_ALT2)

PORTC->PCR[5] = (1UL << 9) | (1UL << 8);

//enable clock for LPTMR0

SIM->SCGC5 |= (1UL << 0);

// reset LPTMR

LPTMR0->CSR = 0;

LPTMR0->PSR = 0;

LPTMR0->CMR = 0; // no need for compare value because i will use counter in free running mode

//select LPTMR0_ALT2 (PTC5) as pulse counter pin

LPTMR0->CSR |= (1 << 5);

//counter is free running

LPTMR0->CSR |= (1 << 2);

//select pulse counter mode

LPTMR0->CSR |= (1 << 1);

//pulse counter input pin directly clocks counter value (bypass glitch filter)

LPTMR0->PSR = (1 << 2);

//enable LPTMR module

LPTMR0->CSR |= (1 << 0);

while (true) {

     LPTMR0->CNR = 1; // write any value first to CNR to read CNR value

     printf("counter value: %lu\r\n", LPTMR0->CNR);

     wait(1.0f);

}

I am expecting this code to return the current pulse count on pulse pin every second. But if I connect PTC5 to GND (no pulse) the pulse counter starts to increment (16000 after 1 second). My question is why it does behave like this? It should not detect any pulses because it is connected to GND. Also when I connect the pin to +3.3V it does behave like this. Am I missing some point here?

0 Kudos
1 Solution
674 Views
arminbego
Contributor II

Thanks for your support! Your project compiled and worked well for me too. The problem I had was this line

wait(1.0f);

The version of mbed library internally manipulates LPTMR0 when using the wait(); function. That was the reason why I was experiencing strange behaviour. With the latest version of mbed it works very well.

View solution in original post

0 Kudos
4 Replies
674 Views
kerryzhou
NXP TechSupport
NXP TechSupport

Hi Armin Bego,

     I use your code and test it on my TWR-K64, when I connect PTC5 to ground, the out put data is always 0, and never increase, but when I connect PTC5 to a PWM wave, the CNR can be increased. It works ok on my side, I think you should check your ground, whether it has the little pulse?

    Beside, I attached my test project, you can use my project(KDS3.0) and run it on your side.

 

    If you still have question, please contact me!

Have a great day,

Jingjing

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

0 Kudos
673 Views
arminbego
Contributor II

Thanks for your effort Jingjing Zhou,

i checked my ground and +3.3V signal. There is no noise. Also I have tried my code on another FRDM-K64 board, it behaves the same. I also tried to compile it with online mbed compiler, no succes.

0 Kudos
674 Views
kerryzhou
NXP TechSupport
NXP TechSupport

Hi Armin Bego,

      I also test it on our FRDM-K64, it also woks ok, attached is the project for K64, it will printf the result, following is my FRDM-K64 test result:

....

counter value: 1903

counter value: 1951

counter value: 1995

counter value: 2041

counter value: 2086

counter value: 2130

counter value: 2175

counter value: 2220

counter value: 2265

counter value: 2310

counter value: 2355

counter value: 2400

counter value: 2445

counter value: 2494

counter value: 2539

counter value: 2583

counter value: 2623

counter value: 2625

counter value: 2625

counter value: 2625

counter value: 2625

counter value: 2625

counter value: 2625

counter value: 2625

counter value: 2625

counter value: 2625

counter value: 2625

counter value: 2625

counter value: 2625

counter value: 2625

counter value: 2625

counter value: 2625

counter value: 2625

counter value: 2625

counter value: 2625

When I connect it to the PWM, it will increase, if I connect it to the ground, the vaulue will not increase, and stopped in 2625, you can use my project and test it on your FRDM-K64.

Besides, take care, PTC5 is the pin15 in J1, you should make sure you connect the correct pin.

Wish it helps you!

Have a great day,

Jingjing

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

0 Kudos
675 Views
arminbego
Contributor II

Thanks for your support! Your project compiled and worked well for me too. The problem I had was this line

wait(1.0f);

The version of mbed library internally manipulates LPTMR0 when using the wait(); function. That was the reason why I was experiencing strange behaviour. With the latest version of mbed it works very well.

0 Kudos