AnsweredAssumed Answered

Reading Counter value in LPTMR?

Question asked by James Smith on Feb 23, 2018
Latest reply on Feb 26, 2018 by Kerry Zhou



I'm trying to simply read (polling) the timer counter value in the LPTMR on the FRDM KL43Z board.  Thought that I'd try that before trying interrupts or anything fancy.  Having read through the documentation I'm aware that I have to enable the clock source to the LPTMR and I read that I have to write a bogus value to the CNR register prior to reading it.  However, all I seem to get is a 0 value from CNR when I step through the code in debug mode.


I'm using MCUXpresso 10.1 and trying to use a CMSIS-style approach to accessing the registers.  Monitoring the register values as I step through the code, the registers appear to be changing as they are supposed to.


Below is the code that I'm using.  Any hints as to what I might be doing wrong?







int main(void) {

/* Init board hardware. */
/* Init FSL debug console. */

/* Set up low power timer */

// System Register 5 has the Low Power Timer in it.
// Enable clock on the lower power timer.
// Bit 0 is the LPTMR clock gate enable bit. 1 for enable.
SIM->SCGC5 |= SIM_SCGC5_LPTMR_MASK; /* enable clock: SIM_SCGC5: LPTMR=1 */

// Disable the Low Power Timer
LPTMR0->CSR &= ~LPTMR_CSR_TEN_MASK; // invert and AND.

// CNR overflow reset, Time counter; don't enable yet.
LPTMR0->CSR = 0b0000000011001100;

// Prescale: use 8MHz clock; full prescale enabled.
LPTMR0->PSR = 0b0000000001111000;

// Last Step... enable timer
LPTMR0->CSR |= LPTMR_CSR_TEN_MASK; // Enable the timer via a mask.

printf("Hello World\n");

/* Force the counter to be placed into memory. */
volatile static int i = 0 ;
/* Enter an infinite loop, just incrementing a counter. */
while(1) {
// write to the CNR to tell the counter that you want to read it.
LPTMR0->CNR = 1u;
// now read the CNR

printf("current counter %d\r\n",i);
return 0 ;