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

PCF8591.cpp

Committer:
embeddedartists
Date:
2013-11-14
Revision:
1:7eabc2242b8f
Parent:
0:4ee0a6513a17
Child:
2:8371322cbdc2

File content as of revision 1:7eabc2242b8f:

#include "mbed.h"
#include "PCF8591.h"

PCF8591::PCF8591(PinName sda, PinName scl, int i2cAddr) : m_i2c(sda, scl)
{
    m_i2c.frequency(400000);
    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;
}