10000 Add support for analogReadResolution, default 10b (#461) · gsexton/arduino-pico@bbae702 · GitHub
[go: up one dir, main page]

Skip to content

Commit bbae702

Browse files
Add support for analogReadResolution, default 10b (earlephilhower#461)
Most other boards and the MBED RP2040 support analogReadResolution which just shifts read data around as needed, with a default of only 10b of resolution. The Pico ADC technically supports 12b, but only has about 8b of real data after noise, so you're not really losing anything in the general case. Fixes earlephilhower#460
1 parent c404660 commit bbae702

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

cores/rp2040/Arduino.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ void noInterrupts();
7070
#define portModeRegister(port) ((volatile uint32_t*) sio_hw->gpio_oe)
7171

7272
// ADC RP2040-specific calls
73+
void analogReadResolution(int bits);
7374
float analogReadTemp(); // Returns core temp in Centigrade
7475

7576
// PWM RP2040-specific calls

cores/rp2040/wiring_analog.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ extern "C" void analogWrite(pin_size_t pin, int val) {
117117
}
118118

119119
auto_init_mutex(_adcMutex);
120+
static int _readBits = 10;
120121

121122
extern "C" int analogRead(pin_size_t pin) {
122123
CoreMutex m(&_adcMutex);
@@ -133,7 +134,7 @@ extern "C" int analogRead(pin_size_t pin) {
133134
}
134135
adc_gpio_init(pin);
135136
adc_select_input(pin - minPin);
136-
return adc_read();
137+
return (_readBits < 12) ? adc_read() >> (12 - _readBits) : adc_read() << (_readBits - 12);
137138
}
138139

139140
extern "C" float analogReadTemp() {
@@ -153,3 +154,10 @@ extern "C" float analogReadTemp() {
153154
float t = 27.0f - ((v * 3.3f / 4096.0f) - 0.706f) / 0.001721f; // From the datasheet
154155
return t;
155156
}
157+
158+
extern "C" void analogReadResolution(int bits) {
159+
CoreMutex m(&_adcMutex);
160+
if (m && ((bits > 0) && (bits < 32))) {
161+
_readBits = bits;
162+
}
163+
}

0 commit comments

Comments
 (0)
0