diff options
-rw-r--r-- | Makefile | 1 | ||||
-rwxr-xr-x[-rw-r--r--] | board.c | 4 | ||||
-rwxr-xr-x[-rw-r--r--] | board.h | 12 | ||||
-rwxr-xr-x[-rw-r--r--] | delay.c | 15 | ||||
-rwxr-xr-x[-rw-r--r--] | delay.h | 2 | ||||
-rwxr-xr-x[-rw-r--r--] | i2c.c | 0 | ||||
-rwxr-xr-x[-rw-r--r--] | i2c.h | 0 | ||||
-rwxr-xr-x | lcd.c | 61 | ||||
-rwxr-xr-x | lcd.h | 6 | ||||
-rwxr-xr-x[-rw-r--r--] | main.c | 26 | ||||
-rwxr-xr-x[-rw-r--r--] | temp.c | 15 | ||||
-rwxr-xr-x[-rw-r--r--] | temp.h | 2 |
12 files changed, 124 insertions, 20 deletions
@@ -5,6 +5,7 @@ CFLAGS = -mmcu=msp430g2553 -std=gnu99 \ CSRC = board.c \ delay.c \ i2c.c \ + lcd.c \ temp.c \ main.c COBJ = $(patsubst %.c, %.o, $(CSRC)) @@ -25,6 +25,10 @@ static void boardInitPins(void); void boardInit(void) { + // Slow down needed for LCD + BCSCTL1 = CALBC1_1MHZ; + DCOCTL = CALDCO_1MHZ; + boardInitWatchdog(); boardInitPins(); } @@ -25,19 +25,19 @@ #include <stdint.h> // Port 1 pins -#define LED (1 << 0) +#define LCD_DATL (0x0F) +#define LCD_RW (1 << 4) +#define LCD_RS (1 << 5) #define TEMP_SCL (1 << 6) #define TEMP_SDA (1 << 7) -#define PORT1_PINS (LED | TEMP_SCL | TEMP_SDA) +#define PORT1_PINS (LCD_DATL | LCD_RW | LCD_RS | TEMP_SCL | TEMP_SDA) // Port 2 pins -#define LCD_DAT (0x0F) +#define LCD_DATH (0x0F) #define LCD_E (1 << 4) -#define LCD_RW (1 << 5) -#define LCD_RS (1 << 6) -#define PORT2_PINS (LCD_DAT | LCD_E | LCD_RW | LCD_RS) +#define PORT2_PINS (LCD_DATH | LCD_E) /** * Initializes pins and core functionality. @@ -20,18 +20,23 @@ #include "board.h" -static unsigned int delayTicks = 0; +volatile unsigned int delayTicks = 0; void delayInit(void) { TACCTL0 |= CCIE; - TACCR0 = 1000; - TACTL |= TASSEL_1 | MC_1; + TACCR0 = 128; + TACTL |= TASSEL_2 | MC_1 | ID_3; +} + +void delay(unsigned int ms) +{ + unsigned int target = delayTicks + ms; + while (delayTicks < target); } __attribute__((__interrupt__(TIMER0_A0_VECTOR))) static void delayInterruptHandler(void) { - P1OUT ^= LED; - delayTicks++; + delayTicks++; } @@ -23,5 +23,7 @@ void delayInit(void); +void delay(unsigned int ms); + #endif // DELAY_H_ @@ -0,0 +1,61 @@ +#include "board.h"
+#include "delay.h"
+#include "lcd.h"
+
+static void lcdPulseE(void);
+static void lcdSetDAT(uint8_t value);
+static void lcdSendCommand(uint8_t command);
+static void lcdSendData(uint8_t data);
+
+void lcdInit(void)
+{
+ // Give time for LCD to prepare itself
+ delay(100);
+ lcdSendCommand(0x38);
+ lcdSendCommand(0x10);
+ lcdSendCommand(0x0D);
+ delay(5);
+ lcdClear();
+}
+
+void lcdClear(void)
+{
+ lcdSendCommand(0x01);
+ delay(2);
+}
+
+void lcdPuts(const char *s)
+{
+ lcdSendCommand(0x06);
+ while (*s != '\0')
+ lcdSendData(*s++);
+}
+
+void lcdPulseE(void)
+{
+ P2OUT |= LCD_E;
+ delay(2);
+ P2OUT &= ~(LCD_E);
+}
+
+void lcdSetDAT(uint8_t value)
+{
+ P2OUT &= ~(LCD_DATH);
+ P2OUT |= (value >> 4) & LCD_DATH;
+ P1OUT &= ~(LCD_DATL);
+ P1OUT |= value & LCD_DATL;
+}
+
+void lcdSendCommand(uint8_t command)
+{
+ P1OUT &= ~(LCD_RS);
+ lcdSetDAT(command);
+ lcdPulseE();
+}
+
+void lcdSendData(uint8_t data)
+{
+ P1OUT |= LCD_RS;
+ lcdSetDAT(data);
+ lcdPulseE();
+}
@@ -0,0 +1,6 @@ +
+void lcdInit(void);
+
+void lcdClear(void);
+void lcdPuts(const char *s);
+
@@ -20,20 +20,36 @@ #include "board.h" #include "delay.h" +#include "lcd.h" #include "temp.h" -void main(void) +#include <stdio.h> + +int main(void) { // Prepare processor and IO boardInit(); delayInit(); + _NOP(); __enable_interrupt(); - // Prepare temperature sensor - //if (tempInit() != 0) - // while (1); // Sensor error, halt + lcdInit(); + + if (tempInit() != 0) { + lcdPuts("Sensor broken."); + while (1); + } + + char buf[8]; + while (1) { + lcdClear(); + int16_t temp = tempGetDieTemperature(); + sprintf(buf, "%04d", temp); + lcdPuts(buf); + delay(1000); + } - while (1); + return 0; } @@ -55,13 +55,20 @@ int tempInit(void) TMP006_CFG_16SAMPLE); // Check that the device is in fact the TMP006 - if (tempI2CRead(TMP006_MANID) != 0x5449 || - tempI2CRead(TMP006_DEVID) != 0x67) - return -1; + uint16_t manid = tempI2CRead(TMP006_MANID); + uint16_t devid = tempI2CRead(TMP006_DEVID); + if (manid != 0x5449 || devid != 0x67) + return 1; return 0; } +int16_t tempGetDieTemperature(void) +{ + int16_t raw = tempI2CRead(TMP006_TAMB); + return (raw >> 2) / 32; +} + void tempI2CWrite(uint8_t reg, uint16_t value) { uint8_t data[3] = { @@ -80,6 +87,6 @@ uint16_t tempI2CRead(uint8_t reg) }; I2C_write(1, data); I2C_read(2, data); - return *((uint16_t *)data); + return (data[0] << 8) | data[1]; } @@ -27,5 +27,7 @@ */ int tempInit(void); +int16_t tempGetDieTemperature(void); + #endif // TEMP_H_ |