#216 OSHChip/LEDx16Module
Driving an SPI LED module with the OSHChip.
Notes
How easy is SPI with the OSHChip? I thought I’d find out by first controlling a module that has a very basic SPI-ish slave interface.
I’m using the LEDx16Module that I designed in the KiCad like a Pro course from Tech Explorations. It has dual 74HC595 shift registers that can be driven with SPI to control 16 onboard LEDs.
SPI Driver Options
It seems there’s at least three options:
- use the SPI class that is part of the mbed standard library
- go direct to registers for SPI control
- pick one of the other SPI libraries out there, for example SPI_Demo_Nano that implements an SPI class very similar to the Arduino SPI library.
I’m going to code this one with the mbed online compiler, so I’ll start with the first option.
Configuring and Using SPI
I’ll probably publish my project on mbed, but for now the source is all here. The demo code is largely copied from the LEDx16Module demo.
It sets SPI for an 8 bit data frame, spi mode 0, 1MHz clock rate:
// Setup spi: 8 bit data frame, spi mode 0, 1MHz clock rate
spi.format(8, 0);
spi.frequency(1000000);
And 16-bits worth of data (for the two shift registers) is sent in two write operations:
led_module_cs = 0;
spi.write(data);
spi.write(data >> 8);
led_module_cs = 1;
Two quirks that I still don’t fully understand:
- I thought IO should be able to set a 16-bit frame with
format(16, 0)
but I couldn’t get it to work. - even though SPI.write takes an “int”, it still only sends the 8 lower bits, hence the transfer in two calls
Also note I haven’t resorted to bulk transfers using the transfer
method in this case,
since I’m not sending large quantities of data, and not expecting any reply.
Construction
There’s not much to the build. Basically hook together three modules:
- the OSHChip
- the LEDx16Module
- 3.3V power supply - I’m using a Buck Converter Module
Credits and References
- OSHChip
- LEDx16Module
- Buck Converter Module
- SPI - mbed handbook
- KiCad like a Pro - course materials
- ..as mentioned on my blog