This is an old revision of the document!


Conversor Analógico-para-Digital

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).

AVR ADC works on the principal of successive approximation. In short, the measured voltage is compared to specific voltage levels and the results are reported as a bit array. This method is relatively slow, as each bit in the final result is calculated separately. AVR spends 13 clock cycles for each measuring, except the first (on start-up), which takes 25 cycles. These cycles are not the controller's duty cycles though, but instead special cycles allocated to the ADC unit by the frequency divider. The ADC frequency should be 50-200 kHz to achieve maximum precision, on higher frequencies the precision fades. In some cases it is more important to get a large number of readings instead of maximum precision, in which case using a larger frequency is totally justified. According to AVR documentation, one measuring takes 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;
}
pt/avr/adc.1448970195.txt.gz · Last modified: 2020/07/20 09:00 (external edit)
CC Attribution-Share Alike 4.0 International
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0