4180 / Mbed 2 deprecated 4180_lab1_part7_ec

Dependencies:   MCP23S17 mbed

Fork of 4180_lab1_part7 by 4180

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 // A simple IO demo using the MCP23S17 library
00002 //
00003 // MCP23S17 Library Copyright (c) 2010 Romilly Cocking
00004 // Released under the MIT License: http://mbed.org/license/mit
00005 //
00006 // See http://mbed.org/users/romilly/notebook/mcp23s17-addressable-16-bit-io-expander-with-spi/
00007 //
00008 //
00009 // MCP23S17 datasheet http://ww1.microchip.com/downloads/en/DeviceDoc/21952b.pdf
00010 // uses MCP23S17 library version 0.4
00011 
00012 #include "mbed.h"
00013 #include "MCP23S17.h"
00014 // Create SPI bus
00015 SPI spi(p5, p6, p7);
00016 // 
00017 // Wiring Connections:
00018 // mbed p5,p6,p7 are tied to MCP23S17 SI, SO, SCK pins
00019 // mbed p20 to MCP23S17 CS
00020 // MCP23S17 reset pin pulled high
00021 // MCP23S17 GPA0 connected to GPB0 for loopback test
00022 // A0, A1, A2 of the MCP23S17  are tied to ground on the breadboard, so the 8-bit address for writes is 0x40
00023 // This is referred to as the opcode in the device datasheet
00024 char OpcodeA = 0x40;
00025 char OpcodeB = 0x42;
00026 
00027 // Next create a MCP23S17
00028 // mbed p20 is connected to ~chipSelect on the MCP23S17
00029 MCP23S17 chip1 = MCP23S17(spi, p20, OpcodeA);
00030 MCP23S17 chip2 = MCP23S17(spi, p21, OpcodeB);
00031 
00032 // Optional software reset - mbed p14 to MCP23S17 reset pin
00033 // DigitalOut reset(p14);
00034 
00035 DigitalOut led1(p22); // mbed LED1 is used for test status display
00036 DigitalIn pb(p19);
00037 
00038 int main() {
00039 //  The MCP23S17 reset pin can just be pulled high, since it has a power on reset circuit.
00040 //  The reset pin can be used for a software forced reset by pulling it low with an mbed GPIO pin.
00041 //  But just leave it pulled high for this simple demo code.
00042 //  After a power on reset, both IO ports default to input mode
00043 //
00044 //  Here is the optional code for a software reset
00045 //  reset = 0;
00046 //  wait_us(1);
00047 //  reset = 1;
00048 
00049     //pb.mode(PullUp);
00050 
00051 //  Set all 8 Port A bits to output direction
00052     chip1.direction(PORT_A, 0x00);
00053     chip2.direction(PORT_A, 0x00);
00054 //  Set all 8 Port B bits to input direction
00055     chip1.direction(PORT_B, 0xFF);
00056     chip2.direction(PORT_B, 0xFF);
00057 //  Start Loopback test sending out and reading back values
00058 //  loopback test uses A0 and B0 pins - so use a wire to jumper those two pins on MCP23S17 together
00059     while (1) {
00060         if(!(chip1.read(PORT_B) & 0x01)) {  
00061             chip1.write(PORT_A, 0x55);
00062             wait(.1);
00063             // read back value from MCP23S17 Port B and display B0 on mbed led1
00064             //led1 = 1;
00065         }
00066         // write 0xAA to MCP23S17 Port A
00067         else {
00068         // write 0x55 to MCP23S17 Port A
00069         chip1.write(PORT_A, 0xAA);
00070         wait(.1);
00071         // read back value from MCP23S17 Port B and display B0 on mbed led1
00072         //led1 = 0;
00073         // led1 should blink slowly when it is all working
00074         }
00075         
00076         if((chip2.read(PORT_B) & 0x01)) {
00077             chip2.write(PORT_A, 0x55);
00078             wait(0.1);
00079             led1 = 1;
00080         }
00081         else {
00082             chip2.write(PORT_A, 0xAA);
00083             wait(0.1);
00084             led1 = 0;
00085         }        
00086     }
00087 }