Library test v 0.4
Diff: main.cpp
- Revision:
- 0:d58b942de71e
- Child:
- 1:d7fe22a24841
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp Sat Aug 21 14:49:00 2010 +0000
@@ -0,0 +1,35 @@
+/* Test drive 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/
+*/
+#include "mbed.h"
+#include "MCP23S17.h"
+
+SPI spi(p5, p6, p7);
+
+char writeOpcode = 0x40; // A0, A1, A2 are tied to ground on the breadboard.
+MCP23S17 chip = MCP23S17(spi, p20, 0x40);
+DigitalOut chipA0(p12);
+DigitalIn chipB0(p10);
+
+void checkEqual(int expected, int actual, char * text) {
+ if (expected != actual) {
+ printf("%s **TEST FAILED** - expected %i but get %i", text, expected, actual);
+ exit(-1);
+ }
+}
+
+int main() {
+ for (int i = 0; i < 100; i++) {
+ chip.directionA(0xFF); // all 8 bits set to input
+ chip.directionB(0xFE); // bit 0 set to output
+ chipA0 = 1;
+ // copy input bit 0 from A to output bit 0 on B
+ chip.outputB(chip.inputA() && 1);
+ checkEqual(1, int(chipB0),"copying 1 from A0 to B0");
+ // copy input bit 0 from A to output bit 0 on B
+ chip.outputB(chip.inputA() && 1);
+ checkEqual(1, int(chipB0), "copying 0 from A0 to B0");
+ }
+}