Skip to content

Commit b29ae79

Browse files
authored
Add files via upload
1 parent 1d60095 commit b29ae79

File tree

1 file changed

+75
-77
lines changed

1 file changed

+75
-77
lines changed

src/avdweb_AnalogReadFast.h

Lines changed: 75 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,98 +1,96 @@
11
/**
2-
* avdweb_AnalogReadFast.h
3-
*
4-
* `avdweb_AnalogReadFast` Library
5-
* ================================
6-
*
7-
* Fast analogRead 10/12 bit ADC for the Arduino Zero and Uno
8-
*
9-
* WEBSITE: http://www.avdweb.nl/arduino/libraries/fast-10-bit-adc.html
10-
*
11-
* Sources
12-
* --------
13-
* https://github.com/avdwebLibraries/avdweb_AnalogReadFast
14-
*
15-
* License
16-
* --------
17-
* SPDX: MIT
18-
*
19-
* `avdweb_AnalogReadFast` - Fast analogRead 10/12 bit ADC for the
20-
* Arduino Zero and Uno
21-
*
22-
* Copyright (C) 2019-2024 Albert van Dalen <http://www.avdweb.nl>
23-
* Copyright (C) 2024 Abhijit Bose (aka. Boseji) :contributor:
24-
*
25-
* Permission is hereby granted, free of charge, to any person obtaining
26-
* a copy of this software and associated documentation files (the "Software"),
27-
* to deal in the Software without restriction, including without limitation
28-
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
29-
* and/or sell copies of the Software, and to permit persons to whom the
30-
* Software is furnished to do so, subject to the following conditions:
31-
*
32-
* The above copyright notice and this permission notice (including the
33-
* next paragraph) shall be included in all copies or substantial portions
34-
* of the Software.
35-
*
36-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
37-
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
38-
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
39-
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
40-
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
41-
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
42-
* OTHER DEALINGS IN THE SOFTWARE.
43-
*
44-
* Notes
45-
* ------
46-
*
47-
* HISTORY:
48-
* 1.0.0 7-3-2018 analogReadFast for SAMD21 (10/12bit) and AVR (10bit)
49-
*
50-
*/
2+
avdweb_AnalogReadFast.h
3+
4+
`avdweb_AnalogReadFast` Library
5+
================================
6+
7+
Fast analogRead 10/12 bit ADC for the Arduino Zero and Uno
8+
9+
WEBSITE: http://www.avdweb.nl/arduino/libraries/fast-10-bit-adc.html
10+
11+
Sources
12+
--------
13+
https://github.com/avdwebLibraries/avdweb_AnalogReadFast
14+
15+
License
16+
--------
17+
SPDX: MIT
18+
19+
`avdweb_AnalogReadFast` - Fast analogRead 10/12 bit ADC for the
20+
Arduino Zero and Uno
21+
22+
Copyright (C) 2019-2024 Albert van Dalen <http://www.avdweb.nl>
23+
Copyright (C) 2024 Abhijit Bose (aka. Boseji) :contributor:
24+
25+
Permission is hereby granted, free of charge, to any person obtaining
26+
a copy of this software and associated documentation files (the "Software"),
27+
to deal in the Software without restriction, including without limitation
28+
the rights to use, copy, modify, merge, publish, distribute, sublicense,
29+
and/or sell copies of the Software, and to permit persons to whom the
30+
Software is furnished to do so, subject to the following conditions:
31+
32+
The above copyright notice and this permission notice (including the
33+
next paragraph) shall be included in all copies or substantial portions
34+
of the Software.
35+
36+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
37+
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
38+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
39+
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
40+
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
41+
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
42+
OTHER DEALINGS IN THE SOFTWARE.
43+
44+
Notes
45+
------
46+
47+
HISTORY:
48+
1.0.0 7-3-2018 analogReadFast for SAMD21 (10/12bit) and AVR (10bit)
49+
50+
*/
5151

5252
#ifndef analogReadFast_H
5353
#define analogReadFast_H
5454

55-
// inline library functions must be in header
56-
int inline analogReadFast(byte ADCpin);
55+
int inline analogReadFast(byte ADCpin); // inline library functions must be in header
5756

