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

Committer:
4180_1
Date:
Thu Jan 27 03:16:18 2011 +0000
Revision:
0:12f4911d7ba0
Child:
1:70e1ebe47253

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
4180_1 0:12f4911d7ba0 1 /* A simple IO demo using the MCP23S17 library
4180_1 0:12f4911d7ba0 2 * Copyright (c) 2010 Romilly Cocking
4180_1 0:12f4911d7ba0 3 * Released under the MIT License: http://mbed.org/license/mit
4180_1 0:12f4911d7ba0 4 *
4180_1 0:12f4911d7ba0 5 * See http://mbed.org/users/romilly/notebook/mcp23s17-addressable-16-bit-io-expander-with-spi/
4180_1 0:12f4911d7ba0 6 *
4180_1 0:12f4911d7ba0 7
4180_1 0:12f4911d7ba0 8 * MCP23S17 datasheet http://ww1.microchip.com/downloads/en/DeviceDoc/21952b.pdf
4180_1 0:12f4911d7ba0 9 * version 0.4
4180_1 0:12f4911d7ba0 10 */
4180_1 0:12f4911d7ba0 11 #include "mbed.h"
4180_1 0:12f4911d7ba0 12 #include "MCP23S17.h"
4180_1 0:12f4911d7ba0 13
4180_1 0:12f4911d7ba0 14 SPI spi(p5, p6, p7);
4180_1 0:12f4911d7ba0 15 // Create SPI bus
4180_1 0:12f4911d7ba0 16 // mbed p5,p6,p7 are tied to MCP23S17 SI, SO, SCK pins
4180_1 0:12f4911d7ba0 17 // A0, A1, A2 of the MCP23S17 are tied to ground on the breadboard, so the 8-bit address for writes is 0x40
4180_1 0:12f4911d7ba0 18 // This is referred to as the opcode in the device datasheet
4180_1 0:12f4911d7ba0 19 char writeOpcode = 0x40;
4180_1 0:12f4911d7ba0 20
4180_1 0:12f4911d7ba0 21 // Next create a MCP23S17
4180_1 0:12f4911d7ba0 22 // mbed p20 is connected to ~chipSelect on the MCP23S17
4180_1 0:12f4911d7ba0 23 MCP23S17 chip = MCP23S17(spi, p20, writeOpcode);
4180_1 0:12f4911d7ba0 24
4180_1 0:12f4911d7ba0 25 DigitalOut led1(LED1); // mbed LED1 is used for test status display
4180_1 0:12f4911d7ba0 26
4180_1 0:12f4911d7ba0 27 int main() {
4180_1 0:12f4911d7ba0 28 // Nothing has to be connected to the MCP23S17 reset pin since it has a power on reset circuit.
4180_1 0:12f4911d7ba0 29 // It can be used for a software forced reset by pulling it low with an mbed GPIO pin.
4180_1 0:12f4911d7ba0 30 // But just leave it unconnected for this simple demo code. It is possible to lock up the chip
4180_1 0:12f4911d7ba0 31 // if you happen to reset the mbed in the middle of an SPI transfer (not too likely).
4180_1 0:12f4911d7ba0 32 // If it happens just remove and attach the USB cable to reset by cycling the power.
4180_1 0:12f4911d7ba0 33 // After a power on reset, both IO ports default to input mode
4180_1 0:12f4911d7ba0 34 //
4180_1 0:12f4911d7ba0 35 // Set all 8 Port A bits to output direction
4180_1 0:12f4911d7ba0 36 chip.direction(PORT_A, 0x00);
4180_1 0:12f4911d7ba0 37 // Set all 8 Port B bits to input direction
4180_1 0:12f4911d7ba0 38 chip.direction(PORT_B, 0xFF);
4180_1 0:12f4911d7ba0 39 led1=0;
4180_1 0:12f4911d7ba0 40 // Start Loopback test sending out and reading back values
4180_1 0:12f4911d7ba0 41 // loopback test uses A0 and B0 pins - so use a wire to jumper those two pins on MCP23S17 together
4180_1 0:12f4911d7ba0 42 while (1) {
4180_1 0:12f4911d7ba0 43 // write 0xAA to MCP23S17 Port A
4180_1 0:12f4911d7ba0 44 chip.write(PORT_A, 0xAA);
4180_1 0:12f4911d7ba0 45 wait(.5);
4180_1 0:12f4911d7ba0 46 // read back value from MCP23S17 Port B and display B0 on mbed led1
4180_1 0:12f4911d7ba0 47 led1 = chip.read(PORT_B)& 0x01;
4180_1 0:12f4911d7ba0 48 // write 0x55 to MCP23S17 Port A
4180_1 0:12f4911d7ba0 49 chip.write(PORT_A, 0x55);
4180_1 0:12f4911d7ba0 50 wait(.5);
4180_1 0:12f4911d7ba0 51 // read back value from MCP23S17 Port B and display B0 on mbed led1
4180_1 0:12f4911d7ba0 52 led1 = chip.read(PORT_B)& 0x01;
4180_1 0:12f4911d7ba0 53 // led1 should blink slowly if it is all working
4180_1 0:12f4911d7ba0 54 }
4180_1 0:12f4911d7ba0 55 }