Craig Raslawski / Mbed 2 deprecated MCP23S17_IO_Expander

Dependencies:   MCP23S17 mbed

Fork of MCP23S17_Basic_IO_Demo by jim hamblen

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers part3.cpp Source File

part3.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(p11, p12, p13);
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 Opcode = 0x40;
00025 
00026 // Next create a MCP23S17
00027 // mbed p20 is connected to ~chipSelect on the MCP23S17
00028 MCP23S17 chip = MCP23S17(spi, p20, Opcode);
00029 
00030 // Optional software reset - mbed p14 to MCP23S17 reset pin
00031 // DigitalOut reset(p14);
00032 
00033 DigitalOut led1(LED1); // mbed LED1 is used for test status display
00034 
00035 int main()
00036 {
00037 //  The MCP23S17 reset pin can just be pulled high, since it has a power on reset circuit.
00038 //  The reset pin can be used for a software forced reset by pulling it low with an mbed GPIO pin.
00039 //  But just leave it pulled high for this simple demo code.
00040 //  After a power on reset, both IO ports default to input mode
00041 //
00042 //  Here is the optional code for a software reset
00043 //  reset = 0;
00044 //  wait_us(1);
00045 //  reset = 1;
00046 //
00047 //  Set all 8 Port A bits to output direction
00048     chip.direction(PORT_A, 0x00);
00049 //  Set all 8 Port B bits to input direction
00050     chip.direction(PORT_B, 0xFF);
00051     led1=0;
00052 //  Start Loopback test sending out and reading back values
00053 //  loopback test uses A0 and B0 pins - so use a wire to jumper those two pins on MCP23S17 together
00054 
00055     /*
00056     while (1) {
00057         // write 0xAA to MCP23S17 Port A
00058         chip.write(PORT_A, 0xAA);
00059         wait(.5);
00060         // read back value from MCP23S17 Port B and display B0 on mbed led1
00061         led1 = chip.read(PORT_B)& 0x01;
00062         // write 0x55 to MCP23S17 Port A
00063         chip.write(PORT_A, 0x55);
00064         wait(.5);
00065         // read back value from MCP23S17 Port B and display B0 on mbed led1
00066         led1 = chip.read(PORT_B)& 0x01;
00067         // led1 should blink slowly when it is all working
00068     }
00069     */
00070     //run while loop to check if button has been pushed down
00071     //read port B for 0x01
00072     bool ledOn = false;
00073     while(1) {
00074         //wait(0.5);
00075         if( chip.read(PORT_B)&0x01 ){ //if button is pushed down, light LED
00076             //wait();
00077             if(ledOn) { //if on, turn led off
00078                 chip.write(PORT_A, 0x00);
00079                 ledOn = false;
00080             } 
00081             else { //if led off, turn on.
00082                 chip.write(PORT_A, 0x01);   // write 1 to LED pin, turning it on
00083                 ledOn = true;
00084             }
00085         }
00086         wait(.2);
00087         //else{
00088         //   chip.write(PORT_A, 0x00);   // write 1 to LED port, turning off
00089         //}
00090 
00091     }
00092 }
00093