temperature calculation can be improved.
in code example, Adc_I1 and Adc_I2 are int32. Using float would avoid to truncate the decimal part. and would produce a more precise CurrentRatio
int32 Adc_I1, Adc_I2
/* Take average of 8 readings */
Adc_I1 = Adc_I1 / 8;
Adc_I2 = Adc_I2 / 8;
/* Subtract the ADC offset from the measured values */
Adc_I1 = Adc_I1 - AdcOffset;
Adc_I2 = Adc_I2 - AdcOffset;
/* IDAC Current ratio is the ratio of the ADC counts */
CurrentRatio = (float)Adc_I2 / (float)Adc_I1;
later in GetDiodeTemperature()
same problem. Vbe1_count and Vbe2_count are int32 and divided by 8 before convertion to float
/* Take average of 8 readings. Divide by 8 is done as Right shift by 3 to speed up computation */
Vbe1_count = Vbe1_count >> 3;
Vbe2_count = Vbe2_count >> 3;
Vbe_diff = (Vbe2_count - Vbe1_count);
diode_temperature =(Vbe_diff * multiply_factor) - ZERO_DEGREE_CELSIUS_KELVIN;
//Temperature in degree Centigrade
see attached file for modified source code
MultipleDiodes.c.zip 2.7 K
I wrote the code for that diode project. The suggestions you have provided are another method of computing the same temperature.
1.) I can as well remove the averaging in the ADC offset calculation and move it to one place where IDAC current ratio is calculated. This will simplify the computation. But I dont want to change it because for a new user or anyone who wants to reuse that offset calculation routine as a general purpose function for other ADC measurements in the application, it would mean that they have to introduce a divide-by-8 in any other place they call this "calculateoffset" function.
2.) Regarding, using a divide-by-8 instead of a ">>3" operation, it wouldn't make much of a difference. The reason is in my project the ADC is in 20-bit mode which means at most I may lose 7 counts (because the remainder is lost). But 7 counts for ADC in such a high resolution will be too low to give any improvement in temperature measurement (< 0.1 C). So I traded off computation for accuracy there.
All the changes suggested by you are correct anyway. Let me know if you have further thoughts on this.
My implementation was for a PSOC3, using a 12 bits ADC.
In that case, the multiplying factor is larger ( x 1.86 )
This produce 2 degree steps with the previous calculation method
For lower resolution ADC configurations, the method you suggested is the better method. I am updating this project over next few weeks for next version of software. During that time, I will add comment in the code with regards to this computation.