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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers PCF8591.cpp Source File

PCF8591.cpp

00001 #include "mbed.h"
00002 #include "PCF8591.h"
00003 
00004 PCF8591::PCF8591(PinName sda, PinName scl, int i2cAddr) : m_i2c(sda, scl)
00005 {
00006     m_i2c.frequency(100000);
00007     m_addr = i2cAddr;
00008 }
00009     
00010 int PCF8591::read(AnalogIn port)
00011 {
00012     char cmd = (port & 0x3); // read from selected port
00013     char data[2];
00014     
00015     // select the port
00016     m_i2c.start();
00017     m_i2c.write(m_addr);
00018     m_i2c.write(cmd);
00019     m_i2c.stop();
00020     
00021     // get the sample
00022     m_i2c.start();
00023     m_i2c.write(m_addr | 1);
00024     data[0] = m_i2c.read(1); // expect ACK
00025     data[1] = m_i2c.read(0); // expect NACK
00026     m_i2c.stop();
00027     
00028     // data[0] is a dummy byte so ignore it
00029     // data[1] is the new value
00030     return data[1];
00031 }
00032