Chris Snow / MCP3424
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MCP3424.cpp Source File

MCP3424.cpp

00001 /* MCP3424 library version 1.2
00002 
00003 Writed by B@tto 
00004 Contact : batto@hotmail.fr
00005 
00006 
00007   MCP3424.cpp - ADC 18 bits i2c library for Wiring & Arduino
00008   Copyright (c) 2012 Yann LEFEBVRE.  All right reserved.
00009 
00010   This library is free software; you can redistribute it and/or
00011   modify it under the terms of the GNU Lesser General Public
00012   License as published by the Free Software Foundation; either
00013   version 2.1 of the License, or (at your option) any later version.
00014 
00015   This library is distributed in the hope that it will be useful,
00016   but WITHOUT ANY WARRANTY; without even the implied warranty of
00017   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018   Lesser General Public License for more details.
00019 
00020   You should have received a copy of the GNU Lesser General Public
00021   License along with this library; if not, write to the Free Software
00022   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00023 */
00024 #include <MCP3424.h>
00025 #include "mbed.h"
00026 
00027 MCP3424::MCP3424(PinName SDA, PinName SCL, uint8_t adresse):i2c(SDA, SCL){
00028 
00029 _adresse|=0x0D<<3;
00030     _adresse = _adresse<<1;
00031 //_adresse|=adresse;
00032 }
00033 
00034 MCP3424::~MCP3424(){
00035 
00036 }
00037 
00038 
00039 void MCP3424::Configuration(char channel,char resolution,bool mode,char PGA){
00040 _PGA=PGA;
00041 
00042 if(resolution!=12 && resolution!=14 && resolution!=16 && resolution!=18) {
00043 _resolution=12;
00044 } else _resolution=resolution;
00045 
00046 _mode=mode;
00047 _cfgbyte=0;
00048 _cfgbyte=_cfgbyte<<2;
00049 _cfgbyte|=(channel-1);
00050 _cfgbyte=_cfgbyte<<1;
00051 _cfgbyte|=mode;
00052 _cfgbyte=_cfgbyte<<2;
00053 _cfgbyte|=int((_resolution-12)/2);
00054 _cfgbyte=_cfgbyte<<2;
00055 switch(PGA)
00056 {
00057     case 1:
00058         _cfgbyte|=0x00;
00059 
00060     break;
00061         case 2:
00062                 _cfgbyte|=0x01;
00063 
00064     break;
00065             case 4:
00066                 _cfgbyte|=0x10;
00067 
00068     break;
00069                 case 8:
00070                 _cfgbyte|=0x11;
00071 
00072     break;
00073             }
00074 
00075 i2c.write(_adresse,&_cfgbyte,1);
00076 
00077 }
00078 
00079 void MCP3424::NewConversion(){
00080     
00081     char byte = _cfgbyte|=128;
00082     i2c.write(_adresse,&byte,1);
00083 }
00084 
00085 bool MCP3424::IsConversionFinished(){
00086 
00087 if(_resolution!=18){
00088 _RequestedByte = 3;    
00089 } else _RequestedByte = 4;
00090 
00091 i2c.read(_adresse, _Buffer,_RequestedByte);
00092 
00093 
00094 _testvariable = _Buffer[_RequestedByte-1]>>7;
00095 
00096 return _testvariable;
00097 
00098 }
00099 
00100 
00101 long MCP3424::Measure(){
00102 
00103   _resultat=0;
00104 
00105 while(IsConversionFinished()==1);
00106 
00107 switch (_resolution){
00108   
00109 case 12:
00110  _resultat = _Buffer[0];
00111  _resultat &= 0x0f;//0b00001111;
00112  _resultat = _resultat << 8;
00113 _resultat |= _Buffer[1];
00114 
00115 if(_resultat>2048-1) {
00116 _resultat=_resultat-4096-1;
00117 }
00118 
00119  _resultat = _resultat*1000/_PGA;      
00120    
00121  break;
00122     
00123  case 14:
00124 _resultat = _Buffer[0];
00125  _resultat &= 0x3f;//0b00111111;
00126 _resultat = _resultat << 8;    
00127  _resultat |= _Buffer[1];
00128 
00129 if(_resultat>8192-1) {
00130 _resultat=_resultat-16384-1;
00131 }
00132 
00133 _resultat = _resultat*250/_PGA;  
00134        
00135  break;
00136     
00137 case 16:
00138 
00139 _resultat = _Buffer[0]; 
00140 _resultat = _resultat << 8;    
00141 _resultat |= _Buffer[1];
00142 
00143 if(_resultat>32768-1) {
00144 _resultat=_resultat-65536-1;
00145 }
00146 
00147 _resultat = _resultat*62.5/_PGA;   
00148       
00149  break;
00150   
00151 case 18:
00152 
00153 _resultat = _Buffer[0];
00154  _resultat&=0x02;//0b00000011;
00155   _resultat = _resultat << 8;
00156   _resultat |= _Buffer[1];
00157  _resultat = _resultat << 8;    
00158  _resultat |= _Buffer[2];
00159  
00160 if(_resultat>131072-1) {
00161 _resultat=_resultat-262144-1;
00162 }
00163 
00164 _resultat = _resultat*15.625/_PGA; 
00165 
00166   break;
00167 }
00168             
00169 return _resultat;
00170     
00171 
00172 }
00173 
00174