Lab 1 Part 7
main.cpp@3:03e0b3710bf0, 2021-02-01 (annotated)
- Committer:
- glanier9
- Date:
- Mon Feb 01 20:04:37 2021 +0000
- Revision:
- 3:03e0b3710bf0
- Parent:
- 2:934a0500abde
final
Who changed what in which revision?
| User | Revision | Line number | New 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 | |
| 4180_1 | 0:12f4911d7ba0 | 12 | #include "mbed.h" |
| 4180_1 | 0:12f4911d7ba0 | 13 | #include "MCP23S17.h" |
| 4180_1 | 2:934a0500abde | 14 | // Create SPI bus |
| 4180_1 | 0:12f4911d7ba0 | 15 | SPI spi(p5, p6, p7); |
| 4180_1 | 2:934a0500abde | 16 | // |
| 4180_1 | 2:934a0500abde | 17 | // Wiring Connections: |
| 4180_1 | 0:12f4911d7ba0 | 18 | // mbed p5,p6,p7 are tied to MCP23S17 SI, SO, SCK pins |
| 4180_1 | 2:934a0500abde | 19 | // mbed p20 to MCP23S17 CS |
| 4180_1 | 2:934a0500abde | 20 | // MCP23S17 reset pin pulled high |
| 4180_1 | 2:934a0500abde | 21 | // MCP23S17 GPA0 connected to GPB0 for loopback test |
| 4180_1 | 0:12f4911d7ba0 | 22 | // 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 | 23 | // This is referred to as the opcode in the device datasheet |
| 4180_1 | 2:934a0500abde | 24 | char Opcode = 0x40; |
| 4180_1 | 0:12f4911d7ba0 | 25 | |
| 4180_1 | 0:12f4911d7ba0 | 26 | // Next create a MCP23S17 |
| 4180_1 | 0:12f4911d7ba0 | 27 | // mbed p20 is connected to ~chipSelect on the MCP23S17 |
| 4180_1 | 2:934a0500abde | 28 | MCP23S17 chip = MCP23S17(spi, p20, Opcode); |
| 4180_1 | 0:12f4911d7ba0 | 29 | |
| 4180_1 | 2:934a0500abde | 30 | // Optional software reset - mbed p14 to MCP23S17 reset pin |
| 4180_1 | 2:934a0500abde | 31 | // DigitalOut reset(p14); |
| 4180_1 | 1:70e1ebe47253 | 32 | |
| 4180_1 | 0:12f4911d7ba0 | 33 | DigitalOut led1(LED1); // mbed LED1 is used for test status display |
| 4180_1 | 0:12f4911d7ba0 | 34 | |
| 4180_1 | 0:12f4911d7ba0 | 35 | int main() { |
| 4180_1 | 1:70e1ebe47253 | 36 | // The MCP23S17 reset pin can just be pulled high, since it has a power on reset circuit. |
| 4180_1 | 1:70e1ebe47253 | 37 | // The reset pin can be used for a software forced reset by pulling it low with an mbed GPIO pin. |
| 4180_1 | 1:70e1ebe47253 | 38 | // But just leave it pulled high for this simple demo code. |
| 4180_1 | 0:12f4911d7ba0 | 39 | // After a power on reset, both IO ports default to input mode |
| 4180_1 | 0:12f4911d7ba0 | 40 | // |
| 4180_1 | 1:70e1ebe47253 | 41 | // Here is the optional code for a software reset |
| 4180_1 | 1:70e1ebe47253 | 42 | // reset = 0; |
| 4180_1 | 1:70e1ebe47253 | 43 | // wait_us(1); |
| 4180_1 | 1:70e1ebe47253 | 44 | // reset = 1; |
| 4180_1 | 1:70e1ebe47253 | 45 | // |
| 4180_1 | 0:12f4911d7ba0 | 46 | // Set all 8 Port A bits to output direction |
| 4180_1 | 0:12f4911d7ba0 | 47 | chip.direction(PORT_A, 0x00); |
| 4180_1 | 0:12f4911d7ba0 | 48 | // Set all 8 Port B bits to input direction |
| 4180_1 | 0:12f4911d7ba0 | 49 | chip.direction(PORT_B, 0xFF); |
| 4180_1 | 0:12f4911d7ba0 | 50 | led1=0; |
| 4180_1 | 0:12f4911d7ba0 | 51 | // Start Loopback test sending out and reading back values |
| 4180_1 | 0:12f4911d7ba0 | 52 | // loopback test uses A0 and B0 pins - so use a wire to jumper those two pins on MCP23S17 together |
| glanier9 | 3:03e0b3710bf0 | 53 | int sw = 0; |
| 4180_1 | 0:12f4911d7ba0 | 54 | while (1) { |
| glanier9 | 3:03e0b3710bf0 | 55 | // // write 0xAA to MCP23S17 Port A |
| glanier9 | 3:03e0b3710bf0 | 56 | // chip.write(PORT_A, 0xAA); |
| glanier9 | 3:03e0b3710bf0 | 57 | // wait(.5); |
| glanier9 | 3:03e0b3710bf0 | 58 | // // read back value from MCP23S17 Port B and display B0 on mbed led1 |
| glanier9 | 3:03e0b3710bf0 | 59 | // led1 = chip.read(PORT_B)& 0x01; |
| glanier9 | 3:03e0b3710bf0 | 60 | // // write 0x55 to MCP23S17 Port A |
| glanier9 | 3:03e0b3710bf0 | 61 | // chip.write(PORT_A, 0x55); |
| glanier9 | 3:03e0b3710bf0 | 62 | // wait(.5); |
| glanier9 | 3:03e0b3710bf0 | 63 | // // read back value from MCP23S17 Port B and display B0 on mbed led1 |
| glanier9 | 3:03e0b3710bf0 | 64 | // led1 = chip.read(PORT_B)& 0x01; |
| glanier9 | 3:03e0b3710bf0 | 65 | // // led1 should blink slowly when it is all working |
| glanier9 | 3:03e0b3710bf0 | 66 | |
| glanier9 | 3:03e0b3710bf0 | 67 | // Read from B |
| glanier9 | 3:03e0b3710bf0 | 68 | sw = chip.read(PORT_B)& 0x01; |
| glanier9 | 3:03e0b3710bf0 | 69 | |
| glanier9 | 3:03e0b3710bf0 | 70 | chip.write(PORT_A, sw); |
| glanier9 | 3:03e0b3710bf0 | 71 | |
| glanier9 | 3:03e0b3710bf0 | 72 | |
| glanier9 | 3:03e0b3710bf0 | 73 | |
| 4180_1 | 0:12f4911d7ba0 | 74 | } |
| 4180_1 | 0:12f4911d7ba0 | 75 | } |