Skip to content

Commit 569e7a1

Browse files
committed
Improved MeasureVoltageAndResistance with new pinout
1 parent 3d9fdde commit 569e7a1

File tree

5 files changed

+298
-142
lines changed

5 files changed

+298
-142
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ Battery mode 0x6081 | 0b110000010000001
160160
Value1=3687, Value2=3667 - Non standard info is supported
161161
Manufacturer Name SANYO | 0x53 41 4E 59 4F
162162
Chemistry LION | 0x4C 49 4F 4E
163-
Manufacturer Data W'4=5 | 0x57 7 27 3 34 E 3D E 1F E 35 E
163+
Manufacturer Data W_'_4_=__5_ | 0x57 7 27 3 34 E 3D E 1F E 35 E
164164
Device Name M10B1 | 0x4D 31 30 42 31
165165
Serial number 11444 | 0x2CB4
166166
Manufacture date (YYYY-MM-DD) 2008-5-25

SBMInfo/ADCUtils.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
// External Reference Current is 150 uA for 5 V and 100 uA for 3.5 V
3333
#define READING_FOR_AREF 1024L // Datasheet 24.2: The minimum value represents GND and the maximum value represents the voltage on the AREF pin minus 1 LSB
34+
#define MAX_ADC_VALUE 1023L
3435

3536
// PRESCALE4 => 13 * 4 = 52 microseconds per ADC conversion at 1 MHz Clock => 19,2 kHz
3637
#define ADC_PRESCALE2 1 // 26 microseconds per ADC conversion at 1 MHz
@@ -172,8 +173,10 @@
172173
extern long sLastVCCCheckMillis;
173174
extern uint8_t sVCCTooLowCounter;
174175

