SPI Port Expander ex code

Dependencies:   MCP23S17 mbed

Fork of MCP23S17_Basic_IO_Demo by jim hamblen

Committer:
kzar
Date:
Tue Oct 16 18:55:29 2018 +0000
Revision:
3:fdb89f47357f
Parent:
2:934a0500abde
SPI portexpander example code

Who changed what in which revision?

UserRevisionLine numberNew contents of line
4180_1 0:12f4911d7ba0 1 #include "mbed.h"
4180_1 0:12f4911d7ba0 2 #include "MCP23S17.h"
4180_1 2:934a0500abde 3 // Create SPI bus
4180_1 0:12f4911d7ba0 4 SPI spi(p5, p6, p7);
kzar 3:fdb89f47357f 5
4180_1 2:934a0500abde 6 // Wiring Connections:
4180_1 0:12f4911d7ba0 7 // mbed p5,p6,p7 are tied to MCP23S17 SI, SO, SCK pins
4180_1 2:934a0500abde 8 // mbed p20 to MCP23S17 CS
4180_1 2:934a0500abde 9 // MCP23S17 reset pin pulled high
4180_1 2:934a0500abde 10 // MCP23S17 GPA0 connected to GPB0 for loopback test
4180_1 0:12f4911d7ba0 11 // A0, A1, A2 of the MCP23S17 are tied to ground on the breadboard, so the 8-bit address for writes is 0x40
4180_1 0:12f4911d7ba0 12 // This is referred to as the opcode in the device datasheet
4180_1 2:934a0500abde 13 char Opcode = 0x40;
4180_1 0:12f4911d7ba0 14
4180_1 0:12f4911d7ba0 15 // Next create a MCP23S17
4180_1 0:12f4911d7ba0 16 // mbed p20 is connected to ~chipSelect on the MCP23S17
4180_1 2:934a0500abde 17 MCP23S17 chip = MCP23S17(spi, p20, Opcode);
4180_1 0:12f4911d7ba0 18
4180_1 2:934a0500abde 19 // Optional software reset - mbed p14 to MCP23S17 reset pin
4180_1 2:934a0500abde 20 // DigitalOut reset(p14);
4180_1 1:70e1ebe47253 21
4180_1 0:12f4911d7ba0 22
4180_1 0:12f4911d7ba0 23 int main() {
4180_1 1:70e1ebe47253 24 // The MCP23S17 reset pin can just be pulled high, since it has a power on reset circuit.
4180_1 1:70e1ebe47253 25 // The reset pin can be used for a software forced reset by pulling it low with an mbed GPIO pin.
4180_1 1:70e1ebe47253 26 // But just leave it pulled high for this simple demo code.
4180_1 0:12f4911d7ba0 27 // After a power on reset, both IO ports default to input mode
kzar 3:fdb89f47357f 28
4180_1 0:12f4911d7ba0 29 // Set all 8 Port A bits to output direction
4180_1 0:12f4911d7ba0 30 chip.direction(PORT_A, 0x00);
4180_1 0:12f4911d7ba0 31 // Set all 8 Port B bits to input direction
4180_1 0:12f4911d7ba0 32 chip.direction(PORT_B, 0xFF);
kzar 3:fdb89f47357f 33
kzar 3:fdb89f47357f 34 // chip.interruptEnable(PORT_B, 0xFF);
kzar 3:fdb89f47357f 35
kzar 3:fdb89f47357f 36 uint8_t trigger;
4180_1 0:12f4911d7ba0 37 while (1) {
kzar 3:fdb89f47357f 38 //Read in PortB and output it to PortA
kzar 3:fdb89f47357f 39 trigger = chip.read(PORT_B);
kzar 3:fdb89f47357f 40 chip.write(PORT_A, !trigger);
4180_1 0:12f4911d7ba0 41 }
4180_1 0:12f4911d7ba0 42 }