Bird
Raised Fist0
Embedded Cprogramming~20 mins

Multi-channel ADC scanning in Embedded C - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
ADC Scan Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of multi-channel ADC scan loop

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?

Embedded C
#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;
}
A
Channel 0: 50
Channel 1: 100
Channel 2: 150
B
Channel 0: 0
Channel 1: 100
Channel 2: 200
C
Channel 0: 50
Channel 1: 150
Channel 2: 250
D
Channel 0: 100
Channel 1: 200
Channel 2: 300
Attempts:
2 left
💡 Hint

Look at how the ADC value is calculated inside the loop: ch * 100 + 50.

🧠 Conceptual
intermediate
1:30remaining
Understanding ADC scan sequence configuration

In multi-channel ADC scanning, what is the main purpose of configuring the scan sequence?

ATo define the order in which ADC channels are converted during a scan
BTo set the voltage reference for the ADC conversion
CTo adjust the ADC clock speed for faster conversions
DTo enable interrupts after each individual channel conversion
Attempts:
2 left
💡 Hint

Think about how multiple channels are read one after another.

🔧 Debug
advanced
2:30remaining
Identify the cause of incorrect ADC values in multi-channel scan

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;
}
AThe main function prints adc_results before the scan completes and callback updates values
BThe read_adc_channel function is not implemented correctly
CThe adc_results array is not initialized before scanning
DThe start_adc_scan function does not clear previous results
Attempts:
2 left
💡 Hint

Consider when the ADC scan completes and when the results are printed.

