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?