#define F_CPU 8000000UL #include #include #include #include #define __LATCH_LOW PORTB &= ~(1 << PB4) #define __LATCH_HIGH PORTB |= (1 << PB4) void setup(void); void loop(void); uint8_t spi_transfer(uint8_t data); int main(void); void setup(void) { DDRB |= _BV(PB0); // set LED pin as output PORTB |= _BV(PB0); // turn the LED on // USI stuff DDRB |= _BV(PB4); // as output (latch) DDRB |= _BV(PB6); // as output (DO) DDRB |= _BV(PB7); // as output (USISCK) DDRB &= ~_BV(PB5); // as input (DI) PORTB |= _BV(PB5); // pullup on (DI) } void loop(void) { __LATCH_LOW; spi_transfer(0x01); // channel 1 active (red) __LATCH_HIGH; _delay_ms(500); PORTB ^= _BV(PB0); // toggle LED __LATCH_LOW; spi_transfer(0x02); // channel 2 active (green) __LATCH_HIGH; _delay_ms(500); PORTB ^= _BV(PB0); // toggle LED __LATCH_LOW; spi_transfer(0x04); // channel 3 active (blue) __LATCH_HIGH; _delay_ms(500); PORTB ^= _BV(PB0); // toggle LED __LATCH_LOW; spi_transfer(0x07); // channels 1,2,3 active (white) __LATCH_HIGH; _delay_ms(500); PORTB ^= _BV(PB0); // toggle LED __LATCH_LOW; spi_transfer(0x00); // all outputs off __LATCH_HIGH; _delay_ms(500); PORTB ^= _BV(PB0); // toggle LED } int main(void) { setup(); for(;;) { loop(); } }; /* Functions dealing with hardware specific jobs / settings */ uint8_t spi_transfer(uint8_t data) { USIDR = data; USISR = _BV(USIOIF); // clear flag while ( (USISR & _BV(USIOIF)) == 0 ) { USICR = (1<