rcc 1.0
RCC - RGB LED Color Cube Documentation
Loading...
Searching...
No Matches
adc.c File Reference

AVR ADC driver implementation for initialization, configuration, and reading. More...

#include "adc.h"
Include dependency graph for adc.c:

Functions

void adc_init (void)
 Initialize the ADC peripheral with pre-configured settings.
void adc_disable (void)
 Disable the ADC module.
void adc_channel (ADC_Channel channel)
 Select the ADC input channel.
void adc_accumulation (ADC_Accumulation samples)
 Set the ADC sample accumulation mode.
unsigned int adc_read (void)
 Perform a single ADC conversion and return the result (polling mode).
unsigned int adc_average (unsigned char samples)
 Perform multiple ADC conversions and return the average result (calculated in software).

Detailed Description

AVR ADC driver implementation for initialization, configuration, and reading.

This source file provides functions to initialize the ADC peripheral on AVR microcontrollers, configure ADC input channels and sample accumulation, enable or disable the ADC, and read ADC conversion results either by polling or averaging multiple samples.

It supports configuration of ADC reference voltage, resolution, prescaler, sample delays, and accumulation modes. Interrupt-driven ADC operation is supported externally through user-defined interrupt handlers.

Author
g.raf
Date
2025-09-18
Version
1.0 Release
Note
This file is part of a larger embedded systems project and subject to the license specified in the repository. For updates and the complete revision history, see the GitHub repository.
See also
https://github.com/0x007e/avr0 "AVR GitHub Repository"

Function Documentation

◆ adc_accumulation()

void adc_accumulation ( ADC_Accumulation samples)

Set the ADC sample accumulation mode.

Parameters
samplesThe ADC_Accumulation enum value specifying how many ADC samples to accumulate and average.

This function configures the ADC to accumulate multiple samples before producing a conversion result, which helps reduce noise and improve measurement accuracy. The accumulation count is set by writing to the ADC control register B.

Note
The ADC must be configured and enabled for the accumulation mode to take effect.

◆ adc_average()

unsigned int adc_average ( unsigned char samples)

Perform multiple ADC conversions and return the average result (calculated in software).

Parameters
samplesNumber of ADC samples to average.

This function reads the ADC conversion result multiple times by calling adc_read(), sums the results, and calculates the average value to reduce noise and improve stability. It returns the averaged ADC value as an unsigned int.

Note
This method provides a simple software averaging filter for ADC measurements.
Returns
The averaged ADC conversion result.
Here is the call graph for this function:

◆ adc_channel()

void adc_channel ( ADC_Channel channel)
inline

Select the ADC input channel.

Parameters
channelADC channel to select from the ADC_Channel enumeration.

This inline function sets the ADC multiplexer positive input selection register to the specified channel, masking and shifting the channel value appropriately. The channel selects which analog input pin or internal reference the ADC samples.

Note
Ensure the channel value is valid and corresponds to a supported ADC input.
Here is the caller graph for this function:

◆ adc_disable()

void adc_disable ( void )
inline

Disable the ADC module.

This inline function disables the ADC by clearing the enable bit in the ADC control register. Disabling the ADC conserves power when ADC functionality is not required.

Note
It is recommended to disable the ADC before entering low-power modes to minimize current consumption.
Here is the caller graph for this function:

◆ adc_init()

void adc_init ( void )

Initialize the ADC peripheral with pre-configured settings.

Configures the ADC control registers for capacitance, reference voltage, prescaler, sample delay variation, initial delay, and sample length according to compile-time macros. Enables the ADC with the selected resolution. If the ADC interrupt mode is enabled, the ADC result ready interrupt is also enabled. When using the internal voltage reference, the voltage reference control register is configured accordingly. This function must be called before starting any ADC conversions to ensure proper setup.

Note
Interrupt vectors and ADC interrupt handling routines must be implemented separately when enabling interrupts.
Here is the caller graph for this function:

◆ adc_read()

unsigned int adc_read ( void )

Perform a single ADC conversion and return the result (polling mode).

Starts an ADC conversion by setting the START command bit in the ADC command register. The function then polls the command register until the conversion completes by checking the START bit. After completion, it returns the 16-bit (10-bit ADC result) value from the ADC result register.

Note
This function blocks execution until the conversion is finished.
Returns
The ADC conversion result as an unsigned int.
Here is the caller graph for this function: