Blink LED using watch crystal driven interrupts at 1s intervals
authorPat Thoyts <patthoyts@users.sourceforge.net>
Thu, 4 Dec 2014 07:45:11 +0000 (07:45 +0000)
committerPat Thoyts <patthoyts@users.sourceforge.net>
Thu, 4 Dec 2014 07:45:11 +0000 (07:45 +0000)
.gitignore [new file with mode: 0644]
Makefile [new file with mode: 0644]
main.c [new file with mode: 0644]

diff --git a/.gitignore b/.gitignore
new file mode 100644 (file)
index 0000000..7a9362f
--- /dev/null
@@ -0,0 +1,2 @@
+*.elf
+*~
diff --git a/Makefile b/Makefile
new file mode 100644 (file)
index 0000000..d46acdc
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,17 @@
+MCU   = msp430g2111
+CC    = msp430-gcc
+CFLAGS=-Wall -g -Os -mmcu=$(MCU)
+LDFLAGS=
+
+main.elf: main.c
+       $(CC) $(CFLAGS) -o $@ $<
+
+%.o: %.c
+       $(CC) $(CFLAGS) -c $< -o $@
+
+prog: program
+program: main.elf
+       MCU=$(MCU) mspdebug rf2500 "prog $<"
+
+clean:
+       rm -f main.elf
diff --git a/main.c b/main.c
new file mode 100644 (file)
index 0000000..155a72c
--- /dev/null
+++ b/main.c
@@ -0,0 +1,49 @@
+#include <msp430.h>
+#include <legacymsp430.h>
+
+#define LED0 BIT0 /* P1.0 */
+
+static void
+init_leds(void)
+{
+    P1DIR |= LED0; /* set P1.0 as output */
+    P1OUT |= LED0; /* set P1.0 low */
+
+    /* set timer A to use the aux clock in UP mode with ACLK/8 divider */
+    TACTL = TASSEL_1 | MC_1 | ID_3;
+    /* set the counter to match on 511 which is 1s for 32kHz/8/8 (timer
+     * re-divides the interrupt clock by 8 */
+    TACCR0 = 511;
+    /* enable the clock interrupt mode for TACCR0 match */
+    TACCTL0 = CCIE;
+}
+
+int
+main(void)
+{
+    /* disable the watchdog */
+    WDTCTL = WDTPW + WDTHOLD;
+    
+    /* external 32.768kHz crystal */
+    BCSCTL1 |= DIVA_3; /* ACLK/8 */
+    BCSCTL3 |= XCAP_3; /* enable 12.5pF internal capacitance */
+    
+    init_leds();
+
+    /* enable global interrupts */
+    eint();
+    
+    while (1) {
+        /* do nothing forever, timer interrupts drive the leds */
+        /* enter low power mode 3 with interrupts enabled) */
+        _BIS_SR(LPM3_bits + GIE);
+    }
+}
+
+interrupt(TIMER0_A0_VECTOR)
+TIMERA0_ISR(void)
+{
+    /* clear the interrupt flag and toggle LED */
+    TACCTL0 &= ~CCIFG;
+    P1OUT ^= LED0;
+}