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.
MCP3424.cpp
00001 #include "MCP3424.h" 00002 00003 extern Serial pc; 00004 00005 MCP3424::MCP3424(PinName sda, PinName scl) : mcp3424_i2c(sda, scl) 00006 { 00007 mcp3424_i2c.frequency(MCP3424_FREQ); 00008 } 00009 00010 MCP3424::~MCP3424(){ 00011 00012 } 00013 00014 bool MCP3424::Config(uint8_t channel, uint8_t resolution, bool mode, uint8_t PGA) 00015 { 00016 bool ok = false; 00017 char addr = 0x00; 00018 if(mcp3424_i2c.write(MCP3424_ADDR, &addr, 1) != 0) 00019 return 0; 00020 00021 _PGA = PGA; 00022 _mode = mode; 00023 00024 if(resolution != 12 && resolution != 14 && resolution != 16 && resolution != 18) 00025 { 00026 _resolution = 12; 00027 } 00028 else _resolution = resolution; 00029 00030 _cfgbyte = 0; 00031 _cfgbyte = _cfgbyte<<2; 00032 _cfgbyte |= (channel-1); 00033 _cfgbyte = _cfgbyte<<1; 00034 _cfgbyte |= _mode; 00035 _cfgbyte = _cfgbyte<<2; 00036 _cfgbyte |= int((_resolution-12)/2); 00037 _cfgbyte = _cfgbyte<<2; 00038 _cfgbyte |= 0x00; 00039 00040 char _send = (char)_cfgbyte; 00041 00042 if(mcp3424_i2c.write(MCP3424_ADDR, &_send, 1) ==0) 00043 { 00044 ok = true; 00045 } 00046 else 00047 { 00048 ok = false; 00049 } 00050 return ok; 00051 } 00052 00053 bool MCP3424::NewConversion() 00054 { 00055 char _send = (char)(_cfgbyte |= 128); 00056 if(mcp3424_i2c.write(MCP3424_ADDR, &_send, 1) != 0) 00057 return 0; 00058 else return 1; 00059 } 00060 00061 bool MCP3424::IsConversionFinished() 00062 { 00063 if(_resolution!=18) 00064 { 00065 _RequestedByte = 3; 00066 } 00067 else _RequestedByte = 4; 00068 00069 00070 if(mcp3424_i2c.read(MCP3424_ADDR, _recvBuff, _RequestedByte)!= 0) return 0; 00071 00072 for(_i = 0; _i < _RequestedByte; _i++) 00073 { 00074 _Buffer[_i] = _recvBuff[_i]; 00075 } 00076 00077 _testvariable = _Buffer[_RequestedByte-1]>>7; 00078 00079 return _testvariable; 00080 } 00081 00082 00083 long MCP3424::Measure() 00084 { 00085 _resultat=0; 00086 00087 while(IsConversionFinished()==1); 00088 00089 switch (_resolution) 00090 { 00091 case 12:{ 00092 _Buffer[0]&=0b00001111; 00093 _resultat = _Buffer[0]*256 + _Buffer[1]; 00094 00095 if(_resultat>2048-1) 00096 { 00097 _resultat=_resultat-4096-1; 00098 } 00099 00100 _resultat = _resultat*1000/_PGA; 00101 00102 break; 00103 } 00104 case 14:{ 00105 _Buffer[0]&=0b00111111; 00106 _resultat = _Buffer[0]*256 + _Buffer[1]; 00107 00108 if(_resultat>8192-1) 00109 { 00110 _resultat=_resultat-16384-1; 00111 } 00112 00113 _resultat = _resultat*250/_PGA; 00114 00115 break; 00116 } 00117 case 16:{ 00118 _resultat = _Buffer[0]*256 + _Buffer[1]; 00119 00120 if(_resultat>32768-1) 00121 { 00122 _resultat=_resultat-65536-1; 00123 } 00124 00125 _resultat = _resultat*62.5/_PGA; 00126 00127 break; 00128 } 00129 case 18:{ 00130 _Buffer[0]&=0b00000011; 00131 _resultat = _Buffer[0]*512 + _Buffer[1]*256 + _Buffer[2]; 00132 00133 if(_resultat>131072-1) 00134 { 00135 _resultat=_resultat-262144-1; 00136 } 00137 00138 _resultat = _resultat*15.625/_PGA; 00139 00140 break; 00141 } 00142 } 00143 return _resultat; 00144 00145 }
Generated on Tue Jul 12 2022 18:36:20 by
