A simple IO demo using the MCP23S17 library

Dependencies:   mbed MCP23S17

Demo for Microchip MCP23S17 16-bit SPI I/O expander chip on mbed LPC1768 using hardware SPI functions. See comments in main.cpp for pin usage. This chip is still available in a DIP package, so it is easy to try on a breadboard. A complete datasheet for the Microchip MCP23SA17 SPI I/O port expander chip can be found at http://ww1.microchip.com/downloads/en/DeviceDoc/20001952c.pdf

Import programMCP23S17_Basic_IO_Demo

A simple IO demo using the MCP23S17 library

Files at this revision

API Documentation at this revision

Comitter:
4180_1
Date:
Thu Jan 27 03:16:18 2011 +0000
Child:
1:70e1ebe47253
Commit message:

Changed in this revision

MCP23S17.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MCP23S17.lib	Thu Jan 27 03:16:18 2011 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/romilly/code/MCP23S17/#068b1e8909bb
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Thu Jan 27 03:16:18 2011 +0000
@@ -0,0 +1,55 @@
+/* A simple IO demo using the MCP23S17 library
+* Copyright (c) 2010 Romilly Cocking
+* Released under the MIT License: http://mbed.org/license/mit
+*
+* See http://mbed.org/users/romilly/notebook/mcp23s17-addressable-16-bit-io-expander-with-spi/
+*
+
+* MCP23S17 datasheet http://ww1.microchip.com/downloads/en/DeviceDoc/21952b.pdf
+* version 0.4
+*/
+#include "mbed.h"
+#include "MCP23S17.h"
+
+SPI spi(p5, p6, p7);
+// Create SPI bus
+// mbed p5,p6,p7 are tied to MCP23S17 SI, SO, SCK pins
+// A0, A1, A2 of the MCP23S17  are tied to ground on the breadboard, so the 8-bit address for writes is 0x40
+// This is referred to as the opcode in the device datasheet
+char writeOpcode = 0x40;
+
+// Next create a MCP23S17
+// mbed p20 is connected to ~chipSelect on the MCP23S17
+MCP23S17 chip = MCP23S17(spi, p20, writeOpcode);
+
+DigitalOut led1(LED1); // mbed LED1 is used for test status display
+
+int main() {
+//  Nothing has to be connected to the MCP23S17 reset pin since it has a power on reset circuit.
+//  It can be used for a software forced reset by pulling it low with an mbed GPIO pin.
+//  But just leave it unconnected for this simple demo code. It is possible to lock up the chip
+//  if you happen to reset the mbed in the middle of an SPI transfer (not too likely). 
+//  If it happens just remove and attach the USB cable to reset by cycling the power.
+//  After a power on reset, both IO ports default to input mode
+//
+//  Set all 8 Port A bits to output direction
+    chip.direction(PORT_A, 0x00);
+//  Set all 8 Port B bits to input direction
+    chip.direction(PORT_B, 0xFF);
+    led1=0;
+//  Start Loopback test sending out and reading back values
+//  loopback test uses A0 and B0 pins - so use a wire to jumper those two pins on MCP23S17 together
+    while (1) {
+        // write 0xAA to MCP23S17 Port A
+        chip.write(PORT_A, 0xAA);
+        wait(.5);
+        // read back value from MCP23S17 Port B and display B0 on mbed led1
+        led1 = chip.read(PORT_B)& 0x01;
+        // write 0x55 to MCP23S17 Port A
+        chip.write(PORT_A, 0x55);
+        wait(.5);
+        // read back value from MCP23S17 Port B and display B0 on mbed led1
+        led1 = chip.read(PORT_B)& 0x01;
+        // led1 should blink slowly if it is all working
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Thu Jan 27 03:16:18 2011 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/9114680c05da