This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Timer value get

Other Parts Discussed in Thread: TM4C123GH6PM

Hi,

I am using TM4C123GH6PM micro controller and ccs6. In my program I need to get timer value when input (Ans) is 0. I am giving input such that, the input goes to zero for every 0.02sec. And I am using TimerValueGet to get time period. But for every cycle the timer value varies (input fall to zero in 0.02 sec interval). Can anyone suggest what may be the error? Timer value mostly changes between 250000 to 310000. And one more thing, the value Time (Time=(TimeCount/320000);) mostly shows 0.0/1609. 

#include <stdbool.h>
#include <stdint.h>
#include "inc/hw_memmap.h"
#include "driverlib/adc.h"
#include "driverlib/gpio.h"
#include "driverlib/sysctl.h"
#include "driverlib/adc.h"
#include "inc/hw_types.h"
#include "driverlib/fpu.h"
#include "driverlib/debug.h"
#include "driverlib/timer.h"
#include "driverlib/uart.h"
#include "utils/uartstdio.h"

#define GPIO_PA0_U0RX           0x00000001
#define GPIO_PA1_U0TX           0x00000401

volatile unsigned long Old_Count;
volatile unsigned long New_Count=0;
int FallingEdges=0;
volatile unsigned long TimeCount;
float Ans;
float frequency;
float freq;
float Time;
float Time2,freq2,frequency2;
int Cycle=0;
int Time1,Temp1,Time3,freq1,Temp2,freq3,frequency1,Temp3,frequency3;

void
ConfigureUART(void)
{
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
    SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
    GPIOPinConfigure(GPIO_PA0_U0RX);
    GPIOPinConfigure(GPIO_PA1_U0TX);
    GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);
    UARTClockSourceSet(UART0_BASE, UART_CLOCK_PIOSC);
    UARTStdioConfig(0, 115200, 16000000);
}

main(void) {

    uint32_t pui32ADC0Value[1];

    SysCtlClockSet(SYSCTL_SYSDIV_10 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN| SYSCTL_XTAL_16MHZ);
    SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC0);
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE);
	SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0);
	SysCtlDelay(3);
	
	TimerDisable(TIMER0_BASE,TIMER_A);
	TimerConfigure(TIMER0_BASE, TIMER_CFG_PERIODIC_UP);
	TimerLoadSet(TIMER0_BASE, TIMER_A, 0x00FFFFFF);
	TimerEnable(TIMER0_BASE, TIMER_A);
    GPIOPinTypeADC(GPIO_PORTE_BASE, GPIO_PIN_5);

    ADCIntEnable(ADC0_BASE, 3);
    ADCSequenceDisable(ADC0_BASE, 3);
    ADCSequenceConfigure(ADC0_BASE, 3, ADC_TRIGGER_PROCESSOR, 0);
    ADCSequenceStepConfigure(ADC0_BASE, 3, 0,ADC_CTL_CH8 | ADC_CTL_IE | ADC_CTL_END);

    ConfigureUART();

    UARTprintf("\033[2JFrequency Measurement:-\n");
    ADCSequenceEnable(ADC0_BASE, 3);
    
    while (1) {
        ADCProcessorTrigger(ADC0_BASE, 3);
        while (!ADCIntStatus(ADC0_BASE, 3, false))
        {
        }

        ADCIntClear(ADC0_BASE, 3);
        ADCSequenceDataGet(ADC0_BASE, 3, pui32ADC0Value);
        Ans=(pui32ADC0Value[0]*3.3)/4095;

        if (Ans==0.000)
        {
        	Old_Count=New_Count;
        	New_Count=TimerValueGet(TIMER0_BASE, TIMER_A);
        	UARTprintf("\nCycles:%d\n",Cycle);
        	TimeCount=New_Count-Old_Count;
      	  Time=(TimeCount/320000);
      	  freq=1/Time;
      	  frequency=freq*50;
      	  //for float
      	  Time1=Time;
      	  Time2=Time*100000;
      	  Temp1=Time1*100000;
      	  Time3=Time2-Temp1;

      	freq1=freq;
      	freq2=freq*100000;
      	Temp2=freq1*100000;
      	freq3=freq2-Temp2;

      	frequency1=frequency;
      	frequency2=frequency*100000;
      	Temp3=frequency1*100000;
      	frequency3=frequency2-Temp3;

      	UARTprintf("\nAns:%d\n",Ans);
        UARTprintf("\nCycles:%d\n",Cycle);
      	UARTprintf("\nNew_Count:%d\n",New_Count);
      	UARTprintf("\nOld_Count:%d\n",Old_Count);
      	UARTprintf("\nTimeCount:%d\n",TimeCount);
      	UARTprintf("\nTime:%d\n",Time);
      	UARTprintf("\nTime:%d.%d\n",Time1,Time3);
      	UARTprintf("\nFrequency:%d\n",freq1,freq3);
      	UARTprintf("\nFrequency:%d\n",frequency1,frequency3);

        }

}
}

 

  • Are you reading a AC(half) voltage(50Hz) on the ADC input, and looking for zero crossing?

    Then you cannot use the ADC triggered in a loop, depending on what you want to 'read', you can use the ADC 'Digital Comparator Unit', the Timer 'Input Edge-Count Mode' or 'Input Edge-Time Mode', with a CCP pin, look at the API or Datasheet for more info.

    The reason you're division 'Time = TimeCount/32000' is most of the time 0(or 1), is because you're dividing an integer(TimeCount) and not a float. You'll have to cast your variable like this 'Time = (float)TimeCount/32000'.

  • Hi Marc_rir,Yes I need to read the zero crossing of AC input.
    I will try your idea (Digital comparator unit). But may i know why the current code is not working, It is similar to comparator (If I use
    "if (pui32ADC0Value[0]==0)" instead of "if (Ans==0)"). Why timer count varies for same time period?
    Than you for 'Time = (float)TimeCount/32000'. It works.