Arduino Serial I/O expansion, Serial Peripheral Interface (SPI) using MPC23S08

Hello!

I was searching for hours for a working sample of the SPI version of the MPC23x08. I run into troubles with the configuration of the SPI Interface (Phase and Clock). For all here's a small running sample:

#define CONTROL_BYPE 0x40 // 0100 0000
//                                |||+-- R/W 0 == write 
//                                ||+--- A0
//                                |+---- A1

#define chipSelectPin 10
#define BITORDER MSBFIRST 
#define DATAMODE SPI_MODE3   // testes with  Mode  2 and 3

void setup() {

  // setup the protocol

  SPI.setClockDivider(SPI_CLOCK_DIV2);   // testes also with higher values mit 2
  SPI.setBitOrder(BITORDER) ;
  SPI.setDataMode(DATAMODE) ;

  // prepare for output

  SPI.begin(); 
  pinMode(chipSelectPin, OUTPUT);
  digitalWrite(chipSelectPin, HIGH);    

  digitalWrite(chipSelectPin, LOW);  
  SPI.transfer(0x40);
  SPI.transfer(0x00);                   // select the IODIR register
  SPI.transfer(0xff);                   // set register value-all high, sets all pins as outputs on MCP23008
  digitalWrite(chipSelectPin, HIGH);    
  SPI.end();
}

// flash all connected LED

void loop() {

  SPI.begin(); 
  digitalWrite(chipSelectPin, LOW);    
  SPI.transfer(0x40);
  SPI.transfer(0x09);                   // select the GPIO register
  SPI.transfer(0xff);                   // set register value-all high
  digitalWrite(chipSelectPin, HIGH);  
  SPI.end();                             // stop talking to the device
  delay(500);                           // wait for 1/2 a second

  SPI.begin(); 
  digitalWrite(chipSelectPin, LOW);  
  SPI.transfer(0x40);
  SPI.transfer(0x09);                   // select the GPIO register
  SPI.transfer(0x00);                   // set register value-all low
  digitalWrite(chipSelectPin, HIGH);  
  SPI.end();                             // stop talking to the device
  delay(500);				// wait for 1/2 a second
}

Hope this will speed up the work for others.

And don't forget Arduino MSIO goes to Chip MSIN and Arduino MSIN to MSIO :~ This was my bigges problem!