Kenji Arai / MCP3425

Dependents:   check_ADC_MCP3425

Files at this revision

API Documentation at this revision

Comitter:
kenjiArai
Date:
Sun Mar 18 01:24:58 2018 +0000
Parent:
0:29be5dda6cf2
Commit message:
modified comments

Changed in this revision

MCP3425.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 29be5dda6cf2 -r 223248a79e87 MCP3425.cpp
--- a/MCP3425.cpp	Sun Mar 18 01:02:54 2018 +0000
+++ b/MCP3425.cpp	Sun Mar 18 01:24:58 2018 +0000
@@ -78,23 +78,23 @@
     }
     do {
         _i2c.read( (int)mcp3425_addr, (char *)buf, 3);
-        if ((buf[2] & RDY_BIT) == 0) {
-            break;      // end of conversion
+        if ((buf[2] & RDY_BIT) == 0) {  // check Config. reg. Ready bit
+            break;      // end of conversion (RDY = 0)
         }
         if (--timeout == 0) {
-            return -1;  // error
+            return -1;  // timeout then error
         }
-        uint8_t spd = (buf[2] >> 2) & 0x03;
-        if (spd == SAMPLE_RATE_60SPS_14BIT) {
+        uint8_t spd = (buf[2] >> 2) & 0x03; // get current sampling rate
+        if (spd == SAMPLE_RATE_60SPS_14BIT) {   // wait next conversion period
             WAIT_MS(6);     // conversion time = 16.7ms
         } else if (spd == SAMPLE_RATE_15SPS_16BIT) {
             WAIT_MS(24);    // conversion time = 66.7ms
         } else {  // == SAMPLE_RATE_240SPS_12BIT
-            WAIT_MS(2);     // no wait
+            WAIT_MS(2);     // conversion time = 4.2ms
         }
     } while(true);
-    dt_adc16 = (uint16_t)buf[0] << 8;
-    dt_adc16 += (uint16_t)buf[1];
+    dt_adc16 = (uint16_t)buf[0] << 8;   // High byte
+    dt_adc16 += (uint16_t)buf[1];       // Low byte
     return dt_adc16;
 }
 
@@ -112,7 +112,7 @@
             dt16 /= 32768.0f; // 15bit (0x7fff +1)
             break;
         default:
-            return -1;
+            return -1;      // error
     }
     switch(config.pga_gain) {
         case PGA_GAIN_1:
@@ -128,11 +128,11 @@
             dt16 /= 8.0f;
             break;
         default:
-            return -1;
+            return -1;      // error
     }
-    dt_adc_f = dt16 * 2.048f;
-    dt_adc_f -= offset_volt;
-    dt_adc_f *= compensation_ref;
+    dt_adc_f = dt16 * 2.048f;       // Vref = 2.048V +/- 0.05%
+    dt_adc_f -= offset_volt;        // adjust offset voltage
+    dt_adc_f *= compensation_ref;   // compensate Vref deviation
     return dt_adc_f;
 }