User manual

LPCXpresso Experiment Kit - User’s Guide
Page 96
Copyright 2013 © Embedded Artists AB
7.14.2 Lab 13b: Read LM75 Temperature Sensor
In this experiment a temperature sensor, LM75, shall be sampled and the temperature presented. It is
essential to study the LM75 datasheet before writing any code. The LM75 has a simple interface and
luckily no register initialization is needed before it is possible to read the temperature. It is just a matter
of reading from the correct register.
The code below presents two functions. One for reading the temperature and one for writing in
configuration registers. Complete the last statement in function lm75a_readTemp() to calculate
the correct temperature. Create a semihosting application that samples the temperature every third
second and prints the result on the console.
#include “i2c.h”
#define LM75B_I2C_ADDR 0x90
#define LM75B_REG_TEMP 0x00
#define LM75B_REG_CMD 0x01
/******************************************************************************
*
* Description:
* Read temperature register of LM75B
*
* Params: None
* Returns: Temperature * 100 in integer format
*
*****************************************************************************/
int32_t lm75b_readTemp(void)
{
uint8_t cmd, temp[2];
int32_t t = 0;
cmd = LM75B_REG_TEMP;
I2CWrite(LM75B_I2C_ADDR, &cmd, 1);
I2CRead( LM75B_I2C_ADDR, &temp[0], 2);
/* 11 MSB bits used. Celsius is calculated as Temp data * 1/8 */
t = ((temp[0] << 8) | (temp[1]));
/* Return temperature times 100, e.g., in 0.01 degrees */
return ...;
}
/******************************************************************************
*
* Description:
* Write to config register of LM75B
*
* Params: Config byte
* Returns: None
*
*****************************************************************************/
void lm75b_config(int8_t config)
{
uint8_t cmd[2];
cmd[0] = LM75B_REG_CMD;
cmd[1] = config;
I2CWrite(LM75B_I2C_ADDR, &cmd[0], 2);
}
/*****************************************************************************
** Function name: main
** Descriptions: The main function
** Parameters: None
** Returned value: None
**