@@ -48,6 +48,7 @@ static struct PWMContext pwmContext[PWM_COUNT] = {
48
48
static int timerEnabled = 0 ;
49
49
50
50
static uint32_t adcReference = ADC_CONFIG_REFSEL_VBG ;
51
+ static uint32_t adcPrescaling = ADC_CONFIG_INPSEL_AnalogInputOneThirdPrescaling ;
51
52
52
53
static uint32_t readResolution = 10 ;
53
54
static uint32_t writeResolution = 8 ;
@@ -80,34 +81,46 @@ static inline uint32_t mapResolution( uint32_t value, uint32_t from, uint32_t to
80
81
}
81
82
82
83
/*
83
- * Internal Reference is at 1.0v
84
- * External Reference should be between 1v and VDDANA-0.6v=2.7v
84
+ * Internal VBG Reference is 1.2 V.
85
+ * External References AREF0 and AREF1 should be between 0.83 V - 1.3 V.
85
86
*
86
- * Warning : On Arduino Zero board the input/output voltage for SAMD21G18 is 3.3 volts maximum
87
+ * Warning : ADC should not be exposed to > 2.4 V, calculated after prescaling.
88
+ * GPIO pins must not be exposed to higher voltage than VDD + 0.3 V.
87
89
*/
88
90
void analogReference ( eAnalogReference ulMode )
89
91
{
90
92
switch ( ulMode ) {
91
93
case AR_DEFAULT :
92
94
case AR_VBG :
93
95
default :
96
+ // 1.2 Reference, 1/3 prescaler = 0 V - 3.6 V range
97
+ // Minimum VDD for full range in safe operation = 3.3V
94
98
adcReference = ADC_CONFIG_REFSEL_VBG ;
99
+ adcPrescaling = ADC_CONFIG_INPSEL_AnalogInputOneThirdPrescaling ;
95
100
break ;
96
101
97
102
case AR_SUPPLY_ONE_HALF :
103
+ // 1/2 VDD Reference, 2/3 prescaler = 0 V - 0.75VDD range
98
104
adcReference = ADC_CONFIG_REFSEL_SupplyOneHalfPrescaling ;
105
+ adcPrescaling = ADC_CONFIG_INPSEL_AnalogInputTwoThirdsPrescaling ;
99
106
break ;
100
107
101
108
case AR_SUPPLY_ONE_THIRD :
109
+ // 1/3 VDD Reference, 1/3 prescaler = 0 V - VDD range
102
110
adcReference = ADC_CONFIG_REFSEL_SupplyOneThirdPrescaling ;
111
+ adcPrescaling = ADC_CONFIG_INPSEL_AnalogInputOneThirdPrescaling ;
103
112
break ;
104
113
105
114
case AR_EXT0 :
115
+ // ARF0 reference, 2/3 prescaler = 0 V - 1.5 ARF0
106
116
adcReference = ADC_CONFIG_REFSEL_External | (ADC_CONFIG_EXTREFSEL_AnalogReference0 << ADC_CONFIG_EXTREFSEL_Pos );
117
+ adcPrescaling = ADC_CONFIG_INPSEL_AnalogInputTwoThirdsPrescaling ;
107
118
break ;
108
119
109
120
case AR_EXT1 :
121
+ // ARF1 reference, 2/3 prescaler = 0 V - 1.5 ARF1
110
122
adcReference = (ADC_CONFIG_REFSEL_External | ADC_CONFIG_EXTREFSEL_AnalogReference1 << ADC_CONFIG_EXTREFSEL_Pos );
123
+ adcPrescaling = ADC_CONFIG_INPSEL_AnalogInputTwoThirdsPrescaling ;
111
124
break ;
112
125
}
113
126
}
@@ -178,7 +191,7 @@ uint32_t analogRead( uint32_t ulPin )
178
191
uint32_t config_reg = 0 ;
179
192
180
193
config_reg |= ((uint32_t )adcResolution << ADC_CONFIG_RES_Pos ) & ADC_CONFIG_RES_Msk ;
181
- config_reg |= ((uint32_t )ADC_CONFIG_RES_10bit << ADC_CONFIG_INPSEL_Pos ) & ADC_CONFIG_INPSEL_Msk ;
194
+ config_reg |= ((uint32_t )adcPrescaling << ADC_CONFIG_INPSEL_Pos ) & ADC_CONFIG_INPSEL_Msk ;
182
195
config_reg |= ((uint32_t )adcReference << ADC_CONFIG_REFSEL_Pos ) & ADC_CONFIG_REFSEL_Msk ;
183
196
184
197
if (adcReference & ADC_CONFIG_EXTREFSEL_Msk )
0 commit comments