MKL28 input to TPM pulse counter example or application note

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

MKL28 input to TPM pulse counter example or application note

1,283 Views
mci
Contributor III

Hello,

I need to setup & use the Timer Pulse Module (TPM) of the MKL28 as a Pulse Counter to count input square waveform from a flow meter.

The SDK example do not exactly do this configuration, except some dual or single edge detection / capture that does not trigger the TPM internal pulse counting to log pulse counts.

I have read the Reference Manual regarding the matter but it would be nice to see an example that shows the steps in configuring the module as such.

Has anybody tried to configure the MKL28's TPM  in Pulse Counter mode?

And/or does anyone seen any mkl28/kinetis/TPM application note document on this?

Thanks for the help.

MI

Labels (1)
0 Kudos
5 Replies

1,021 Views
mci
Contributor III

Just letting you guys know that I was able to make it work today.

Firstly, to confirm if the input flow meter is generating pulses, I programmed PTC5 port pin as regular GPIO with interrupt trigger on falling edge, so the code is simply incrementing the counter everytime falling edge is detected.  I accumulated edge count & displayed amount every minute.  That works nice & simple but I can't settle with that because this product needs to do many other operations (including the touch button sensing) etc.   I need a module that will do this exactly as its job in INTERNAL REGISTER (not external variable).

After proving the flowmeter works, I went back to LPTMR in pulse counter mode.   I disabled even more of the above quoted code in main() by commenting out everything that pertains to "timer counter" mode as opposed to "pulse counter" mode.   The deal that made LPTMR module finally started counting was simply TO ENABLE it.    There was no function call to enable it so I had to create one which I called after the LPTMR configuration.

I figured these from re-reading the reference manual again & some general LPTMR presentation document I found from NXP/Freescale.   None of these stated a pulse counter mode example.   Put what struck me was the statement, "enable LPTMR as the last step".   So I figured out how to enable it & created a function call & called it.

So finally pulse count was no longer 0.   It started counting whenever the flowmeter simulator is on. At the minimum, it pulses 365 every minute.

Thanks for those who tried to help.

Let me know if anyone has any comments or questions.

MI

0 Kudos

1,021 Views
mci
Contributor III

Hi again,

So I dove in & tried to adapt KL28Z SDK lptmr project & changed it from being LPTMR timer counter mode to be in pulse counter mode taking input pulses from PTC5 setup as LPTMR0_ALT2 input function.

I ran this modified code but the CNR register count is always 0 even when I turn on the flowmeter simulator gadget that is attached to PTC5 which is generating square wave pulses in different duty cycles which should trigger pulse counts.

Original code sets up an lptmr timer to trigger interrupt every second & display a declared timer variable;  NOT from lptmr CNR counter register.

When you have a chance, kindly review this code snippets & see what I'm missing or where I am wrong.  

NOTE:  I am using my custom prototype board with the KL28Z as its MCU.

I setup a millisecond TPM timer such that every 30 seconds (half a minute), the system will dump a string on debug console that displays what is read from the CNR (lptmr count register), using function call: 

         fsl_lptmr.c/LPTMR_GetCurrentTimerCount(LPTMR0);     // correctly writes 0 to CNR first before reading...

OUTPUT:   (even after I turn on the flowmeter pulse generator, counter remains zero.)

Low Power Timer Pulse Counter 7/22/19
Pulse Count after half a minute = 0
Pulse Count after half a minute = 0
Pulse Count after half a minute = 0
Pulse Count after half a minute = 0
Pulse Count after half a minute = 0
Pulse Count after half a minute = 0
Pulse Count after half a minute = 0
Pulse Count after half a minute = 0

...

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

CODE:

*****pinmux.c/BOARD_InitPins();

void BOARD_InitPins(void)

{

/* Clock Gate Control: 0x01u */

CLOCK_EnableClock(kCLOCK_PortA);

/* Clock Gate Control: 0x01u */

CLOCK_EnableClock(kCLOCK_PortC);

/* Clock Gate Control: 0x01u */

CLOCK_EnableClock(kCLOCK_PortE);

// v3 5/30/19 mi ELMNT debug console

// PORTA1 (pin 35) is configured as LPUART0_RX

PORT_SetPinMux(PORTA, 1U, kPORT_MuxAlt2);

// PORTA2 (pin 36) is configured as LPUART0_TX

PORT_SetPinMux(PORTA, 2U, kPORT_MuxAlt2);

// PORTC5 (pin 77) is configured as PTC5

PORT_SetPinMux(PORTC, 5U, kPORT_MuxAlt3);       // v1 7/22/19 mi LPTMR_ALT2 pulse counter input

// PORTE26 (pin 33) is configured as PTE26

PORT_SetPinMux(PORTE, 26U, kPORT_MuxAsGpio);    // v1 7/22/19 mi Filter RGB Green to blink off/on

}

