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.

CCS/TM4C1230H6PM: reading data from temp/humidity sensor HTU21D(F)

Part Number: TM4C1230H6PM

Tool/software: Code Composer Studio

Hi all! 

Right now I'm currently doing a research project and need to create an if then statement to control a servo and fan based off of a temperature reading. I am using the TM4C123 and an external temperature sensor HTU21DF. I know that the servo and motor control work properly but i am not getting a proper reading from the temp sensor. Currently the only value i am getting is tempf = -52 degrees. I'll get this reading even if the temp sensor is not connected to the board. I am not exactly sure where i have gone wrong in coding the i2c portion of the code and have no idea what to do next. any help would be awesome.  attached is my ccs code. 

- Lucas 


#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"
#define PWM_FREQUENCY 55 //pwm freq is 55hz

#define RED_LED GPIO_PIN_1
#define BLUE_LED GPIO_PIN_2
#define GREEN_LED GPIO_PIN_3

/**************************************************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"

/***************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 Read_temp(unsigned char *data); // Read Temperature sensor
void Slave_Setup();
void SysTick_Init(void);
//unsigned char temp[2]; // storage for data
float tempf;


void main(void) {

/*******************Clock Setup for 120MHz****************************************/

// ui32SysClkFreq = SysCtlClockSet(SYSCTL_SYSDIV_5|SYSCTL_USE_PLL|SYSCTL_OSC_MAIN|SYSCTL_XTAL_16MHZ);

ui32SysClkFreq = SysCtlClockFreqSet((SYSCTL_XTAL_25MHZ | SYSCTL_OSC_MAIN | SYSCTL_USE_PLL
| SYSCTL_CFG_VCO_480), 120000000); //sets clocl to 120mhz


//SysCtlClockSet(SYSCTL_SYSDIV_5|SYSCTL_USE_PLL|SYSCTL_OSC_MAIN|SYSCTL_XTAL_16MHZ);


LED();
SysTick_Init();

Configurei2C0();

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

volatile uint32_t ui32Load;
volatile uint32_t ui32PWMClock;
volatile uint8_t ui8Adjust;
ui8Adjust = 83;

SysCtlPWMClockSet(SYSCTL_PWMDIV_64);
SysCtlPeripheralEnable(SYSCTL_PERIPH_PWM1);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
GPIOPinTypePWM(GPIO_PORTD_BASE, GPIO_PIN_0);
GPIOPinConfigure(GPIO_PD0_M1PWM0);
GPIODirModeSet(GPIO_PORTF_BASE, GPIO_PIN_4|GPIO_PIN_0, GPIO_DIR_MODE_IN);
GPIOPadConfigSet(GPIO_PORTF_BASE, GPIO_PIN_4|GPIO_PIN_0, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD_WPU);
ui32PWMClock = SysCtlClockGet() / 64;
ui32Load = (ui32PWMClock / PWM_FREQUENCY) - 1;
PWMGenConfigure(PWM1_BASE, PWM_GEN_0, PWM_GEN_MODE_DOWN);
PWMGenPeriodSet(PWM1_BASE, PWM_GEN_0, ui32Load);
//PWMPulseWidthSet(PWM1_BASE, PWM_OUT_0, ui8Adjust * ui32Load / 1000);
PWMOutputState(PWM1_BASE, PWM_OUT_0_BIT, true);
PWMGenEnable(PWM1_BASE, PWM_GEN_0);


GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, RED_LED|BLUE_LED|GREEN_LED);
GPIOPinWrite(GPIO_PORTF_BASE, RED_LED|BLUE_LED|GREEN_LED, RED_LED);

while (1) {

//PWMPulseWidthSet(PWM1_BASE, PWM_OUT_0, ui8Adjust * ui32Load / 1000);


//GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_6, 0x40);


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_BURST_RECEIVE_START);
I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_SINGLE_RECEIVE);
SysCtlDelay(20000);
//while(I2CMasterBusy(I2C0_BASE)){GPIOPinWrite(GPIO_PORTN_BASE, GPIO_PIN_0, 0x01);}
g_I2C0Data[0] = I2CMasterDataGet(I2C0_BASE);
SysCtlDelay(50000);


// Master read data byte 2 and store
//I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_RECEIVE_CONT);
I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_SINGLE_RECEIVE);
SysCtlDelay(50000);
//while(I2CMasterBusy(I2C0_BASE)){GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_4, 0x01); }
g_I2C0Data[1] = I2CMasterDataGet(I2C0_BASE);
SysCtlDelay(50000);
//GPIOPinWrite(GPIO_PORTN_BASE, GPIO_PIN_0, 0x01); //turn on led when it reaches here to show it is reading the temp
// Master read data byte 3 (checksum) and store
//I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_RECEIVE_FINISH);
I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_SINGLE_RECEIVE);
SysCtlDelay(50000);
//GPIOPinWrite(GPIO_PORTN_BASE, GPIO_PIN_0, 0x00);
//while(I2CMasterBusy(I2C0_BASE)){GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_0, 0x01);}
g_I2C0Data[2] = I2CMasterDataGet(I2C0_BASE);
SysCtlDelay(50000);
//GPIOPinWrite(GPIO_PORTN_BASE, GPIO_PIN_0, 0x01); //turn on led when it reaches here to show it is reading the temp

//Convert to temperature
uint32_t tempConvert = ((uint32_t) g_I2C0Data[0] << 8)| (uint32_t) (g_I2C0Data[1]);
SysCtlDelay(50000);
//GPIOPinWrite(GPIO_PORTN_BASE, GPIO_PIN_0, 0x00);
float temp = (float) (tempConvert & 0xFFFC); //------this is when a number stops for "temp"
SysCtlDelay(50000);
//GPIOPinWrite(GPIO_PORTN_BASE, GPIO_PIN_0, 0x01);
temp = -46.85f + 175.72f * (temp / 65536.0f);
tempf = temp*(9.0f/5.0f)+32;

SysCtlDelay(50000);
//GPIOPinWrite(GPIO_PORTN_BASE, GPIO_PIN_0, 0x00);

//if (temp > 28.00f) {
if (tempf < 50.00f) {
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_2, 0x02);

GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_6, 0x40);

//GPIOPinWrite(GPIO_PORTF_BASE, RED_LED|BLUE_LED|GREEN_LED, RED_LED); // turns onboard led on


PWMPulseWidthSet(PWM1_BASE, PWM_OUT_0, (ui8Adjust/2) * ui32Load / 1000); //set servo to close vent

}


else if (tempf > 50.00f) {
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_2, 0x00);
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_6, 0x00);

//PWMPulseWidthSet(PWM1_BASE, PWM_OUT_0, ui8Adjust * ui32Load / 1000);
}
}
}

// 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
//Setup_i2C();
}

void LED(void) {
//SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
//GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_1 | GPIO_PIN_2);

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;
}