Zápisník experimentátora
Hierarchy: A/D prevodník
The A/D converter in Arduino Uno has a resolution of 10 bits. If you do not need such a large resolution, we can also use a resolution of 8 bits. This will allow us to convert the analog value to a digital value that has exactly 8 bits. This is done by adjusting bit alignment in the resulting 16-bit register. Using an 8 bit A / D conversion has the advantage that we do not have to comply with the recommended 50 kHz - 200 kHz conversion frequency and we can also use higher frequencies.
The ADLAR bit setting adjusts the alignment of the results.
bit | 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 |
ADCH | - | - | - | - | - | - | ADC9 | ADC8 |
ADCL | ADC7 | ADC6 | ADC5 | ADC4 | ADC3 | ADC2 | ADC1 | ADC0 |
bit | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
bit | 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 |
ADCH | ADC9 | ADC8 | ADC7 | ADC6 | ADC5 | ADC4 | ADC3 | ADC2 |
ADCL | ADC1 | ADC0 | - | - | - | - | - | - |
bit | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
The example is almost the same as the previous examples (A/D converter without blocking). It varies only in the ADMUX.ADLAR
bit setting that adjusts the alignment of the result. With this setting, just read the contents of the ADCH
registry to get the resulting 8 bit result.
const byte adcPin = A0; // = 14 (pins_arduino.h)
bool adc_conversion_working = false;
void setup() {
Serial.begin(115200);
Serial.println("ADC without blocking 8bit");
ADCSRA = bit(ADEN) // Turn ADC on
| bit(ADPS0) | bit(ADPS1) | bit(ADPS2); // Prescaler of 128
ADMUX = bit(REFS0) // AVCC
| bit(ADLAR) // ADC Left Adjust Result
| ((adcPin - 14) & 0x07); // Arduino Uno to ADC pin
}
void loop() {
if (!adc_conversion_working) {
bitSet(ADCSRA, ADSC); // Start a conversion
adc_conversion_working = true;
}
// The ADC clears the bit when done
if (bit_is_clear(ADCSRA, ADSC)) {
int value = ADCH; // Read result
adc_conversion_working = false;
Serial.println(value);
delay(500);
}
}
The source code is located on the GitHub server.
03.11.2018