Skip to content

Instantly share code, notes, and snippets.

@PhirePhly
Created February 6, 2011 21:04
Show Gist options
  • Save PhirePhly/813720 to your computer and use it in GitHub Desktop.
Save PhirePhly/813720 to your computer and use it in GitHub Desktop.
Low Power LCD Clock - MSP430G2231
// Low-power LCD Clock
// Kenneth Finnegan, 2011
// http://kennethfinnegan.blogspot.com/
#include "msp430G2231.h"
// Pin Assignments
#define MOSI 0
#define SCK 1
#define SS 2
// Which display segment is being displayed
volatile unsigned char subsec, second, minute, hour,
butdeb[2];
// FUNCTIONS
void shiftOut(unsigned char d);
void updateTime(void);
void main(void) {
WDTCTL = WDTPW + WDTHOLD; // Stop Watchdog Timer
BCSCTL2 = 0xF8; // MCLK:LFXT1CLK/8 SMCLK:LFXT1CLK
butdeb[0] = butdeb[1] = 0; // Reset button debounce
P1DIR = 0x3F; // I/O
P1OUT = 0xFF; // All high
P1REN = 0xC0; // Pullup on inputs
P1IES = 0xC0; // H to L transition
P1IFG = 0x00; // Clear noise int
P1IE = 0xC0; // Enable button int
second = minute = hour = 0;
/*shiftOut(0x80 | (minute % 10));
shiftOut(0x40 | (minute / 10));
shiftOut(0x20 | (hour % 10));
shiftOut(0x10 | (hour / 10));*/
shiftOut(0xF0); // Everything is zero
CCTL0 = CCIE;
CCR0 = 0;
TACCR0 = 0x3FF; // Period of 1023 + 1 cnts, which is 32Hz
TACTL = 0x0211; // Timer_A: SMCLK, UP MODE, TAIE
// Sleep forever...
_BIS_SR(LPM1_bits + GIE);
}
#pragma vector=PORT1_VECTOR
__interrupt void Port1 (void) {
// Increment minute
if ((P1IFG & 0x80) && !butdeb[1]) {
butdeb[1] = 8;
second = 0;
minute = (minute + 1) % 60;
shiftOut(0x80 | (minute % 10));
shiftOut(0x40 | (minute / 10));
}
// Increment hour
if ((P1IFG & 0x40) && !butdeb[0]) {
butdeb[0] = 8;
hour = (hour + 1) % 24;
shiftOut(0x20 | (hour % 10));
shiftOut(0x10 | (hour / 10));
}
P1IFG = 0x00;
}
// 32Hz heartbeat
#pragma vector=TIMERA0_VECTOR
__interrupt void Timer_A (void) {
subsec = (subsec + 1) % 32;
if (subsec == 0) { // New second
updateTime();
}
butdeb[0]?butdeb[0]--:1;
butdeb[1]?butdeb[1]--:1;
}
void updateTime( void ) {
second = (second + 1) % 60;
if ( second == 0 ) { // new minute
minute = (minute + 1) % 60;
shiftOut(0x80 | (minute % 10));
if ( (minute % 10) == 0) { // new 10s min
shiftOut(0x40 | (minute / 10));
}
if ( minute == 0) { // new hour
hour = (hour + 1) % 24;
shiftOut(0x20 | (hour % 10));
if ( (hour % 10) == 0) { // new 10s hr
shiftOut(0x10 | (hour / 10));
}
}
}
}
void shiftOut(unsigned char d) {
int i;
P1OUT = P1OUT & ~(1<<SS);
for (i = 7; i>=0; i--) {
P1OUT = (P1OUT & ~(1<<MOSI)) | (((d>>i)&1)<<MOSI);
P1OUT = P1OUT & ~(1<<SCK);
P1OUT = P1OUT | 1<<SCK;
}
P1OUT |= (1<<SS);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment