0.1.3 MCP23S17

This commit is contained in:
rob tillaart 2022-04-14 10:53:15 +02:00
parent f83e86edb1
commit da5eb4c0a5
8 changed files with 48 additions and 18 deletions

View File

@ -1,7 +1,7 @@
//
// FILE: MCP23S17.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.1.2
// VERSION: 0.1.3
// PURPOSE: Arduino library for SPI MCP23S17 16 channel port expander
// DATE: 2021-12-30
// URL: https://github.com/RobTillaart/MCP23S17
@ -11,6 +11,7 @@
// 0.1.0 2021-12-30 initial version (a 2019 version did not make it)
// 0.1.1 2022-01-10 add 16 bit interface
// 0.1.2 2022-01-12 change the URL for library manager
// 0.1.3 2022-04-13 fix compiling for NANO33 BLE
#include "Arduino.h"
@ -41,6 +42,15 @@
#define MCP23S17_OLAT_A 0x14 // NOT USED output latch P24
#define MCP23S17_OLAT_B 0x15 // NOT USED
// IOCR bit masks (details datasheet P20)
#define MCP23S17_IOCR_BANK 0x80 // Controls how the registers are addressed.
#define MCP23S17_IOCR_MIRROR 0x40 // INT Pins Mirror bit.
#define MCP23S17_IOCR_SEQOP 0x20 // Sequential Operation mode bit.
#define MCP23S17_IOCR_DISSLW 0x10 // Slew Rate control bit for SDA output.
#define MCP23S17_IOCR_HAEN 0x08 // Hardware Address Enable bit (MCP23S17 only).
#define MCP23S17_IOCR_ODR 0x04 // Configures the INT pin as an open-drain output.
#define MCP23S17_IOCR_INTPOL 0x02 // This bit sets the polarity of the INT output pin.
#define MCP23S17_IOCR_NI 0x01 // Not implemented.
// low level read / write masks
#define MCP23S17_WRITE_REG 0x40
@ -100,11 +110,15 @@ bool MCP23S17::begin()
// check connected
if (! isConnected()) return false;
// disable address increment (datasheet)
if (! writeReg(MCP23S17_IOCR, 0b00100000)) return false; // TODO MAGIC NR
// disable address increment (datasheet P20
// SEQOP: Sequential Operation mode bit
// 1 = Sequential operation disabled, address pointer does not increment.
// 0 = Sequential operation enabled, address pointer increments.
if (! writeReg(MCP23S17_IOCR, MCP23S17_IOCR_SEQOP)) return false;
// Force INPUT_PULLUP
if (! writeReg(MCP23S17_PUR_A, 0xFF)) return false;
if (! writeReg(MCP23S17_PUR_B, 0xFF)) return false;
if (! writeReg(MCP23S17_PUR_A, 0xFF)) return false; // 0xFF == all UP
if (! writeReg(MCP23S17_PUR_B, 0xFF)) return false; // 0xFF == all UP
return true;
}
@ -649,7 +663,7 @@ uint8_t MCP23S17::swSPI_transfer(uint8_t val)
uint8_t rv = 0;
for (uint8_t mask = 0x80; mask > 0; mask >>= 1)
{
::digitalWrite(dao, (val & mask));
::digitalWrite(dao, (val & mask) ? HIGH : LOW);
::digitalWrite(clk, HIGH);
if (::digitalRead(dai) == HIGH) rv |= mask;
::digitalWrite(clk, LOW);

View File

@ -2,7 +2,7 @@
//
// FILE: MCP23S17.h
// AUTHOR: Rob Tillaart
// VERSION: 0.1.2
// VERSION: 0.1.3
// PURPOSE: Arduino library for SPI MCP23S17 16 channel port expander
// DATE: 2021-12-30
// URL: https://github.com/RobTillaart/MCP23S17
@ -12,7 +12,7 @@
#include "SPI.h"
#define MCP23S17_LIB_VERSION (F("0.1.2"))
#define MCP23S17_LIB_VERSION (F("0.1.3"))
#define MCP23S17_OK 0x00
#define MCP23S17_PIN_ERROR 0x81

View File

@ -67,9 +67,9 @@ Programming Interface is kept the same as much as possible.
- **int lastError()** Above functions set an error flag that can be read with this function.
Reading it will reset the flag to **MCP23S17_OK**.
| DESCRIPTION | VALUE |
|:----------------------|:-----:|
| MCP23S17_OK | 0x00 |
| NAME | VALUE | DESCRIPTION |
|:----------------------|:-----:|:------------|
| MCP23S17_OK | 0x00 | No error |
| MCP23S17_PIN_ERROR | 0x81 |
| MCP23S17_I2C_ERROR | 0x82 |
| MCP23S17_VALUE_ERROR | 0x83 |
@ -85,3 +85,6 @@ See examples.
- keep functional in sync with MCP23017_RT
- **isConnected()** is not really needed
- implement ESP32 specific support - see MCP_ADC.begin()
- replace magic numbers with a defined constant

View File

@ -23,7 +23,7 @@ void setup()
SPI.begin();
rv = MCP.begin();
Serial.println(rv);
Serial.println(rv ? "true" : "false");
rv = MCP.pinMode8(0, 0xFF); // CHECK
Serial.println(rv);

View File

@ -21,7 +21,7 @@ void setup()
SPI.begin();
bool b = MCP.begin();
// Serial.println(b ? "true" : "false");
Serial.println(b ? "true" : "false");
delay(100);
MCP.pinMode8(0, 0x00); // 0 = output , 1 = input

View File

@ -13,6 +13,10 @@ MCP23S17 MCP(10); // HW SPI address 0x00
uint32_t start, stop;
volatile int val1;
volatile int val8;
volatile uint16_t val16;
void setup()
{
@ -25,6 +29,7 @@ void setup()
SPI.begin();
bool b = MCP.begin();
Serial.println(b ? "true" : "false");
Serial.print("HWSPI: ");
Serial.println(MCP.usesHWSPI());
@ -63,7 +68,7 @@ void setup()
start = micros();
for (int pin = 0; pin < 16; pin++)
{
volatile int val = MCP.digitalRead(pin);
val1 = MCP.digitalRead(pin);
}
stop = micros();
Serial.println((stop - start) / 16.0);
@ -86,7 +91,7 @@ void setup()
Serial.print("TEST read8(port):\t");
delay(100);
start = micros();
volatile int val8 = MCP.read8(0);
val8 = MCP.read8(0);
val8 = MCP.read8(1);
stop = micros();
Serial.println((stop - start) / 2.0);
@ -108,11 +113,19 @@ void setup()
Serial.print("TEST read16():\t");
delay(100);
start = micros();
volatile uint16_t val16 = MCP.read16();
val16 = MCP.read16();
stop = micros();
Serial.println((stop - start) / 2.0);
Serial.println();
// keep compiler happy
Serial.print("VAL1:\t");
Serial.println(val1);
Serial.print("VAL8:\t");
Serial.println(val8);
Serial.print("VAL16:\t");
Serial.println(val16);
Serial.println("\ndone...");
}

View File

@ -15,7 +15,7 @@
"type": "git",
"url": "https://github.com/RobTillaart/MCP23S17.git"
},
"version": "0.1.2",
"version": "0.1.3",
"license": "MIT",
"frameworks": "arduino",
"platforms": "*",

View File

@ -1,5 +1,5 @@
name=MCP23S17
version=0.1.2
version=0.1.3
author=Rob Tillaart <rob.tillaart@gmail.com>
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
sentence=Arduino library for SPI MCP23S17 16 channel port expander 16 IO-lines