📝 Syntax
advanced
1:30remaining
Identify the syntax error in ADC scan configuration

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]);
}
Afor (int i = 0; i < num_channels; i++) { configure_adc_channel(channels[i]); }
B} ;)]i[slennahc(lennahc_cda_erugifnoc { )++i ;slennahc_mun < i ;0 = i tni( rof
Cfor (int i = 0; i < num_channels; i++) configure_adc_channel(channels[i]);
Dfor int i = 0; i < num_channels; i++ { configure_adc_channel(channels[i]); }
Attempts:
2 left
💡 Hint

Check the syntax of the for loop header.

🚀 Application
expert
3:00remaining
Calculate total ADC scan time for multiple channels

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?

A60 microseconds
B135 microseconds
C75 microseconds
D1350 microseconds
Attempts:
2 left
💡 Hint

Total time per channel = sample time + conversion time. Multiply by number of channels. Clock frequency is 1 MHz (1 clock cycle = 1 microsecond).

Practice

(1/5)
1.

What is the main purpose of multi-channel ADC scanning in embedded C?

easy
A. To read multiple analog inputs one after another automatically
B. To write digital output signals to multiple pins
C. To increase the clock speed of the microcontroller
D. To store data in external memory

Solution

  1. Step 1: Understand ADC scanning concept

    Multi-channel ADC scanning reads several analog inputs sequentially without manual switching.
  2. Step 2: Identify the purpose

    This allows efficient sensor data collection from multiple sources automatically.
  3. Final Answer:

    To read multiple analog inputs one after another automatically -> Option A
  4. Quick Check:

    Multi-channel ADC scanning = reading multiple analog inputs [OK]
Hint: Remember ADC scanning reads many inputs in sequence [OK]
Common Mistakes:
  • Confusing ADC scanning with digital output control
  • Thinking it speeds up the microcontroller clock
  • Assuming it stores data externally
2.

Which of the following code snippets correctly initializes a multi-channel ADC scan for 3 channels in embedded C?

void ADC_Init() {
    ADCON0 = 0x01; // Enable ADC
    ADCON1 = 0x00; // Configure ports as analog
    ADCON2 = 0xA9; // Right justify, 12 TAD, Fosc/8
    // Missing code here
}
easy
A. ADCHS = 0x00; // Select channel 0 only
B. ADCHS = 0x03; // Select channels 0 and 1 only
C. ADCHS = 0xFF; // Select all channels
D. ADCHS = 0x07; // Select channels 0 to 2 for scanning

Solution

  1. Step 1: Understand ADCHS register role

    ADCHS selects which ADC channels to scan; bits 0-2 correspond to channels 0-2.
  2. Step 2: Choose correct bitmask for 3 channels

    0x07 in binary is 00000111, enabling channels 0,1,2 for scanning.
  3. Final Answer:

    ADCHS = 0x07; // Select channels 0 to 2 for scanning -> Option D
  4. Quick Check:

    0x07 = channels 0,1,2 selected [OK]
Hint: Use bitmask 0x07 to select first 3 ADC channels [OK]
Common Mistakes:
  • Selecting only one channel instead of multiple
  • Using 0xFF which may select invalid channels
  • Confusing ADCHS with ADCON registers
3.

Consider the following embedded C code snippet for multi-channel ADC scanning:

int adc_values[3];
for(int ch = 0; ch < 3; ch++) {
    ADCON0 = (ch << 2) | 0x01; // Select channel and turn on ADC
    __delay_ms(2); // Acquisition time
    ADCON0 |= 0x02; // Start conversion
    while(ADCON0 & 0x02); // Wait for conversion
    adc_values[ch] = (ADRESH << 8) | ADRESL;
}

What will be the content of adc_values after this loop?

medium
A. Array with zeros because ADC is not started
B. Array with ADC readings from channels 0, 1, and 2
C. Array with same value repeated 3 times
D. Compilation error due to wrong bit operations

Solution

  1. Step 1: Analyze loop operation

    The loop runs for ch = 0 to 2, selecting each ADC channel and starting conversion.
  2. Step 2: Understand ADC reading storage

    After conversion, the 10-bit ADC result is stored in adc_values[ch] for each channel.
  3. Final Answer:

    Array with ADC readings from channels 0, 1, and 2 -> Option B
  4. Quick Check:

    Loop reads channels 0-2 storing results in array [OK]
Hint: Loop reads and stores ADC values channel-wise [OK]
Common Mistakes:
  • Assuming ADC conversion never starts
  • Thinking all values are identical
  • Misreading bitwise operations as errors
4.

Identify the error in this multi-channel ADC scanning code snippet:

for(int i = 0; i < 3; i++) {
    ADCON0 = (i << 2); // Select channel
    ADCON0 |= 0x01; // Turn on ADC
    ADCON0 |= 0x02; // Start conversion
    while(ADCON0 & 0x02);
    adc_results[i] = (ADRESH << 8) | ADRESL;
}
medium
A. The ADC result is stored incorrectly
B. The conversion start bit is never set
C. ADCON0 is overwritten before turning on ADC, losing previous bits
D. The loop runs one time too many

Solution

  1. Step 1: Check ADCON0 assignment

    ADCON0 is assigned with (i << 2), which clears other bits including ADC enable.
  2. Step 2: Understand bitwise OR usage

    Turning on ADC with |= 0x01 works, but previous bits are lost due to overwrite.
  3. Final Answer:

    ADCON0 is overwritten before turning on ADC, losing previous bits -> Option C
  4. Quick Check:

    Overwriting ADCON0 clears important bits [OK]
Hint: Use |= to set bits, not = to overwrite [OK]
Common Mistakes:
  • Ignoring that ADCON0 bits get reset
  • Thinking start bit is not set
  • Miscounting loop iterations
  • Misunderstanding ADC result storage
5.

You want to scan 4 analog sensors using a microcontroller ADC that supports hardware scanning. Which approach best improves efficiency?

// Option 1: Manually switch channels in software loop
// Option 2: Enable hardware scan mode and read results from buffer
// Option 3: Use single channel repeatedly
// Option 4: Use digital inputs instead
hard
A. Use hardware scan mode and read results from buffer
B. Manually switch channels in software loop
C. Use single channel repeatedly with delays
D. Use digital inputs instead of ADC

Solution

  1. Step 1: Compare manual vs hardware scanning

    Manual switching requires CPU cycles and delays; hardware scan automates channel switching.
  2. Step 2: Identify efficiency gain

    Hardware scan mode reads multiple channels automatically, storing results in buffer for quick access.
  3. Step 3: Evaluate other options

    Using single channel repeatedly or digital inputs does not meet multi-channel analog reading needs efficiently.
  4. Final Answer:

    Use hardware scan mode and read results from buffer -> Option A
  5. Quick Check:

    Hardware scan mode = efficient multi-channel ADC reading [OK]
Hint: Hardware scan automates multi-channel ADC reading [OK]
Common Mistakes:
  • Choosing manual switching which wastes CPU time
  • Using single channel repeatedly causing delays
  • Confusing digital inputs with analog ADC inputs
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard