Here is a MCP23S17 SPI library I have been working on. It's still in progress for more features but functions pretty well.
It has chip detection, can identify how many chips are on the given chip select, and assign the pin ordering properly. Example, if you have 3 chips addressed as 0, 3, and 7, using pinMode(16, OUTPUT) would essentially put 2nd MCP chip (address 3) bankA pin 0 in OUTPUT mode. Similarly, digitalWriting pin 32 would put 3rd MCP chip (address 7) bankA pin 0 in the given state. 3 chips tally up to 16*3 = 48 gpios, which are 0->47.
Displayed output of demo:
Sketch code:
board tested with, designed for rasbpi, but wired to teensy 4
it has 8x mcp23s17's
it has it's own regulator and gpios are 3.3v, so i run it off the VIN of T4
PS, didn't know what to call the library, so I named it MCP23S17........
Repo: https://github.com/tonton81/MCP23S17
It has chip detection, can identify how many chips are on the given chip select, and assign the pin ordering properly. Example, if you have 3 chips addressed as 0, 3, and 7, using pinMode(16, OUTPUT) would essentially put 2nd MCP chip (address 3) bankA pin 0 in OUTPUT mode. Similarly, digitalWriting pin 32 would put 3rd MCP chip (address 7) bankA pin 0 in the given state. 3 chips tally up to 16*3 = 48 gpios, which are 0->47.
Code:
void pinMode(uint8_t pin, uint8_t mode); // set pin mode
void digitalWrite(uint8_t pin, bool level); // write pin
bool digitalRead(uint8_t pin); // read pin
void toggle(uint8_t pin); // toggle pin
void invert(uint8_t pin); // invert input pin state
void enableInterrupt(uint8_t pin, uint8_t mode); // functional in register configuration, but not handled yet (supports RISING, FALLING, and CHANGE), planning to make it event driven.
void disableInterrupt(uint8_t pin); // disables interrupt if enabled.
void info(); // printout of all chip pin states, and registers in both HEX and BIN formats
Displayed output of demo:
Sketch code:
Code:
#include <SPI.h>
#include "mcp23s17.h"
MCP23S17<&SPI, 10, 400000> mcp; // SPI BUS, CS, SPISPEED
void setup() {
Serial.begin(115200);
delay(1000);
SPI.begin();
mcp.begin(); // must be always ran AFTER SPI's begin call.
mcp.pinMode(14, OUTPUT);
mcp.pinMode(12, OUTPUT);
for ( uint8_t i = 0; i < 8; i++ ) {
mcp.pinMode(i, INPUT_PULLUP);
}
}
void loop() {
static uint32_t cycle = millis();
if ( millis() - cycle > 1000 ) {
mcp.digitalWrite(12, !mcp.digitalRead(12));
mcp.digitalWrite(14, !mcp.digitalRead(14));
mcp.info();
cycle = millis();
}
}
board tested with, designed for rasbpi, but wired to teensy 4
it has 8x mcp23s17's
it has it's own regulator and gpios are 3.3v, so i run it off the VIN of T4
PS, didn't know what to call the library, so I named it MCP23S17........
Repo: https://github.com/tonton81/MCP23S17
Last edited: