12-Bit, 8-Channel, ADC System Monitor w/ Temp Sensor, Internal/External Reference, & I2C Interface
Dependents: ADC128D818_HelloWorld
ADC128D818.cpp
00001 /** 00002 * @brief ADC128D818 12-Bit, 8-Channel, ADC System Monitor w/ Temp Sensor, Internal/External Reference, & I2C Interfac 00003 * http://www.ti.com/product/adc128d818/ 00004 * @date 02/09/2013 00005 * @author F.BLANC LAAS-CNRS 00006 * http://homepages.laas.fr/fblanc/ 00007 */ 00008 00009 #include "ADC128D818.h" 00010 00011 /** 00012 * @brief Constructor. 00013 * 00014 * @param sda I2C 00015 * @param scl I2C 00016 * @param adc_int 00017 */ 00018 ADC128D818::ADC128D818(PinName sda, PinName scl, PinName adc_int) : _i2c(sda, scl), _Adc_Int (adc_int) 00019 { 00020 } 00021 00022 /** 00023 * @brief Destructor. 00024 */ 00025 ADC128D818::~ADC128D818() 00026 { 00027 } 00028 00029 /** 00030 * @brief init 00031 * 00032 * @param address I2C (7bits) 00033 ADC_ADDRESS_LOW_LOW 00034 ADC_ADDRESS_LOW_MID 00035 ADC_ADDRESS_LOW_HIGH 00036 ADC_ADDRESS_MID_LOW 00037 ADC_ADDRESS_MID_MID 00038 ADC_ADDRESS_MID_HIGH 00039 ADC_ADDRESS_HIGH_LOW 00040 ADC_ADDRESS_HIGH_MID 00041 ADC_ADDRESS_HIGH_HIGH 00042 * @param mode : 00043 ADC_MODE_0 00044 ADC_MODE_1 00045 ADC_MODE_2 00046 ADC_MODE_3 00047 * @param vref 00048 ADC_VREF_INT 00049 ADC_VREF_EXT 00050 * @param rate 00051 ADC_RATE_LOW_POWER 00052 ADC_RATE_CONTINUOUS 00053 * @param mask_channel 00054 * @param mask_int 00055 * @return error 0 OK, -1 NO DEVICE, -2 ADC is BUSY 00056 * @date 02/09/2013 00057 */ 00058 int ADC128D818::init(char address, char mode, char vref, char rate, char mask_channel, char mask_int) 00059 { 00060 00061 char data; 00062 char cmd_data[2]; 00063 00064 _address=address << 1; //bug ? 00065 00066 //2 test Busy_Status_Register 00067 cmd_data[0]= ADC_REG_Busy_Status_Register; 00068 00069 if(_i2c.write(_address, cmd_data, 1)) 00070 return -1; //NO DEVICE 00071 _i2c.read(_address,&data,1); //read a byte 00072 00073 if ((data & Busy_Status_Register_Not_Ready) == 1) 00074 return -2; //ADC is BUSY 00075 00076 ADC128D818::stop(); 00077 //3 Program the Advanced Configuration Register 00078 data=0; 00079 switch (vref) 00080 { 00081 case ADC_VREF_INT: 00082 data&=~Advanced_Configuration_Register_External_Reference_Enable; //0 00083 break; 00084 case ADC_VREF_EXT: 00085 data|=Advanced_Configuration_Register_External_Reference_Enable; //1 00086 break; 00087 } 00088 switch (mode) 00089 { 00090 case ADC_MODE_0: 00091 data&=~Advanced_Configuration_Register_Mode_Select_0; //0 00092 data&=~Advanced_Configuration_Register_Mode_Select_1; //0 00093 break; 00094 case ADC_MODE_1: 00095 data&=~Advanced_Configuration_Register_Mode_Select_0; //0 00096 data|=Advanced_Configuration_Register_Mode_Select_1; //1 00097 break; 00098 case ADC_MODE_2: 00099 data|=~Advanced_Configuration_Register_Mode_Select_0; //1 00100 data&=Advanced_Configuration_Register_Mode_Select_1; //0 00101 break; 00102 case ADC_MODE_3: 00103 data|=~Advanced_Configuration_Register_Mode_Select_0; //1 00104 data|=Advanced_Configuration_Register_Mode_Select_1; //1 00105 break; 00106 } 00107 00108 cmd_data[0]=ADC_REG_Advanced_Configuration_Register; 00109 cmd_data[1]=data; 00110 00111 _i2c.write(_address, cmd_data, 2); //send a byte & wait acknowledged 00112 00113 //4 Program the Conversion Rate Register 00114 data=0; 00115 switch (rate) 00116 { 00117 case ADC_RATE_LOW_POWER: 00118 data&=~Advanced_Configuration_Register_External_Reference_Enable; //0 00119 break; 00120 case ADC_RATE_CONTINUOUS: 00121 data|=Advanced_Configuration_Register_External_Reference_Enable; //1 00122 break; 00123 } 00124 00125 cmd_data[0]=ADC_REG_Conversion_Rate_Register; 00126 cmd_data[1]=data; 00127 00128 _i2c.write(_address, cmd_data, 2); //send a byte & wait acknowledged 00129 00130 //5 Choose to enable or disable the channels using the Channel Disable Register 00131 00132 cmd_data[0]=ADC_REG_Channel_Disable_Register; 00133 cmd_data[1]=mask_channel; 00134 00135 _i2c.write(_address, cmd_data, 2); //send a byte & wait acknowledged 00136 00137 //6 Using the Interrupt Mask Register 00138 00139 cmd_data[0]=ADC_REG_Interrupt_Mask_Register; 00140 cmd_data[1]=mask_int; 00141 00142 _i2c.write(_address, cmd_data, 2); //send a byte & wait acknowledged 00143 00144 return 0; 00145 } 00146 00147 /** 00148 * @brief init_limit 00149 * 00150 * @param limit 00151 * @param high_low 00152 ADC_LIMIT_HIGH 00153 ADC_LIMIT_LOW 00154 * @return error 0 OK 00155 * @date 02/09/2013 00156 */ 00157 int ADC128D818::init_limit(char channel, char limit, char high_low) 00158 { 00159 char cmd_data[2]; 00160 00161 cmd_data[0]=ADC_REG_Limit_Registers + channel * 2 + high_low; 00162 00163 cmd_data[1]=limit; 00164 00165 00166 _i2c.write(_address, cmd_data, 2); //send a byte & wait acknowledged 00167 00168 return 0; 00169 } 00170 /** 00171 * @brief read_channel 00172 * @param channel 00173 * @return u32_data 00174 * @date 02/09/2013 00175 */ 00176 int ADC128D818::read_channel(char channel) 00177 { 00178 int u32_data=0; 00179 char data[2]; 00180 char *ptr; 00181 char cmd[1] ; 00182 cmd[0] = ADC_REG_Channel_Readings_Registers + channel; 00183 _i2c.write(_address, cmd, 1); //send a byte & wait acknowledged 00184 00185 _i2c.read(_address,data,2); //read a byte 00186 ptr=(char *) & u32_data; 00187 ptr[0]=data[1]; 00188 ptr[1]=data[0]; 00189 return u32_data; 00190 } 00191 00192 /** 00193 * @brief read_register 00194 * @param Register 00195 * @return u8_data 00196 * @date 02/09/2013 00197 */ 00198 00199 char ADC128D818::read_register(char Register) 00200 { 00201 char cmd ; 00202 cmd = Register; 00203 00204 _i2c.write(_address, &cmd, 1); //send a byte 00205 _i2c.read(_address,&cmd,1); //read a byte 00206 00207 return cmd; 00208 } 00209 /** 00210 * @brief start 00211 * @date 02/09/2013 00212 */ 00213 void ADC128D818::start() 00214 { 00215 char cmd_data[2]; 00216 cmd_data[0]= ADC_REG_Configuration_Register; 00217 cmd_data[1]= Configuration_Register_Start | Configuration_Register_INT_Enable ; 00218 00219 _i2c.write(_address, cmd_data, 2); //send a 2 byte 00220 00221 } 00222 /** 00223 * @brief stop 00224 * @date 02/09/2013 00225 */ 00226 void ADC128D818::stop() 00227 { 00228 char cmd_data[2]; 00229 cmd_data[0]= ADC_REG_Configuration_Register; 00230 cmd_data[1]= 0 ; 00231 00232 _i2c.write(_address, cmd_data, 2); //send a byte 00233 00234 }
Generated on Mon Jul 18 2022 15:31:51 by 1.7.2