****** lptmr.c  -   NOTE:  I COMMENTED OUT SetTimer...() because the pulse counter flowmeter external input should be triggering timer count in this code modification.    Am I right to comment out those lines?

main()

{

. . .

LPTMR_GetDefaultConfig(&lptmrConfig);

/* Initialize the LPTMR */

LPTMR_Init(DEMO_LPTMR_BASE, &lptmrConfig);

/*

* Set timer period.

* Note : the parameter "ticks" of LPTMR_SetTimerPeriod should be equal or greater than 1.

*/

// LPTMR_SetTimerPeriod(DEMO_LPTMR_BASE, USEC_TO_COUNT(LPTMR_USEC_COUNT, LPTMR_SOURCE_CLOCK));

/* Enable timer interrupt */

// LPTMR_EnableInterrupts(DEMO_LPTMR_BASE, kLPTMR_TimerInterruptEnable);

/* Enable at the NVIC */

EnableIRQ(DEMO_LPTMR_IRQn);            // should I enable this code to execute? or comment it out too?

PRINTF("Low Power Timer Pulse Counter 7/22/19\r\n");

. . .

}

The 3rd code snippet at the end is the lptmr initialization configuration.    Did I do it right?    And note:   I'm bypassing both prescaler & also glitch filter in this pulse counter mode.

***** fsl_lptmr.c     IS SETUP HERE CORRECT ???

void LPTMR_GetDefaultConfig(lptmr_config_t *config)

{

assert(config);

/* Initializes the configure structure to zero. */

memset(config, 0, sizeof(*config));

// v1 7/22/19 mi modified to behave in pulse counter mode

/* Use time counter mode */

config->timerMode = kLPTMR_TimerModePulseCounter;                // set as pulse counter

/* Use input 0 as source in pulse counter mode */

config->pinSelect = kLPTMR_PinSelectInput_2;                // input 2, LPTMR0_ALT2

/* Pulse input pin polarity is active-high */

config->pinPolarity = kLPTMR_PinPolarityActiveHigh;

/* Counter CNR resets on overflow */

config->enableFreeRunning = true;                // reset on CNR overflow?   instead of when CNR = CMR

/* Bypass the prescaler */

config->bypassPrescaler = true;                            // yes, bypass prescaler

/* LPTMR clock source */

config->prescalerClockSource = kLPTMR_PrescalerClock_2;                   // LPTMR0_ALT2?  or don't care

/* Divide the prescaler clock by 2 */

config->value = kLPTMR_Prescale_Glitch_0; // don't care?

}

Any questions or what other code you want me to post here for clarity?

Thanks for the help.

MI

0 Kudos

1,021 Views
mci
Contributor III

Hi Robin,

You are right that I should go back to my original plan - use design plan, PTC5 (LPTMR0_ALT2) to connect flow meter input.

I thought I could use TPM to manually code from scratch something that will simulate a pulse counter.

That problem which confused me late Friday while researching further & looking at SDK examples, there is NO PULSE COUNTER example using LPTMR module.   There's only TPM pulse edge detection examples so I thought to ask about that. 

And the one example lptmr on SDK has setup PTE29 as ALT0 "CMP0_IN5" is assigned as output toggle,  connected to RED RGB LED on FRDM-KL28Z schematic diagram.   So this is output mode.   I need example of square wave (flow meter) input pulses mode for LPTMR to detect the pulses & save in its counter so I can check the counter say every minute, to measure how much water has flowed etc.

Do you know any example configuration of PTC5 or I'll just dive in & figure that out myself based on what I think I understand from the KL28Z reference manual?

Thanks for the help.

MI

0 Kudos

1,021 Views
mjbcswitzerland
Specialist V

Hi

It sounds as though the reference is using the CMP as LPTMR counter trigger. This is because the ALT0 input function is connected internally to the CMP_OUT. For this to work (which allows adjustable voltage thresholds to be set) the CMP input was used.

To connect to PTC5 the only change is to select ALT2 instead of ALT0 in the LPTMR configuration and to configure PTC5 as its LPTMR_ALT2 function (which can be shared between LPTMR0 and LPTMR1).

Regards

Mark

Complete Kinetis solutions for professional needs, training and support: http://www.utasker.com/kinetis.html
Kinetis KL28:
- http://www.utasker.com/kinetis/FRDM-KL28Z.html
uTasker: supporting >1'000 registered Kinetis users get products faster and cheaper to market
Request Free emergency remote desk-top consulting at http://www.utasker.com/services.html

Open Source version at https://github.com/uTasker/uTasker-Kinetis

0 Kudos

1,021 Views
Robin_Shen
NXP TechSupport
NXP TechSupport

Hi MI,

I am not sure if you can accept the use of the LPTMR module as a pulse counter.

LPTMR pulse counter.png

If TPM module is must, then select External Clock as TPM clock source.

TPM_EXTCLK.png

 

Best Regards,

Robin

 

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

0 Kudos