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.
volatile uint32_t countArray[2];
volatile uint8_t countIndex;
volatile uint32_t realFrequency;
const uint32_t icFreq = 20000000;
void GPIO_ISR(void) {
PORTC->ISFR = PORT_ISFR_ISF_MASK;
if (countIndex != 2) {
LPTMR0->CNR = (0U & LPTMR_CNR_COUNTER_MASK);
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);
PINS_DRV_Init(NUM_OF_CONFIGURED_PINS, g_pin_mux_InitConfigArr);
INT_SYS_InstallHandler(PORT_IRQn, &GPIO_ISR, (isr_t *) 0);
INT_SYS_EnableIRQ(PORT_IRQn);
LPTMR_DRV_Init(INST_LPTMR1, &lpTmr1_config0, true);
for (;;) {
if (countIndex == 2) {
if (countArray[1] < countArray[0]) {
realFrequency = icFreq / (countArray[1] + 65535 - countArray[0]);
} else {
realFrequency = icFreq / (countArray[1] - countArray[0]);
}
}
}