This is an old revision of the document!
Um conversor Analógico-para-Digital (ADC) transforma um valor analógico de voltagem num valor digital. Os limites para a tensão permitida numa entrada ADC do microcontrolador AVR são 0-5,5 V. O número de bits do valor digital é de 10, e a sua precisão é de ± 2 unidades. O erro pode ser ainda maior, se a tensão de alimentação do microcontrolador não estiver protegida de qualquer interferência. O AVR tem um pinos de tensão de alimentação e de tensão de comparação separados para o ADC. A tensão de alimentação separada ajuda a reduzir a interferência e pode não diferir mais de 0,3 V da tensão principal de alimentação. A comparação das tensões define o valor digital máximo. Por exemplo, se a tensão de comparação é de 3 V, então uma entrada com a mesma tensão vai ser lida como 210 - 1 (1023).
Um AVR ADC funciona segundo o princípio de aproximação sucessiva. Em suma, a tensão medida é comparada com os níveis específicos de voltagem e os resultados são reportados como uma matriz de bits. Este método é relativamente lento, pois cada bit no resultado final é calculado separadamente. O AVR gasta 13 ciclos de relógio para cada medição, com excepção do primeiro (no start-up), no qual consome 25 ciclos. Esses ciclos não são os ciclos de funcionamento do controlador, porém, mas ciclos especiais atribuídos à unidade ADC pelo divisor de frequência. A freqüência do ADC deve ser 50-200 kHz para permitir a máxima precisão. Em freqüências mais altas a precisão decai. Em alguns casos, é muito mais importante obter um grande número de leituras em vez de uma maior precisão, sendo neste caso o uso de uma frequência maior totalmente justificado. De acordo com a documentação do AVR, uma medição leva 13-260 µs.
The measured value can be read as an 8- or 10-bit value. Since AVR itself is an 8-bit device, it has two 8-bit registers for storing the ADC values. It is possible to specify in the settings whether the first two or the last two bits go to a separate register. If the two younger bits, which characterize the result less, are separated, the result can be read as an 8-bit value - a combination like that is called a left-aligned result. The other combination, where both registers are read and the value is in 10-bit format, is called a right-aligned result.
A typical AVR has 8 analog voltage input channels, ATtiny series have only a few, some ATmega devices have 16, but there is always only one converter. To make it possible to use different inputs, the device has a built-in multiplexer. The input of the multiplexer is definable using a special register. The ADC unit has a few more properties: using the processor's sleep mode for converting to reduce the interference and the option to use an internal fixed comparison voltage (usually 2.65 V, in some models 1 V).
Example
Task: measure the voltage in ADC channel 3 of an ATmega128. The voltage is in range of 0-5 V and the result should be at 8-bit precision.
#include <avr/io.h> int main() { unsigned char result; // Choose AREF pin for the comparison voltage // (it is assumed AREF is connected to the +5V supply) // Choose channel 3 in the multiplexer // Left align the result ADMUX = (1 << REFS0) | (1 << ADLAR) | (3); // Start the ADC unit, // set the conversion cycle 16 times slower than the duty cycle ADCSRA = (1 << ADEN) | (1 << ADPS2) | (1 << ADSC); // Wait for the measuring process to finish while (ADCSRA & (1 << ADSC)) continue; // Read the 8-bit value result = ADCH; }