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.

How to get system up time in tm4c123g?

Other Parts Discussed in Thread: ENERGIA

So here is the code for my enduring effort with DHT22 sensor interfacing with tm4c123g. I run it on Arduino every day just to test if it has gone haywire. But till now it hasn't I know both the DHT22 and Arduino are not the best hardware but since it is a compulsion of situation I am forced to use the dht22 sensor and also use Arduino just as a proof of concept.

Here is a code snippet I have written to mirror an Arduino library on CCS.

int dht_read(void)
{
uint8_t laststate = 0xFF;
uint8_t counter = 0;
uint8_t j = 0, i;
uint32_t currenttime;

SysCtlDelay(((SysCtlClockGet()/3)*2));

currenttime= SysTickValueGet();

if (currenttime < lastreadtime)
{
// ie there was a rollover
lastreadtime = 0;
}

if (!firstreading && ((currenttime - lastreadtime) < ((SysCtlClockGet()/3)*2) ))
{
return 1; // return last correct measurement
//delay(2000 - (currenttime - _lastreadtime));
}

firstreading = false;
lastreadtime= currenttime;

data[0] = data[1] = data[2] = data[3] = data[4] = 0;

GPIOPinTypeGPIOOutput(DHT_PORT, DHT_PIN);
GPIOPinWrite(DHT_PORT, DHT_PIN,DHT_PIN);
SysCtlDelay(((SysCtlClockGet()/3)/4));

GPIOPinTypeGPIOOutput(DHT_PORT, DHT_PIN);
GPIOPinWrite(DHT_PORT, DHT_PIN,0x00);
SysCtlDelay(((SysCtlClockGet()/3)/50));

GPIOPinTypeGPIOOutput(DHT_PORT, DHT_PIN);
GPIOPinWrite(DHT_PORT, DHT_PIN,DHT_PIN);
SysCtlDelay(167);

GPIOPinTypeGPIOInput(DHT_PORT, DHT_PIN);

// read in timings
for ( i=0; i< MAXTIMINGS; i++) {
counter = 0;
while (GPIOPinRead(DHT_PORT, DHT_PIN) == laststate) {
counter++;
SysCtlDelay(5);
if (counter == 255) {
break;
}
}
laststate = GPIOPinRead(DHT_PORT, DHT_PIN) ;

if (counter == 255)
{
break;
}

// ignore first 3 transitions
if ((i >= 4) && (i%2 == 0)) {
// shove each bit into the storage bytes
data[j/8] <<= 1;
if (counter > count)
data[j/8] |= 1;
j++;
}

}

if ((j >= 40) &&
(data[4] == ((data[0] + data[1] + data[2] + data[3]) & 0xFF)) )
{
return 1;
}

return 0;

}

So the statement currenttime=SysTickValueGet(); is supposed to mirror the millis(); function which returns a value in milliseconds representing the amount of time since the CPU. has been on or reset.

I am attaching the arduino library file here. If you can suggest some better alternatives to this. It would be great.

You would find the code in DHT.cpp

DHT.cppDHT.h

Also thank you to Bruno and f.m to redirect me here.

  • Hi,

    Please see this link below - Energia people contribution:

    Take into account that these libraries uses to globally disable all interrupts to have enough accuracy for delays.

  • Quick reply, I won't review this code example too much - use with care.

    This function configures a wide (64bit) timer in TM4C123x:

    void Init_Uptime(void) // WTimer5
    {
    	// Configure WTimer5 to control system up time
    	SysCtlPeripheralDisable(SYSCTL_PERIPH_WTIMER5);
    	SysCtlPeripheralReset(SYSCTL_PERIPH_WTIMER5);
    	SysCtlPeripheralEnable(SYSCTL_PERIPH_WTIMER5);
    	while( !SysCtlPeripheralReady( SYSCTL_PERIPH_WTIMER5 ) ){}
    	TimerConfigure(WTIMER5_BASE,TIMER_CFG_PERIODIC_UP);
    	TimerEnable(WTIMER5_BASE, TIMER_A);
    }
    

    Before your main loop, attribute the system clock to a variable, and initialize that timer with:

    g_ui32SysClock = MAP_SysCtlClockGet();
    
    Init_Uptime();
    
    

    Then, whenever you need to know the elapsed time in seconds, use:

    TimeInSec = (int32_t)(TimerValueGet64(WTIMER5_BASE)/g_ui32SysClock);

    Same concept can be used for milliseconds with different math.

    Nota that this does not apply to time critical applications - the clock is quite good using crystals, but it is not precise time (nothing is...).