Craig Raslawski
/
MCP23S17_IO_Expander
4180Lab1Part3
Fork of MCP23S17_Basic_IO_Demo by
part3.cpp@3:3081585fbce1, 2017-02-27 (annotated)
- Committer:
- CRaslawski
- Date:
- Mon Feb 27 22:31:04 2017 +0000
- Revision:
- 3:3081585fbce1
initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
CRaslawski | 3:3081585fbce1 | 1 | // A simple IO demo using the MCP23S17 library |
CRaslawski | 3:3081585fbce1 | 2 | // |
CRaslawski | 3:3081585fbce1 | 3 | // MCP23S17 Library Copyright (c) 2010 Romilly Cocking |
CRaslawski | 3:3081585fbce1 | 4 | // Released under the MIT License: http://mbed.org/license/mit |
CRaslawski | 3:3081585fbce1 | 5 | // |
CRaslawski | 3:3081585fbce1 | 6 | // See http://mbed.org/users/romilly/notebook/mcp23s17-addressable-16-bit-io-expander-with-spi/ |
CRaslawski | 3:3081585fbce1 | 7 | // |
CRaslawski | 3:3081585fbce1 | 8 | // |
CRaslawski | 3:3081585fbce1 | 9 | // MCP23S17 datasheet http://ww1.microchip.com/downloads/en/DeviceDoc/21952b.pdf |
CRaslawski | 3:3081585fbce1 | 10 | // uses MCP23S17 library version 0.4 |
CRaslawski | 3:3081585fbce1 | 11 | |
CRaslawski | 3:3081585fbce1 | 12 | #include "mbed.h" |
CRaslawski | 3:3081585fbce1 | 13 | #include "MCP23S17.h" |
CRaslawski | 3:3081585fbce1 | 14 | // Create SPI bus |
CRaslawski | 3:3081585fbce1 | 15 | SPI spi(p11, p12, p13); |
CRaslawski | 3:3081585fbce1 | 16 | // |
CRaslawski | 3:3081585fbce1 | 17 | // Wiring Connections: |
CRaslawski | 3:3081585fbce1 | 18 | // mbed p5,p6,p7 are tied to MCP23S17 SI, SO, SCK pins |
CRaslawski | 3:3081585fbce1 | 19 | // mbed p20 to MCP23S17 CS |
CRaslawski | 3:3081585fbce1 | 20 | // MCP23S17 reset pin pulled high |
CRaslawski | 3:3081585fbce1 | 21 | // MCP23S17 GPA0 connected to GPB0 for loopback test |
CRaslawski | 3:3081585fbce1 | 22 | // A0, A1, A2 of the MCP23S17 are tied to ground on the breadboard, so the 8-bit address for writes is 0x40 |
CRaslawski | 3:3081585fbce1 | 23 | // This is referred to as the opcode in the device datasheet |
CRaslawski | 3:3081585fbce1 | 24 | char Opcode = 0x40; |
CRaslawski | 3:3081585fbce1 | 25 | |
CRaslawski | 3:3081585fbce1 | 26 | // Next create a MCP23S17 |
CRaslawski | 3:3081585fbce1 | 27 | // mbed p20 is connected to ~chipSelect on the MCP23S17 |
CRaslawski | 3:3081585fbce1 | 28 | MCP23S17 chip = MCP23S17(spi, p20, Opcode); |
CRaslawski | 3:3081585fbce1 | 29 | |
CRaslawski | 3:3081585fbce1 | 30 | // Optional software reset - mbed p14 to MCP23S17 reset pin |
CRaslawski | 3:3081585fbce1 | 31 | // DigitalOut reset(p14); |
CRaslawski | 3:3081585fbce1 | 32 | |
CRaslawski | 3:3081585fbce1 | 33 | DigitalOut led1(LED1); // mbed LED1 is used for test status display |
CRaslawski | 3:3081585fbce1 | 34 | |
CRaslawski | 3:3081585fbce1 | 35 | int main() |
CRaslawski | 3:3081585fbce1 | 36 | { |
CRaslawski | 3:3081585fbce1 | 37 | // The MCP23S17 reset pin can just be pulled high, since it has a power on reset circuit. |
CRaslawski | 3:3081585fbce1 | 38 | // The reset pin can be used for a software forced reset by pulling it low with an mbed GPIO pin. |
CRaslawski | 3:3081585fbce1 | 39 | // But just leave it pulled high for this simple demo code. |
CRaslawski | 3:3081585fbce1 | 40 | // After a power on reset, both IO ports default to input mode |
CRaslawski | 3:3081585fbce1 | 41 | // |
CRaslawski | 3:3081585fbce1 | 42 | // Here is the optional code for a software reset |
CRaslawski | 3:3081585fbce1 | 43 | // reset = 0; |
CRaslawski | 3:3081585fbce1 | 44 | // wait_us(1); |
CRaslawski | 3:3081585fbce1 | 45 | // reset = 1; |
CRaslawski | 3:3081585fbce1 | 46 | // |
CRaslawski | 3:3081585fbce1 | 47 | // Set all 8 Port A bits to output direction |
CRaslawski | 3:3081585fbce1 | 48 | chip.direction(PORT_A, 0x00); |
CRaslawski | 3:3081585fbce1 | 49 | // Set all 8 Port B bits to input direction |
CRaslawski | 3:3081585fbce1 | 50 | chip.direction(PORT_B, 0xFF); |
CRaslawski | 3:3081585fbce1 | 51 | led1=0; |
CRaslawski | 3:3081585fbce1 | 52 | // Start Loopback test sending out and reading back values |
CRaslawski | 3:3081585fbce1 | 53 | // loopback test uses A0 and B0 pins - so use a wire to jumper those two pins on MCP23S17 together |
CRaslawski | 3:3081585fbce1 | 54 | |
CRaslawski | 3:3081585fbce1 | 55 | /* |
CRaslawski | 3:3081585fbce1 | 56 | while (1) { |
CRaslawski | 3:3081585fbce1 | 57 | // write 0xAA to MCP23S17 Port A |
CRaslawski | 3:3081585fbce1 | 58 | chip.write(PORT_A, 0xAA); |
CRaslawski | 3:3081585fbce1 | 59 | wait(.5); |
CRaslawski | 3:3081585fbce1 | 60 | // read back value from MCP23S17 Port B and display B0 on mbed led1 |
CRaslawski | 3:3081585fbce1 | 61 | led1 = chip.read(PORT_B)& 0x01; |
CRaslawski | 3:3081585fbce1 | 62 | // write 0x55 to MCP23S17 Port A |
CRaslawski | 3:3081585fbce1 | 63 | chip.write(PORT_A, 0x55); |
CRaslawski | 3:3081585fbce1 | 64 | wait(.5); |
CRaslawski | 3:3081585fbce1 | 65 | // read back value from MCP23S17 Port B and display B0 on mbed led1 |
CRaslawski | 3:3081585fbce1 | 66 | led1 = chip.read(PORT_B)& 0x01; |
CRaslawski | 3:3081585fbce1 | 67 | // led1 should blink slowly when it is all working |
CRaslawski | 3:3081585fbce1 | 68 | } |
CRaslawski | 3:3081585fbce1 | 69 | */ |
CRaslawski | 3:3081585fbce1 | 70 | //run while loop to check if button has been pushed down |
CRaslawski | 3:3081585fbce1 | 71 | //read port B for 0x01 |
CRaslawski | 3:3081585fbce1 | 72 | bool ledOn = false; |
CRaslawski | 3:3081585fbce1 | 73 | while(1) { |
CRaslawski | 3:3081585fbce1 | 74 | //wait(0.5); |
CRaslawski | 3:3081585fbce1 | 75 | if( chip.read(PORT_B)&0x01 ){ //if button is pushed down, light LED |
CRaslawski | 3:3081585fbce1 | 76 | //wait(); |
CRaslawski | 3:3081585fbce1 | 77 | if(ledOn) { //if on, turn led off |
CRaslawski | 3:3081585fbce1 | 78 | chip.write(PORT_A, 0x00); |
CRaslawski | 3:3081585fbce1 | 79 | ledOn = false; |
CRaslawski | 3:3081585fbce1 | 80 | } |
CRaslawski | 3:3081585fbce1 | 81 | else { //if led off, turn on. |
CRaslawski | 3:3081585fbce1 | 82 | chip.write(PORT_A, 0x01); // write 1 to LED pin, turning it on |
CRaslawski | 3:3081585fbce1 | 83 | ledOn = true; |
CRaslawski | 3:3081585fbce1 | 84 | } |
CRaslawski | 3:3081585fbce1 | 85 | } |
CRaslawski | 3:3081585fbce1 | 86 | wait(.2); |
CRaslawski | 3:3081585fbce1 | 87 | //else{ |
CRaslawski | 3:3081585fbce1 | 88 | // chip.write(PORT_A, 0x00); // write 1 to LED port, turning off |
CRaslawski | 3:3081585fbce1 | 89 | //} |
CRaslawski | 3:3081585fbce1 | 90 | |
CRaslawski | 3:3081585fbce1 | 91 | } |
CRaslawski | 3:3081585fbce1 | 92 | } |
CRaslawski | 3:3081585fbce1 | 93 |