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
Also thank you to Bruno and f.m to redirect me here.