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.

code to put DHT11 to communicate with TM4C1294XL

I had some problems to put DHT11 to communicate with TM4C1294XL launchpad, so I am sharing the code... Its not very polished, but, works, and can be used as base for someone that wants to test. 

The code was based on (http://forum.stellarisiti.com/topic/1929-dht11-temperature-humidity-sensor-code-snippet/) that did not work well on TM4C1294  (thanks any way!!) :(

#include <stdint.h>
#include <stdbool.h>
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "driverlib/gpio.h"
#include "drivers/pinout.h"
#include "driverlib/pin_map.h"
#include "driverlib/rom.h"
#include "driverlib/rom_map.h"
#include "driverlib/sysctl.h"
#include "driverlib/uart.h"
#include "driverlib/timer.h"
#include "utils/uartstdio.h"



int temp;
int humidity;

uint32_t g_ui32SysClock;

#ifdef DEBUG
void
__error__(char *pcFilename, uint32_t ui32Line)
{
}
#endif

void
ConfigureUART(void)
{
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);

    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);

    ROM_GPIOPinConfigure(GPIO_PA0_U0RX);
    ROM_GPIOPinConfigure(GPIO_PA1_U0TX);
    ROM_GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);

    UARTStdioConfig(0, 115200, g_ui32SysClock);
}





int ReadDHT()
{
	// bit buffers & timeout
	char bitcount;
	char byte;
	char bits[5] = {0,0,0,0,0};
	unsigned int bitints[6];
	long timerval;
	int clockMhz;

	unsigned int loopCnt = 10000;

	#define MY_PIN_PERIPH		SYSCTL_PERIPH_GPIOM
	#define MY_PIN_PORTBASE		GPIO_PORTM_BASE
	#define MY_PINNR			GPIO_PIN_6

	#define MY_TIMER_PERIPH		SYSCTL_PERIPH_TIMER0
	#define MY_TIMERBASE		TIMER0_BASE
	#define MY_TIMER			TIMER_B

	SysCtlPeripheralEnable(MY_TIMER_PERIPH);
	TimerConfigure(MY_TIMERBASE, TIMER_CFG_SPLIT_PAIR | TIMER_CFG_B_ONE_SHOT);


	SysCtlPeripheralEnable(MY_PIN_PERIPH);

	ROM_GPIOPinTypeGPIOOutput(MY_PIN_PORTBASE, MY_PINNR);


	GPIOPinWrite(MY_PIN_PORTBASE, MY_PINNR, 0x0);

	// #loops = loops_per_us*time_in_us = time in us * loops/us = time_in_us *(clockfreq/10000000)
	SysCtlDelay((g_ui32SysClock/3)/30 ); // -> 30 ms

	ROM_GPIOPinTypeGPIOInput(MY_PIN_PORTBASE, MY_PINNR);


	loopCnt = g_ui32SysClock/100;
	while(GPIOPinRead(MY_PIN_PORTBASE, MY_PINNR))
	{
		loopCnt = loopCnt -1;
		if (loopCnt == 0)
			return 0;
	}


	loopCnt = g_ui32SysClock/100;
	while(!(GPIOPinRead(MY_PIN_PORTBASE, MY_PINNR)))  //0
	{
		loopCnt = loopCnt -1;
		if (loopCnt == 0)
			return 0;
	}

	loopCnt = g_ui32SysClock/100;
	while(GPIOPinRead(MY_PIN_PORTBASE, MY_PINNR))
	{
		loopCnt = loopCnt -1;
		if (loopCnt == 0)
			return 0;
	}

	// start receiving 40 bits
	char i;
	bitcount = 7;
	byte = 0;

	int conta = 0;
	int timerval2=0;

	int largest = 0;
	int lowest  = 99999;

	for (i=0; i < 40; i++)
	{
    	        loopCnt = g_ui32SysClock/100;
		while(!(GPIOPinRead(MY_PIN_PORTBASE, MY_PINNR)))
		{
			loopCnt = loopCnt -1;
			if (loopCnt == 0)
				return 0;
		}

		
		TimerDisable(MY_TIMERBASE, MY_TIMER);
		TimerConfigure(MY_TIMERBASE, TIMER_CFG_SPLIT_PAIR | TIMER_CFG_B_ONE_SHOT);
		TimerLoadSet(MY_TIMERBASE, MY_TIMER, g_ui32SysClock/4000);
		TimerEnable(MY_TIMERBASE, MY_TIMER);
		timerval = TimerValueGet(MY_TIMERBASE, MY_TIMER);

		loopCnt = g_ui32SysClock/100;
		while(GPIOPinRead(MY_PIN_PORTBASE, MY_PINNR))
		{
			loopCnt = loopCnt -1;
			if (loopCnt == 0)
				return 0;
		}

		timerval2 = TimerValueGet(MY_TIMERBASE, MY_TIMER);


		conta =  timerval - timerval2;

		if (conta>largest)
			largest = conta;

		if (conta<lowest)
			lowest = conta;

		if ((conta) > (1000))
			   bits[byte] |= (1 << bitcount);
		if (bitcount == 0)
		{
			bitcount = 7;
			byte++;
		}else{
			bitcount--;
		}
	}

	bitints[0] = ((unsigned int) bits[0]  & (0x000000FF));
	bitints[1] = ((unsigned int) bits[1]  & (0x000000FF));
	bitints[2] = ((unsigned int) bits[2]  & (0x000000FF));
	bitints[3] = ((unsigned int) bits[3]  & (0x000000FF));
	bitints[4] = ((unsigned int) bits[4]  & (0x000000FF));
	
	if(((bitints[0] + bitints[1] + bitints[2] + bitints[3]) & (0x000000FF)) == bitints[4]){
		temp = bitints[2];
		humidity =  bitints[0];
		return 1;
	}else{
		return 0;
	}

}


int
main(void)
{
    g_ui32SysClock = MAP_SysCtlClockFreqSet((SYSCTL_XTAL_25MHZ |
                SYSCTL_OSC_MAIN | SYSCTL_USE_PLL |
                SYSCTL_CFG_VCO_480), 40000000);

    PinoutSet(false, false);

    ROM_GPIOPinTypeGPIOOutput(GPIO_PORTN_BASE, GPIO_PIN_1);

    ConfigureUART();

    UARTprintf("Its runing at : %d\n",g_ui32SysClock);


    while(1)
    {
        //
        // Turn on D1. LED 1
        //
        LEDWrite(CLP_D1, 1);

        SysCtlDelay(g_ui32SysClock  / 3); //%1s Delay its required because if the sensor is ready to fast the comunication goes down.

        LEDWrite(CLP_D1, 0);

        SysCtlDelay(g_ui32SysClock  / 3);

        if(ReadDHT() == 0)
        	{
        	 UARTprintf("Erro!\n");
        	}
        else
        {
        	UARTprintf("Temp %d !\n",temp);
        	UARTprintf("Hum %d !\n",humidity);

        }

    }
}