/****************************************************************************** * led_cloud_test * Keith Neufeld * November 23, 2008 * * Test LED cloud A6276 driver. * * Demo system uses Arduino and an A6276 LED driver wired with * LE on pin 0, CK on pin 1, and SDI on pin 2. * On boards, latch is pin 1, clock is pin 2, data is pin 3. ******************************************************************************/ #define FIRST_BANK (2) #define LAST_BANK (6) long leds[LAST_BANK + 1]; void setup() { a6276_init(); } void loop() { int latch; /* // flick all consecutively for (latch = FIRST_BANK; latch <= LAST_BANK; ++ latch) { a6276_cycle(latch, 0, 31, 10); } */ // flash 32 at a time /* for (latch = FIRST_BANK; latch <= LAST_BANK; ++ latch) { a6276_long(0xffffffff); a6276_latch(latch); delay(400); a6276_long(0x0); a6276_latch(latch); } */ int cont, led; cont = FIRST_BANK + random(LAST_BANK + 1 - FIRST_BANK); led = random(32); if (random(10) > 3) { leds[cont] |= (long) 1 << led; } else { leds[cont] &= ~ ((long) 1 << led); } a6276_long(leds[cont]); a6276_latch(cont); } /****************************************************************************** * Control an Allegro Microsystems A6276 LED driver. ******************************************************************************/ #define SDI 9 #define CK 8 #define LE 2 #define A6276_LEDS 32 // number of LEDs in string // Initialize ports for A6276 interfacing and turn off all LEDS. void a6276_init(void) { int i; digitalWrite(SDI, LOW); digitalWrite(CK, LOW); pinMode(SDI, OUTPUT); pinMode(CK, OUTPUT); pinMode(LE, OUTPUT); for (i = 0; i < 7; ++ i) { digitalWrite(i, LOW); pinMode(i, OUTPUT); } a6276_clear(); } // Shift A6276 register one bit. void a6276_shift(void) { digitalWrite(CK, HIGH); //delay(1); digitalWrite(CK, LOW); //delay(1); } // Feed one bit into A6276. DOES NOT latch to output register. void a6276_bit(int bit) { digitalWrite(SDI, bit ? HIGH : LOW); a6276_shift(); } // Latch A6276 contents to output register. void a6276_latch(int latch) { digitalWrite(latch, HIGH); delay(1); digitalWrite(latch, LOW); delay(1); } void a6276_long(long l32) { int i; for (i = 0; i < A6276_LEDS; ++ i) { a6276_bit(l32 & 1 ? HIGH : LOW); l32 >>= 1; } } void a6276_clear(void) { int latch; a6276_long(0); for (latch = FIRST_BANK; latch <= LAST_BANK; ++ latch) { a6276_latch(latch); } } #if 0 int a6276_contents; // Current contents when using a6276_write() // Write all LEDs to pattern in . Assumes int is large enough. void a6276_write(int bits) { int i; a6276_contents = bits; for (i = 0; i < A6276_LEDS; ++ i) { a6276_bit(bits & 1 ? HIGH : LOW); bits >>= 1; } a6276_latch(); } // Turn off all LEDs. void a6276_clear(void) { a6276_write(0); } // Turn on one LED. void a6276_on(int bit) { a6276_write(a6276_contents | (1 << bit)); } // Turn off one LED. void a6276_off(int bit) { a6276_write(a6276_contents & ! (1 << bit)); } // Turn on only one LED. void a6276_only(int bit) { a6276_write(1 << bit); } #endif //0 // Cycle LEDs from to with ms delay between. void a6276_cycle(int latch, int first, int last, int dlay) { int i; for (i = 0; i < A6276_LEDS; ++ i) { a6276_bit(i == first ? HIGH : LOW); if (first <= i && i <= last) { a6276_latch(latch); delay(dlay); } } a6276_bit(LOW); a6276_latch(latch); } #if 0 // Display a sequence of LEDs. void a6276_sequence(int *seq, int len, int dlay) { int i; for (i = 0; i < len; ++ i) { a6276_write(seq[i]); delay(dlay); } } #endif //0