Hello.
I am using WT5CCP1 to measure the frequency of a square signal comparing it with an internal Timer of 1 second in Wide Timer 4.
However, the value I get is higher than expected. For example, if I set up my signal generator at 200kHz, I get 205kHz
If I set up 50kHz I get 52kHz, and if I set up 30 kHz I get 31. The scope measures the expected frequency correctly, not the one the launchpad is reading.
Is there anything delaying the internal 1second timer? It happens the same even with a different internal 1 second Timer.
My inizialization code is as follows:
int32_t cuenta_interrupcion=1000;//Measuring kHz
void init_WTIMER5_READ(void)
{
MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_WTIMER5);
// Enable port PD7 for WTIMER5 WT5CCP1
// First open the lock and select the bits we want to modify in the GPIO commit register.
//
HWREG(GPIO_PORTD_BASE + GPIO_O_LOCK) = GPIO_LOCK_KEY;
HWREG(GPIO_PORTD_BASE + GPIO_O_CR) = 0x80;
//SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD);
MAP_GPIOPinConfigure(GPIO_PD7_WT5CCP1);
MAP_GPIOPinTypeTimer(GPIO_PORTD_BASE, GPIO_PIN_7);
//TimerConfigure(WTIMER5_BASE, TIMER_CFG_SPLIT_PAIR | TIMER_CFG_B_CAP_TIME); //Every edge
TimerConfigure(WTIMER5_BASE, TIMER_CFG_SPLIT_PAIR | TIMER_CFG_B_CAP_COUNT_UP);//FUNCIONA!!!!
// Freeze the timer counting if we are debugging (the counting is enabled automatically with the cpu).
//TimerControlStall(WTIMER5_BASE, TIMER_B, true);
TimerControlEvent(WTIMER5_BASE,TIMER_B,TIMER_EVENT_POS_EDGE);
TimerLoadSet(WTIMER5_BASE, TIMER_B, cuenta_interrupcion+1); // 1MHz OK
TimerMatchSet(WTIMER5_BASE,TIMER_B, cuenta_interrupcion+1);
//TimerLoadSet(WTIMER5_BASE, TIMER_B, 1600); //Count for 50KHz at 80MHz clock.
//TimerMatchSet(WTIMER5_BASE, TIMER_B, 800); //50% duty cycle at init
// TimerIntRegister(WTIMER5_BASE, TIMER_B, WTimer5BIntHandler);
// TimerIntEnable(WTIMER5_BASE, TIMER_CAPB_EVENT);//Every edge
TimerIntEnable(WTIMER5_BASE, TIMER_CAPB_MATCH); //FUNCIONA!!
TimerEnable(WTIMER5_BASE, TIMER_B);
IntEnable(INT_WTIMER5B);
}
void InitTimer_1sec()
{//Timer 1 second
//SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER2);
ROM_SysCtlPeripheralEnable (SYSCTL_PERIPH_WTIMER4);
TimerConfigure(WTIMER4_BASE, TIMER_CFG_PERIODIC);
TimerLoadSet(WTIMER4_BASE, TIMER_A, ROM_SysCtlClockGet() );//Tried also with value: ROM_SysCtlClockGet() -1
IntEnable(INT_WTIMER4A);
TimerIntEnable(WTIMER4_BASE, TIMER_TIMA_TIMEOUT);
TimerEnable(WTIMER4_BASE, TIMER_A);
}
The interrrupt routines are:
void WTimer5BIntHandler(void)
{//COUNTER INTERRUPT
//TimerIntClear(WTIMER5_BASE, TIMER_CAPB_EVENT);//Para cada una
//TimerIntEnable(WTIMER5_BASE, TIMER_CAPB_MATCH); //FUNCIONA!!
cont_wTimer5B++;
cont_wTimer5B_periodo++;
TimerIntClear(WTIMER5_BASE, TIMER_CAPB_MATCH);
}
void WTimer4AIntHandler(void)
{//1second interrupt
//
// Clear the timer interrupt.
//
TimerIntClear(WTIMER4_BASE, TIMER_TIMA_TIMEOUT);
contador_pulsos_Hz=cont_wTimer5B_periodo;
cont_wTimer5B_periodo=0;
flag_print=true;
/* TimerLoadSet(WTIMER4_BASE, TIMER_A, ROM_SysCtlClockGet()-1 );
TimerEnable(WTIMER4_BASE, TIMER_A);/**/
}
In the while loop:
if (flag_print)
{
flag_print=false;
UARTprintf("FREQ: %d kHz ",contador_pulsos_Hz);
}