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
Revision 0:4ee0a6513a17, committed 2013-11-14
- Comitter:
- embeddedartists
- Date:
- Thu Nov 14 12:19:26 2013 +0000
- Child:
- 1:7eabc2242b8f
- Commit message:
- First version
Changed in this revision
PCF8591.cpp | Show annotated file Show diff for this revision Revisions of this file |
PCF8591.h | Show annotated file Show diff for this revision Revisions of this file |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/PCF8591.cpp Thu Nov 14 12:19:26 2013 +0000 @@ -0,0 +1,25 @@ +#include "mbed.h" +#include "PCF8591.h" + +PCF8591::PCF8591(PinName sda, PinName scl, int i2cAddr) : m_i2c(sda, scl) +{ + m_addr = i2cAddr; +} + +int PCF8591::read(AnalogIn port) +{ + char cmd = port & 0x3; // read from selcted port + char val[2] = { 0, 0 }; + + // select the port + m_i2c.write(m_addr, &cmd, 1, true); + + // get the sample + if (m_i2c.read(m_addr, val, 2) == 0) { + // val[0] is conversion result code from last time (always 0x80) + // val[1] is the new value + return val[1]; + } + return -1; +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/PCF8591.h Thu Nov 14 12:19:26 2013 +0000 @@ -0,0 +1,65 @@ +#ifndef PCF8591_H +#define PCF8591_H + +/** + * Interface to the PCF8591 chip (http://www.nxp.com/documents/data_sheet/PCF8591.pdf) + * which has four 8-bit analog inputs and one 8-bit analog output. + * + * @code + * #include "mbed.h" + * #include "LcdController.h" + * + * PCF8591 pcf; + * + * int main(void) { + * + * while(1) { + * // read analog value (keep trying until valid) + * int val; + * do { + * val = pcf.read(PCF8591::A0); + * wait(0.01); + * } while(val == -1); + * + * // do something with value... + * } + * } + * @endcode + */ +class PCF8591 { +public: + + enum AnalogIn { + A0, + A1, + A2, + A3 + }; + + + /** Create an interface to the PCF8591 chip + * + * + * @param sda the I2C SDA pin + * @param scl the I2C SCL pin + * @param i2cAddr the upper 7 bits of the chip's address + */ + PCF8591(PinName sda = P0_10, PinName scl = P0_11, int i2cAddr = 0x9E); + + /** Reads one value for the specified analog port + * + * @param port the analog in to read (A0..A3) + * + * @returns + * the value on success (0..255) + * -1 on failure + */ + int read(AnalogIn port); + +private: + I2C m_i2c; + int m_addr; +}; + +#endif +