Library for NXP (Philips) PCF8591 I2C 4 Channel, 8bit Analog to Digital converter and a 1 Channel, 8bit Digital to Analog converter.

Dependents:   812_hello PCF8591_test_LAAS

Committer:
wim
Date:
Sat Sep 21 19:25:09 2013 +0000
Revision:
0:1116b0d151fc
Extended functionality to include mbed type AnalogIn and AnalogOut; Fixed issue related to LPC812 I2C bug

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wim 0:1116b0d151fc 1 /* PCF8591 - I2C 8 bit, 4 channel A/D and 8 bit, 1 channel D/A converter
wim 0:1116b0d151fc 2 * Copyright (c) 2013 Wim Huiskamp
wim 0:1116b0d151fc 3 *
wim 0:1116b0d151fc 4 * Released under the MIT License: http://mbed.org/license/mit
wim 0:1116b0d151fc 5 *
wim 0:1116b0d151fc 6 * version 0.2 Initial Release
wim 0:1116b0d151fc 7 */
wim 0:1116b0d151fc 8 #ifndef _PCF8591_H
wim 0:1116b0d151fc 9 #define _PCF8591_H
wim 0:1116b0d151fc 10
wim 0:1116b0d151fc 11 /** Driver for PCF8591 I2C I2C A/D and D/A converter
wim 0:1116b0d151fc 12 *
wim 0:1116b0d151fc 13 * @code
wim 0:1116b0d151fc 14 * #include "mbed.h"
wim 0:1116b0d151fc 15 * #include "PCF8591.h"
wim 0:1116b0d151fc 16 *
wim 0:1116b0d151fc 17 * // I2C Communication
wim 0:1116b0d151fc 18 * I2C i2c_adcdac(p28,p27); // SDA, SCL for LPC1768
wim 0:1116b0d151fc 19 * //I2C i2c_adcdac(P0_10,P0_11); // SDA, SCL for LPC812
wim 0:1116b0d151fc 20 *
wim 0:1116b0d151fc 21 * //Declare a composite ADC and DAC device that may be used through public methods
wim 0:1116b0d151fc 22 * PCF8591 adc_dac(&i2c_adcdac); // I2C bus, Default PCF8591 Slaveaddress
wim 0:1116b0d151fc 23 *
wim 0:1116b0d151fc 24 * //Declare independent ADC and DAC objects that may be used similar to mbed AnalogIn and AnalogOut pins
wim 0:1116b0d151fc 25 * //PCF8591_AnalogOut anaOut(&i2c_bus);
wim 0:1116b0d151fc 26 * //
wim 0:1116b0d151fc 27 * //PCF8591_AnalogIn anaIn(&i2c_bus, PCF8591_ADC0);
wim 0:1116b0d151fc 28 *
wim 0:1116b0d151fc 29 *
wim 0:1116b0d151fc 30 * int main() {
wim 0:1116b0d151fc 31 * uint8_t count = 0;
wim 0:1116b0d151fc 32 * uint8_t analog;
wim 0:1116b0d151fc 33 *
wim 0:1116b0d151fc 34 * while(1) {
wim 0:1116b0d151fc 35 * wait(0.2);
wim 0:1116b0d151fc 36 * count++;
wim 0:1116b0d151fc 37 *
wim 0:1116b0d151fc 38 * // Composite device methods
wim 0:1116b0d151fc 39 * adc_dac.write(count); // write D/A value
wim 0:1116b0d151fc 40 * analog = adc_dac.read(PCF8591_ADC0); // read A/D value for Channel 0
wim 0:1116b0d151fc 41 *
wim 0:1116b0d151fc 42 * // mbed pin type methods
wim 0:1116b0d151fc 43 * //anaOut = (float)count / 255.0; // write D/A value using range 0.0f .. 1.0f
wim 0:1116b0d151fc 44 * //analog = anaIn * 33.0; // read A/D value for Channel 0 in (Volts*10)
wim 0:1116b0d151fc 45 * }
wim 0:1116b0d151fc 46 *
wim 0:1116b0d151fc 47 * }
wim 0:1116b0d151fc 48 * @endcode
wim 0:1116b0d151fc 49 */
wim 0:1116b0d151fc 50
wim 0:1116b0d151fc 51
wim 0:1116b0d151fc 52 //Address Defines for PCF8591
wim 0:1116b0d151fc 53 #define PCF8591_SA0 0x90
wim 0:1116b0d151fc 54 #define PCF8591_SA1 0x92
wim 0:1116b0d151fc 55 #define PCF8591_SA2 0x94
wim 0:1116b0d151fc 56 #define PCF8591_SA3 0x96
wim 0:1116b0d151fc 57 #define PCF8591_SA4 0x98
wim 0:1116b0d151fc 58 #define PCF8591_SA5 0x9A
wim 0:1116b0d151fc 59 #define PCF8591_SA6 0x9C
wim 0:1116b0d151fc 60 #define PCF8591_SA7 0x9E
wim 0:1116b0d151fc 61
wim 0:1116b0d151fc 62 //Register Defines for PCF8591
wim 0:1116b0d151fc 63 #define PCF8591_CTRL 0x00
wim 0:1116b0d151fc 64 #define PCF8591_ADR0 0x01
wim 0:1116b0d151fc 65 #define PCF8591_ADR1 0x02
wim 0:1116b0d151fc 66 #define PCF8591_ADR2 0x03
wim 0:1116b0d151fc 67 #define PCF8591_ADR3 0x04
wim 0:1116b0d151fc 68 #define PCF8591_DAR 0x01
wim 0:1116b0d151fc 69
wim 0:1116b0d151fc 70 //Control Register Defines for PCF8591
wim 0:1116b0d151fc 71 //AD Channels
wim 0:1116b0d151fc 72 #define PCF8591_ADC0 0x00
wim 0:1116b0d151fc 73 #define PCF8591_ADC1 0x01
wim 0:1116b0d151fc 74 #define PCF8591_ADC2 0x02
wim 0:1116b0d151fc 75 #define PCF8591_ADC3 0x03
wim 0:1116b0d151fc 76 //AD Channel Mask
wim 0:1116b0d151fc 77 #define PCF8591_CHMSK 0x03
wim 0:1116b0d151fc 78
wim 0:1116b0d151fc 79 //Auto Increment flag (0=disable, 1=enable)
wim 0:1116b0d151fc 80 #define PCF8591_AIF 0x04
wim 0:1116b0d151fc 81
wim 0:1116b0d151fc 82 //AD Channels Input modes
wim 0:1116b0d151fc 83 //4 Single Channels (Chan0=AIN0, Chan1=AIN0, Chan2=AIN0, Chan3=AIN0)
wim 0:1116b0d151fc 84 #define PCF8591_4S 0x00
wim 0:1116b0d151fc 85 //3 Diff Channels (Chan0=AIN0-AIN3, Chan1=AIN1-AIN3, Chan2=AIN2-AIN3)
wim 0:1116b0d151fc 86 #define PCF8591_3D 0x10
wim 0:1116b0d151fc 87 //2 Single Channels (Chan0=AIN0, Chan1=AIN1)
wim 0:1116b0d151fc 88 //1 Diff Channel (Chan2=AIN2-AIN3)
wim 0:1116b0d151fc 89 #define PCF8591_2S_1D 0x20
wim 0:1116b0d151fc 90 //2 Diff Channels (Chan0=AIN0-AIN1, Chan1=AIN2-AIN3)
wim 0:1116b0d151fc 91 #define PCF8591_2D 0x30
wim 0:1116b0d151fc 92 //ChannelMode Mask
wim 0:1116b0d151fc 93 #define PCF8591_CHMDMSK 0x30
wim 0:1116b0d151fc 94
wim 0:1116b0d151fc 95 //Analog Output enable flag (0=disable, 1=enable)
wim 0:1116b0d151fc 96 #define PCF8591_AOUT 0x40
wim 0:1116b0d151fc 97
wim 0:1116b0d151fc 98 //Default Mode: ADC0, AutoIncr Off, 4 Single Chan, AnalogOut On
wim 0:1116b0d151fc 99 #define PCF8591_CTRL_DEF (PCF8591_ADC0 | PCF8591_4S | PCF8591_AOUT)
wim 0:1116b0d151fc 100
wim 0:1116b0d151fc 101
wim 0:1116b0d151fc 102 /** Create a PCF8591 object connected to the specified I2C bus and deviceAddress
wim 0:1116b0d151fc 103 *
wim 0:1116b0d151fc 104 */
wim 0:1116b0d151fc 105 class PCF8591 {
wim 0:1116b0d151fc 106 public:
wim 0:1116b0d151fc 107 /** Create a PCF8591 AD and DA object using a specified I2C bus and slaveaddress
wim 0:1116b0d151fc 108 *
wim 0:1116b0d151fc 109 * @param I2C &i2c the I2C port to connect to
wim 0:1116b0d151fc 110 * @param char deviceAddress the address of the PCF8591
wim 0:1116b0d151fc 111 */
wim 0:1116b0d151fc 112 PCF8591(I2C *i2c, uint8_t deviceAddress = PCF8591_SA0);
wim 0:1116b0d151fc 113
wim 0:1116b0d151fc 114 #if(0)
wim 0:1116b0d151fc 115 //These methods are disabled since they interfere with normal expected behaviour for mbed type Analog I/O
wim 0:1116b0d151fc 116 //
wim 0:1116b0d151fc 117 /** Set ADC mode
wim 0:1116b0d151fc 118 *
wim 0:1116b0d151fc 119 * @param adc_mode ADC Channel mode, valid Range (PCF8591_4S, PCF8591_3D, PCF8591_2S_1D, PCF8591_2D)
wim 0:1116b0d151fc 120 */
wim 0:1116b0d151fc 121 void setADCMode(uint8_t mode);
wim 0:1116b0d151fc 122
wim 0:1116b0d151fc 123 /** Set DAC mode
wim 0:1116b0d151fc 124 *
wim 0:1116b0d151fc 125 * @param dac_mode DAC Channel mode, valid Range (false=disable, true=enable)
wim 0:1116b0d151fc 126 */
wim 0:1116b0d151fc 127 void setDACMode(bool mode);
wim 0:1116b0d151fc 128 #endif
wim 0:1116b0d151fc 129
wim 0:1116b0d151fc 130 /** Write Analog Output
wim 0:1116b0d151fc 131 *
wim 0:1116b0d151fc 132 * @param analogOut output value (0..255)
wim 0:1116b0d151fc 133 */
wim 0:1116b0d151fc 134 void write(uint8_t analogOut);
wim 0:1116b0d151fc 135
wim 0:1116b0d151fc 136 /** Read Analog Channel
wim 0:1116b0d151fc 137 *
wim 0:1116b0d151fc 138 * @param Channel ADC channel, valid range PCF8591_ADC0, PCF8591_ADC1, PCF8591_ADC2 or PCF8591_ADC3
wim 0:1116b0d151fc 139 * @return value uint8_t AD converted value (0..255, representing 0V..Vcc)
wim 0:1116b0d151fc 140 */
wim 0:1116b0d151fc 141 uint8_t read(uint8_t channel);
wim 0:1116b0d151fc 142
wim 0:1116b0d151fc 143
wim 0:1116b0d151fc 144
wim 0:1116b0d151fc 145 protected:
wim 0:1116b0d151fc 146 I2C *_i2c; //I2C bus reference
wim 0:1116b0d151fc 147 uint8_t _slaveAddress; //I2C Slave address of device
wim 0:1116b0d151fc 148 uint8_t _mode; //Device mode
wim 0:1116b0d151fc 149
wim 0:1116b0d151fc 150 /** Initialise AD and DA driver
wim 0:1116b0d151fc 151 *
wim 0:1116b0d151fc 152 */
wim 0:1116b0d151fc 153 void _init();
wim 0:1116b0d151fc 154 };
wim 0:1116b0d151fc 155
wim 0:1116b0d151fc 156
wim 0:1116b0d151fc 157 /** Create a PCF8591 AnalogOut object connected to the specified I2C bus and deviceAddress
wim 0:1116b0d151fc 158 *
wim 0:1116b0d151fc 159 */
wim 0:1116b0d151fc 160 class PCF8591_AnalogOut {
wim 0:1116b0d151fc 161 public:
wim 0:1116b0d151fc 162 /** Create a PCF8591 Analogout object using a specified I2C bus and slaveaddress
wim 0:1116b0d151fc 163 *
wim 0:1116b0d151fc 164 * @param I2C &i2c the I2C port to connect to
wim 0:1116b0d151fc 165 * @param char deviceAddress the address of the PCF8591
wim 0:1116b0d151fc 166 */
wim 0:1116b0d151fc 167 PCF8591_AnalogOut(I2C *i2c, uint8_t deviceAddress = PCF8591_SA0);
wim 0:1116b0d151fc 168
wim 0:1116b0d151fc 169 void write(float value);
wim 0:1116b0d151fc 170 #ifdef MBED_OPERATORS
wim 0:1116b0d151fc 171 /** An operator shorthand for write()
wim 0:1116b0d151fc 172 */
wim 0:1116b0d151fc 173 PCF8591_AnalogOut& operator= (float value);
wim 0:1116b0d151fc 174 #endif
wim 0:1116b0d151fc 175
wim 0:1116b0d151fc 176 protected:
wim 0:1116b0d151fc 177 I2C *_i2c; //I2C bus reference
wim 0:1116b0d151fc 178 uint8_t _slaveAddress; //I2C Slave address of device
wim 0:1116b0d151fc 179 uint8_t _mode; //Device mode
wim 0:1116b0d151fc 180
wim 0:1116b0d151fc 181 /** Initialise DAC driver
wim 0:1116b0d151fc 182 *
wim 0:1116b0d151fc 183 */
wim 0:1116b0d151fc 184 void _init();
wim 0:1116b0d151fc 185 };
wim 0:1116b0d151fc 186
wim 0:1116b0d151fc 187
wim 0:1116b0d151fc 188 /** Create a PCF8591 AnalogIn object connected to the specified I2C bus and deviceAddress
wim 0:1116b0d151fc 189 *
wim 0:1116b0d151fc 190 */
wim 0:1116b0d151fc 191 class PCF8591_AnalogIn {
wim 0:1116b0d151fc 192 public:
wim 0:1116b0d151fc 193 /** Create a PCF8591 AnalogIn object using a specified I2C bus and slaveaddress
wim 0:1116b0d151fc 194 *
wim 0:1116b0d151fc 195 * @param I2C &i2c the I2C port to connect to
wim 0:1116b0d151fc 196 * @param channel ADC channel of the PCF8591
wim 0:1116b0d151fc 197 * @param char deviceAddress the address of the PCF8591
wim 0:1116b0d151fc 198 */
wim 0:1116b0d151fc 199 PCF8591_AnalogIn(I2C *i2c, uint8_t channel, uint8_t deviceAddress = PCF8591_SA0);
wim 0:1116b0d151fc 200
wim 0:1116b0d151fc 201 float read();
wim 0:1116b0d151fc 202 #ifdef MBED_OPERATORS
wim 0:1116b0d151fc 203 /** An operator shorthand for read()
wim 0:1116b0d151fc 204 */
wim 0:1116b0d151fc 205 operator float();
wim 0:1116b0d151fc 206 #endif
wim 0:1116b0d151fc 207
wim 0:1116b0d151fc 208 protected:
wim 0:1116b0d151fc 209 I2C *_i2c; //I2C bus reference
wim 0:1116b0d151fc 210 uint8_t _slaveAddress; //I2C Slave address of device
wim 0:1116b0d151fc 211 uint8_t _mode; //Device mode
wim 0:1116b0d151fc 212 uint8_t _channel; //Channel
wim 0:1116b0d151fc 213
wim 0:1116b0d151fc 214 /** Initialise DAC driver
wim 0:1116b0d151fc 215 *
wim 0:1116b0d151fc 216 */
wim 0:1116b0d151fc 217 void _init();
wim 0:1116b0d151fc 218 };
wim 0:1116b0d151fc 219
wim 0:1116b0d151fc 220
wim 0:1116b0d151fc 221 #endif