Skip to content

Commit b2fb879

Browse files
committed
Updated ADCUtils and HexDump
1 parent 48489e2 commit b2fb879

File tree

7 files changed

+371
-182
lines changed

7 files changed

+371
-182
lines changed

README.md

+21-19
Original file line numberDiff line numberDiff line change
@@ -22,33 +22,35 @@ Not yet available as Arduino library.
2222
   
2323
[![Button API](https://img.shields.io/badge/API-yellow?logoColor=white&logo=OpenStreetMap)](https://arminjo.github.io/Arduino-Utils/classArduino-Utils.html)
2424
   
25-
[![Button Changelog](https://img.shields.io/badge/Changelog-blue?logoColor=white&logo=AzureArtifacts)](https://github.com/ArminJo/Arduino-Utils#revision-history)
25+
[![Button Changelog](https://img.shields.io/badge/Changelog-blue?logoColor=white&logo=AzureArtifacts)](https://github.com/ArminJo/Arduino-Utils?tab=readme-ov-file#revision-history)
2626

2727
</div>
2828

2929
#### If you find this library useful, please give it a star.
3030

31+
&#x1F30E; [Google Translate](https://translate.google.com/translate?sl=en&u=https://github.com/ArminJo/Arduino-Utils)
32+
3133
<br/>
3234

3335
# Table of content
34-
* [SimpleEMAFilters.hpp](https://github.com/ArminJo/Arduino-Utils#simpleemafiltershpp)
35-
* [ADCUtils.cpp](https://github.com/ArminJo/Arduino-Utils#adcutilscpp)
36-
* [HCSR04](https://github.com/ArminJo/Arduino-Utils#hcsr04)
37-
* [MeasureVoltageAndResistance.hpp](https://github.com/ArminJo/Arduino-Utils#measurevoltageandresistancehpp)
38-
* [BlinkLed.cpp](https://github.com/ArminJo/Arduino-Utils#blinkledcpp)
39-
* [ShowInfo](https://github.com/ArminJo/Arduino-Utils#showinfo)
40-
* [HexDump](https://github.com/ArminJo/Arduino-Utils#hexdump)
41-
* [AVRUtils.cpp](https://github.com/ArminJo/Arduino-Utils#avrutilscpp)
42-
* [MillisUtils.cpp](https://github.com/ArminJo/Arduino-Utils#millisutilscpp)
43-
* [DebugLevel.h](https://github.com/ArminJo/Arduino-Utils#debuglevelh)
44-
* [ATtinyUtils.cpp](https://github.com/ArminJo/Arduino-Utils#attinyutilscpp)
45-
* [AvrTracing.hpp](https://github.com/ArminJo/Arduino-Utils#avrtracinghpp)
46-
* [Utilities available as separate Arduino library](https://github.com/ArminJo/Arduino-Utils#utilities-available-as-separate-arduino-library)
47-
* [ATtinySerialOut.cpp](https://github.com/ArminJo/Arduino-Utils#attinyserialoutcpp)
48-
* [EasyButtonAtInt01.hpp](https://github.com/ArminJo/Arduino-Utils#easybuttonatint01hpp)
49-
* [The very useful digitalWriteFast.h file based on the version from Watterott electronic.](https://github.com/ArminJo/Arduino-Utils#the-very-useful-digitalwritefasth-file-based-on-the-version-from-watterott-electronic)
50-
* [Revision History](https://github.com/ArminJo/Arduino-Utils#revision-history)
51-
* [CI](https://github.com/ArminJo/Arduino-Utils#ci)
36+
* [SimpleEMAFilters.hpp](https://github.com/ArminJo/Arduino-Utils?tab=readme-ov-file#simpleemafiltershpp)
37+
* [ADCUtils.cpp](https://github.com/ArminJo/Arduino-Utils?tab=readme-ov-file#adcutilscpp)
38+
* [HCSR04](https://github.com/ArminJo/Arduino-Utils?tab=readme-ov-file#hcsr04)
39+
* [MeasureVoltageAndResistance.hpp](https://github.com/ArminJo/Arduino-Utils?tab=readme-ov-file#measurevoltageandresistancehpp)
40+
* [BlinkLed.cpp](https://github.com/ArminJo/Arduino-Utils?tab=readme-ov-file#blinkledcpp)
41+
* [ShowInfo](https://github.com/ArminJo/Arduino-Utils?tab=readme-ov-file#showinfo)
42+
* [HexDump](https://github.com/ArminJo/Arduino-Utils?tab=readme-ov-file#hexdump)
43+
* [AVRUtils.cpp](https://github.com/ArminJo/Arduino-Utils?tab=readme-ov-file#avrutilscpp)
44+
* [MillisUtils.cpp](https://github.com/ArminJo/Arduino-Utils?tab=readme-ov-file#millisutilscpp)
45+
* [DebugLevel.h](https://github.com/ArminJo/Arduino-Utils?tab=readme-ov-file#debuglevelh)
46+
* [ATtinyUtils.cpp](https://github.com/ArminJo/Arduino-Utils?tab=readme-ov-file#attinyutilscpp)
47+
* [AvrTracing.hpp](https://github.com/ArminJo/Arduino-Utils?tab=readme-ov-file#avrtracinghpp)
48+
* [Utilities available as separate Arduino library](https://github.com/ArminJo/Arduino-Utils?tab=readme-ov-file#utilities-available-as-separate-arduino-library)
49+
* [ATtinySerialOut.cpp](https://github.com/ArminJo/Arduino-Utils?tab=readme-ov-file#attinyserialoutcpp)
50+
* [EasyButtonAtInt01.hpp](https://github.com/ArminJo/Arduino-Utils?tab=readme-ov-file#easybuttonatint01hpp)
51+
* [The very useful digitalWriteFast.h file based on the version from Watterott electronic.](https://github.com/ArminJo/Arduino-Utils?tab=readme-ov-file#the-very-useful-digitalwritefasth-file-based-on-the-version-from-watterott-electronic)
52+
* [Revision History](https://github.com/ArminJo/Arduino-Utils?tab=readme-ov-file#revision-history)
53+
* [CI](https://github.com/ArminJo/Arduino-Utils?tab=readme-ov-file#ci)
5254

5355
# SimpleEMAFilters.hpp
5456
An EMA (**Exponential Moving Average**) filter behaves like an RC lowpass filter with RC = SamplePeriod((1-alpha)/alpha) see [here](https://en.wikipedia.org/wiki/Low-pass_filter#Simple_infinite_impulse_response_filter).<br/>

src/ADCUtils.h

+60-6
Original file line numberDiff line numberDiff line change
@@ -85,33 +85,81 @@
8585
#define ADC_TEMPERATURE_CHANNEL_MUX 15
8686
#define ADC_1_1_VOLT_CHANNEL_MUX 12
8787
#define ADC_GND_CHANNEL_MUX 13
88+
#define ADC_CHANNEL_MUX_MASK 0x0F
8889

8990
#elif defined(__AVR_ATtiny87__) || defined(__AVR_ATtiny167__)
9091
#define ADC_ISCR_CHANNEL_MUX 3
9192
#define ADC_TEMPERATURE_CHANNEL_MUX 11
9293
#define ADC_1_1_VOLT_CHANNEL_MUX 12
9394
#define ADC_GND_CHANNEL_MUX 14
9495
#define ADC_VCC_4TH_CHANNEL_MUX 13
96+
#define ADC_CHANNEL_MUX_MASK 0x1F
9597

9698
#elif defined(__AVR_ATmega328P__)
9799
#define ADC_TEMPERATURE_CHANNEL_MUX 8
98100
#define ADC_1_1_VOLT_CHANNEL_MUX 14
99101
#define ADC_GND_CHANNEL_MUX 15
102+
#define ADC_CHANNEL_MUX_MASK 0x0F
100103

101104
#elif defined(__AVR_ATmega32U4__)
102105
#define ADC_TEMPERATURE_CHANNEL_MUX 0x27
103106
#define ADC_1_1_VOLT_CHANNEL_MUX 0x1E
104107
#define ADC_GND_CHANNEL_MUX 0x1F
108+
#define ADC_CHANNEL_MUX_MASK 0x3F
105109

106110
#elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__) || defined(__AVR_ATmega644__) || defined(__AVR_ATmega644A__) || defined(__AVR_ATmega644P__) || defined(__AVR_ATmega644PA__)
107111
#define ADC_1_1_VOLT_CHANNEL_MUX 0x1E
108112
#define ADC_GND_CHANNEL_MUX 0x1F
113+
#define ADC_CHANNEL_MUX_MASK 0x1F
114+
109115
#define INTERNAL INTERNAL1V1
110116

111117
#else
112118
#error "No temperature channel definitions specified for this AVR CPU"
113119
#endif
114120

121+
/*
122+
* Thresholds for OVER and UNDER voltage and detection of kind of power supply (USB or Li-ion)
123+
*
124+
* Default values are suitable for Li-ion batteries.
125+
* We normally have voltage drop at the connectors, so the battery voltage is assumed slightly higher, than the Arduino VCC.
126+
* But keep in mind that the ultrasonic distance module HC-SR04 may not work reliable below 3.7 volt.
127+
*/
128+
#if !defined(LI_ION_VCC_UNDERVOLTAGE_THRESHOLD_MILLIVOLT)
129+
#define LI_ION_VCC_UNDERVOLTAGE_THRESHOLD_MILLIVOLT 3400 // Do not stress your battery and we require some power for standby
130+
#endif
131+
#if !defined(LI_ION_VCC_EMERGENCY_UNDERVOLTAGE_THRESHOLD_MILLIVOLT)
132+
#define LI_ION_VCC_EMERGENCY_UNDERVOLTAGE_THRESHOLD_MILLIVOLT 3000 // Many Li-ions are specified down to 3.0 volt
133+
#endif
134+
135+
#if !defined(VCC_UNDERVOLTAGE_THRESHOLD_MILLIVOLT)
136+
#define VCC_UNDERVOLTAGE_THRESHOLD_MILLIVOLT LI_ION_VCC_UNDERVOLTAGE_THRESHOLD_MILLIVOLT
137+
#endif
138+
#if !defined(VCC_EMERGENCY_UNDERVOLTAGE_THRESHOLD_MILLIVOLT)
139+
#define VCC_EMERGENCY_UNDERVOLTAGE_THRESHOLD_MILLIVOLT LI_ION_VCC_EMERGENCY_UNDERVOLTAGE_THRESHOLD_MILLIVOLT
140+
#endif
141+
#if !defined(VCC_OVERVOLTAGE_THRESHOLD_MILLIVOLT)
142+
#define VCC_OVERVOLTAGE_THRESHOLD_MILLIVOLT 5250 // + 5 % operation voltage
143+
#endif
144+
#if !defined(VCC_EMERGENCY_OVERVOLTAGE_THRESHOLD_MILLIVOLT)
145+
#define VCC_EMERGENCY_OVERVOLTAGE_THRESHOLD_MILLIVOLT 5500 // +10 %. Max recommended operation voltage
146+
#endif
147+
#if !defined(VCC_CHECK_PERIOD_MILLIS)
148+
#define VCC_CHECK_PERIOD_MILLIS 10000L // 10 seconds period of VCC checks
149+
#endif
150+
#if !defined(VCC_UNDERVOLTAGE_CHECKS_BEFORE_STOP)
151+
#define VCC_UNDERVOLTAGE_CHECKS_BEFORE_STOP 6 // Shutdown after 6 times (60 seconds) VCC below VCC_UNDERVOLTAGE_THRESHOLD_MILLIVOLT or 1 time below VCC_EMERGENCY_UNDERVOLTAGE_THRESHOLD_MILLIVOLT
152+
#endif
153+
154+
#if !defined(VOLTAGE_USB_POWERED_LOWER_THRESHOLD_MILLIVOLT)
155+
#define VOLTAGE_USB_POWERED_LOWER_THRESHOLD_MILLIVOLT 4300 // Assume USB powered above this voltage
156+
#endif
157+
158+
#if !defined(VOLTAGE_USB_POWERED_UPPER_THRESHOLD_MILLIVOLT)
159+
#define VOLTAGE_USB_POWERED_UPPER_THRESHOLD_MILLIVOLT 4950 // Assume USB powered below this voltage, because of the loss in USB cable. If we have > 4950, we assume to be powered by VIN.
160+
// In contrast to e.g. powered by VIN, which results in almost perfect 5 volt supply
161+
#endif
162+
115163
extern long sLastVCCCheckMillis;
116164
extern uint8_t sVCCTooLowCounter;
117165

@@ -122,7 +170,10 @@ uint16_t waitAndReadADCChannelWithReferenceAndRestoreADMUXAndReference(uint8_t a
122170
uint16_t readADCChannelWithOversample(uint8_t aADCChannelNumber, uint8_t aOversampleExponent);
123171
void setADCChannelAndReferenceForNextConversion(uint8_t aADCChannelNumber, uint8_t aReference);
124172
uint16_t readADCChannelWithReferenceOversampleFast(uint8_t aADCChannelNumber, uint8_t aReference, uint8_t aOversampleExponent);
125-
uint16_t readADCChannelWithReferenceMultiSamples(uint8_t aADCChannelNumber, uint8_t aReference, uint8_t aNumberOfSamples);
173+
uint32_t readADCChannelMultiSamples(uint8_t aPrescale, uint16_t aNumberOfSamples);
174+
uint16_t readADCChannelMultiSamplesWithReference(uint8_t aADCChannelNumber, uint8_t aReference, uint8_t aNumberOfSamples);
175+
uint32_t readADCChannelMultiSamplesWithReferenceAndPrescaler(uint8_t aADCChannelNumber, uint8_t aReference, uint8_t aPrescale,
176+
uint16_t aNumberOfSamples);
126177
uint16_t readADCChannelWithReferenceMax(uint8_t aADCChannelNumber, uint8_t aReference, uint16_t aNumberOfSamples);
127178
uint16_t readADCChannelWithReferenceMaxMicros(uint8_t aADCChannelNumber, uint8_t aReference, uint16_t aMicrosecondsToAquire);
128179
uint16_t readUntil4ConsecutiveValuesAreEqual(uint8_t aADCChannelNumber, uint8_t aReference, uint8_t aDelay,
@@ -150,11 +201,14 @@ float getCPUTemperatureSimple(void);
150201
float getCPUTemperature(void);
151202
float getTemperature(void) __attribute__ ((deprecated ("Renamed to getCPUTemperature()"))); // deprecated
152203

153-
bool isVCCTooLowMultipleTimes();
154-
void resetVCCTooLowMultipleTimes();
155-
bool isVCCTooLow();
156-
bool isVCCTooHigh();
157-
bool isVCCTooHighSimple();
204+
bool isVCCUSBPowered();
205+
bool isVCCUSBPowered(Print *aSerial);
206+
bool isVCCUndervoltageMultipleTimes();
207+
void resetCounterForVCCUndervoltageMultipleTimes();
208+
bool isVCCUndervoltage();
209+
bool isVCCEmergencyUndervoltage();
210+
bool isVCCOvervoltage();
211+
bool isVCCOvervoltageSimple();
158212

159213
#endif // defined(__AVR__) ...
160214

0 commit comments

Comments
 (0)