NHBSystems / NHB_AD7794

Library for using the Analog Devices AD7794 6ch 24 bit ADC
MIT License
5 stars 3 forks source link

Calculation problems with gain 128. #4

Open an2an1970 opened 2 months ago

an2an1970 commented 2 months ago

https://github.com/NHBSystems/NHB_AD7794/blob/af71f8e0f3ee304d96244e48dc4ab5652c7b3cfa/src/NHB_AD7794.cpp#L319 https://github.com/NHBSystems/NHB_AD7794/blob/af71f8e0f3ee304d96244e48dc4ab5652c7b3cfa/src/NHB_AD7794.h#L48C2-L48C9

This formula cannot work correctly with a gain of 128, because it will overflow when multiplying. The constant should be declared unsigned (16777216u). Then everything will be ok.

#define AD7794_ADC_MAX_UP     16777216
result = (adcRaw * Channel[currentCh].vRef) / (AD7794_ADC_MAX_UP * Channel[currentCh].gain);            //Unipolar formula
jjuliano77 commented 4 days ago

Fixed. Thanks for the suggestion.