BAT-SD4012_2017 / Mbed 2 deprecated ECE4180Lab1_3

Dependencies:   MCP23S17 mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

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