Craig Raslawski
/
MCP23S17_IO_Expander
4180Lab1Part3
Fork of MCP23S17_Basic_IO_Demo by
Revision 3:3081585fbce1, committed 2017-02-27
- Comitter:
- CRaslawski
- Date:
- Mon Feb 27 22:31:04 2017 +0000
- Parent:
- 2:934a0500abde
- Commit message:
- initial commit
Changed in this revision
main.cpp | Show diff for this revision Revisions of this file |
part3.cpp | Show annotated file Show diff for this revision Revisions of this file |
diff -r 934a0500abde -r 3081585fbce1 main.cpp --- a/main.cpp Fri Jan 28 02:04:05 2011 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,66 +0,0 @@ -// A simple IO demo using the MCP23S17 library -// -// 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 -// uses MCP23S17 library version 0.4 - -#include "mbed.h" -#include "MCP23S17.h" -// Create SPI bus -SPI spi(p5, p6, p7); -// -// Wiring Connections: -// mbed p5,p6,p7 are tied to MCP23S17 SI, SO, SCK pins -// mbed p20 to MCP23S17 CS -// MCP23S17 reset pin pulled high -// MCP23S17 GPA0 connected to GPB0 for loopback test -// 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 Opcode = 0x40; - -// Next create a MCP23S17 -// mbed p20 is connected to ~chipSelect on the MCP23S17 -MCP23S17 chip = MCP23S17(spi, p20, Opcode); - -// Optional software reset - mbed p14 to MCP23S17 reset pin -// DigitalOut reset(p14); - -DigitalOut led1(LED1); // mbed LED1 is used for test status display - -int main() { -// The MCP23S17 reset pin can just be pulled high, since it has a power on reset circuit. -// The reset pin can be used for a software forced reset by pulling it low with an mbed GPIO pin. -// But just leave it pulled high for this simple demo code. -// After a power on reset, both IO ports default to input mode -// -// Here is the optional code for a software reset -// reset = 0; -// wait_us(1); -// reset = 1; -// -// 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 when it is all working - } -}
diff -r 934a0500abde -r 3081585fbce1 part3.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/part3.cpp Mon Feb 27 22:31:04 2017 +0000 @@ -0,0 +1,93 @@ +// A simple IO demo using the MCP23S17 library +// +// 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 +// uses MCP23S17 library version 0.4 + +#include "mbed.h" +#include "MCP23S17.h" +// Create SPI bus +SPI spi(p11, p12, p13); +// +// Wiring Connections: +// mbed p5,p6,p7 are tied to MCP23S17 SI, SO, SCK pins +// mbed p20 to MCP23S17 CS +// MCP23S17 reset pin pulled high +// MCP23S17 GPA0 connected to GPB0 for loopback test +// 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 Opcode = 0x40; + +// Next create a MCP23S17 +// mbed p20 is connected to ~chipSelect on the MCP23S17 +MCP23S17 chip = MCP23S17(spi, p20, Opcode); + +// Optional software reset - mbed p14 to MCP23S17 reset pin +// DigitalOut reset(p14); + +DigitalOut led1(LED1); // mbed LED1 is used for test status display + +int main() +{ +// The MCP23S17 reset pin can just be pulled high, since it has a power on reset circuit. +// The reset pin can be used for a software forced reset by pulling it low with an mbed GPIO pin. +// But just leave it pulled high for this simple demo code. +// After a power on reset, both IO ports default to input mode +// +// Here is the optional code for a software reset +// reset = 0; +// wait_us(1); +// reset = 1; +// +// 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 when it is all working + } + */ + //run while loop to check if button has been pushed down + //read port B for 0x01 + bool ledOn = false; + while(1) { + //wait(0.5); + if( chip.read(PORT_B)&0x01 ){ //if button is pushed down, light LED + //wait(); + if(ledOn) { //if on, turn led off + chip.write(PORT_A, 0x00); + ledOn = false; + } + else { //if led off, turn on. + chip.write(PORT_A, 0x01); // write 1 to LED pin, turning it on + ledOn = true; + } + } + wait(.2); + //else{ + // chip.write(PORT_A, 0x00); // write 1 to LED port, turning off + //} + + } +} +