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 EmbeddedArtists AB

Committer:
embeddedartists
Date:
Thu Nov 14 14:58:49 2013 +0000
Revision:
1:7eabc2242b8f
Parent:
0:4ee0a6513a17
Child:
2:8371322cbdc2
Changed the I2C-bus frequency to 400KHz

Who changed what in which revision?

UserRevisionLine numberNew 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 1:7eabc2242b8f 6 m_i2c.frequency(400000);
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 0:4ee0a6513a17 12 char cmd = port & 0x3; // read from selcted port
embeddedartists 0:4ee0a6513a17 13 char val[2] = { 0, 0 };
embeddedartists 0:4ee0a6513a17 14
embeddedartists 0:4ee0a6513a17 15 // select the port
embeddedartists 0:4ee0a6513a17 16 m_i2c.write(m_addr, &cmd, 1, true);
embeddedartists 0:4ee0a6513a17 17
embeddedartists 0:4ee0a6513a17 18 // get the sample
embeddedartists 0:4ee0a6513a17 19 if (m_i2c.read(m_addr, val, 2) == 0) {
embeddedartists 0:4ee0a6513a17 20 // val[0] is conversion result code from last time (always 0x80)
embeddedartists 0:4ee0a6513a17 21 // val[1] is the new value
embeddedartists 0:4ee0a6513a17 22 return val[1];
embeddedartists 0:4ee0a6513a17 23 }
embeddedartists 0:4ee0a6513a17 24 return -1;
embeddedartists 0:4ee0a6513a17 25 }
embeddedartists 0:4ee0a6513a17 26