4180 lab 1

Dependencies:   mbed MCP23S17 PinDetect USBDevice

Committer:
emilywilson
Date:
Wed Jan 22 13:08:48 2020 +0000
Revision:
12:cc5bda248946
Parent:
8:a5689a2b6af3
mouse extra credit and power management extra credit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
emilywilson 7:b7720a8623b5 1 // A simple IO demo using the MCP23S17 library
emilywilson 7:b7720a8623b5 2 //
emilywilson 7:b7720a8623b5 3 // MCP23S17 Library Copyright (c) 2010 Romilly Cocking
emilywilson 7:b7720a8623b5 4 // Released under the MIT License: http://mbed.org/license/mit
emilywilson 7:b7720a8623b5 5 //
emilywilson 7:b7720a8623b5 6 // See http://mbed.org/users/romilly/notebook/mcp23s17-addressable-16-bit-io-expander-with-spi/
emilywilson 7:b7720a8623b5 7 //
emilywilson 7:b7720a8623b5 8 //
emilywilson 7:b7720a8623b5 9 // MCP23S17 datasheet http://ww1.microchip.com/downloads/en/DeviceDoc/21952b.pdf
emilywilson 7:b7720a8623b5 10 // uses MCP23S17 library version 0.4
emilywilson 7:b7720a8623b5 11
emilywilson 7:b7720a8623b5 12 #include "mbed.h"
emilywilson 7:b7720a8623b5 13 #include "MCP23S17.h"
emilywilson 7:b7720a8623b5 14 // Create SPI bus
emilywilson 7:b7720a8623b5 15 SPI spi(p5, p6, p7);
emilywilson 7:b7720a8623b5 16 //
emilywilson 7:b7720a8623b5 17 // Wiring Connections:
emilywilson 7:b7720a8623b5 18 // mbed p5,p6,p7 are tied to MCP23S17 SI, SO, SCK pins
emilywilson 7:b7720a8623b5 19 // mbed p20 to MCP23S17 CS
emilywilson 7:b7720a8623b5 20 // MCP23S17 reset pin pulled high
emilywilson 7:b7720a8623b5 21 // MCP23S17 GPA0 connected to GPB0 for loopback test
emilywilson 7:b7720a8623b5 22 // A0, A1, A2 of the MCP23S17 are tied to ground on the breadboard, so the 8-bit address for writes is 0x40
emilywilson 7:b7720a8623b5 23 // This is referred to as the opcode in the device datasheet
emilywilson 7:b7720a8623b5 24 char Opcode = 0x40;
emilywilson 7:b7720a8623b5 25
emilywilson 7:b7720a8623b5 26 // Next create a MCP23S17
emilywilson 7:b7720a8623b5 27 // mbed p20 is connected to ~chipSelect on the MCP23S17
emilywilson 7:b7720a8623b5 28 MCP23S17 chip = MCP23S17(spi, p20, Opcode);
emilywilson 7:b7720a8623b5 29
emilywilson 7:b7720a8623b5 30 // Optional software reset - mbed p14 to MCP23S17 reset pin
emilywilson 7:b7720a8623b5 31 // DigitalOut reset(p14);
emilywilson 7:b7720a8623b5 32
emilywilson 8:a5689a2b6af3 33 int pb;
emilywilson 7:b7720a8623b5 34
emilywilson 7:b7720a8623b5 35 int run_part7() {
emilywilson 7:b7720a8623b5 36 // The MCP23S17 reset pin can just be pulled high, since it has a power on reset circuit.
emilywilson 7:b7720a8623b5 37 // The reset pin can be used for a software forced reset by pulling it low with an mbed GPIO pin.
emilywilson 7:b7720a8623b5 38 // But just leave it pulled high for this simple demo code.
emilywilson 7:b7720a8623b5 39 // After a power on reset, both IO ports default to input mode
emilywilson 7:b7720a8623b5 40 //
emilywilson 7:b7720a8623b5 41 // Here is the optional code for a software reset
emilywilson 7:b7720a8623b5 42 // reset = 0;
emilywilson 7:b7720a8623b5 43 // wait_us(1);
emilywilson 7:b7720a8623b5 44 // reset = 1;
emilywilson 7:b7720a8623b5 45 //
emilywilson 7:b7720a8623b5 46 // Set all 8 Port A bits to output direction
emilywilson 7:b7720a8623b5 47 chip.direction(PORT_A, 0x00);
emilywilson 7:b7720a8623b5 48 // Set all 8 Port B bits to input direction
emilywilson 7:b7720a8623b5 49 chip.direction(PORT_B, 0xFF);
emilywilson 7:b7720a8623b5 50 // Start Loopback test sending out and reading back values
emilywilson 7:b7720a8623b5 51 // loopback test uses A0 and B0 pins - so use a wire to jumper those two pins on MCP23S17 together
emilywilson 7:b7720a8623b5 52 while (1) {
emilywilson 7:b7720a8623b5 53 // write value of pushbutton to Port A
emilywilson 8:a5689a2b6af3 54 pb = chip.read(PORT_B) & 0x01;
emilywilson 7:b7720a8623b5 55 chip.write(PORT_A, !pb);
emilywilson 7:b7720a8623b5 56 wait(.5);
emilywilson 7:b7720a8623b5 57 // read back value from MCP23S17 Port B and display B0 on myled
emilywilson 7:b7720a8623b5 58 }
emilywilson 7:b7720a8623b5 59 }