AnsweredAssumed Answered

S32K116 Measure wave frequency

Question asked by Rui Almeida on Sep 27, 2019
Latest reply on Oct 2, 2019 by Daniel Martynek

Hi, I'm trying to measure the frequency of a square wave using the MCU on title. I have LPTMR running in free mode and I'm capturing its value every rising edge interrupt of the GPIO pin of the wave. I've configured the system clock to 40MHz and the BUS and LPTMR to 20MHz. I can correctly read frequencies until 300kHz, but after this value, the result is wrong. Does anyone have any idea why? The configurations using Processor Expert are on attachment.
Thanks in advance.

/* Variables both used inside ISR and out */
volatile uint32_t countArray[2];
volatile uint8_t countIndex;
/* Frequency variables */
volatile uint32_t realFrequency;
const uint32_t icFreq = 20000000;

void GPIO_ISR(void) {
/* Clear Port B Interrupt Flag */
PORTC->ISFR = PORT_ISFR_ISF_MASK;
if (countIndex != 2) {
/* Get current counter value */
LPTMR0->CNR = (0U & LPTMR_CNR_COUNTER_MASK); // Write a dummy value before reading
countArray[countIndex++] = LPTMR0->CNR;
}
}

int main(void) {
CLOCK_SYS_Init(g_clockManConfigsArr, CLOCK_MANAGER_CONFIG_CNT,
g_clockManCallbacksArr, CLOCK_MANAGER_CALLBACK_CNT);
CLOCK_SYS_UpdateConfiguration(0U, CLOCK_MANAGER_POLICY_AGREEMENT);
/* Initialize Pins */
PINS_DRV_Init(NUM_OF_CONFIGURED_PINS, g_pin_mux_InitConfigArr);
/* Install GPIO ISR */
INT_SYS_InstallHandler(PORT_IRQn, &GPIO_ISR, (isr_t *) 0);
INT_SYS_EnableIRQ(PORT_IRQn);
/* Initialize LPTMR */
LPTMR_DRV_Init(INST_LPTMR1, &lpTmr1_config0, true);
/* MAIN LOOP */
for (;;) {
if (countIndex == 2) {
/* Verify timer overflow */
if (countArray[1] < countArray[0]) {
realFrequency = icFreq / (countArray[1] + 65535 - countArray[0]);
} else {
realFrequency = icFreq / (countArray[1] - countArray[0]);
}
}
}

Attachments

Outcomes