ECE 4180 Lab 1 Extra Credit 4

Dependencies:   MCP23S17 mbed

Fork of MCP23S17_Basic_IO_Demo by jim hamblen

Committer:
abraha2d
Date:
Tue Oct 09 00:16:03 2018 +0000
Revision:
3:52b465070371
Parent:
2:934a0500abde
Save point

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
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);
abraha2d 3:52b465070371 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
abraha2d 3:52b465070371 19 // mbed p19 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;
abraha2d 3:52b465070371 25 char Opcode2 = 0x42;
4180_1 0:12f4911d7ba0 26
4180_1 0:12f4911d7ba0 27 // Next create a MCP23S17
abraha2d 3:52b465070371 28 // mbed p19 is connected to ~chipSelect on the MCP23S17
abraha2d 3:52b465070371 29 MCP23S17 chip = MCP23S17(spi, p19, Opcode);
abraha2d 3:52b465070371 30 MCP23S17 chip2 = MCP23S17(spi, p19, Opcode2);
4180_1 0:12f4911d7ba0 31
4180_1 2:934a0500abde 32 // Optional software reset - mbed p14 to MCP23S17 reset pin
4180_1 2:934a0500abde 33 // DigitalOut reset(p14);
4180_1 1:70e1ebe47253 34
4180_1 0:12f4911d7ba0 35 DigitalOut led1(LED1); // mbed LED1 is used for test status display
4180_1 0:12f4911d7ba0 36
abraha2d 3:52b465070371 37 int main()
abraha2d 3:52b465070371 38 {
4180_1 1:70e1ebe47253 39 // The MCP23S17 reset pin can just be pulled high, since it has a power on reset circuit.
4180_1 1:70e1ebe47253 40 // The reset pin can be used for a software forced reset by pulling it low with an mbed GPIO pin.
4180_1 1:70e1ebe47253 41 // But just leave it pulled high for this simple demo code.
4180_1 0:12f4911d7ba0 42 // After a power on reset, both IO ports default to input mode
4180_1 0:12f4911d7ba0 43 //
4180_1 1:70e1ebe47253 44 // Here is the optional code for a software reset
4180_1 1:70e1ebe47253 45 // reset = 0;
4180_1 1:70e1ebe47253 46 // wait_us(1);
4180_1 1:70e1ebe47253 47 // reset = 1;
4180_1 1:70e1ebe47253 48 //
4180_1 0:12f4911d7ba0 49 // Set all 8 Port A bits to output direction
4180_1 0:12f4911d7ba0 50 chip.direction(PORT_A, 0x00);
abraha2d 3:52b465070371 51 chip2.direction(PORT_A, 0x00);
4180_1 0:12f4911d7ba0 52 // Set all 8 Port B bits to input direction
4180_1 0:12f4911d7ba0 53 chip.direction(PORT_B, 0xFF);
abraha2d 3:52b465070371 54 chip2.direction(PORT_B, 0xFF);
4180_1 0:12f4911d7ba0 55 led1=0;
abraha2d 3:52b465070371 56
4180_1 0:12f4911d7ba0 57 while (1) {
abraha2d 3:52b465070371 58 chip2.write(PORT_A, !chip.read(PORT_B) & 0x01);
4180_1 0:12f4911d7ba0 59 }
4180_1 0:12f4911d7ba0 60 }