11//
22// FILE: ACS712.cpp
33// AUTHOR: Rob Tillaart, Pete Thompson
4- // VERSION: 0.3.4
4+ // VERSION: 0.3.5
55// DATE: 2020-08-02
66// PURPOSE: ACS712 library - current measurement
77// URL: https://github.com/RobTillaart/ACS712
@@ -26,7 +26,7 @@ ACS712::ACS712(uint8_t analogPin, float volts, uint16_t maxADC, float mVperAmper
2626 _midPoint = maxADC / 2 ;
2727
2828 // default ADC is internal.
29- setADC (_internalAnalog , volts, maxADC);
29+ setADC (NULL , volts, maxADC);
3030}
3131
3232
@@ -42,16 +42,16 @@ float ACS712::mA_peak2peak(float frequency, uint16_t cycles)
4242 {
4343 int minimum, maximum;
4444 // Better than using midPoint
45- minimum = maximum = _readADC (_pin);
45+ minimum = maximum = _analogRead (_pin);
4646
4747 // find minimum and maximum
4848 uint32_t start = micros ();
4949 while (micros () - start < period) // UNO ~180 samples...
5050 {
51- int value = _readADC (_pin);
51+ int value = _analogRead (_pin);
5252 if (_suppresNoise) // average 2 samples.
5353 {
54- value = (value + _readADC (_pin))/2 ;
54+ value = (value + _analogRead (_pin))/2 ;
5555 }
5656 // determine extremes
5757 if (value < minimum) minimum = value;
@@ -82,17 +82,17 @@ float ACS712::mA_AC(float frequency, uint16_t cycles)
8282 uint16_t zeros = 0 ;
8383
8484 int _min, _max;
85- _min = _max = _readADC (_pin);
85+ _min = _max = _analogRead (_pin);
8686
8787 // find minimum and maximum and count the zero-level "percentage"
8888 uint32_t start = micros ();
8989 while (micros () - start < period) // UNO ~180 samples...
9090 {
9191 samples++;
92- int value = _readADC (_pin);
92+ int value = _analogRead (_pin);
9393 if (_suppresNoise) // average 2 samples.
9494 {
95- value = (value + _readADC (_pin))/2 ;
95+ value = (value + _analogRead (_pin))/2 ;
9696 }
9797 // determine extremes
9898 if (value < _min) _min = value;
@@ -144,10 +144,10 @@ float ACS712::mA_AC_sampling(float frequency, uint16_t cycles)
144144 while (micros () - start < period)
145145 {
146146 samples++;
147- int value = _readADC (_pin);
147+ int value = _analogRead (_pin);
148148 if (_suppresNoise) // average 2 samples.
149149 {
150- value = (value + _readADC (_pin))/2 ;
150+ value = (value + _analogRead (_pin))/2 ;
151151 }
152152 float current = value - _midPoint;
153153 sumSquared += (current * current);
@@ -169,15 +169,15 @@ float ACS712::mA_AC_sampling(float frequency, uint16_t cycles)
169169float ACS712::mA_DC (uint16_t cycles)
170170{
171171 // read at least twice to stabilize the ADC
172- _readADC (_pin);
172+ _analogRead (_pin);
173173 if (cycles == 0 ) cycles = 1 ;
174174 float sum = 0 ;
175175 for (uint16_t i = 0 ; i < cycles; i++)
176176 {
177- int value = _readADC (_pin);
177+ int value = _analogRead (_pin);
178178 if (_suppresNoise) // average 2 samples.
179179 {
180- value = (value + _readADC (_pin))/2 ;
180+ value = (value + _analogRead (_pin))/2 ;
181181 }
182182 sum += (value - _midPoint);
183183 }
@@ -233,7 +233,7 @@ uint16_t ACS712::autoMidPoint(float frequency, uint16_t cycles)
233233 uint32_t start = micros ();
234234 while (micros () - start < twoPeriods)
235235 {
236- uint16_t reading = _readADC (_pin);
236+ uint16_t reading = _analogRead (_pin);
237237 subTotal += reading;
238238 samples++;
239239 // Delaying prevents overflow
@@ -329,14 +329,14 @@ float ACS712::detectFrequency(float minimalFrequency)
329329{
330330 int maximum = 0 ;
331331 int minimum = 0 ;
332- maximum = minimum = _readADC (_pin);
332+ maximum = minimum = _analogRead (_pin);
333333
334334 // determine maxima
335335 uint32_t timeOut = round (1000000.0 / minimalFrequency);
336336 uint32_t start = micros ();
337337 while (micros () - start < timeOut)
338338 {
339- int value = _readADC (_pin);
339+ int value = _analogRead (_pin);
340340 if (value > maximum) maximum = value;
341341 if (value < minimum) minimum = value;
342342 }
@@ -352,13 +352,13 @@ float ACS712::detectFrequency(float minimalFrequency)
352352 timeOut *= 10 ;
353353 start = micros ();
354354 // casting to int to keep compiler happy.
355- while ((int (_readADC (_pin)) > Q1) && ((micros () - start) < timeOut));
356- while ((int (_readADC (_pin)) <= Q3) && ((micros () - start) < timeOut));
355+ while ((int (_analogRead (_pin)) > Q1) && ((micros () - start) < timeOut));
356+ while ((int (_analogRead (_pin)) <= Q3) && ((micros () - start) < timeOut));
357357 start = micros ();
358358 for (int i = 0 ; i < 10 ; i++)
359359 {
360- while ((int (_readADC (_pin)) > Q1) && ((micros () - start) < timeOut));
361- while ((int (_readADC (_pin)) <= Q3) && ((micros () - start) < timeOut));
360+ while ((int (_analogRead (_pin)) > Q1) && ((micros () - start) < timeOut));
361+ while ((int (_analogRead (_pin)) <= Q3) && ((micros () - start) < timeOut));
362362 }
363363 uint32_t stop = micros ();
364364
@@ -386,13 +386,13 @@ float ACS712::getMicrosAdjust()
386386// DEBUG
387387uint16_t ACS712::getMinimum (uint16_t milliSeconds)
388388{
389- uint16_t minimum = _readADC (_pin);
389+ uint16_t minimum = _analogRead (_pin);
390390
391391 // find minimum
392392 uint32_t start = millis ();
393393 while (millis () - start < milliSeconds)
394394 {
395- uint16_t value = _readADC (_pin);
395+ uint16_t value = _analogRead (_pin);
396396 if (value < minimum) minimum = value;
397397 }
398398 return minimum;
@@ -401,13 +401,13 @@ uint16_t ACS712::getMinimum(uint16_t milliSeconds)
401401
402402uint16_t ACS712::getMaximum (uint16_t milliSeconds)
403403{
404- uint16_t maximum = _readADC (_pin);
404+ uint16_t maximum = _analogRead (_pin);
405405
406406 // find minimum
407407 uint32_t start = millis ();
408408 while (millis () - start < milliSeconds)
409409 {
410- uint16_t value = _readADC (_pin);
410+ uint16_t value = _analogRead (_pin);
411411 if (value > maximum) maximum = value;
412412 }
413413 return maximum;
@@ -425,5 +425,17 @@ void ACS712::setADC(uint16_t (* f)(uint8_t), float volts, uint16_t maxADC)
425425}
426426
427427
428+ // ////////////////////////////////////////////////////////////////////
429+ //
430+ // PRIVATE
431+ //
432+ uint16_t ACS712::_analogRead (uint8_t pin)
433+ {
434+ // if extern ADC is defined use it.
435+ if (_readADC != NULL ) return _readADC (pin);
436+ return analogRead (pin);
437+ }
438+
439+
428440// -- END OF FILE --
429441
0 commit comments