f

Dependencies:   mbed MCP23S17

Committer:
SpiralUp
Date:
Fri Jan 25 17:13:58 2019 +0000
Revision:
0:4e0673615a5f
Lab1Task8

Who changed what in which revision?

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