Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
AD7190.cpp
00001 /***************************************************************************//** 00002 * @file AD7190.c 00003 * @brief Implementation of AD7190 Driver. 00004 * @author DNechita (Dan.Nechita@analog.com) 00005 ******************************************************************************** 00006 * Copyright 2012(c) Analog Devices, Inc. 00007 * 00008 * All rights reserved. 00009 * 00010 * Redistribution and use in source and binary forms, with or without 00011 * modification, are permitted provided that the following conditions are met: 00012 * - Redistributions of source code must retain the above copyright 00013 * notice, this list of conditions and the following disclaimer. 00014 * - Redistributions in binary form must reproduce the above copyright 00015 * notice, this list of conditions and the following disclaimer in 00016 * the documentation and/or other materials provided with the 00017 * distribution. 00018 * - Neither the name of Analog Devices, Inc. nor the names of its 00019 * contributors may be used to endorse or promote products derived 00020 * from this software without specific prior written permission. 00021 * - The use of this software may or may not infringe the patent rights 00022 * of one or more patent holders. This license does not release you 00023 * from the requirement that you obtain separate licenses from these 00024 * patent holders to use this software. 00025 * - Use of the software either in source or binary form, must be run 00026 * on or directly connected to an Analog Devices Inc. component. 00027 * 00028 * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR 00029 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, 00030 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 00031 * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, 00032 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 00033 * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR 00034 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00035 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 00036 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00037 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00038 * 00039 ******************************************************************************** 00040 * SVN Revision: 903 00041 *******************************************************************************/ 00042 00043 /******************************************************************************/ 00044 /***************************** Include Files **********************************/ 00045 /******************************************************************************/ 00046 #include "AD7190.h" // AD7190 definitions. 00047 #include "TIME_AD.h" // TIME definitions. 00048 00049 /***************************************************************************//** 00050 * @brief Writes data into a register. 00051 * 00052 * @param registerAddress - Address of the register. 00053 * @param registerValue - Data value to write. 00054 * @param bytesNumber - Number of bytes to be written. 00055 * 00056 * @return none. 00057 *******************************************************************************/ 00058 void AD7190_SetRegisterValue(unsigned char registerAddress, 00059 unsigned long registerValue, 00060 unsigned char bytesNumber) 00061 { 00062 unsigned char writeCommand[5] = {0, 0, 0, 0, 0}; 00063 unsigned char* dataPointer = (unsigned char*)®isterValue; 00064 unsigned char bytesNr = bytesNumber; 00065 00066 writeCommand[0] = AD7190_COMM_WRITE | 00067 AD7190_COMM_ADDR(registerAddress); 00068 while(bytesNr > 0) 00069 { 00070 writeCommand[bytesNr] = *dataPointer; 00071 dataPointer ++; 00072 bytesNr --; 00073 } 00074 SPI_Write(writeCommand, bytesNumber + 1); 00075 } 00076 00077 /***************************************************************************//** 00078 * @brief Reads the value of a register. 00079 * 00080 * @param registerAddress - Address of the register. 00081 * @param bytesNumber - Number of bytes that will be read. 00082 * 00083 * @return buffer - Value of the register. 00084 *******************************************************************************/ 00085 unsigned long AD7190_GetRegisterValue(unsigned char registerAddress, 00086 unsigned char bytesNumber) 00087 { 00088 cs.write(0); 00089 unsigned char registerWord[5] = {0, 0, 0, 0, 0}; 00090 unsigned long buffer = 0x0; 00091 unsigned char i = 0; 00092 00093 registerWord[0] = AD7190_COMM_READ | 00094 AD7190_COMM_ADDR(registerAddress); 00095 SPI_Read(registerWord, bytesNumber + 1); 00096 for(i = 1; i < bytesNumber + 1; i++) 00097 { 00098 buffer = (buffer << 8) + registerWord[i]; 00099 } 00100 //cs.write(1); 00101 return buffer; 00102 } 00103 00104 /***************************************************************************//** 00105 * @brief Checks if the AD7190 part is present. 00106 * 00107 * @return status - Indicates if the part is present or not. 1 for ok , 0 for error 00108 *******************************************************************************/ 00109 unsigned char AD7190_Init(unsigned int speed) 00110 { 00111 unsigned char status = 1; 00112 unsigned char regVal = 0; 00113 00114 00115 SPI_Init(0, (unsigned long) 1000*speed, 1, 0); 00116 00117 AD7190_Reset(); 00118 /* Allow at least 500 us before accessing any of the on-chip registers. */ 00119 TIME_DelayMs(1); 00120 regVal = AD7190_GetRegisterValue(AD7190_REG_ID, 1); 00121 if( (regVal & AD7190_ID_MASK) != ID_AD7190) 00122 { 00123 status = 0; 00124 } 00125 return status ; 00126 } 00127 00128 /***************************************************************************//** 00129 * @brief Resets the device. 00130 * 00131 * @return none. 00132 *******************************************************************************/ 00133 void AD7190_Reset(void) 00134 { 00135 unsigned char registerWord[7]; 00136 00137 registerWord[0] = 0x01; 00138 registerWord[1] = 0xFF; 00139 registerWord[2] = 0xFF; 00140 registerWord[3] = 0xFF; 00141 registerWord[4] = 0xFF; 00142 registerWord[5] = 0xFF; 00143 registerWord[6] = 0xFF; 00144 cs.write(1); 00145 SPI_Write(registerWord, 7); 00146 cs.write(0); 00147 AD7190_WaitRdyGoLow(); 00148 } 00149 00150 /***************************************************************************//** 00151 * @brief Set device to idle or power-down. 00152 * 00153 * @param pwrMode - Selects idle mode or power-down mode. 00154 * Example: 0 - power-down 00155 * 1 - idle 00156 * 00157 * @return none. 00158 *******************************************************************************/ 00159 void AD7190_SetPower(unsigned char pwrMode) 00160 { 00161 unsigned long oldPwrMode = 0x0; 00162 unsigned long newPwrMode = 0x0; 00163 00164 oldPwrMode = AD7190_GetRegisterValue(AD7190_REG_MODE, 3); 00165 oldPwrMode &= ~(AD7190_MODE_SEL(0x7)); 00166 newPwrMode = oldPwrMode | 00167 AD7190_MODE_SEL((pwrMode * (AD7190_MODE_IDLE)) | 00168 (!pwrMode * (AD7190_MODE_PWRDN))); 00169 AD7190_SetRegisterValue(AD7190_REG_MODE, newPwrMode, 3); 00170 } 00171 00172 /***************************************************************************//** 00173 * @brief Waits for RDY pin to go low. 00174 * @comment timeOutCnt related to CPU clock 00175 * @return none. 00176 *******************************************************************************/ 00177 void AD7190_WaitRdyGoLow(void) 00178 { 00179 unsigned long timeOutCnt = 0x4FFFF; 00180 00181 while(ad_rdy.read()==1 && timeOutCnt--); 00182 //wait_us(500); 00183 } 00184 00185 /***************************************************************************//** 00186 * @brief Selects the channel to be enabled. 00187 * 00188 * @param channel - Selects a channel. 00189 * 00190 * @return none. 00191 *******************************************************************************/ 00192 void AD7190_ChannelSelect(unsigned short channel) 00193 { 00194 unsigned long oldRegValue = 0x0; 00195 unsigned long newRegValue = 0x0; 00196 00197 oldRegValue = AD7190_GetRegisterValue(AD7190_REG_CONF, 3); 00198 oldRegValue &= ~(AD7190_CONF_CHAN(0xFF)); 00199 newRegValue = oldRegValue | AD7190_CONF_CHAN(1 << channel); 00200 AD7190_SetRegisterValue(AD7190_REG_CONF, newRegValue, 3); 00201 } 00202 00203 /***************************************************************************//** 00204 * @brief Performs the given calibration to the specified channel. 00205 * 00206 * @param mode - Calibration type. 00207 * @param channel - Channel to be calibrated. 00208 * 00209 * @return none. 00210 *******************************************************************************/ 00211 void AD7190_Calibrate(unsigned char mode, unsigned char channel) 00212 { 00213 unsigned long oldRegValue = 0x0; 00214 unsigned long newRegValue = 0x0; 00215 00216 AD7190_ChannelSelect(channel); 00217 oldRegValue = AD7190_GetRegisterValue(AD7190_REG_MODE, 3); 00218 oldRegValue &= ~AD7190_MODE_SEL(0x7); 00219 newRegValue = oldRegValue | AD7190_MODE_SEL(mode); 00220 cs.write(0); //ADI_PART_CS_LOW; 00221 AD7190_SetRegisterValue(AD7190_REG_MODE, newRegValue, 3); // CS is not modified. 00222 AD7190_WaitRdyGoLow(); 00223 cs.write(1); //ADI_PART_CS_HIGH; 00224 } 00225 00226 /***************************************************************************//** 00227 * @brief Selects the polarity of the conversion and the ADC input range. 00228 * 00229 * @param polarity - Polarity select bit. 00230 Example: 0 - bipolar operation is selected. 00231 1 - unipolar operation is selected. 00232 * @param range - Gain select bits. These bits are written by the user to select 00233 the ADC input range. 00234 * 00235 * @return none. 00236 *******************************************************************************/ 00237 void AD7190_RangeSetup(unsigned char polarity, unsigned char range) 00238 { 00239 unsigned long oldRegValue = 0x0; 00240 unsigned long newRegValue = 0x0; 00241 00242 oldRegValue = AD7190_GetRegisterValue(AD7190_REG_CONF,3); 00243 oldRegValue &= ~(AD7190_CONF_UNIPOLAR | 00244 AD7190_CONF_GAIN(0x7)); 00245 newRegValue = oldRegValue | 00246 (polarity * AD7190_CONF_UNIPOLAR) | 00247 AD7190_CONF_GAIN(range); 00248 AD7190_SetRegisterValue(AD7190_REG_CONF, newRegValue, 3); 00249 } 00250 00251 /***************************************************************************//** 00252 * @brief Returns the result of a single conversion. 00253 * 00254 * @return regData - Result of a single analog-to-digital conversion. 00255 *******************************************************************************/ 00256 unsigned long AD7190_SingleConversion(void) 00257 { 00258 unsigned long command = 0x0; 00259 unsigned long regData = 0x0; 00260 00261 command = AD7190_MODE_SEL(AD7190_MODE_SINGLE) | 00262 AD7190_MODE_CLKSRC(AD7190_CLK_INT) | 00263 AD7190_MODE_RATE(0x060); 00264 cs.write(0); //ADI_PART_CS_LOW; 00265 AD7190_SetRegisterValue(AD7190_REG_MODE, command, 3); // CS is not modified. 00266 AD7190_WaitRdyGoLow(); 00267 //wait_us(500); 00268 regData = AD7190_GetRegisterValue(AD7190_REG_DATA, 3); 00269 cs.write(1); //ADI_PART_CS_HIGH; 00270 00271 return regData; 00272 } 00273 00274 /***************************************************************************//** 00275 * @brief Returns the average of several conversion results. 00276 * 00277 * @return samplesAverage - The average of the conversion results. 00278 *******************************************************************************/ 00279 unsigned long AD7190_ContinuousReadAvg(unsigned char sampleNumber) 00280 { 00281 unsigned long samplesAverage = 0x0; 00282 unsigned char count = 0x0; 00283 unsigned long command = 0x0; 00284 00285 command = AD7190_MODE_SEL(AD7190_MODE_CONT) | 00286 AD7190_MODE_CLKSRC(AD7190_CLK_INT) | 00287 AD7190_MODE_RATE(0x060); 00288 cs.write(0); //ADI_PART_CS_LOW; 00289 AD7190_SetRegisterValue(AD7190_REG_MODE, command, 3); // CS is not modified. 00290 for(count = 0;count < sampleNumber;count ++) 00291 { 00292 AD7190_WaitRdyGoLow(); 00293 samplesAverage += AD7190_GetRegisterValue(AD7190_REG_DATA, 3); // CS is not modified. 00294 } 00295 cs.write(1); //ADI_PART_CS_HIGH; 00296 samplesAverage = samplesAverage / sampleNumber; 00297 00298 return samplesAverage ; 00299 } 00300 00301 /***************************************************************************//** 00302 * @brief Read data from temperature sensor and converts it to Celsius degrees. 00303 * 00304 * @return temperature - Celsius degrees. 00305 *******************************************************************************/ 00306 unsigned long AD7190_TemperatureRead(void) 00307 { 00308 unsigned char temperature = 0x0; 00309 unsigned long dataReg = 0x0; 00310 AD7190_RangeSetup(0, AD7190_CONF_GAIN_1); 00311 AD7190_ChannelSelect(AD7190_CH_TEMP_SENSOR); 00312 dataReg = AD7190_SingleConversion(); 00313 dataReg -= 0x800000; 00314 dataReg /= 2815; // Kelvin Temperature 00315 dataReg -= 273; //Celsius Temperature 00316 temperature = (unsigned long) dataReg; 00317 00318 return temperature; 00319 }
Generated on Wed Jul 13 2022 09:47:46 by
1.7.2