Library with PCF8591 support for the experiments for LPC812 MAX
Dependents: lpc812_exp_solution_analog-in lpc812_exp_solution_7-segment lpc812_exp_solution_7-segment-shift lpc812_exp_solution_pwm ... more
Fork of lpc812_exp_lib_PCF8591 by
PCF8591.cpp@0:4ee0a6513a17, 2013-11-14 (annotated)
- Committer:
- embeddedartists
- Date:
- Thu Nov 14 12:19:26 2013 +0000
- Revision:
- 0:4ee0a6513a17
- Child:
- 1:7eabc2242b8f
First version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
embeddedartists | 0:4ee0a6513a17 | 1 | #include "mbed.h" |
embeddedartists | 0:4ee0a6513a17 | 2 | #include "PCF8591.h" |
embeddedartists | 0:4ee0a6513a17 | 3 | |
embeddedartists | 0:4ee0a6513a17 | 4 | PCF8591::PCF8591(PinName sda, PinName scl, int i2cAddr) : m_i2c(sda, scl) |
embeddedartists | 0:4ee0a6513a17 | 5 | { |
embeddedartists | 0:4ee0a6513a17 | 6 | m_addr = i2cAddr; |
embeddedartists | 0:4ee0a6513a17 | 7 | } |
embeddedartists | 0:4ee0a6513a17 | 8 | |
embeddedartists | 0:4ee0a6513a17 | 9 | int PCF8591::read(AnalogIn port) |
embeddedartists | 0:4ee0a6513a17 | 10 | { |
embeddedartists | 0:4ee0a6513a17 | 11 | char cmd = port & 0x3; // read from selcted port |
embeddedartists | 0:4ee0a6513a17 | 12 | char val[2] = { 0, 0 }; |
embeddedartists | 0:4ee0a6513a17 | 13 | |
embeddedartists | 0:4ee0a6513a17 | 14 | // select the port |
embeddedartists | 0:4ee0a6513a17 | 15 | m_i2c.write(m_addr, &cmd, 1, true); |
embeddedartists | 0:4ee0a6513a17 | 16 | |
embeddedartists | 0:4ee0a6513a17 | 17 | // get the sample |
embeddedartists | 0:4ee0a6513a17 | 18 | if (m_i2c.read(m_addr, val, 2) == 0) { |
embeddedartists | 0:4ee0a6513a17 | 19 | // val[0] is conversion result code from last time (always 0x80) |
embeddedartists | 0:4ee0a6513a17 | 20 | // val[1] is the new value |
embeddedartists | 0:4ee0a6513a17 | 21 | return val[1]; |
embeddedartists | 0:4ee0a6513a17 | 22 | } |
embeddedartists | 0:4ee0a6513a17 | 23 | return -1; |
embeddedartists | 0:4ee0a6513a17 | 24 | } |
embeddedartists | 0:4ee0a6513a17 | 25 |