BSP430  20141115
Board Support Package for MSP430 microcontrollers
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
Sensors: TMP102

The TMP102 is a continuously monitoring temperature sensor accessed through I2C.

main.c

#include <bsp430/clock.h>
#include <string.h>
unsigned int temp_xCel;
void main ()
{
hBSP430halSERIAL i2c = hBSP430serialLookup(APP_TMP102_I2C_PERIPH_HANDLE);
uint8_t pr = 0;
cprintf("I2C interface on %s is %p\n", xBSP430serialName(APP_TMP102_I2C_PERIPH_HANDLE), i2c);
#if BSP430_PLATFORM_PERIPHERAL_HELP
cprintf("TMP102 I2C Pins: %s\n", xBSP430platformPeripheralHelp(APP_TMP102_I2C_PERIPH_HANDLE, BSP430_PERIPHCFG_SERIAL_I2C));
#endif /* BSP430_PLATFORM_PERIPHERAL_HELP */
0, 0);
if (! i2c) {
cprintf("I2C open failed.\n");
return;
}
(void)iBSP430i2cSetAddresses_rh(i2c, -1, APP_TMP102_I2C_ADDRESS);
#define TMP102_RAW_TO_dC_(raw_) (5 * ((raw_) >> 3) / 16)
#define TMP102_RAW_TO_dC(raw_) ((0 <= (int)(raw_)) ? TMP102_RAW_TO_dC_(raw_) : -TMP102_RAW_TO_dC_(-(int)(raw_)))
#define dC_TO_dF(dC_) (320 + 9 * (dC_) / 5)
#if 0
{
unsigned int data[] = { 0x7FF0, 0x4B00, 0x1900, 0xFFC0, 0xE700, 0xC908 };
int i;
for (i = 0; i < sizeof(data)/sizeof(*data); ++i) {
int temp_dC = TMP102_RAW_TO_dC(data[i]);
cprintf("temp 0x%04x = %d dC = %d d[degF]\n", data[i], temp_dC, dC_TO_dF(temp_dC));
}
}
#endif
while (1) {
int rc;
uint8_t data[2];
uint16_t raw;
rc = iBSP430i2cTxData_rh(i2c, &pr, 1);
if (0 > rc) {
cprintf("I2C TX ERROR\n");
break;
}
memset(data, 0, sizeof(data));
rc = iBSP430i2cRxData_rh(i2c, data, sizeof(data));
if (0 > rc) {
cprintf("I2C RX ERROR\n");
break;
}
raw = data[1] | (data[0] << 8);
if (0 == pr) {
int temp_dC = TMP102_RAW_TO_dC(raw);
cprintf("temp 0x%04x = %d dC = %d d[degF]\n", raw, temp_dC, dC_TO_dF(temp_dC));
} else {
cprintf("reg %d is 0x%04x\n", pr, raw);
}
pr = (pr + 1) & 0x03;
}
}

bsp430_config.h

/* Use a crystal if one is installed. Much more accurate timing
* results. */
#define BSP430_PLATFORM_BOOT_CONFIGURE_LFXT1 1
/* Application does output: support spin-for-jumper */
#define configBSP430_PLATFORM_SPIN_FOR_JUMPER 1
/* Support console output */
#define configBSP430_CONSOLE 1
/* We need serial I2C for the TMP102 access */
#define configBSP430_SERIAL_ENABLE_I2C 1
/* What we use to access the TMP102 */
#if (BSP430_PLATFORM_EXP430F5438 - 0) || (BSP430_PLATFORM_TRXEB - 0)
#define APP_TMP102_I2C_PERIPH_HANDLE BSP430_PERIPH_USCI5_B3
#define configBSP430_HAL_USCI5_B3 1
#elif (BSP430_PLATFORM_EXP430F5529 - 0)
#define APP_TMP102_I2C_PERIPH_HANDLE BSP430_PERIPH_USCI5_B0
#define configBSP430_HAL_USCI5_B0 1
#elif (BSP430_PLATFORM_EXP430F5529LP - 0)
#define APP_TMP102_I2C_PERIPH_HANDLE BSP430_PERIPH_USCI5_B1
#define configBSP430_HAL_USCI5_B1 1
#elif ((BSP430_PLATFORM_EXP430FR5739 - 0) \
|| (BSP430_PLATFORM_EXP430FR4133 - 0) \
|| (BSP430_PLATFORM_EXP430FR5969 - 0) \
|| (BSP430_PLATFORM_WOLVERINE - 0))
#define APP_TMP102_I2C_PERIPH_HANDLE BSP430_PERIPH_EUSCI_B0
#define configBSP430_HAL_EUSCI_B0 1
#else
#define APP_TMP102_I2C_PERIPH_HANDLE BSP430_PERIPH_USCI_B0
#define configBSP430_HAL_USCI_B0 1
#endif
/* Address for the thing. 0x48 when ADD0 is shorted to GND. */
#define APP_TMP102_I2C_ADDRESS 0x48
/* Get platform defaults */

Makefile

PLATFORM ?= exp430fr5739
TEST_PLATFORMS_EXCLUDE=em430 surf wolverine exp430fr5969
MODULES=$(MODULES_PLATFORM)
MODULES += $(MODULES_UPTIME)
MODULES += $(MODULES_CONSOLE)
SRC=main.c
include $(BSP430_ROOT)/make/Makefile.common