Skip to content

Instantly share code, notes, and snippets.

@stianeikeland
Created March 20, 2011 15:06
Show Gist options
  • Save stianeikeland/878362 to your computer and use it in GitHub Desktop.
Save stianeikeland/878362 to your computer and use it in GitHub Desktop.
MSP430 Coffeetimer
/*
* Coffetimer (safetytimer)
*
* Activated when button (pin 4) is grounded.
* Connected to a RF - remote control on pin 5 and 6
* via a couple of transistors, controling the
* coffemakers power outlet.
*
* */
#include "msp430g2231.h"
#define LONGDELAY (1000000 / (8*8)) * 4
#define SHORTDELAY (1000000 / (8*8*8))
#define POWERONTIME 4500
const int redLed = BIT0;
const int button = BIT4;
const int powerOn = BIT5;
const int powerOff = BIT6;
volatile int powerActive = 0;
volatile unsigned int onTime = 0;
volatile int blinkState = 0;
unsigned int i, y;
void main(void) {
WDTCTL = WDTPW + WDTHOLD;
P1DIR = ~button; // Button is input, rest output..
P1OUT &= ~(powerOn | powerOff | redLed);
P1REN |= button; // Pull up
P1IES |= button; // High to low interrupt
P1IE |= button; // Interrupt on button..
P1IFG = 0; // Clear INT flag
// Use internal occilator as timer source.. / 8.. /8
BCSCTL1 = CALBC1_1MHZ; // Set range
DCOCTL = CALDCO_1MHZ; // SMCLK = DCO = 1MHz
BCSCTL2 |= DIVS_3; // Divder / 8 for smclk
TACTL = TASSEL_2 + MC_1 + ID_3 + TACLR; // Timer A, SMCLK src, up mode, divider / 8..
CCTL0 = CCIE; // capture/compare interrupt go!
CCR0 = SHORTDELAY;
__enable_interrupt();
while(1) {
powerActive = 0;
onTime = 0;
// Go to sleep, wait for button
P1IFG = 0;
LPM4;
// Waking up from button push..
// Blink and power on coffemaker..
for (i = 0; i < 4; i++) {
P1OUT ^= redLed | powerOn;
for (y = 0; y < 40; y++)
__delay_cycles(40000);
}
P1OUT &= ~(redLed | powerOn);
powerActive = 2;
LPM1;
// Waking up from timer.. blink and turn off coffeemaker..
for (i = 0; i < 12; i++) {
P1OUT ^= redLed | powerOff;
for (y = 0; y < 40; y++)
__delay_cycles(40000);
}
P1OUT &= ~(redLed | powerOff);
}
}
// Button interrupt
#pragma vector=PORT1_VECTOR
__interrupt void port1_isr(void)
{
if (powerActive == 0) {
// Power on coffeemaker..
powerActive = 1;
LPM4_EXIT;
} else if (powerActive == 2) {
// Power off coffeemaker ahead of time..
onTime = POWERONTIME + 1;
P1OUT &= ~redLed;
powerActive = 0;
LPM1_EXIT;
}
P1IFG = 0;
}
// Timer interrupt, blink twice every few seconds..
#pragma vector=TIMERA0_VECTOR
__interrupt void timera0_isr(void)
{
if (powerActive != 2)
return;
CCR0 = (++blinkState % 4) ? SHORTDELAY : LONGDELAY;
if (blinkState % 2)
P1OUT |= redLed;
else
P1OUT &= ~redLed;
onTime += (blinkState % 4) ? 1 : 4;
// Wake from sleep if coffeemaker has been on long enough
if (onTime > POWERONTIME) {
P1OUT &= ~redLed;
powerActive = 0;
LPM1_EXIT;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment