Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 16 of 16

Thread: New MCP23S17 Library

Threaded View

  1. #1
    Senior Member
    Join Date
    Dec 2016
    Location
    Montreal, Canada
    Posts
    3,451

    New MCP23S17 Library

    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.

    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:
    Click image for larger version. 

Name:	mcp23s17.jpg 
Views:	55 
Size:	107.7 KB 
ID:	19290

    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

    Click image for larger version. 

Name:	1582705159294-1.jpg 
Views:	39 
Size:	146.5 KB 
ID:	19291

    PS, didn't know what to call the library, so I named it MCP23S17........

    Repo: https://github.com/tonton81/MCP23S17
    Last edited by tonton81; 03-06-2020 at 08:49 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •