Skip to content

Commit daf34ae

Browse files
committed
Fixed analogReadResolution behaviour
1 parent 5e4be6d commit daf34ae

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

cores/arduino/wiring_analog.c

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ extern "C" {
2525
#endif
2626

2727
static int _readResolution = 10;
28-
static int _writeResolution = 10;
28+
static int _ADCResolution = 10;
29+
static int _writeResolution = 8;
2930

3031
// Wait for synchronization of registers between the clock domains
3132
static __inline__ void syncADC() __attribute__((always_inline, unused));
@@ -43,20 +44,23 @@ static void syncDAC() {
4344

4445
void analogReadResolution( int res )
4546
{
47+
_readResolution = res ;
4648
syncADC();
47-
switch ( res )
49+
if (res > 10)
4850
{
49-
case 12:
50-
ADC->CTRLB.bit.RESSEL = ADC_CTRLB_RESSEL_12BIT_Val;
51-
break;
52-
case 8:
53-
ADC->CTRLB.bit.RESSEL = ADC_CTRLB_RESSEL_8BIT_Val;
54-
break;
55-
default:
56-
ADC->CTRLB.bit.RESSEL = ADC_CTRLB_RESSEL_10BIT_Val;
57-
break;
51+
ADC->CTRLB.bit.RESSEL = ADC_CTRLB_RESSEL_12BIT_Val;
52+
_ADCResolution = 12;
53+
}
54+
else if (res > 8)
55+
{
56+
ADC->CTRLB.bit.RESSEL = ADC_CTRLB_RESSEL_10BIT_Val;
57+
_ADCResolution = 10;
58+
}
59+
else
60+
{
61+
ADC->CTRLB.bit.RESSEL = ADC_CTRLB_RESSEL_8BIT_Val;
62+
_ADCResolution = 8;
5863
}
59-
_readResolution = res ;
6064
}
6165

6266
void analogWriteResolution( int res )
@@ -179,7 +183,7 @@ uint32_t analogRead( uint32_t ulPin )
179183
ADC->CTRLA.bit.ENABLE = 0x00; // Disable ADC
180184
syncADC();
181185

182-
return valueRead;
186+
return mapResolution(valueRead, _ADCResolution, _readResolution);
183187
}
184188

185189

0 commit comments

Comments
 (0)