Analog to Digital Converter

This library provides functions to use AVR analog to digital converter. All the conversion functions in library are blocking, which means the processor waits as long as it takes to get the results. The conversion time depends on the ADC clock.

Data Types

  • adc_reference
    ADC reference voltage enumeration data type. Options:
    • ADC_REF_AREF - Reference voltage from the AREF pin.
    • ADC_REF_AVCC - Reference voltage from the AVCC pin.
    • ADC_REF_2V56 - Internal 2,56 V reference voltage.
  • 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);
}
en/software/homelab/library/adc.txt · Last modified: 2020/07/20 09:00 by 127.0.0.1
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