EmbeddedArtists AB
/
lpc812_exp_solution_i2c
Solutions for the I2C experiments for LPC812 MAX
main.cpp@0:9aab4157fdae, 2013-11-24 (annotated)
- Committer:
- embeddedartists
- Date:
- Sun Nov 24 12:34:47 2013 +0000
- Revision:
- 0:9aab4157fdae
First version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
embeddedartists | 0:9aab4157fdae | 1 | #include "mbed.h" |
embeddedartists | 0:9aab4157fdae | 2 | #include "PCA9672.h" |
embeddedartists | 0:9aab4157fdae | 3 | |
embeddedartists | 0:9aab4157fdae | 4 | PCA9672 ioxp(P0_10, P0_11); //I2C connected to PCA9672 in LPC800-MAX |
embeddedartists | 0:9aab4157fdae | 5 | |
embeddedartists | 0:9aab4157fdae | 6 | Serial pc(USBTX, USBRX); // tx, rx |
embeddedartists | 0:9aab4157fdae | 7 | |
embeddedartists | 0:9aab4157fdae | 8 | static void experiment1_alt1() |
embeddedartists | 0:9aab4157fdae | 9 | { |
embeddedartists | 0:9aab4157fdae | 10 | ioxp.frequency(100000); |
embeddedartists | 0:9aab4157fdae | 11 | ioxp.direction(0x80); // mark the push-button pin as input and the rest as outputs |
embeddedartists | 0:9aab4157fdae | 12 | |
embeddedartists | 0:9aab4157fdae | 13 | while (1) { |
embeddedartists | 0:9aab4157fdae | 14 | uint8_t val = ioxp.read(); |
embeddedartists | 0:9aab4157fdae | 15 | pc.printf("Reading 0x%02x - bit 7 is %d\n", val, val>>7); |
embeddedartists | 0:9aab4157fdae | 16 | if (val & 0x80) |
embeddedartists | 0:9aab4157fdae | 17 | { |
embeddedartists | 0:9aab4157fdae | 18 | ioxp.write(0x10); |
embeddedartists | 0:9aab4157fdae | 19 | } |
embeddedartists | 0:9aab4157fdae | 20 | else |
embeddedartists | 0:9aab4157fdae | 21 | { |
embeddedartists | 0:9aab4157fdae | 22 | ioxp.write(0x20); |
embeddedartists | 0:9aab4157fdae | 23 | } |
embeddedartists | 0:9aab4157fdae | 24 | wait(.50); |
embeddedartists | 0:9aab4157fdae | 25 | } |
embeddedartists | 0:9aab4157fdae | 26 | } |
embeddedartists | 0:9aab4157fdae | 27 | |
embeddedartists | 0:9aab4157fdae | 28 | static void experiment1_alt2() |
embeddedartists | 0:9aab4157fdae | 29 | { |
embeddedartists | 0:9aab4157fdae | 30 | ioxp.frequency(100000); |
embeddedartists | 0:9aab4157fdae | 31 | ioxp.direction(0x80 | 0x20); // mark the push-button pins (on breadboard and onboard) as inputs |
embeddedartists | 0:9aab4157fdae | 32 | |
embeddedartists | 0:9aab4157fdae | 33 | while (1) { |
embeddedartists | 0:9aab4157fdae | 34 | uint8_t val = ioxp.read(); |
embeddedartists | 0:9aab4157fdae | 35 | pc.printf("Reading 0x%02x - bit 7 is %d, bit 5 is %d\n", val, (val>>7)&1, (val>>5)&1); |
embeddedartists | 0:9aab4157fdae | 36 | if (((val & 0xa0)==0xa0) || ((val & 0xa0)==0x00)) |
embeddedartists | 0:9aab4157fdae | 37 | { |
embeddedartists | 0:9aab4157fdae | 38 | // both or neither buttons are pressed |
embeddedartists | 0:9aab4157fdae | 39 | ioxp.write(0x10); |
embeddedartists | 0:9aab4157fdae | 40 | } |
embeddedartists | 0:9aab4157fdae | 41 | else |
embeddedartists | 0:9aab4157fdae | 42 | { |
embeddedartists | 0:9aab4157fdae | 43 | // exactly one of the buttons are pressed |
embeddedartists | 0:9aab4157fdae | 44 | ioxp.write(0x00); |
embeddedartists | 0:9aab4157fdae | 45 | } |
embeddedartists | 0:9aab4157fdae | 46 | wait(.50); |
embeddedartists | 0:9aab4157fdae | 47 | } |
embeddedartists | 0:9aab4157fdae | 48 | } |
embeddedartists | 0:9aab4157fdae | 49 | |
embeddedartists | 0:9aab4157fdae | 50 | int main() |
embeddedartists | 0:9aab4157fdae | 51 | { |
embeddedartists | 0:9aab4157fdae | 52 | //experiment1_alt1(); //onboard button controls yellow (pressed) and green (released) LEDs |
embeddedartists | 0:9aab4157fdae | 53 | experiment1_alt2(); //two buttons. Yellow LED lit with exactly one is pressed |
embeddedartists | 0:9aab4157fdae | 54 | } |