Last active
May 30, 2017 01:09
-
-
Save electricimp/6158398 to your computer and use it in GitHub Desktop.
Example code for writing to a shift register via SPI
This file contains hidden or 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
// Shift Register via SPI Example | |
// Configure Hardware | |
// Pin 1 is the reset line and should be high unless driven low | |
hardware.pin1.configure(DIGITAL_OUT_OD_PULLUP); | |
// Pin 8 is the output enable line and should also be high unless driven low | |
hardware.pin8.configure(DIGITAL_OUT_OD_PULLUP); | |
// Configure the operating parameters of the SPI bus | |
hardware.spi257.configure(SIMPLEX_TX | MSB_FIRST | CLOCK_IDLE_LOW, 400); | |
// Function to reset the shift registers | |
function clear() | |
{ | |
hardware.pin1.write(0); | |
imp.sleep(0.1); | |
hardware.pin1.write(1); | |
} | |
// Function to send out some data | |
function send(data) | |
{ | |
// Make sure the output is disabled before we start clocking out data | |
hardware.pin8.write(1); | |
// Now send the data | |
hardware.spi257.write(data); | |
// Last, re-enable the output | |
hardware.pin8.write(0); | |
} | |
// Clear the shift registers and send some data | |
clear(); | |
// Create a four-byte blob and write a 32-bit number to it | |
local value = blob(4); | |
value.writen(0xDEADBEEF,'i'); | |
// Pass it to the send() function | |
send(value); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This no longer works. When running as device code you get
'DIGITAL_OUT_PULLUP' does not exist