Library test v 0.4

Dependencies:   mbed MCP23S17

Committer:
romilly
Date:
Sat Aug 21 14:49:00 2010 +0000
Revision:
0:d58b942de71e
Child:
1:d7fe22a24841

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
romilly 0:d58b942de71e 1 /* Test drive the MCP23S17 library
romilly 0:d58b942de71e 2 * Copyright (c) 2010 Romilly Cocking
romilly 0:d58b942de71e 3 * Released under the MIT License: http://mbed.org/license/mit
romilly 0:d58b942de71e 4 * See http://mbed.org/users/romilly/notebook/mcp23s17-addressable-16-bit-io-expander-with-spi/
romilly 0:d58b942de71e 5 */
romilly 0:d58b942de71e 6 #include "mbed.h"
romilly 0:d58b942de71e 7 #include "MCP23S17.h"
romilly 0:d58b942de71e 8
romilly 0:d58b942de71e 9 SPI spi(p5, p6, p7);
romilly 0:d58b942de71e 10
romilly 0:d58b942de71e 11 char writeOpcode = 0x40; // A0, A1, A2 are tied to ground on the breadboard.
romilly 0:d58b942de71e 12 MCP23S17 chip = MCP23S17(spi, p20, 0x40);
romilly 0:d58b942de71e 13 DigitalOut chipA0(p12);
romilly 0:d58b942de71e 14 DigitalIn chipB0(p10);
romilly 0:d58b942de71e 15
romilly 0:d58b942de71e 16 void checkEqual(int expected, int actual, char * text) {
romilly 0:d58b942de71e 17 if (expected != actual) {
romilly 0:d58b942de71e 18 printf("%s **TEST FAILED** - expected %i but get %i", text, expected, actual);
romilly 0:d58b942de71e 19 exit(-1);
romilly 0:d58b942de71e 20 }
romilly 0:d58b942de71e 21 }
romilly 0:d58b942de71e 22
romilly 0:d58b942de71e 23 int main() {
romilly 0:d58b942de71e 24 for (int i = 0; i < 100; i++) {
romilly 0:d58b942de71e 25 chip.directionA(0xFF); // all 8 bits set to input
romilly 0:d58b942de71e 26 chip.directionB(0xFE); // bit 0 set to output
romilly 0:d58b942de71e 27 chipA0 = 1;
romilly 0:d58b942de71e 28 // copy input bit 0 from A to output bit 0 on B
romilly 0:d58b942de71e 29 chip.outputB(chip.inputA() && 1);
romilly 0:d58b942de71e 30 checkEqual(1, int(chipB0),"copying 1 from A0 to B0");
romilly 0:d58b942de71e 31 // copy input bit 0 from A to output bit 0 on B
romilly 0:d58b942de71e 32 chip.outputB(chip.inputA() && 1);
romilly 0:d58b942de71e 33 checkEqual(1, int(chipB0), "copying 0 from A0 to B0");
romilly 0:d58b942de71e 34 }
romilly 0:d58b942de71e 35 }