Skip to content

Commit 0c0a09a

Browse files
dhalberttannewt
authored andcommitted
Speed up AnalogIn.value.
1 parent 6d603e4 commit 0c0a09a

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

atmel-samd/common-hal/analogio/AnalogIn.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,10 @@ uint16_t common_hal_analogio_analogin_get_value(analogio_analogin_obj_t *self) {
7474
config_adc.reference = ADC_REFERENCE_INTVCC1;
7575
config_adc.gain_factor = ADC_GAIN_FACTOR_DIV2;
7676
config_adc.positive_input = self->pin->adc_input;
77-
config_adc.resolution = ADC_RESOLUTION_16BIT;
78-
config_adc.clock_prescaler = ADC_CLOCK_PRESCALER_DIV128;
77+
config_adc.resolution = ADC_RESOLUTION_12BIT;
78+
// Default input clock is GCLK0 (48 MHz)
79+
// 48Mhz / 32 = 1.5MHz. Max ADC clock is 2.1MHz
80+
config_adc.clock_prescaler = ADC_CLOCK_PRESCALER_DIV32;
7981

8082
struct adc_module adc_instance;
8183
// ADC must have been disabled before adc_init() is called.
@@ -108,7 +110,8 @@ uint16_t common_hal_analogio_analogin_get_value(analogio_analogin_obj_t *self) {
108110
}
109111

110112
adc_disable(&adc_instance);
111-
return data;
113+
// Scale to 16 bits. In the future we might make this be this be under API control.
114+
return data * 16;
112115
}
113116

114117
float common_hal_analogio_analogin_get_reference_voltage(analogio_analogin_obj_t *self) {

atmel-samd/common-hal/microcontroller/Processor.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,9 @@ STATIC void configure_adc_temp(struct adc_module *adc_instance) {
111111
// "Discard the first conversion result whenever there is a change
112112
// in ADC configuration like voltage reference / ADC channel change."
113113

114-
config_adc.clock_prescaler = ADC_CLOCK_PRESCALER_DIV16;
114+
// Default input clock is GCLK0 (48 MHz)
115+
// 48Mhz / 32 = 1.5MHz. Max ADC clock is 2.1MHz
116+
config_adc.clock_prescaler = ADC_CLOCK_PRESCALER_DIV32;
115117
config_adc.reference = ADC_REFERENCE_INT1V;
116118
config_adc.positive_input = ADC_POSITIVE_INPUT_TEMP;
117119
config_adc.negative_input = ADC_NEGATIVE_INPUT_GND;

0 commit comments

Comments
 (0)