CodeShare
Fork of MCP23S17_Basic_IO_Demo by
Revision 3:ff23f7e9c064, committed 2016-09-25
- Comitter:
- jeremycai3721
- Date:
- Sun Sep 25 20:18:06 2016 +0000
- Parent:
- 2:934a0500abde
- Commit message:
- CodeShare
Changed in this revision
| main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Fri Jan 28 02:04:05 2011 +0000
+++ b/main.cpp Sun Sep 25 20:18:06 2016 +0000
@@ -9,6 +9,15 @@
// MCP23S17 datasheet http://ww1.microchip.com/downloads/en/DeviceDoc/21952b.pdf
// uses MCP23S17 library version 0.4
+
+// Extra Credit
+// Chip 2 is used to read in pushbutton data on
+// CS "Chip Select" for chip2 is P19
+// Chip 2 reads Pushbutton input from B0
+// Chip 1 outputs LED high on A0
+
+
+
#include "mbed.h"
#include "MCP23S17.h"
// Create SPI bus
@@ -27,11 +36,17 @@
// mbed p20 is connected to ~chipSelect on the MCP23S17
MCP23S17 chip = MCP23S17(spi, p20, Opcode);
+// Chip 2 instance
+// CS pin (chip select) is P19 on mbed
+MCP23S17 chip2 = MCP23S17(spi, p19, 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 B0;
+
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.
@@ -46,21 +61,32 @@
// 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);
+ chip2.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
+ // read in B0 and see if it's 0 (button being pressed)
+ B0 = chip2.read(PORT_B)& 0x01;
+ if (B0 == 0)
+ {
+ chip.write(PORT_A,1); // output 1 at A0 to light up LED
+ } else {
+ chip.write(PORT_A,0); // turn off LED
+ }
+
+ // if yes, out put Port A0 to 1, to light up LED
+ //
+// // 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
}
}
