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@2:8371322cbdc2, 2013-11-15 (annotated)
- Committer:
- embeddedartists
- Date:
- Fri Nov 15 10:33:06 2013 +0000
- Revision:
- 2:8371322cbdc2
- Parent:
- 1:7eabc2242b8f
Fixed reading bug. Changed speed back to 100kHz
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 | 2:8371322cbdc2 | 6 | m_i2c.frequency(100000); |
embeddedartists | 0:4ee0a6513a17 | 7 | m_addr = i2cAddr; |
embeddedartists | 0:4ee0a6513a17 | 8 | } |
embeddedartists | 0:4ee0a6513a17 | 9 | |
embeddedartists | 0:4ee0a6513a17 | 10 | int PCF8591::read(AnalogIn port) |
embeddedartists | 0:4ee0a6513a17 | 11 | { |
embeddedartists | 2:8371322cbdc2 | 12 | char cmd = (port & 0x3); // read from selected port |
embeddedartists | 2:8371322cbdc2 | 13 | char data[2]; |
embeddedartists | 0:4ee0a6513a17 | 14 | |
embeddedartists | 0:4ee0a6513a17 | 15 | // select the port |
embeddedartists | 2:8371322cbdc2 | 16 | m_i2c.start(); |
embeddedartists | 2:8371322cbdc2 | 17 | m_i2c.write(m_addr); |
embeddedartists | 2:8371322cbdc2 | 18 | m_i2c.write(cmd); |
embeddedartists | 2:8371322cbdc2 | 19 | m_i2c.stop(); |
embeddedartists | 0:4ee0a6513a17 | 20 | |
embeddedartists | 0:4ee0a6513a17 | 21 | // get the sample |
embeddedartists | 2:8371322cbdc2 | 22 | m_i2c.start(); |
embeddedartists | 2:8371322cbdc2 | 23 | m_i2c.write(m_addr | 1); |
embeddedartists | 2:8371322cbdc2 | 24 | data[0] = m_i2c.read(1); // expect ACK |
embeddedartists | 2:8371322cbdc2 | 25 | data[1] = m_i2c.read(0); // expect NACK |
embeddedartists | 2:8371322cbdc2 | 26 | m_i2c.stop(); |
embeddedartists | 2:8371322cbdc2 | 27 | |
embeddedartists | 2:8371322cbdc2 | 28 | // data[0] is a dummy byte so ignore it |
embeddedartists | 2:8371322cbdc2 | 29 | // data[1] is the new value |
embeddedartists | 2:8371322cbdc2 | 30 | return data[1]; |
embeddedartists | 0:4ee0a6513a17 | 31 | } |
embeddedartists | 0:4ee0a6513a17 | 32 |