-1

thanks for any help I need to read a gpio expander mcp23s08 set as 8 bits input. my code write correctly (I believe) the setup register using SPI library, with the enclosed example modified. But I do not understand how read a gpio register (0x09) code (with the read out routine wrong:

#define GPIO_SEL_pin 14                         // D14 = MCP23S08 GPIO SPI select pin
#define SCLK 18                                 // D18 SPI SCLK 
#define MISO 19                                 // D19 SPI MISO
#define MOSI 23                                 // D23 SPI MOSI

#include <SPI.h>

uint8_t keyboard;

static const int spiClk = 1000000; // 1 MHz

//uninitalised pointers to SPI objects
SPIClass * mcp23s08 = NULL;

void setup() {
  Serial.begin(115200);
  pinMode(GPIO_SEL_pin, OUTPUT);                     //MCP23S08 SPI select pin
  digitalWrite(GPIO_SEL_pin, HIGH);                  //pull ss high to disable data transfer
  mcp23s08 = new SPIClass(VSPI);
  mcp23s08->begin(SCLK, MISO, MOSI, GPIO_SEL_pin);   //SCLK, MISO, MOSI, SS
  mcp23s08SetUp();
  delay(1);

}

void loop() {
  keyboard = mcp23s08Read();
  Serial.println (keyboard);
  delay(100);
}

// ================================== routines =================================
void mcp23s08SetUp( void ) {
  uint8_t dev_adr  = 0x40;                          // MCP23S08 address 0; R/W = write
  uint8_t reg_adr  = 0x00;                          // MCP23S08 IODIR register
  uint8_t reg_data = 0xFF;                          // pin configured as input

  digitalWrite(GPIO_SEL_pin, LOW);                 //pull SS slow to prep other end for transfer
// ============================= MCP3S08 pin as input ==============================
  mcp23s08->beginTransaction(SPISettings(spiClk, MSBFIRST, SPI_MODE0));
  mcp23s08->transfer(dev_adr);  
  mcp23s08->transfer(reg_adr);  
  mcp23s08->transfer(reg_data);  
  mcp23s08->endTransaction();

  reg_adr  = 0x01;                                 // MCP23S08 IPOL register
  reg_data = 0xFF;                                 // invert input polarity
// ============================= MCP3S08 invert input ==============================
  mcp23s08->beginTransaction(SPISettings(spiClk, MSBFIRST, SPI_MODE0));
  mcp23s08->transfer(dev_adr);  
  mcp23s08->transfer(reg_adr);  
  mcp23s08->transfer(reg_data);  
  mcp23s08->endTransaction();

  reg_adr  = 0x06;                                 // MCP23S08 GPPU pull-up register
  reg_data = 0xFF;                                 // set pull-up register
// ============================= MCP3S08 pullup resistor ==============================
  mcp23s08->beginTransaction(SPISettings(spiClk, MSBFIRST, SPI_MODE0));
  mcp23s08->transfer(dev_adr);  
  mcp23s08->transfer(reg_adr);  
  mcp23s08->transfer(reg_data);  
  mcp23s08->endTransaction();

  digitalWrite(GPIO_SEL_pin, HIGH);                //pull ss high to signify end of data transfer
}

uint8_t mcp23s08Read ( void ) {
  uint8_t dev_adr  = 0x41;                          // MCP23S08 address 0; R/W = read
  uint8_t reg_adr  = 0x09;                          // MCP23S08 GPIO register
  uint8_t reg_data;                                 // GPIO register
  mcp23s08->beginTransaction(SPISettings(spiClk, MSBFIRST, SPI_MODE0));
  mcp23s08->transfer(dev_adr);  
  mcp23s08->transfer(reg_adr);  
  reg_data = mcp23s08->transfer(0x00);  
  mcp23s08->endTransaction(); 
  return reg_data; 
}
  • Why do you toggle SS inside mcp23s08SetUp() function and NOT inside mcp23s08Read()? I don't remember if SS pin is managed by SPI Arduino library, but you can try it. – leoc7 Dec 26 '18 at 19:16
  • Yes, you are right. Just forgot to toggle the ss pin. – Angelo Gilin Maggiora Dec 28 '18 at 8:04
  • Hi @Angelo Gilin Maggiora if this answer has solved your question please consider accepting it by clicking the check-mark. This indicates to the wider community that you've found a solution and gives some reputation to both the answerer and yourself. There is no obligation to do this. – leoc7 Dec 28 '18 at 15:43
  • Thanks a lot for your help. @leoc7 could you tell me where is the check-mark button? I did not find in the page. Sorry, I am a newbe – Angelo Gilin Maggiora Dec 31 '18 at 15:30
  • Read answer of this question in order to know how to accept an answer. :) meta.stackexchange.com/questions/5234/… – leoc7 Dec 31 '18 at 21:21
0

You forgot to toggle SS pin in mcp23s08Read() function.

 uint8_t mcp23s08Read ( void ) {
      ----> digitalWrite(GPIO_SEL_pin, LOW);                 //pull SS slow to prep other end for transfer
      uint8_t dev_adr  = 0x41;                          // MCP23S08 address 0; R/W = read
      uint8_t reg_adr  = 0x09;                          // MCP23S08 GPIO register
      uint8_t reg_data;                                 // GPIO register
      mcp23s08->beginTransaction(SPISettings(spiClk, MSBFIRST, SPI_MODE0));
      mcp23s08->transfer(dev_adr);  
      mcp23s08->transfer(reg_adr);  
      reg_data = mcp23s08->transfer(0x00);  
      mcp23s08->endTransaction(); 
      ----> digitalWrite(GPIO_SEL_pin, HIGH);                //pull ss high to signify end of data transfer
      return reg_data; 
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.