Consider the following embedded C code snippet that simulates scanning 3 ADC channels and storing their values in an array. What will be the output printed?
#include <stdio.h> int main() { int adc_values[3]; for (int ch = 0; ch < 3; ch++) { adc_values[ch] = ch * 100 + 50; // Simulated ADC reading } for (int ch = 0; ch < 3; ch++) { printf("Channel %d: %d\n", ch, adc_values[ch]); } return 0; }
Look at how the ADC value is calculated inside the loop: ch * 100 + 50.
The code assigns each channel a value calculated as ch * 100 + 50. For channels 0, 1, and 2, the values are 50, 150, and 250 respectively.
In multi-channel ADC scanning, what is the main purpose of configuring the scan sequence?
Think about how multiple channels are read one after another.
The scan sequence determines the order in which the ADC hardware converts multiple channels during a scan cycle.
Given the following code snippet for scanning 4 ADC channels, the output values are all zero. What is the most likely cause?
#define NUM_CHANNELS 4
int adc_results[NUM_CHANNELS];
void start_adc_scan() {
for (int i = 0; i < NUM_CHANNELS; i++) {
adc_results[i] = 0;
}
// Start ADC hardware scan here
}
void adc_scan_complete_callback() {
for (int i = 0; i < NUM_CHANNELS; i++) {
adc_results[i] = read_adc_channel(i);
}
}
int main() {
start_adc_scan();
// Wait for scan to complete
for (int i = 0; i < NUM_CHANNELS; i++) {
printf("Channel %d: %d\n", i, adc_results[i]);
}
return 0;
}Consider when the ADC scan completes and when the results are printed.
The main function prints adc_results immediately after starting the scan, before the callback updates the values, so all values remain zero.
Which option contains a syntax error in this ADC scan channel configuration snippet?
int channels[] = {0, 1, 2, 3};
int num_channels = 4;
for (int i = 0; i < num_channels; i++) {
configure_adc_channel(channels[i]);
}Check the syntax of the for loop header.
Option D is missing parentheses around the for loop initialization and condition, causing a syntax error.
An ADC has a sample time of 12 clock cycles per channel and a conversion time of 15 clock cycles per channel. The ADC clock runs at 1 MHz. If you scan 5 channels in sequence, what is the total time in microseconds to complete the full scan?
Total time per channel = sample time + conversion time. Multiply by number of channels. Clock frequency is 1 MHz (1 clock cycle = 1 microsecond).
Each channel takes 12 + 15 = 27 clock cycles. For 5 channels: 27 * 5 = 135 clock cycles. At 1 MHz, 1 clock cycle = 1 microsecond, so total time = 135 microseconds.