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
PCF8591.h
00001 /* PCF8591 - I2C 8 bit, 4 channel A/D and 8 bit, 1 channel D/A converter 00002 * Copyright (c) 2013 Wim Huiskamp 00003 * 00004 * Released under the MIT License: http://mbed.org/license/mit 00005 * 00006 * version 0.2 Initial Release 00007 */ 00008 #ifndef _PCF8591_H 00009 #define _PCF8591_H 00010 00011 /** Driver for PCF8591 I2C I2C A/D and D/A converter 00012 * 00013 * @code 00014 * #include "mbed.h" 00015 * #include "PCF8591.h" 00016 * 00017 * // I2C Communication 00018 * I2C i2c_adcdac(p28,p27); // SDA, SCL for LPC1768 00019 * //I2C i2c_adcdac(P0_10,P0_11); // SDA, SCL for LPC812 00020 * 00021 * //Declare a composite ADC and DAC device that may be used through public methods 00022 * PCF8591 adc_dac(&i2c_adcdac); // I2C bus, Default PCF8591 Slaveaddress 00023 * 00024 * //Declare independent ADC and DAC objects that may be used similar to mbed AnalogIn and AnalogOut pins 00025 * //PCF8591_AnalogOut anaOut(&i2c_bus); 00026 * // 00027 * //PCF8591_AnalogIn anaIn(&i2c_bus, PCF8591_ADC0); 00028 * 00029 * 00030 * int main() { 00031 * uint8_t count = 0; 00032 * uint8_t analog; 00033 * 00034 * while(1) { 00035 * wait(0.2); 00036 * count++; 00037 * 00038 * // Composite device methods 00039 * adc_dac.write(count); // write D/A value 00040 * analog = adc_dac.read(PCF8591_ADC0); // read A/D value for Channel 0 00041 * 00042 * // mbed pin type methods 00043 * //anaOut = (float)count / 255.0; // write D/A value using range 0.0f .. 1.0f 00044 * //analog = anaIn * 33.0; // read A/D value for Channel 0 in (Volts*10) 00045 * } 00046 * 00047 * } 00048 * @endcode 00049 */ 00050 00051 00052 //Address Defines for PCF8591 00053 #define PCF8591_SA0 0x90 00054 #define PCF8591_SA1 0x92 00055 #define PCF8591_SA2 0x94 00056 #define PCF8591_SA3 0x96 00057 #define PCF8591_SA4 0x98 00058 #define PCF8591_SA5 0x9A 00059 #define PCF8591_SA6 0x9C 00060 #define PCF8591_SA7 0x9E 00061 00062 //Register Defines for PCF8591 00063 #define PCF8591_CTRL 0x00 00064 #define PCF8591_ADR0 0x01 00065 #define PCF8591_ADR1 0x02 00066 #define PCF8591_ADR2 0x03 00067 #define PCF8591_ADR3 0x04 00068 #define PCF8591_DAR 0x01 00069 00070 //Control Register Defines for PCF8591 00071 //AD Channels 00072 #define PCF8591_ADC0 0x00 00073 #define PCF8591_ADC1 0x01 00074 #define PCF8591_ADC2 0x02 00075 #define PCF8591_ADC3 0x03 00076 //AD Channel Mask 00077 #define PCF8591_CHMSK 0x03 00078 00079 //Auto Increment flag (0=disable, 1=enable) 00080 #define PCF8591_AIF 0x04 00081 00082 //AD Channels Input modes 00083 //4 Single Channels (Chan0=AIN0, Chan1=AIN0, Chan2=AIN0, Chan3=AIN0) 00084 #define PCF8591_4S 0x00 00085 //3 Diff Channels (Chan0=AIN0-AIN3, Chan1=AIN1-AIN3, Chan2=AIN2-AIN3) 00086 #define PCF8591_3D 0x10 00087 //2 Single Channels (Chan0=AIN0, Chan1=AIN1) 00088 //1 Diff Channel (Chan2=AIN2-AIN3) 00089 #define PCF8591_2S_1D 0x20 00090 //2 Diff Channels (Chan0=AIN0-AIN1, Chan1=AIN2-AIN3) 00091 #define PCF8591_2D 0x30 00092 //ChannelMode Mask 00093 #define PCF8591_CHMDMSK 0x30 00094 00095 //Analog Output enable flag (0=disable, 1=enable) 00096 #define PCF8591_AOUT 0x40 00097 00098 //Default Mode: ADC0, AutoIncr Off, 4 Single Chan, AnalogOut On 00099 #define PCF8591_CTRL_DEF (PCF8591_ADC0 | PCF8591_4S | PCF8591_AOUT) 00100 00101 00102 /** Create a PCF8591 object connected to the specified I2C bus and deviceAddress 00103 * 00104 */ 00105 class PCF8591 { 00106 public: 00107 /** Create a PCF8591 AD and DA object using a specified I2C bus and slaveaddress 00108 * 00109 * @param I2C &i2c the I2C port to connect to 00110 * @param char deviceAddress the address of the PCF8591 00111 */ 00112 PCF8591(I2C *i2c, uint8_t deviceAddress = PCF8591_SA0); 00113 00114 #if(0) 00115 //These methods are disabled since they interfere with normal expected behaviour for mbed type Analog I/O 00116 // 00117 /** Set ADC mode 00118 * 00119 * @param adc_mode ADC Channel mode, valid Range (PCF8591_4S, PCF8591_3D, PCF8591_2S_1D, PCF8591_2D) 00120 */ 00121 void setADCMode(uint8_t mode); 00122 00123 /** Set DAC mode 00124 * 00125 * @param dac_mode DAC Channel mode, valid Range (false=disable, true=enable) 00126 */ 00127 void setDACMode(bool mode); 00128 #endif 00129 00130 /** Write Analog Output 00131 * 00132 * @param analogOut output value (0..255) 00133 */ 00134 void write(uint8_t analogOut); 00135 00136 /** Read Analog Channel 00137 * 00138 * @param Channel ADC channel, valid range PCF8591_ADC0, PCF8591_ADC1, PCF8591_ADC2 or PCF8591_ADC3 00139 * @return value uint8_t AD converted value (0..255, representing 0V..Vcc) 00140 */ 00141 uint8_t read(uint8_t channel); 00142 00143 00144 00145 protected: 00146 I2C *_i2c; //I2C bus reference 00147 uint8_t _slaveAddress; //I2C Slave address of device 00148 uint8_t _mode; //Device mode 00149 00150 /** Initialise AD and DA driver 00151 * 00152 */ 00153 void _init(); 00154 }; 00155 00156 00157 /** Create a PCF8591 AnalogOut object connected to the specified I2C bus and deviceAddress 00158 * 00159 */ 00160 class PCF8591_AnalogOut { 00161 public: 00162 /** Create a PCF8591 Analogout object using a specified I2C bus and slaveaddress 00163 * 00164 * @param I2C &i2c the I2C port to connect to 00165 * @param char deviceAddress the address of the PCF8591 00166 */ 00167 PCF8591_AnalogOut(I2C *i2c, uint8_t deviceAddress = PCF8591_SA0); 00168 00169 void write(float value); 00170 #ifdef MBED_OPERATORS 00171 /** An operator shorthand for write() 00172 */ 00173 PCF8591_AnalogOut& operator= (float value); 00174 #endif 00175 00176 protected: 00177 I2C *_i2c; //I2C bus reference 00178 uint8_t _slaveAddress; //I2C Slave address of device 00179 uint8_t _mode; //Device mode 00180 00181 /** Initialise DAC driver 00182 * 00183 */ 00184 void _init(); 00185 }; 00186 00187 00188 /** Create a PCF8591 AnalogIn object connected to the specified I2C bus and deviceAddress 00189 * 00190 */ 00191 class PCF8591_AnalogIn { 00192 public: 00193 /** Create a PCF8591 AnalogIn object using a specified I2C bus and slaveaddress 00194 * 00195 * @param I2C &i2c the I2C port to connect to 00196 * @param channel ADC channel of the PCF8591 00197 * @param char deviceAddress the address of the PCF8591 00198 */ 00199 PCF8591_AnalogIn(I2C *i2c, uint8_t channel, uint8_t deviceAddress = PCF8591_SA0); 00200 00201 float read(); 00202 #ifdef MBED_OPERATORS 00203 /** An operator shorthand for read() 00204 */ 00205 operator float(); 00206 #endif 00207 00208 protected: 00209 I2C *_i2c; //I2C bus reference 00210 uint8_t _slaveAddress; //I2C Slave address of device 00211 uint8_t _mode; //Device mode 00212 uint8_t _channel; //Channel 00213 00214 /** Initialise DAC driver 00215 * 00216 */ 00217 void _init(); 00218 }; 00219 00220 00221 #endif
Generated on Tue Jul 12 2022 17:26:42 by 1.7.2