From ac544de10e3d1e97bc75acf81b9c638a93c32675 Mon Sep 17 00:00:00 2001 From: Ulrich Kuehn Date: Mon, 25 Oct 2021 20:50:11 +0200 Subject: [PATCH 1/2] Fix 2's complement calculation for Vsense. --- userial/ad26.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/userial/ad26.c b/userial/ad26.c index 6186ede..497c620 100644 --- a/userial/ad26.c +++ b/userial/ad26.c @@ -251,10 +251,17 @@ float Volt_Reading(int portnum, int vdd, int *cad) if(cad) { /* Get Current reading as well */ + /* convert from 12-bit 2's complement, see + * https://en.wikipedia.org/wiki/Two%27s_complement + */ c = send_block[8] & 0x3; - *cad = (c << 8) | send_block[7]; - if(send_block[8] & 0x4) *cad = - *cad; + if(send_block[8] & 0x4) { + /* Negative value represented by set sign bit + * with weight of -2^12 + */ + *cad = *cad - 4096; + } // printf("CAD=%d\n", *cad); } From 4d64342a8b1d2b07578e7510d3dd1438e67d2d1e Mon Sep 17 00:00:00 2001 From: Ulrich Kuehn Date: Sun, 12 Mar 2023 17:19:46 +0100 Subject: [PATCH 2/2] Use correct register width for current register. --- userial/ad26.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/userial/ad26.c b/userial/ad26.c index 497c620..4fdc104 100644 --- a/userial/ad26.c +++ b/userial/ad26.c @@ -253,14 +253,22 @@ float Volt_Reading(int portnum, int vdd, int *cad) /* Get Current reading as well */ /* convert from 12-bit 2's complement, see * https://en.wikipedia.org/wiki/Two%27s_complement + * According to the DS2438 datasheet the current register + * has 10 significant bits (Page 6, Table 3). However, + * the stated conversion formulae seem to hint at + * 12 bits. Nevertheless, the stated voltage unit of + * 0.2441mV per digital unit and the maximum voltage + * rating on the Vsense pins of 250mV are consistent with + * a 10-bit register format plus sign bits. This is + * what we use here. */ c = send_block[8] & 0x3; *cad = (c << 8) | send_block[7]; if(send_block[8] & 0x4) { /* Negative value represented by set sign bit - * with weight of -2^12 + * with weight of -2^10 */ - *cad = *cad - 4096; + *cad = *cad - 1024; } // printf("CAD=%d\n", *cad);