#include #include enum event_type { EVENT_NONE = 0, EVENT_PWM1_ON, EVENT_PWM2_ON, EVENT_PWM1_OFF, EVENT_PWM2_OFF, }; struct event { uint8_t time; enum event_type type; }; static struct event events[] = { { .time = 0, .type = EVENT_PWM1_ON, }, // { .time = 0, .type = EVENT_PWM2_ON, }, { .time = 1, .type = EVENT_PWM1_OFF, }, // { .time = 20, .type = EVENT_PWM2_OFF, }, { .time = 0, .type = EVENT_NONE, }, }; int main(void) { uint8_t i = 0; WDTCTL = WDTPW | WDTHOLD; P1DIR = BIT0 | BIT6; P1OUT = BIT0; TACCTL0 = CCIE; _BIS_SR(GIE); TACTL = TASSEL_2 | MC_2 | ID_3; TACCR0 = 100; // _BIS_SR(CPUOFF + GIE); while (1) { __delay_cycles(20000); --events[1].time; } return 0; } static inline void timer_event(struct event *event) { switch(event->type) { case EVENT_PWM1_ON: P1OUT |= BIT0; break; case EVENT_PWM1_OFF: P1OUT &= ~BIT0; break; case EVENT_PWM2_ON: P1OUT |= BIT6; break; case EVENT_PWM2_OFF: P1OUT &= ~BIT6; break; default: break; } } #pragma vector=TIMERA0_VECTOR __interrupt void timera_intr(void) { static uint8_t i = 0; if (events[i].type == EVENT_NONE) i = 0; if (i == 0) TAR = 0; found_event: timer_event(&events[i]); ++i; if (events[i].type != EVENT_NONE && events[i].time <= TAR) { goto found_event; } if (events[i].type == EVENT_NONE) { TACCR0 = 255 + events[0].time; } else { TACCR0 = events[i].time; } }