Here is some code to use the

Here is some code to use the SPI of an AVR to program the Shiftbrites. It uses the SPI Data and Clock signals to transfer the data, the latching happens via PB1.


void
ShiftOutByte (unsigned char byte)
{
SPCR = (1 << SPE) | (1 << MSTR);
SPSR = (1 << SPI2X);
SPDR = byte;
while (!(SPSR & (1 << SPIF)));
}

#define SB_DAT (0x04)
#define SB_CLK (0x02)
#define SB_LAT (0x01)
#define SB_DELAY 50

void
ShiftOutCmd (unsigned char dest,
unsigned int red,
unsigned int green,
unsigned int blue)
{
unsigned long cmd = dest ? 1 : 0;

cmd = (cmd << 2) | (blue & 1023);
cmd = (cmd << 10) | (red & 1023);
cmd = (cmd << 10) | (green & 1023);

DDRB |= (SB_DAT | SB_CLK | SB_LAT);

ShiftOutByte ((cmd >> 24) & 0xff);
ShiftOutByte ((cmd >> 16) & 0xff);
ShiftOutByte ((cmd >> 8) & 0xff);
ShiftOutByte ((cmd >> 0) & 0xff);
}

void
Latch (void)
{
int j;

for (j = 0; j < SB_DELAY/2; j++);
PORTB |= SB_LAT;
for (j = 0; j < SB_DELAY/2; j++);
PORTB &= ~SB_LAT;
for (j = 0; j < SB_DELAY/2; j++);
}

Note that you need to Latch() if you want the shiftbrites to show the new color.

Hope this helps,
Simon

Reply

The content of this field is kept private and will not be shown publicly.