Axel Utech
/
Nucleo_i2c_pcf8574
Demo for PCF8574
main.cpp@0:daf4d39b8c98, 2014-11-05 (annotated)
- Committer:
- aAXEe
- Date:
- Wed Nov 05 08:01:34 2014 +0000
- Revision:
- 0:daf4d39b8c98
basic handling working
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
aAXEe | 0:daf4d39b8c98 | 1 | #include "mbed.h" |
aAXEe | 0:daf4d39b8c98 | 2 | |
aAXEe | 0:daf4d39b8c98 | 3 | #define PCF8574_ADDR (0x40) |
aAXEe | 0:daf4d39b8c98 | 4 | |
aAXEe | 0:daf4d39b8c98 | 5 | I2C i2c(I2C_SDA, I2C_SCL); |
aAXEe | 0:daf4d39b8c98 | 6 | |
aAXEe | 0:daf4d39b8c98 | 7 | |
aAXEe | 0:daf4d39b8c98 | 8 | Serial pc(SERIAL_TX, SERIAL_RX); |
aAXEe | 0:daf4d39b8c98 | 9 | |
aAXEe | 0:daf4d39b8c98 | 10 | |
aAXEe | 0:daf4d39b8c98 | 11 | bool pcf8574_write(uint8_t data){ |
aAXEe | 0:daf4d39b8c98 | 12 | return i2c.write(PCF8574_ADDR, (char*) &data, 1, 0) == 0; |
aAXEe | 0:daf4d39b8c98 | 13 | } |
aAXEe | 0:daf4d39b8c98 | 14 | |
aAXEe | 0:daf4d39b8c98 | 15 | bool pcf8574_read(uint8_t* data){ |
aAXEe | 0:daf4d39b8c98 | 16 | return i2c.read(PCF8574_ADDR, (char*) data, 1, 0) == 0; |
aAXEe | 0:daf4d39b8c98 | 17 | } |
aAXEe | 0:daf4d39b8c98 | 18 | |
aAXEe | 0:daf4d39b8c98 | 19 | int pcf8574_test(uint8_t value){ |
aAXEe | 0:daf4d39b8c98 | 20 | int ret; |
aAXEe | 0:daf4d39b8c98 | 21 | uint8_t data=0; |
aAXEe | 0:daf4d39b8c98 | 22 | |
aAXEe | 0:daf4d39b8c98 | 23 | ret = pcf8574_write(value); |
aAXEe | 0:daf4d39b8c98 | 24 | if(!ret) return -1; |
aAXEe | 0:daf4d39b8c98 | 25 | |
aAXEe | 0:daf4d39b8c98 | 26 | ret = pcf8574_read(&data); |
aAXEe | 0:daf4d39b8c98 | 27 | if(!ret) return -2; |
aAXEe | 0:daf4d39b8c98 | 28 | |
aAXEe | 0:daf4d39b8c98 | 29 | return data; |
aAXEe | 0:daf4d39b8c98 | 30 | } |
aAXEe | 0:daf4d39b8c98 | 31 | |
aAXEe | 0:daf4d39b8c98 | 32 | int main() |
aAXEe | 0:daf4d39b8c98 | 33 | { |
aAXEe | 0:daf4d39b8c98 | 34 | |
aAXEe | 0:daf4d39b8c98 | 35 | printf("Hello PCF8574\n"); |
aAXEe | 0:daf4d39b8c98 | 36 | while (1) { |
aAXEe | 0:daf4d39b8c98 | 37 | pc.printf("pcf8574_test: low: %i\n", pcf8574_test(0x00)); |
aAXEe | 0:daf4d39b8c98 | 38 | wait(0.5); |
aAXEe | 0:daf4d39b8c98 | 39 | pc.printf("pcf8574_test: high: %i\n", pcf8574_test(0xff)); |
aAXEe | 0:daf4d39b8c98 | 40 | wait(0.5); |
aAXEe | 0:daf4d39b8c98 | 41 | } |
aAXEe | 0:daf4d39b8c98 | 42 | |
aAXEe | 0:daf4d39b8c98 | 43 | } |
aAXEe | 0:daf4d39b8c98 | 44 | |
aAXEe | 0:daf4d39b8c98 | 45 |