From f8887251a7dd158490c7bcb3ba4cd7757fb7f2f7 Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Mon, 29 Apr 2019 10:20:40 -0400 Subject: temperature sensor draft --- .gitmodules | 3 +++ Makefile | 2 ++ board.c | 4 +++ board.h | 1 + i2c | 1 + main.c | 5 ++++ temp.c | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ temp.h | 31 ++++++++++++++++++++++ 8 files changed, 132 insertions(+) create mode 100644 .gitmodules create mode 160000 i2c create mode 100644 temp.c create mode 100644 temp.h diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..2fbd7ce --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "i2c"] + path = i2c + url = https://github.com/heliocapella/msp430g2553_i2c diff --git a/Makefile b/Makefile index cdfb395..7653660 100644 --- a/Makefile +++ b/Makefile @@ -3,6 +3,8 @@ CFLAGS = -mmcu=msp430g2553 -std=gnu99 \ -Os CSRC = board.c \ + i2c/i2c.c \ + temp.c \ main.c COBJ = $(patsubst %.c, %.o, $(CSRC)) diff --git a/board.c b/board.c index 26db473..27af0da 100644 --- a/board.c +++ b/board.c @@ -43,5 +43,9 @@ void boardInitPins(void) // All pins should start at a low state P1OUT = 0; P2OUT = 0; + + // Set I2C pins to the proper mode + P1SEL |= TEMP_SDA | TEMP_SCL; + P1SEL2 |= TEMP_SDA | TEMP_SCL; } diff --git a/board.h b/board.h index c521e7e..abbc952 100644 --- a/board.h +++ b/board.h @@ -45,3 +45,4 @@ void boardInit(void); #endif // BOARD_H_ + diff --git a/i2c b/i2c new file mode 160000 index 0000000..6c90102 --- /dev/null +++ b/i2c @@ -0,0 +1 @@ +Subproject commit 6c90102daca09c221d62391ddf2fe989cc0a1ce7 diff --git a/main.c b/main.c index ed788a5..257cebf 100644 --- a/main.c +++ b/main.c @@ -19,12 +19,17 @@ */ #include "board.h" +#include "temp.h" void main(void) { // Prepare processor and IO boardInit(); + // Prepare temperature sensor + if (tempInit() != 0) + while (1); // Sensor error, halt + while (1); } diff --git a/temp.c b/temp.c new file mode 100644 index 0000000..38e873d --- /dev/null +++ b/temp.c @@ -0,0 +1,85 @@ +/** + * @file temp.c + * Provides support for the TMP006 temperature sensor. + * + * Copyright (C) 2019 Clyne Sullivan + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "board.h" +#include "i2c/i2c.h" + +// TMP006 I2C address +#define TEMP_ADDR (0x40) + +// TMP006 registers +#define TMP006_CONFIG (0x02) +#define TMP006_VOBJ (0x00) +#define TMP006_TAMB (0x01) +#define TMP006_MANID (0xFE) +#define TMP006_DEVID (0xFF) + +// TMP006 CONFIG register parameters +#define TMP006_CFG_RESET (0x8000) +#define TMP006_CFG_MODEON (0x7000) +#define TMP006_CFG_1SAMPLE (0x0000) +#define TMP006_CFG_2SAMPLE (0x0200) +#define TMP006_CFG_4SAMPLE (0x0400) +#define TMP006_CFG_8SAMPLE (0x0600) +#define TMP006_CFG_16SAMPLE (0x0800) +#define TMP006_CFG_DRDYEN (0x0100) +#define TMP006_CFG_DRDY (0x0080) + +static void tempI2CWrite(uint8_t reg, uint16_t value); +static uint16_t tempI2CRead(uint8_t reg); + +int tempInit(void) +{ + // Prepare I2C + I2C_init(TEMP_ADDR); + + // Initialize the TMP006 sensor + tempI2CWrite(TMP006_CONFIG, TMP006_CFG_MODEON | TMP006_CFG_DRDYEN | + TMP006_CFG_16SAMPLE); + + // Check that the device is in fact the TMP006 + if (tempI2CRead(TMP006_MANID) != 0x5449 || + tempI2CRead(TMP006_DEVID) != 0x67) + return -1; + + return 0; +} + +void tempI2CWrite(uint8_t reg, uint16_t value) +{ + uint8_t data[3] = { + reg, + value >> 8, + value & 0xFF + }; + I2C_write(3, data); +} + +uint16_t tempI2CRead(uint8_t reg) +{ + uint8_t data[2] = { + reg, + 0 + }; + I2C_write(1, data); + I2C_read(2, data); + return *((uint16_t *)data); +} + diff --git a/temp.h b/temp.h new file mode 100644 index 0000000..184becd --- /dev/null +++ b/temp.h @@ -0,0 +1,31 @@ +/** + * @file temp.h + * Provides support for the TMP006 temperature sensor. + * + * Copyright (C) 2019 Clyne Sullivan + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef TEMP_H_ +#define TEMP_H_ + +/** + * Attempts to initialize the TMP006 temperature sensor. + * @return Zero on success, non-zero on failure + */ +int tempInit(void); + +#endif // TEMP_H_ + -- cgit v1.2.3