Consider this embedded C code simulating an ADC sample and hold process. What will be printed?
#include <stdio.h> int main() { int analog_signal = 512; // Simulated analog input (10-bit ADC max 1023) int sample_and_hold = 0; int digital_value = 0; // Sample phase sample_and_hold = analog_signal; // Hold phase (conversion simulated) digital_value = sample_and_hold; printf("Digital value: %d\n", digital_value); return 0; }
Think about what the sample and hold phase does to the analog signal before conversion.
The sample and hold phase captures the analog input value (512) and holds it steady for conversion. The digital value printed is the held sample, which is 512.
Analyze this code snippet simulating ADC sample and hold. What error will it produce?
#include <stdio.h> int main() { int *sample_and_hold = NULL; int analog_signal = 256; // Sample phase *sample_and_hold = analog_signal; // Attempt to store value printf("Value: %d\n", *sample_and_hold); return 0; }
What happens if you try to write to a NULL pointer?
The pointer sample_and_hold is NULL and dereferencing it causes a segmentation fault at runtime.
Choose the correct description of the sample and hold phase in an ADC conversion process.
Think about why the ADC needs a stable input during conversion.
The sample and hold circuit captures the analog voltage and holds it constant so the ADC can convert it accurately without changes during conversion.
Given this code simulating ADC sample and hold timing, what will be the output?
#include <stdio.h> #include <stdbool.h> int main() { int analog_signal = 700; int sample_and_hold = 0; bool sample_phase = true; if (sample_phase) { sample_and_hold = analog_signal; sample_phase = false; } // Hold phase int digital_value = sample_and_hold; printf("Digital value: %d\n", digital_value); return 0; }
Check what happens during the sample phase before hold.
The sample phase stores the analog signal (700) into sample_and_hold. The hold phase outputs this value, so the printed digital value is 700.
Analyze this embedded C code simulating an ADC sample and hold with noise. What is the final value of digital_value?
#include <stdio.h> #include <stdlib.h> #include <time.h> int main() { srand(0); int analog_signal = 400; int sample_and_hold = 0; int noise = 0; // Sample phase with noise noise = rand() % 5 - 2; // Noise between -2 and +2 sample_and_hold = analog_signal + noise; // Hold phase int digital_value = sample_and_hold; printf("Digital value: %d\n", digital_value); return 0; }
Check the random noise generated with srand(0) and rand() % 5 - 2.
With srand(0), the first rand() % 5 is 1, so noise = 1 - 2 = -1. The sample_and_hold = 400 + (-1) = 399.