#include "msp430x20x1.h" #define PERIOD 64 #define MAX_THRESH PERIOD #define MIN_THRESH 0 #define DELTA 1 #define LED_COUNT 8 void main(void) { int i, j; unsigned char dir[] = {1, 1, 1, 1, 1, 1, 1, 1}; unsigned char p1_value; int threshold[] = { 0, PERIOD / LED_COUNT, 2 * PERIOD / LED_COUNT, 3 * PERIOD / LED_COUNT, 4 * PERIOD / LED_COUNT, 5 * PERIOD / LED_COUNT, 6 * PERIOD / LED_COUNT, 7 * PERIOD / LED_COUNT }; WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer P1DIR = 0xFF; while (1) { for (i = 0; i < PERIOD; i++) { int mask = 1; for (j = 0; j < LED_COUNT; ++j) { if (i > threshold[j]) { p1_value &= ~mask; } else { p1_value |= mask; } mask <<= 1; } P1OUT = p1_value; } // Update thresholds and directions. for (j = 0; j < LED_COUNT; ++j) { if (dir[j]) { threshold[j] += DELTA; } else { threshold[j] -= DELTA; } if (threshold[j] >= MAX_THRESH) { dir[j] = 0; } else if (threshold[j] <= MIN_THRESH) { dir[j] = 1; } } } }