Romilly Cocking
/
MCP23S17Test
Library test v 0.4
main.cpp@1:d7fe22a24841, 2010-08-21 (annotated)
- Committer:
- romilly
- Date:
- Sat Aug 21 16:51:33 2010 +0000
- Revision:
- 1:d7fe22a24841
- Parent:
- 0:d58b942de71e
- Child:
- 2:2e8fca65efaf
Extended and corrected tests
Who changed what in which revision?
User | Revision | Line number | New 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 | 1:d7fe22a24841 | 13 | DigitalInOut chipA0(p12); |
romilly | 1:d7fe22a24841 | 14 | DigitalInOut 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 | 1:d7fe22a24841 | 18 | printf("%s **TEST FAILED** - expected %i but get %i\r\n", 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 | 1:d7fe22a24841 | 24 | chipA0.output(); // output from the mbed |
romilly | 1:d7fe22a24841 | 25 | chipB0.input(); // input to the mbed |
romilly | 0:d58b942de71e | 26 | for (int i = 0; i < 100; i++) { |
romilly | 0:d58b942de71e | 27 | chip.directionA(0xFF); // all 8 bits set to input |
romilly | 0:d58b942de71e | 28 | chip.directionB(0xFE); // bit 0 set to output |
romilly | 0:d58b942de71e | 29 | chipA0 = 1; |
romilly | 0:d58b942de71e | 30 | // copy input bit 0 from A to output bit 0 on B |
romilly | 0:d58b942de71e | 31 | chip.outputB(chip.inputA() && 1); |
romilly | 0:d58b942de71e | 32 | checkEqual(1, int(chipB0),"copying 1 from A0 to B0"); |
romilly | 0:d58b942de71e | 33 | // copy input bit 0 from A to output bit 0 on B |
romilly | 1:d7fe22a24841 | 34 | chipA0 = 0; |
romilly | 0:d58b942de71e | 35 | chip.outputB(chip.inputA() && 1); |
romilly | 1:d7fe22a24841 | 36 | checkEqual(0, int(chipB0), "copying 0 from A0 to B0"); |
romilly | 0:d58b942de71e | 37 | } |
romilly | 1:d7fe22a24841 | 38 | |
romilly | 1:d7fe22a24841 | 39 | chipB0.output(); // output from the mbed |
romilly | 1:d7fe22a24841 | 40 | chipA0.input(); // input to the mbed |
romilly | 1:d7fe22a24841 | 41 | for (int i = 0; i < 100; i++) { |
romilly | 1:d7fe22a24841 | 42 | chip.directionB(0xFF); // all 8 bits set to input |
romilly | 1:d7fe22a24841 | 43 | chip.directionA(0xFE); // bit 0 set to output |
romilly | 1:d7fe22a24841 | 44 | chipB0 = 1; |
romilly | 1:d7fe22a24841 | 45 | // copy input bit 0 from B to output bit 0 on A |
romilly | 1:d7fe22a24841 | 46 | chip.outputA(chip.inputB() && 1); |
romilly | 1:d7fe22a24841 | 47 | checkEqual(1, int(chipB0),"copying 1 from B0 to A0"); |
romilly | 1:d7fe22a24841 | 48 | // copy input bit 0 from B to output bit 0 on A |
romilly | 1:d7fe22a24841 | 49 | chipB0 = 0; |
romilly | 1:d7fe22a24841 | 50 | chip.outputA(chip.inputB() && 1); |
romilly | 1:d7fe22a24841 | 51 | checkEqual(0, int(chipB0), "copying 0 from B0 to A0"); |
romilly | 1:d7fe22a24841 | 52 | } |
romilly | 1:d7fe22a24841 | 53 | |
romilly | 0:d58b942de71e | 54 | } |