PArt seven of lab 1

Dependencies:   MCP23S17 mbed

Committer:
mus3
Date:
Fri Sep 09 20:14:29 2022 +0000
Revision:
4:e63bb299bf30
Parent:
3:cb2f079f1f97
part7 now can use external led and pushbutton

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
mus3 4:e63bb299bf30 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);
mus3 4:e63bb299bf30 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;
mus3 4:e63bb299bf30 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);
mus3 4:e63bb299bf30 29
4180_1 2:934a0500abde 30 // Optional software reset - mbed p14 to MCP23S17 reset pin
mus3 4:e63bb299bf30 31 DigitalOut reset(p14);
mus3 4:e63bb299bf30 32
mus3 4:e63bb299bf30 33 DigitalOut red(p9);
mus3 4:e63bb299bf30 34 DigitalIn pblight(p8);
mus3 4:e63bb299bf30 35
mus3 4:e63bb299bf30 36 // DigitalOut led1(LED1); // mbed LED1 is used for test status display
mus3 4:e63bb299bf30 37
4180_1 0:12f4911d7ba0 38 int main() {
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
mus3 4:e63bb299bf30 45 reset = 0;
mus3 4:e63bb299bf30 46 wait_us(1);
mus3 4:e63bb299bf30 47 reset = 1;
mus3 4:e63bb299bf30 48
mus3 4:e63bb299bf30 49 pblight.mode(PullUp);
mus3 4:e63bb299bf30 50 wait(0.001);
4180_1 1:70e1ebe47253 51 //
4180_1 0:12f4911d7ba0 52 // Set all 8 Port A bits to output direction
4180_1 0:12f4911d7ba0 53 chip.direction(PORT_A, 0x00);
4180_1 0:12f4911d7ba0 54 // Set all 8 Port B bits to input direction
4180_1 0:12f4911d7ba0 55 chip.direction(PORT_B, 0xFF);
4180_1 0:12f4911d7ba0 56 // Start Loopback test sending out and reading back values
4180_1 0:12f4911d7ba0 57 // loopback test uses A0 and B0 pins - so use a wire to jumper those two pins on MCP23S17 together
4180_1 0:12f4911d7ba0 58 while (1) {
mus3 4:e63bb299bf30 59 // write 0xAA to MCP23S17 Port A
mus3 4:e63bb299bf30 60 chip.write(PORT_A, !pblight);
mus3 4:e63bb299bf30 61 wait(.01);
mus3 4:e63bb299bf30 62 // read back value from MCP23S17 Port B and display B0 on mbed led1
mus3 4:e63bb299bf30 63 red = chip.read(PORT_B)& 0x01;
mus3 4:e63bb299bf30 64 // write 0x55 to MCP23S17 Port A
mus3 4:e63bb299bf30 65 // chip.write(PORT_A, 0x55);
mus3 4:e63bb299bf30 66 // wait(.5);
mus3 4:e63bb299bf30 67 // read back value from MCP23S17 Port B and display B0 on mbed led1
mus3 4:e63bb299bf30 68 // led1 = chip.read(PORT_B)& 0x01;
mus3 4:e63bb299bf30 69 // led1 should blink slowly when it is all working
4180_1 0:12f4911d7ba0 70 }
mus3 4:e63bb299bf30 71 }