]> code.bitgloo.com Git - clyne/msp430-temp-lcd.git/commitdiff
temperature sensor draft
authorClyne Sullivan <tullivan99@gmail.com>
Mon, 29 Apr 2019 14:20:40 +0000 (10:20 -0400)
committerClyne Sullivan <tullivan99@gmail.com>
Mon, 29 Apr 2019 14:20:40 +0000 (10:20 -0400)
.gitmodules [new file with mode: 0644]
Makefile
board.c
board.h
i2c [new submodule]
main.c
temp.c [new file with mode: 0644]
temp.h [new file with mode: 0644]

diff --git a/.gitmodules b/.gitmodules
new file mode 100644 (file)
index 0000000..2fbd7ce
--- /dev/null
@@ -0,0 +1,3 @@
+[submodule "i2c"]
+       path = i2c
+       url = https://github.com/heliocapella/msp430g2553_i2c
index cdfb395b13d04332e79698b3effccec6b0400a9a..76536609380e227ed9a80e67b5dfe4e14933ea50 100644 (file)
--- 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 26db473b091a9fea38cb762cd7aa69c2e9bf08a7..27af0daef4bd35e6c174abdab76991ff4a1d9f1d 100644 (file)
--- 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 c521e7e08b11f04496c17154a93403d709c373b3..abbc952ec3634de2a17031a963005d840443f8cc 100644 (file)
--- 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 (submodule)
index 0000000..6c90102
--- /dev/null
+++ b/i2c
@@ -0,0 +1 @@
+Subproject commit 6c90102daca09c221d62391ddf2fe989cc0a1ce7
diff --git a/main.c b/main.c
index ed788a5241cceaf88478bf813bb458ed33da3aa5..257cebfd36de93a3c410412548721fc2871ca62a 100644 (file)
--- a/main.c
+++ b/main.c
  */
 
 #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 (file)
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 <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);
+}
+
diff --git a/temp.h b/temp.h
new file mode 100644 (file)
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 <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_
+