This is an old revision of the document!


Conversor Analógico para Digital

Esta biblioteca fornece funções para usar o conversor analógico para digital do AVR. Todas as funções desta biblioteca bloqueiam o processador até chegarem ao resultado. O tempo de conversão depende do relógio ADC.

Tipos de dados

  • adc_reference
    Enumeração dos tipos para voltagem de referência do ADC. Opções:
  • ADC_REF_AREF - Voltagem de referência do pin AREF.
  • ADC_REF_AVCC - Voltagem de referência do pin AVCC.
  • ADC_REF_2V56 - Voltagem de referência interna - 2,56 V.
  • adc_prescale
    ADC clock prescaler enumration data type. It determines the division factor of system clock. Options:
    • ADC_PRESCALE_2 - Division factor 2.
    • ADC_PRESCALE_4 - Division factor 4.
    • ADC_PRESCALE_8 - Division factor 8.
    • ADC_PRESCALE_16 - Division factor 16.
    • ADC_PRESCALE_32 - Division factor 32.
    • ADC_PRESCALE_64 - Division factor 64.
    • ADC_PRESCALE_128 - Division factor 128.

Functions

  • void adc_init(adc_reference reference, adc_prescale prescale)

Initializes ADC. Parameters:

  • reference - Reference voltage selection.
  • prescale - Clock prescaler selection.
  • unsigned short adc_get_value(unsigned char channel)

Converts specified ADC channel analog value to digital. Function is blocking. Parameter:

  • channel - ADC channel number (0 to 7).
  • Return 10-bit digital value.
  • unsigned short adc_get_average_value(unsigned char channel, unsigned char num_samples)

Converts specified ADC channel analog value to digital desired number of times and calculates the average. Function is blocking. Parameters:

  • channel - ADC channel number (0 to 7).
  • num_samples - Number of samples for calculation (1 to 64).
  • Return 10-bit digital value.

Example

For example ADC is initialized and two analog channel values are converted to digital. Value of channel 0 is assigned to variable x and averaged value of channel 1 to variable y.

#include <homelab/adc.h>
 
int main(void)
{
	unsigned short x, y;
 
	// Initializing ADC. Reference voltage from AVCC.
	// Clock is 8 times slower than system clock.
	adc_init(ADC_REF_AVCC, ADC_PRESCALE_8);
 
	// Converting channel 0 value.
	x = adc_get_value(0);
 
	// Converting and averaging channel 1 value.
	y = adc_get_average_value(1, 10);
}
pt/software/homelab/library/adc.1449508887.txt.gz · Last modified: (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