0
0
Embedded Cprogramming~5 mins

ADC conversion process (sample and hold) in Embedded C

Choose your learning style9 modes available
Introduction

The ADC conversion process changes an analog signal into a digital number. The sample and hold step captures the signal at a moment so the ADC can read it accurately.

When reading sensor values like temperature or light that change over time.
When you need to convert a smooth voltage into a number for a microcontroller.
When you want to measure a signal at a specific instant without it changing during conversion.
When programming microcontrollers to read analog inputs for control or monitoring.
Syntax
Embedded C
void ADC_SampleAndHold_Start(void);
uint16_t ADC_ReadValue(void);

ADC_SampleAndHold_Start() begins the sample and hold process.

ADC_ReadValue() returns the digital value after conversion.

Examples
Start sampling and then read the converted digital value.
Embedded C
ADC_SampleAndHold_Start();
uint16_t value = ADC_ReadValue();
Wait until ADC conversion is done before reading the value.
Embedded C
while(!ADC_ConversionComplete()) {
    // wait for conversion to finish
}
uint16_t result = ADC_ReadValue();
Sample Program

This program simulates the ADC sample and hold process. It starts sampling, waits until conversion is done, then prints the digital value.

Embedded C
#include <stdint.h>
#include <stdio.h>

// Simulated ADC hardware registers
volatile uint16_t ADC_DATA = 0;
volatile int ADC_BUSY = 0;

// Start sample and hold process
void ADC_SampleAndHold_Start(void) {
    ADC_BUSY = 1; // ADC busy sampling
    // Simulate sampling delay
    // In real hardware, this would be automatic
    ADC_DATA = 512; // Example analog value converted
    ADC_BUSY = 0; // Sampling done
}

// Check if conversion is complete
int ADC_ConversionComplete(void) {
    return ADC_BUSY == 0;
}

// Read ADC converted value
uint16_t ADC_ReadValue(void) {
    return ADC_DATA;
}

int main(void) {
    ADC_SampleAndHold_Start();
    while(!ADC_ConversionComplete()) {
        // wait for ADC to finish
    }
    uint16_t digitalValue = ADC_ReadValue();
    printf("ADC digital value: %u\n", digitalValue);
    return 0;
}
OutputSuccess
Important Notes

The sample and hold step freezes the input voltage so the ADC can convert it without changes.

In real microcontrollers, the sample and hold is often automatic inside the ADC hardware.

Always wait for the conversion to finish before reading the ADC value to get correct data.

Summary

ADC sample and hold captures the analog signal at a moment for accurate conversion.

Start sampling, wait for conversion, then read the digital result.

This process is essential for reading sensors with microcontrollers.