#include <msp430.h>
#include <legacymsp430.h>
+#include <stdint.h>
+#include <stdlib.h>
#define LED0 BIT0 /* P1.0 */
+#define CLKP BIT1 /* P1.1 */
+#define CLKN BIT2 /* P1.2 */
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;
+ //P1DIR |= LED0 | CLKP | CLKN; /* set P1.0,P1.1,P1.2 as output */
+ //P1OUT |= LED0 | CLKP | CLKN; /* set P1.0,P1.1,P1.2 low */
+ P1DIR = 0xff; /* set P1.* to output for reduced power consumption */
+ P1OUT = LED0 | CLKP | CLKN;
+
/* set the counter to match on 511 which is 1s for 32kHz/8/8 (timer
* re-divides the interrupt clock by 8 */
- TACCR0 = 511;
+ TACCR0 = 15; /* count 16 intervals for 31ms delay */
/* enable the clock interrupt mode for TACCR0 match */
TACCTL0 = CCIE;
+ /* set timer A to use the aux clock in UP mode with ACLK/8 divider */
+ TACTL = TASSEL_1 | MC_1 | ID_3; /* and starts the timer */
}
int
BCSCTL1 |= DIVA_3; /* ACLK/8 */
BCSCTL3 |= XCAP_3; /* enable 12.5pF internal capacitance */
+ srand(0xf00d);
init_leds();
/* enable global interrupts */
}
}
+enum State {State_Wait, State_Delay, State_Driving};
+enum State state = State_Wait;
+static uint8_t count = 0;
+static int8_t offset = 0;
+static uint8_t pin_pos = CLKP;
+static uint8_t pin_neg = CLKN;
+
interrupt(TIMER0_A0_VECTOR)
TIMERA0_ISR(void)
{
- /* clear the interrupt flag and toggle LED */
+ /* clear the interrupt flag */
TACCTL0 &= ~CCIFG;
- P1OUT ^= LED0;
+ ++count;
+
+ /* if driving the stepper coil, stop after 32ms (1 interrupt) */
+ if (state == State_Driving) {
+ state = State_Wait;
+ P1OUT &= ~(CLKP | CLKN | LED0);
+ }
+
+ if (count == 32) {
+ count = 0;
+ offset = 1 + (rand() & 0x0f);
+ state = State_Delay;
+ }
+ if (state == State_Delay) {
+ --offset;
+ if (offset == 0) {
+ state = State_Driving;
+ P1OUT |= pin_pos | LED0;
+ P1OUT &= ~pin_neg;
+ /* swap the pins for the next tick */
+ uint8_t t = pin_pos;
+ pin_pos = pin_neg;
+ pin_neg = t;
+ }
+ }
}