From: Pat Thoyts Date: Sat, 6 Dec 2014 00:16:36 +0000 (+0000) Subject: Implemented irregular ticks. X-Git-Url: https://privyetmir.co.uk/gitweb.cgi?a=commitdiff_plain;h=773c831d213d508cf2cb115f29664bea92173f65;p=vetinari_clock Implemented irregular ticks. With two /8 dividers we get 512 interrupts per second. By counting 16 of these we get a 30ms interval to energise the coil for a tick. We add a random number up to 15 additional 30ms intervals to offset the actual time of the tick. --- diff --git a/main.c b/main.c index 155a72c..85ce41f 100644 --- a/main.c +++ b/main.c @@ -1,21 +1,27 @@ #include #include +#include +#include #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 @@ -28,6 +34,7 @@ main(void) BCSCTL1 |= DIVA_3; /* ACLK/8 */ BCSCTL3 |= XCAP_3; /* enable 12.5pF internal capacitance */ + srand(0xf00d); init_leds(); /* enable global interrupts */ @@ -40,10 +47,41 @@ main(void) } } +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; + } + } }