5857
#if defined(__arm__)
5958
int inline analogReadFast(byte ADCpin)
6059
{
61-
ADC->CTRLA.bit.ENABLE = 0; // disable ADC
62-
while (ADC->STATUS.bit.SYNCBUSY == 1)
63-
; // wait for synchronization
60+
ADC->CTRLA.bit.ENABLE = 0; // disable ADC
61+
while (ADC->STATUS.bit.SYNCBUSY == 1); // wait for synchronization
6462

65-
int CTRLBoriginal = ADC->CTRLB.reg;
66-
int AVGCTRLoriginal = ADC->AVGCTRL.reg;
67-
int SAMPCTRLoriginal = ADC->SAMPCTRL.reg;
63+
int CTRLBoriginal = ADC->CTRLB.reg;
64+
int AVGCTRLoriginal = ADC->AVGCTRL.reg;
65+
int SAMPCTRLoriginal = ADC->SAMPCTRL.reg;
6866

69-
ADC->CTRLB.reg &= 0b1111100011111111; // mask PRESCALER bits
70-
ADC->CTRLB.reg |= ADC_CTRLB_PRESCALER_DIV64; // divide Clock by 64
71-
ADC->AVGCTRL.reg = ADC_AVGCTRL_SAMPLENUM_1 | // take 1 sample
72-
ADC_AVGCTRL_ADJRES(0x00ul); // adjusting result by 0
73-
ADC->SAMPCTRL.reg = 0x00; // sampling Time Length = 0
67+
ADC->CTRLB.reg &= 0b1111100011111111; // mask PRESCALER bits
68+
ADC->CTRLB.reg |= ADC_CTRLB_PRESCALER_DIV64; // divide Clock by 64
69+
ADC->AVGCTRL.reg = ADC_AVGCTRL_SAMPLENUM_1 | // take 1 sample
70+
ADC_AVGCTRL_ADJRES(0x00ul); // adjusting result by 0
71+
ADC->SAMPCTRL.reg = 0x00; // sampling Time Length = 0
7472

75-
ADC->CTRLA.bit.ENABLE = 1; // enable ADC
76-
while (ADC->STATUS.bit.SYNCBUSY == 1)
77-
; // wait for synchronization
73+
ADC->CTRLA.bit.ENABLE = 1; // enable ADC
74+
while (ADC->STATUS.bit.SYNCBUSY == 1); // wait for synchronization
7875

79-
int adc = analogRead(ADCpin);
76+
int adc = analogRead(ADCpin);
8077

81-
ADC->CTRLB.reg = CTRLBoriginal;
82-
ADC->AVGCTRL.reg = AVGCTRLoriginal;
83-
ADC->SAMPCTRL.reg = SAMPCTRLoriginal;
78+
ADC->CTRLB.reg = CTRLBoriginal;
79+
ADC->AVGCTRL.reg = AVGCTRLoriginal;
80+
ADC->SAMPCTRL.reg = SAMPCTRLoriginal;
8481

85-
return adc;
82+
return adc;
8683
}
8784
#else
85+
8886
int inline analogReadFast(byte ADCpin)
89-
{
90-
byte ADCSRAoriginal = ADCSRA;
91-
ADCSRA = (ADCSRA & B11111000) | 4;
92-
int adc = analogRead(ADCpin); // Initialize variable and burn a read
93-
adc = analogRead(ADCpin); // because ADMUX takes 2.5us to switch
94-
ADCSRA = ADCSRAoriginal;
95-
return adc;
87+
{ byte ADCSRAoriginal = ADCSRA; // Save original settings
88+
ADCSRA = (ADCSRA & B11111000) | 4; // Reduce ADC clock prescaler for faster reading
89+
int adc = analogRead(ADCpin); // Dummy analog read to stabilize
90+
adc = analogRead(ADCpin);
91+
ADCSRA = ADCSRAoriginal; // Restore original settings
92+
return adc;
9693
}
94+
9795
#endif
9896
#endif /* analogReadFast_H */

0 commit comments

Comments
 (0)