Hello
i am trying to calculate frequency of an input signal with timer capture mode.
however it running good but sometime the result get 0 value .
how could this happen ?
how eliminate this error?
#include <stdbool.h> #include <stdint.h> #include "inc/hw_ints.h" #include "inc/hw_types.h" #include "inc/hw_memmap.h" #include "driverlib/gpio.h" #include "driverlib/fpu.h" #include "driverlib/interrupt.h" #include "driverlib/pin_map.h" #include "driverlib/sysctl.h" #include "driverlib/systick.h" #include "driverlib/timer.h" #include "driverlib/uart.h" #include "utils/uartstdio.h" void calc_freq(void) ; volatile uint32_t g_ui32Counter = 0; uint32_t ui32SysClock=0, time=0, start=0,end=0; bool flag=false; void SysTickIntHandler(void) { // // Update the Systick interrupt counter. // if(g_ui32Counter>10000000){ g_ui32Counter=0;} g_ui32Counter++; } void InitConsole(void) { SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA); GPIOPinConfigure(GPIO_PA0_U0RX); GPIOPinConfigure(GPIO_PA1_U0TX); SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0); UARTClockSourceSet(UART0_BASE, UART_CLOCK_PIOSC); GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1); UARTStdioConfig(0, 115200, 16000000); } void edge_capture(void) { TimerIntClear(TIMER1_BASE, TIMER_CAPB_EVENT); if (flag==false){ start = TimerValueGet(TIMER1_BASE, TIMER_B); flag=true; } else if (flag==true){ end=TimerValueGet(TIMER1_BASE, TIMER_B); time=end-start; flag=false; TimerLoadSet(TIMER1_BASE, TIMER_B,(10000-1)); } } void Configure_timer(void) { SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD); SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER1); GPIOPinTypeTimer(GPIO_PORTD_BASE, GPIO_PIN_3); GPIOPinConfigure(GPIO_PD3_T1CCP1); TimerConfigure(TIMER1_BASE,(TIMER_CFG_SPLIT_PAIR | TIMER_CFG_B_CAP_TIME_UP)); TimerControlEvent(TIMER1_BASE,TIMER_B,TIMER_EVENT_POS_EDGE); TimerPrescaleSet(TIMER1_BASE, TIMER_B, (120-1)); TimerLoadSet(TIMER1_BASE, TIMER_B,(10000-1)); IntRegister(INT_TIMER1B, edge_capture); TimerIntClear(TIMER1_BASE, TIMER_CAPB_EVENT); TimerIntEnable(TIMER1_BASE, TIMER_CAPB_EVENT); IntEnable(INT_TIMER1B); TimerEnable(TIMER1_BASE, TIMER_B); } int main(void) { FPULazyStackingEnable(); ui32SysClock = SysCtlClockFreqSet((SYSCTL_XTAL_25MHZ | SYSCTL_OSC_MAIN | SYSCTL_USE_PLL|SYSCTL_CFG_VCO_480), 120000000); InitConsole(); Configure_timer(); SysTickPeriodSet(ui32SysClock/1000000); IntMasterEnable(); SysTickIntEnable(); SysTickEnable(); while(1) { if(time!=0) { calc_freq(); } } } void calc_freq(void) { float y; y=time/1200 ; y=1/y; time=100000*y; UARTprintf("start :%d\n",time); time=0; end=0; start=0; }