From 3d407534642b295bb896ad5673847a47e263f469 Mon Sep 17 00:00:00 2001 From: Travis Goodspeed Date: Sun, 24 Sep 2017 18:00:54 -0400 Subject: [PATCH] A basic clock is now functional. Close #11. --- firmware/Makefile | 2 +- firmware/main.c | 9 +++-- firmware/rtc.c | 86 +++++++++++++++++++++++++++++++++++++++++++++++ firmware/rtc.h | 7 ++++ 4 files changed, 101 insertions(+), 3 deletions(-) create mode 100644 firmware/rtc.c create mode 100644 firmware/rtc.h diff --git a/firmware/Makefile b/firmware/Makefile index f1ed677..09dbae4 100644 --- a/firmware/Makefile +++ b/firmware/Makefile @@ -5,7 +5,7 @@ CC = msp430-gcc -mmcu=cc430f6137 -Wall BSL = ../bin/cc430-bsl.py -modules=main.o lcd.o lcdtext.o +modules=main.o lcd.o lcdtext.o rtc.o all: goodwatch.hex diff --git a/firmware/main.c b/firmware/main.c index adde4b2..c0de6c2 100644 --- a/firmware/main.c +++ b/firmware/main.c @@ -11,9 +11,10 @@ int main(void) { WDTCTL = WDTPW + WDTHOLD; // Stop WDT lcd_init(); + rtc_init(); // Setup and enable WDT 1000ms, ACLK, interval timer - WDTCTL = WDT_ADLY_1000; + WDTCTL = WDT_ADLY_250; SFRIE1 |= WDTIE; __bis_SR_register(LPM0_bits + GIE); // Enter LPM3 w/interrupt @@ -22,5 +23,9 @@ int main(void) { // Watchdog Timer interrupt service routine, calls back to handler functions. void __attribute__ ((interrupt(WDT_VECTOR))) watchdog_timer (void) { - lcd_wdt(); + //lcd_wdt(); + //draw_time(); + + //Blink the colon faster than the RTC. + setcolon(3); } diff --git a/firmware/rtc.c b/firmware/rtc.c new file mode 100644 index 0000000..8a56e4b --- /dev/null +++ b/firmware/rtc.c @@ -0,0 +1,86 @@ +/* RTC driver for the GoodWatch. + */ + +#include + +#include "rtc.h" +#include "lcd.h" +#include "lcdtext.h" + + + +static int h=17, m=9, s=0; + +//! Get the hour. +int get_hour(){ + return h%24; +} +//! Get the minute. +int get_minute(){ + return m%60; +} +//! Get the second. +int get_second(){ + return s%60; +} + +//! Increase the clock by one second. +void tick_clock(){ + if(++s>60){ + m++; + s-=60; + } + if(m>60){ + h++; + m-=60; + } + if(h>24){ + h-=24; + } +} + +void draw_time(){ + int hour=get_hour(); + int min=get_minute(); + int sec=get_second(); + + lcd_zero(); + lcd_digit(7,hour/10); + lcd_digit(6,hour%10); + setcolon(1); + lcd_digit(4,min/10); + lcd_digit(3,min%10); + //space + lcd_digit(1,sec/10); + lcd_digit(0,sec%10); +} + +void rtc_init(){ + // Setup RTC Timer + RTCCTL01 = RTCTEVIE + RTCSSEL_2 + RTCTEV_0; // Counter Mode, RTC1PS, 8-bit ovf + // overflow interrupt enable + RTCPS0CTL = RT0PSDIV_2; // ACLK, /8, start timer + RTCPS1CTL = RT1SSEL_2 + RT1PSDIV_3; // out from RT0PS, /16, start timer + + //__bis_SR_register(LPM3_bits + GIE); +} + +void __attribute__ ((interrupt(RTC_VECTOR))) RTC_ISR (void){ + //switch(__even_in_range(RTCIV,16)) + switch(RTCIV&~1){ + case 0: break; // No interrupts + case 2: break; // RTCRDYIFG + case 4: // RTCEVIFG + tick_clock(); + draw_time(); + break; + case 6: break; // RTCAIFG + case 8: break; // RT0PSIFG + case 10: break; // RT1PSIFG + case 12: break; // Reserved + case 14: break; // Reserved + case 16: break; // Reserved + default: break; + } + +} diff --git a/firmware/rtc.h b/firmware/rtc.h new file mode 100644 index 0000000..9a661f2 --- /dev/null +++ b/firmware/rtc.h @@ -0,0 +1,7 @@ + + +int get_hour(); +int get_minute(); +int get_second(); + +void draw_time();