Romilly Cocking / Mbed 2 deprecated MCP23S17Test

Dependencies:   mbed MCP23S17

main.cpp

Committer:
romilly
Date:
2010-08-21
Revision:
0:d58b942de71e
Child:
1:d7fe22a24841

File content as of revision 0:d58b942de71e:

/* 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");
    }
}