I need help sending 3 10bit integers and one 2bit command mode using SPI.
I'm trying to send 10bit color values (0-1024) to an array of shiftbrite led modules.
There is an arduino sketch that uses the arduino SPI class to send data:
http://docs.macetech.com/doku.php/shiftbrite
I've unsuccessfully tried reworking a similar example of the sketch using maples hardwareSPI class, something like this:
////////////////////////////////////////////////
int r = 900;
int g = 100;
int b = 100;
int cmode = 1;
for (int h = 0;h<11;h++)
{
// reworked to 7 as the first bite is insignificant, and the second should be 0 so that command mode is 0;
spi.write((cmode << 7 | b >>4));
spi.write((b << 4 | r >>6));
spi.write((r << 2 | g >> 8));
spi.write(g);
}
delayMicroseconds(1);
digitalWrite(latchpin,HIGH); // latch data into registers
delayMicroseconds(1);
digitalWrite(latchpin,LOW);
delay(1000);
///////////////////////////////////////////////////
I've also tried using a software SPI solution using shiftOut():
///////////////////////////////////////////////////
int r = 900;
int g = 100;
int b = 100;
int cmode = 1
for (int h = 0;h<11;h++)
{
shiftOut(dataPin, clockPin, MSBFIRST, c << 6 | b>>4);
shiftOut(dataPin, clockPin, MSBFIRST, b << 4 | r >> 6);
shiftOut(dataPin, clockPin, MSBFIRST, r << 2 | g >> 8);
shiftOut(dataPin, clockPin, MSBFIRST, g);
}
////////////////////////////////////////////////////
Data IS being sent out to the shiftbrights through the SPI MOSI as modding the values gives me varied results. But I'm not seeing what I should (colors are different on each module, some are blank, sometimes colors change).
1. I have a hunch that the shiftleft and shiftright functions aren't outputting an 8bit integer (uint8)that can be sent as a clean byte; as a standard integer is uint16 (2bytes). Example:
int r = 1023 // binary: 00000011 11111111);
int8 rshifted = (r << 4); binary 00111111 11110000, or uint16 16368);
when I convert rshifted to a byte, will binary result be:
a) 11110000 (the last 8 bits)
b) 00111111 (the first 8 bits) ;
c) some other value like 255 or null?
I've tried SerialUSB sending the result but I get a blank line.
2. What happens when I send less than 8 bits through the SPI using MSB first? If I send
spi.write(1) (binary 00000001); does it send the 0's or just the MSB (1);
3. In SPI's begin(frequency, bitOrder, mode); what exactly is the mode?