spi

Dependencies:   MCP23S17 mbed

Committer:
aallaire3
Date:
Thu Jan 18 18:15:18 2018 +0000
Revision:
0:b08b1d99aaf3
spi

Who changed what in which revision?

UserRevisionLine numberNew contents of line
aallaire3 0:b08b1d99aaf3 1 // A simple IO demo using the MCP23S17 library
aallaire3 0:b08b1d99aaf3 2 //
aallaire3 0:b08b1d99aaf3 3 // MCP23S17 Library Copyright (c) 2010 Romilly Cocking
aallaire3 0:b08b1d99aaf3 4 // Released under the MIT License: http://mbed.org/license/mit
aallaire3 0:b08b1d99aaf3 5 //
aallaire3 0:b08b1d99aaf3 6 // See http://mbed.org/users/romilly/notebook/mcp23s17-addressable-16-bit-io-expander-with-spi/
aallaire3 0:b08b1d99aaf3 7 //
aallaire3 0:b08b1d99aaf3 8 //
aallaire3 0:b08b1d99aaf3 9 // MCP23S17 datasheet http://ww1.microchip.com/downloads/en/DeviceDoc/21952b.pdf
aallaire3 0:b08b1d99aaf3 10 // uses MCP23S17 library version 0.4
aallaire3 0:b08b1d99aaf3 11
aallaire3 0:b08b1d99aaf3 12 #include "mbed.h"
aallaire3 0:b08b1d99aaf3 13 #include "MCP23S17.h"
aallaire3 0:b08b1d99aaf3 14 // Create SPI bus
aallaire3 0:b08b1d99aaf3 15 SPI spi(p5, p6, p7);
aallaire3 0:b08b1d99aaf3 16 //
aallaire3 0:b08b1d99aaf3 17 // Wiring Connections:
aallaire3 0:b08b1d99aaf3 18 // mbed p5,p6,p7 are tied to MCP23S17 SI, SO, SCK pins
aallaire3 0:b08b1d99aaf3 19 // mbed p20 to MCP23S17 CS
aallaire3 0:b08b1d99aaf3 20 // MCP23S17 reset pin pulled high
aallaire3 0:b08b1d99aaf3 21 // MCP23S17 GPA0 connected to GPB0 for loopback test
aallaire3 0:b08b1d99aaf3 22 // A0, A1, A2 of the MCP23S17 are tied to ground on the breadboard, so the 8-bit address for writes is 0x40
aallaire3 0:b08b1d99aaf3 23 // This is referred to as the opcode in the device datasheet
aallaire3 0:b08b1d99aaf3 24 char Opcode = 0x40;
aallaire3 0:b08b1d99aaf3 25
aallaire3 0:b08b1d99aaf3 26 // Next create a MCP23S17
aallaire3 0:b08b1d99aaf3 27 // mbed p20 is connected to ~chipSelect on the MCP23S17
aallaire3 0:b08b1d99aaf3 28 MCP23S17 chip = MCP23S17(spi, p20, Opcode);
aallaire3 0:b08b1d99aaf3 29
aallaire3 0:b08b1d99aaf3 30 // Optional software reset - mbed p14 to MCP23S17 reset pin
aallaire3 0:b08b1d99aaf3 31 DigitalOut reset(p14);
aallaire3 0:b08b1d99aaf3 32
aallaire3 0:b08b1d99aaf3 33
aallaire3 0:b08b1d99aaf3 34 int main() {
aallaire3 0:b08b1d99aaf3 35 // The MCP23S17 reset pin can just be pulled high, since it has a power on reset circuit.
aallaire3 0:b08b1d99aaf3 36 // The reset pin can be used for a software forced reset by pulling it low with an mbed GPIO pin.
aallaire3 0:b08b1d99aaf3 37 // But just leave it pulled high for this simple demo code.
aallaire3 0:b08b1d99aaf3 38 // After a power on reset, both IO ports default to input mode
aallaire3 0:b08b1d99aaf3 39 //
aallaire3 0:b08b1d99aaf3 40 // Here is the optional code for a software reset
aallaire3 0:b08b1d99aaf3 41 reset = 0;
aallaire3 0:b08b1d99aaf3 42 wait_us(1);
aallaire3 0:b08b1d99aaf3 43 reset = 1;
aallaire3 0:b08b1d99aaf3 44 //
aallaire3 0:b08b1d99aaf3 45 // Set all 8 Port A bits to output direction
aallaire3 0:b08b1d99aaf3 46 chip.direction(PORT_A, 0x00);
aallaire3 0:b08b1d99aaf3 47 // Set all 8 Port B bits to input direction
aallaire3 0:b08b1d99aaf3 48 chip.direction(PORT_B, 0xFF);
aallaire3 0:b08b1d99aaf3 49
aallaire3 0:b08b1d99aaf3 50 // Start Loopback test sending out and reading back values
aallaire3 0:b08b1d99aaf3 51 // loopback test uses A0 and B0 pins - so use a wire to jumper those two pins on MCP23S17 together
aallaire3 0:b08b1d99aaf3 52 while (1) {
aallaire3 0:b08b1d99aaf3 53
aallaire3 0:b08b1d99aaf3 54 // read back value from MCP23S17 Port B and display B0 on mbed led1
aallaire3 0:b08b1d99aaf3 55 int led = !(chip.read(PORT_B) & 0x01);
aallaire3 0:b08b1d99aaf3 56 chip.write(PORT_A, led);
aallaire3 0:b08b1d99aaf3 57 // led1 should blink slowly when it is all working
aallaire3 0:b08b1d99aaf3 58 }
aallaire3 0:b08b1d99aaf3 59 }