176+
uint16_t readADCChannel();
175177
uint16_t readADCChannel(uint8_t aADCChannelNumber);
176178
uint16_t readADCChannelWithReference(uint8_t aADCChannelNumber, uint8_t aReference);
179+
uint16_t readADCChannelWithReferenceUsingInternalReference(uint8_t aADCChannelNumber);
177180
uint16_t waitAndReadADCChannelWithReference(uint8_t aADCChannelNumber, uint8_t aReference);
178181
uint16_t waitAndReadADCChannelWithReferenceAndRestoreADMUXAndReference(uint8_t aADCChannelNumber, uint8_t aReference);
179182
uint16_t readADCChannelWithOversample(uint8_t aADCChannelNumber, uint8_t aOversampleExponent);
@@ -188,6 +191,8 @@ uint16_t readADCChannelWithReferenceMaxMicros(uint8_t aADCChannelNumber, uint8_t
188191
uint16_t readUntil4ConsecutiveValuesAreEqual(uint8_t aADCChannelNumber, uint8_t aReference, uint8_t aDelay,
189192
uint8_t aAllowedDifference, uint8_t aMaxRetries);
190193

194+
void setADCChannelForNextConversionAndWaitUsingInternalReference(uint8_t aADCChannelNumber);
195+
void setADCChannelForNextConversionAndWaitUsingDefaultReference(uint8_t aADCChannelNumber);
191196
uint8_t checkAndWaitForReferenceAndChannelToSwitch(uint8_t aADCChannelNumber, uint8_t aReference);
192197

193198
/*

SBMInfo/ADCUtils.hpp

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
* with INTERNAL you can calibrate your ADC readout. For my Nanos I measured e.g. 1060 mV and 1093 mV.
4343
*/
4444
#if !defined(ADC_INTERNAL_REFERENCE_MILLIVOLT)
45-
#define ADC_INTERNAL_REFERENCE_MILLIVOLT 1100L // Change to value measured at the AREF pin. If value > real AREF voltage, measured values are > real values
45+
#define ADC_INTERNAL_REFERENCE_MILLIVOLT 1100UL // Change to value measured at the AREF pin. If value > real AREF voltage, measured values are > real values
4646
#endif
4747

4848
// Union to speed up the combination of low and high bytes to a word
@@ -85,6 +85,27 @@ uint8_t sVCCTooLowCounter = 0;
8585

8686
/*
8787
* Conversion time is defined as 0.104 milliseconds by ADC_PRESCALE in ADCUtils.h.
88+
* Use previous settings
89+
*/
90+
uint16_t readADCChannel() {
91+
WordUnionForADCUtils tUValue;
92+
93+
// ADCSRB = 0; // Only active if ADATE is set to 1.
94+
// ADSC-StartConversion ADIF-Reset Interrupt Flag - NOT free running mode
95+
ADCSRA = (_BV(ADEN) | _BV(ADSC) | _BV(ADIF) | ADC_PRESCALE);
96+
97+
// wait for single conversion to finish
98+
loop_until_bit_is_clear(ADCSRA, ADSC);
99+
100+
// Get value
101+
tUValue.UByte.LowByte = ADCL;
102+
tUValue.UByte.HighByte = ADCH;
103+
return tUValue.UWord;
104+
// return ADCL | (ADCH <<8); // needs 4 bytes more
105+
}
106+
107+
/*
108+
* Use new channel aADCChannelNumber, but do not wait for channel switching
88109
*/
89110
uint16_t readADCChannel(uint8_t aADCChannelNumber) {
90111
WordUnionForADCUtils tUValue;
@@ -103,7 +124,6 @@ uint16_t readADCChannel(uint8_t aADCChannelNumber) {
103124
return tUValue.UWord;
104125
// return ADCL | (ADCH <<8); // needs 4 bytes more
105126
}
106-
107127
/*
108128
* Conversion time is defined as 0.104 milliseconds by ADC_PRESCALE in ADCUtils.h.
109129
*/
@@ -124,6 +144,22 @@ uint16_t readADCChannelWithReference(uint8_t aADCChannelNumber, uint8_t aReferen
124144
return tUValue.UWord;
125145
}
126146

147+
uint16_t readADCChannelWithReferenceUsingInternalReference(uint8_t aADCChannelNumber) {
148+
WordUnionForADCUtils tUValue;
149+
ADMUX = aADCChannelNumber | (INTERNAL << SHIFT_VALUE_FOR_REFERENCE);
150+
151+
// ADCSRB = 0; // Only active if ADATE is set to 1.
152+
// ADSC-StartConversion ADIF-Reset Interrupt Flag - NOT free running mode
153+
ADCSRA = (_BV(ADEN) | _BV(ADSC) | _BV(ADIF) | ADC_PRESCALE);
154+
155+
// wait for single conversion to finish
156+
loop_until_bit_is_clear(ADCSRA, ADSC);
157+
158+
// Get value
159+
tUValue.UByte.LowByte = ADCL;
160+
tUValue.UByte.HighByte = ADCH;
161+
return tUValue.UWord;
162+
}
127163
/*
128164
* Conversion time is defined as 0.104 milliseconds by ADC_PRESCALE in ADCUtils.h.
129165
* Does NOT restore ADMUX after reading
@@ -151,6 +187,19 @@ void setADCChannelAndReferenceForNextConversion(uint8_t aADCChannelNumber, uint8
151187
ADMUX = aADCChannelNumber | (aReference << SHIFT_VALUE_FOR_REFERENCE);
152188
}
153189

190+
/*
191+
* 100 kOhm requires < 100 us, 1 MOhm requires 120 us S&H switching time
192+
*/
193+
void setADCChannelForNextConversionAndWaitUsingInternalReference(uint8_t aADCChannelNumber) {
194+
ADMUX = aADCChannelNumber | (INTERNAL << SHIFT_VALUE_FOR_REFERENCE);
195+
delayMicroseconds(120); // experimental value is <= 1100 us for Nano board
196+
}
197+
198+
void setADCChannelForNextConversionAndWaitUsingDefaultReference(uint8_t aADCChannelNumber) {
199+
ADMUX = aADCChannelNumber | (DEFAULT << SHIFT_VALUE_FOR_REFERENCE);
200+
delayMicroseconds(120); // experimental value is <= 1100 us for Nano board
201+
}
202+
154203
/*
155204
* @return original ADMUX register content for optional later restoring values
156205
* All experimental values are acquired by using the ADCSwitchingTest example from this library

0 commit comments

Comments
 (0)