CodeShare

Dependencies:   MCP23S17 mbed

Fork of MCP23S17_Basic_IO_Demo by jim hamblen

Committer:
jeremycai3721
Date:
Sun Sep 25 20:18:06 2016 +0000
Revision:
3:ff23f7e9c064
Parent:
2:934a0500abde
CodeShare

Who changed what in which revision?

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