diff options
author | Clyne Sullivan <tullivan99@gmail.com> | 2019-04-29 10:20:40 -0400 |
---|---|---|
committer | Clyne Sullivan <tullivan99@gmail.com> | 2019-04-29 10:20:40 -0400 |
commit | f8887251a7dd158490c7bcb3ba4cd7757fb7f2f7 (patch) | |
tree | 8f5bcce7dbd46d7b9346b45f45d2856af7377771 | |
parent | 249fa2dd7bcd8ed0be64a1a1b36da499d6bbaca1 (diff) |
temperature sensor draft
-rw-r--r-- | .gitmodules | 3 | ||||
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | board.c | 4 | ||||
-rw-r--r-- | board.h | 1 | ||||
m--------- | i2c | 0 | ||||
-rw-r--r-- | main.c | 5 | ||||
-rw-r--r-- | temp.c | 85 | ||||
-rw-r--r-- | temp.h | 31 |
8 files changed, 131 insertions, 0 deletions
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 @@ -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)) @@ -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; } @@ -45,3 +45,4 @@ void boardInit(void); #endif // BOARD_H_ + diff --git a/i2c b/i2c new file mode 160000 +Subproject 6c90102daca09c221d62391ddf2fe989cc0a1ce @@ -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); } @@ -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 <https://www.gnu.org/licenses/>. + */ + +#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); +} + @@ -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 <https://www.gnu.org/licenses/>. + */ + +#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_ + |