Skip to content

Instantly share code, notes, and snippets.

@Tmw
Created March 14, 2015 16:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Tmw/8fdb56e35554b17f0477 to your computer and use it in GitHub Desktop.
Save Tmw/8fdb56e35554b17f0477 to your computer and use it in GitHub Desktop.
Getting started with Arduino and SPI.h
#include <SPI.h>
// define pins and other variables
const int latchPin = 8;
const int clockPin = 13;
const int dataPin = 11;
// setup the correct pins and initialize SPI library
void setup() {
// setup pins
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
// setup SPI library
SPI.setBitOrder(MSBFIRST);
SPI.setDataMode(SPI_MODE0);
SPI.setClockDivider(SPI_CLOCK_DIV2);
SPI.begin();
}
void loop() {
for(int i =0; i < 16; i++){
turnOnLed(i);
}
}
// turn on correct LED using 595's
void turnOnLed(int ledNr) {
digitalWrite(latchPin, LOW);
if (ledNr >= 8) {
SPI.transfer(1<<ledNr-8);
SPI.transfer(0);
}
else {
SPI.transfer(0);
SPI.transfer(1<<ledNr);
}
digitalWrite(latchPin, HIGH);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment