CCS/EK-TM4C123GXL: Trouble reading data from external temp sensor

Part Number: EK-TM4C123GXL

Tool/software: Code Composer Studio

Hi!

i am attempting to read a temperature from the temp/humidty sensor HTU21D-F and I am not receiving any data. I am stuck as to what i should focus on to resolve this issue. any hints/help would be awesome. when i run my code, regardless if the temp sensor is connected i get -52.3299 as "tempf" and 1.0842E-19 for "temp". 

thanks,

- lucas

here is my code


/**************************************************Temp code for SHT21************************************************/

/* Libraries*/
#include <stdbool.h>
#include <stdint.h>
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "driverlib/gpio.h"
#include "driverlib/pin_map.h"
#include "driverlib/sysctl.h"
#include "driverlib/uart.h"
#include "utils/uartstdio.h"
#include "inc/hw_i2c.h"
#include "driverlib/i2c.h"
#include "inc/hw_ints.h"
#include "driverlib/interrupt.h"
#include "driverlib/timer.h"
#include <stdint.h>
#include <stdbool.h>
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "driverlib/sysctl.h"
#include "driverlib/gpio.h"
#include "driverlib/debug.h"
#include "driverlib/pwm.h"
#include "driverlib/pin_map.h"
#include "inc/hw_gpio.h"
#include "driverlib/rom.h"

/***************Temp Sensor Driver Library*********************************/

#include "sensorlib/hw_sht21.h"
#include "sensorlib/i2cm_drv.h"
#include "sensorlib/sht21.h"
#include "inc/hw_ints.h"
#include "driverlib/debug.h"
#include "driverlib/rom.h"
#include "driverlib/rom_map.h"


/*************************************************************************************************************************************/

#define SHT21_I2C_ADDRESS 0x40 // temperature sensor address
#define SHT21_TEMP_NOBLOCK 0xF3 // Value to send to read Temperature from Sensor


#define ST_CTRL_R (*((volatile unsigned long *)0xE000E010))
#define ST_RELOAD_R (*((volatile unsigned long *)0xE000E014))
#define ST_CURRENT_R (*((volatile unsigned long *)0xE000E018))


uint32_t ui32SysClkFreq; //global variable for clock
static uint32_t g_I2C0Data[3]; // Data byte gathered from I2C3 master
void Configurei2C0(); // Function for I2C setup
void LED();

void Slave_Setup();
void SysTick_Init(void);

float tempf;
float temp;


void main(void) {


SysCtlClockSet(SYSCTL_SYSDIV_5|SYSCTL_USE_PLL|SYSCTL_OSC_MAIN|SYSCTL_XTAL_16MHZ);

ui32SysClkFreq = SysCtlClockGet();


LED();
SysTick_Init();

Configurei2C0();

/*********************I2C Slave setup***************************/

while (1) {


SysCtlDelay(20000);

I2CMasterInitExpClk(I2C0_BASE, ui32SysClkFreq, false); // The False sets the controller to 100kHz communication
//Setup_i2C();


I2CMasterSlaveAddrSet(I2C0_BASE, SHT21_I2C_ADDRESS, false); //Setup Slave and send Temp address
SysCtlDelay(20000);
I2CMasterDataPut(I2C0_BASE, 0xFE); // Soft Reset
SysCtlDelay(20000);
I2CMasterDataPut(I2C0_BASE, SHT21_TEMP_NOBLOCK); //Send Temp read info
SysCtlDelay(20000);
I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_SINGLE_SEND); //Setup I2c master for single send
SysCtlDelay(20000);


// Set address to read
I2CMasterSlaveAddrSet(I2C0_BASE, SHT21_I2C_ADDRESS, true);
SysCtlDelay(50000);
// Master read data byte 1 and store
I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_SINGLE_RECEIVE);
SysCtlDelay(20000);
g_I2C0Data[0] = I2CMasterDataGet(I2C0_BASE);
SysCtlDelay(50000);
// Master read data byte 2 and store
I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_SINGLE_RECEIVE);
SysCtlDelay(50000);
g_I2C0Data[1] = I2CMasterDataGet(I2C0_BASE);
SysCtlDelay(50000);
// Master read data byte 3 (checksum) and store
I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_SINGLE_RECEIVE);
SysCtlDelay(50000);

//Convert to temperature
uint32_t tempConvert = ((uint32_t) g_I2C0Data[0] << 8)| (uint32_t) (g_I2C0Data[1]);
SysCtlDelay(50000);

float temp = (float) (tempConvert & 0xFFFC); //------this is when a number stops for "temp"
SysCtlDelay(50000);

temp = -46.85f + 175.72f * (temp / 65536.0f);
tempf = temp*(9.0f/5.0f)+32;

SysCtlDelay(50000);

if (temp < 50.00f) {


GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_6, 0x40);

}


else if (temp > 50.00f) {

GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_6, 0x00);


}
}
}

// Function definitions ************************************************************************************************************************************

void Configurei2C0(void) {
SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C0); // Enable I2C hardware
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB); // Enable Pin hardware

GPIOPinConfigure(GPIO_PB3_I2C0SDA); // Configure GPIO pin for I2C Data line
GPIOPinConfigure(GPIO_PB2_I2C0SCL); // Configure GPIO Pin for I2C clock line

GPIOPinTypeI2C(GPIO_PORTB_BASE, GPIO_PIN_2 | GPIO_PIN_3); // Set Pin Type

GPIOPadConfigSet(GPIO_PORTB_BASE, GPIO_PIN_2, GPIO_STRENGTH_2MA,
GPIO_PIN_TYPE_STD); // SDA MUST BE STD
GPIOPadConfigSet(GPIO_PORTB_BASE, GPIO_PIN_3, GPIO_STRENGTH_2MA,
GPIO_PIN_TYPE_STD);


I2CMasterInitExpClk(I2C0_BASE, ui32SysClkFreq, false); // The False sets the controller to 100kHz communication

}

void LED(void) {


SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
GPIOPinTypeGPIOOutput(GPIO_PORTA_BASE, GPIO_PIN_6);


}

void SysTick_Init(void) // Initialize SysTick running at bus clock.
{
ST_CTRL_R = 0; // disable SysTick during setup
ST_RELOAD_R = 0x00ffffff; // set maximum reload value
ST_CURRENT_R = 0; // any write to current clears it
// enable SysTick (write 1 to bit 0), turn off interupts (0 to bit 1), use core clock (1 to bit2)
ST_CTRL_R = 0x05;
}



1 Reply

  • Hi,
    I will suggest you compare between HTU21D and SHT21 temp sensors. It seems like they are very similar. If this is the case you may try to use the example in the sensor library. There is a program example in the TivaWare Sensor Library UserGuide in section 18.3.

    I will also suggest you go through the app note on using TM4C I2C in master mode. www.ti.com/.../spma073.pdf

    You should also make sure you have proper pullup resistors on the SCL and SDA bus. Please also use the scope to check the bus activities.

    regards,

    Charles

    Please click the Verify Answer button on this post if it answers your question.