Created
March 14, 2015 16:48
-
-
Save Tmw/8fdb56e35554b17f0477 to your computer and use it in GitHub Desktop.
Getting started with Arduino and SPI.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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