ec

Dependencies:   MCP23S17 mbed

Fork of 4180_lab1_part7 by 4180

Committer:
Josahty
Date:
Mon Jan 22 18:34:48 2018 +0000
Revision:
1:26e3d87d1f49
Parent:
0:8efa2ed1bc48
ec

Who changed what in which revision?

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