I am trying to use a MCP23S17 (SPI) Port expander on a ESP32 Project (Squeezelite Player) but have never got it to work. I thought it was maybe a bad chip, so this evening i connected it to a arduino UNO and loaded the example:- MCP23S17_test.ino from the library and it works, so the chip is ok!
The address of the chip is set to 0 (pin 16,17,18 to ground) but in line 13 of the code it seems to be set to 10 but the comment reads 0 ??? it works and if i change it to 0 it dosn't. can anyone please explain this as in the actual application i need to tell it this address is it 10 or 0 ?
thanks Chris
Eh? SPI doesn't have addresses!
EDIT: Ah, SPI doesn't; but the chip uses them in its Control Byte. But this can be disabled with IOCON.HAEN


What code? Please post the complete code!
//
// FILE: MCP23S17_digitalRead.ino
// AUTHOR: Rob Tillaart
// DATE: 2021-12-30
// PUPROSE: test MCP23017 library
#include "MCP23S17.h"
#include "SPI.h"
MCP23S17 MCP(10); // HW SPI address 0x00
int rv = 0;
void setup()
{
Serial.begin(115200);
Serial.println();
Serial.print("MCP23S17_test version: ");
Serial.println(MCP23S17_LIB_VERSION);
delay(100);
SPI.begin();
rv = MCP.begin();
Serial.println(rv ? "true" : "false");
rv = MCP.pinMode8(0, 0xFF); // CHECK
Serial.println(rv);
rv = MCP.pinMode8(1, 0xFF);
Serial.println(rv);
Serial.print("HWSPI: ");
Serial.println(MCP.usesHWSPI());
Serial.println("TEST digitalRead(pin)");
for (int pin = 0; pin < 16; pin++)
{
int val = MCP.digitalRead(pin);
Serial.print(val);
Serial.print(' ');
delay(100);
}
Serial.println();
}
void loop()
{
delay(1000);
Serial.println("TEST digitalRead(pin)");
for (int pin = 0; pin < 16; pin++)
{
int val = MCP.digitalRead(pin);
Serial.print(val);
Serial.print(' ');
delay(10);
}
Serial.println();
}
// -- END OF FILE --
[quote="awneil, post:3, topic:1153682, full:true"]
[quote="8art-5imson, post:1, topic:1153682"]
MCP23S17 (**SPI**) Port expander ... The address of the chip is set to 0
[/quote]
Eh? SPI doesn't have addresses!
*EDIT: Ah, SPI doesn't; but the chip uses them in its Control Byte. But this can be disabled with IOCON.HAEN*


[quote="8art-5imson, post:1, topic:1153682"]
in line 13 of the code
[/quote]
What code? Please post the complete code!
[/quote]

I believe that the 10 refers to the pin used for the SPI chip select signal for the MCP23S17.
You need to change this for your ESP32.
Are now i get it, I was confused as the squeezelite software I have on the esp32 has a GPIO setting for CS, but also a place to put the address. I guess the address is optional as i only have the one MCP connected !!
Thanks Chris.
I can't recall off the top of my head. Even the SPI variant has a 3-bit address that's setup using those A0, A1 & A2 pins. You could look at the library code and see what address it uses as the default - maybe address 0, and set your pins on the chip accordingly.
Indeed - see post #3, as edited.
As noted, for the SPI chip, the addressing is enabled/disabled by IOCON.HAEN;
The default is disabled:
When disabled, the chip hardware uses all-zeros for the address:
No, the address is always required - see post #9
As already mentioned, it is required.
Looking at the code for the MCP23S17 library, it defaults to address 0 to match the default setting of the actual chip.
This line of code:
Sets the SPI CS pin to use but the library behind it auto sets the address to 0, making it look like it's not required but it's still passed in the SPI command to the device.
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.

