- SPI library:
#include <SPI.h>
#include "Streaming.h"
/* Notes on SPI:
https://learn.sparkfun.com/tutorials/serial-peripheral-interface-spi/all
*/
- pin outs
// define SPI pins
// Due pin out: http://www.robgray.com/temp/Due-pinout-WEB.png
#define CLK 110 // clock pin. CLK. SCK.
#define DATA 109 // data in. DIN. MOSI.
#define OPERATE 25 // operation. OE.
#define LATCH 52 // latch. LE. CS pin, but inverted.
// use a NPN transistor to invert the CS pin for LE usage.
// Inverter schematic: transistors - How to invert a digital signal - Electrical Engineering Stack Exchange
// kind of a trick, but otherwise you have to wait some amount of time for the data to propogate before latching.
// this way, the CS (slave select) will toggle low when starting to write then low after writing completes.
// this is the opposite logic from what you need with LE, so use a NPN to invert.
- support functions:
// OE pin must be low to allow latched data to have effect
void startOperation(){
digitalWrite(OPERATE, LOW);
}
void stopOperation(){
digitalWrite(OPERATE, HIGH);
}
- setup()
pinMode(OPERATE, OUTPUT);
SPI.setDataMode(LATCH, SPI_MODE0); // CPOL=0, CPHA=0
// "DIN is the serial-data input, and must be stable when it is sampled on the rising edge of CLK."
// see Serial Peripheral Interface - Wikipedia
// At CPOL=0 the base value of the clock is zero.
// For CPHA=0, data are captured on the clock's rising edge (low?high transition) and data is propagated on a falling edge (high?low clock transition).
// this ought to work. MAXIM claims 30 MHz.
SPI.setClockDivider(LATCH, 4); // 82/4=20.5 MHz on the DUE
// "Data is shifted in, MSB first. This means that data bit D15 is clocked in
// first, followed by 15 more data bits finishing with the LSB, D0."
SPI.setBitOrder(LATCH, LSBFIRST);
// tinker as needed.
// begin
SPI.begin(LATCH);
// I have 2 Maxims, so 4 bytes of data
SPI.transfer(LATCH, 0, SPI_CONTINUE );
SPI.transfer(LATCH, 0, SPI_CONTINUE );
SPI.transfer(LATCH, 0, SPI_CONTINUE );
SPI.transfer(LATCH, 0 );
// OE enable
startOperation();
- loop()
// two Maxims, so 32 bits, which is the size of a long.
static unsigned long sl = 0;
SPI.transfer(LATCH, sl >> 24, SPI_CONTINUE );
SPI.transfer(LATCH, sl >> 16, SPI_CONTINUE );
SPI.transfer(LATCH, sl >> 8, SPI_CONTINUE);
SPI.transfer(LATCH, sl );
delay(100);
sl++;