Skip to content

Commit 9ebdfe8

Browse files
samples: adc_sequence: differential mode handling in ADC conversion
This commit updates the ADC sequence sample to improve handling of differential mode readings. It modifies the conversion logic to correctly interpret 16-bit/32-bit signed values and adjusts the resolution accordingly. This ensures accurate millivolt conversion for both differential and single-ended configurations. Signed-off-by: Martin Hoff <[email protected]>
1 parent 1dd4a22 commit 9ebdfe8

File tree

1 file changed

+21
-4
lines changed
  • samples/drivers/adc/adc_sequence/src

1 file changed

+21
-4
lines changed

samples/drivers/adc/adc_sequence/src/main.c

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,30 @@ int main(void)
9292
CONFIG_SEQUENCE_SAMPLES);
9393
for (size_t sample_index = 0U; sample_index < CONFIG_SEQUENCE_SAMPLES;
9494
sample_index++) {
95-
96-
val_mv = channel_reading[sample_index][channel_index];
97-
95+
uint8_t res = CONFIG_SEQUENCE_RESOLUTION;
96+
97+
/*
98+
* If using differential mode, the 16/32 bit value
99+
* in the ADC sample buffer should be a signed 2's
100+
* complement value.
101+
* Also reduce the resolution by 1 for the conversion
102+
*/
103+
if (channel_cfgs[channel_index].differential) {
104+
#ifdef CONFIG_SEQUENCE_32BITS_REGISTERS
105+
val_mv = (int32_t)
106+
channel_reading[sample_index][channel_index];
107+
#else
108+
val_mv = (int32_t)((int16_t)channel_reading[sample_index]
109+
[channel_index]);
110+
#endif
111+
res -= 1;
112+
} else {
113+
val_mv = channel_reading[sample_index][channel_index];
114+
}
98115
printf("- - %" PRId32, val_mv);
99116
err = adc_raw_to_millivolts(vrefs_mv[channel_index],
100117
channel_cfgs[channel_index].gain,
101-
CONFIG_SEQUENCE_RESOLUTION, &val_mv);
118+
res, &val_mv);
102119

103120
/* conversion to mV may not be supported, skip if not */
104121
if ((err < 0) || vrefs_mv[channel_index] == 0) {

0 commit comments

Comments
 (0)