Permalink
Switch branches/tags
Nothing to show
Find file Copy path
fdfad44 Jan 5, 2015
1 contributor

Users who have contributed to this file

75 lines (61 sloc) 1.38 KB
const int strobe = 7;
const int clock = 9;
const int data = 8;
void sendCommand(uint8_t value)
{
digitalWrite(strobe, LOW);
shiftOut(data, clock, LSBFIRST, value);
digitalWrite(strobe, HIGH);
}
void reset()
{
sendCommand(0x40); // set auto increment mode
digitalWrite(strobe, LOW);
shiftOut(data, clock, LSBFIRST, 0xc0); // set starting address to 0
for(uint8_t i = 0; i < 16; i++)
{
shiftOut(data, clock, LSBFIRST, 0x00);
}
digitalWrite(strobe, HIGH);
}
void setup()
{
pinMode(strobe, OUTPUT);
pinMode(clock, OUTPUT);
pinMode(data, OUTPUT);
sendCommand(0x8f); // activate
reset();
}
uint8_t readButtons(void)
{
uint8_t buttons = 0;
digitalWrite(strobe, LOW);
shiftOut(data, clock, LSBFIRST, 0x42);
pinMode(data, INPUT);
for (uint8_t i = 0; i < 4; i++)
{
uint8_t v = shiftIn(data, clock, LSBFIRST) << i;
buttons |= v;
}
pinMode(data, OUTPUT);
digitalWrite(strobe, HIGH);
return buttons;
}
void setLed(uint8_t value, uint8_t position)
{
pinMode(data, OUTPUT);
sendCommand(0x44);
digitalWrite(strobe, LOW);
shiftOut(data, clock, LSBFIRST, 0xC1 + (position << 1));
shiftOut(data, clock, LSBFIRST, value);
digitalWrite(strobe, HIGH);
}
void loop()
{
uint8_t buttons = readButtons();
for(uint8_t position = 0; position < 8; position++)
{
uint8_t mask = 0x1 << position;
setLed(buttons & mask ? 1 : 0, position);
}
}