Library test v 0.4

Dependencies:   mbed MCP23S17

main.cpp

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

File content as of revision 1:d7fe22a24841:

/* 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);
DigitalInOut chipA0(p12);
DigitalInOut  chipB0(p10);

void checkEqual(int expected, int actual, char * text) {
    if (expected != actual) {
        printf("%s **TEST FAILED** - expected %i but get %i\r\n", text, expected, actual);
        exit(-1);
    }
}

int main() {
    chipA0.output(); // output from the mbed
    chipB0.input();   // input to the mbed
    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
        chipA0 = 0;
        chip.outputB(chip.inputA() && 1);
        checkEqual(0, int(chipB0), "copying 0 from A0 to B0");
    }
    
    chipB0.output(); // output from the mbed
    chipA0.input();   // input to the mbed
     for (int i = 0; i < 100; i++) {
        chip.directionB(0xFF); // all 8 bits set to input
        chip.directionA(0xFE); // bit 0 set to output
        chipB0 = 1;
        // copy input bit 0 from B to output bit 0 on A
        chip.outputA(chip.inputB() && 1);
        checkEqual(1, int(chipB0),"copying 1 from B0 to A0");
        // copy input bit 0 from B to output bit 0 on A
        chipB0 = 0;
        chip.outputA(chip.inputB() && 1);
        checkEqual(0, int(chipB0), "copying 0 from B0 to A0");
    }
    
}