#include <stdio.h>
#include <stdint.h>
typedef union {
struct {
uint16_t adc_value : 12;
uint16_t channel : 4;
};
uint16_t adc_reg;
} ADC_Result;
int main() {
uint16_t reg;
scanf("%hx", ®); // Read hexadecimal input
ADC_Result result;
result.adc_reg = reg;
printf("Channel: %u\n", result.channel);
printf("ADC Value: %u", result.adc_value);
return 0;
}
What is this about?
This mimics a real ADC result register layout where raw analog values and their source channel are packed into one word.
Why it’s important in firmware?
Solution Logic
Input
0xC3F5
Expected Output
Channel: 12 ADC Value: 1013