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