This is an old revision of the document!


Convertisseur analogique - numérique

Le convertisseur analogique numérique (ADC) transforme une valeur de tension analogique en une valeur numérique. Le niveau de tension autorisé en entrée de l'ADC d'un micro-contrôleur est entre 0 et 5.5V. La taille de la valeur numérique est de 10 bits, mais sa précision est de ±2 unités. L'erreur pourrait être plus importante, si l'énergie d'alimentation du micro-contrôleur n'est pas protégée contre les interférences. Un AVR contient des broches différentes pour la tension d'alimentation et la tension de comparaison. La tension d'alimentation séparée aide à contrer les interférences et ne doit pas être différent de 0.3V de différence avec la tension d'alimentation. La tension de comparaison défini la valeur de tension maximale. Par exemple, si la tension de comparaison est de 3V par rapport à une entrée de même tension sera lue comme 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;
}
fr/avr/adc.1269269148.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