Skip to content

Instantly share code, notes, and snippets.

@electricimp
Last active May 30, 2017 01:09
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save electricimp/6158398 to your computer and use it in GitHub Desktop.
Save electricimp/6158398 to your computer and use it in GitHub Desktop.
Example code for writing to a shift register via SPI
// 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);
@pcon
Copy link

pcon commented Jan 9, 2014

This no longer works. When running as device code you get 'DIGITAL_OUT_PULLUP' does not exist

@electricimp
Copy link
Author

Now (at long last) fixed, @pcon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment