问题
I am currently working on a project that will use a thermister as a temperature sensor and displaying this information among other things onto a gui using the raspberry pi. However I am currently stuck on the Analog to digital conversion. Using the sample code from the waveshare ad board I am using I manage to get the voltage to display, however I then need to use this vout in my voltage divider equation to get the resistance of my thermister and I can't figure out how to actually use the 32 bit integer iTemp variable and properly convert it so thats its the actual number displayed on the console. Currently the 2 print lines with itemp print out numbers like (1.186 391 V). which is correct but I need to convert that into a actual number that I can then plug into my voltage divider equation. Ps: I included the part of the code with the print statements. Any help would be greatly appreaciated.
Code:
while((ADS1256_Scan() == 0));
for (i = 0; i < ch_num; i++)
{
adc[i] = ADS1256_GetAdc(i);
volt[i] = (adc[i] * 100) / 167;
}
for (i = 0; i < ch_num; i++)
{
buf[0] = ((uint32_t)adc[i] >> 16) & 0xFF;
buf[1] = ((uint32_t)adc[i] >> 8) & 0xFF;
buf[2] = ((uint32_t)adc[i] >> 0) & 0xFF;
printf("%d=%02X%02X%02X, %8ld", (int)i, (int)buf[0],
(int)buf[1], (int)buf[2], (long)adc[i]);
iTemp = volt[i]; /* uV */
if (iTemp < 0)
{
iTemp = -iTemp;
printf(" (-%ld.%03ld %03ld V) \r\n", iTemp /1000000, (iTemp%1000000)/1000, iTemp%1000);
}
else
{
printf(" ( %ld.%03ld %03ld V) \r\n", iTemp /1000000, (iTemp%1000000)/1000, iTemp%1000);
}
}
//printf("\33[%dA", (int)ch_num);
bsp_DelayUS(100000);
}
bcm2835_spi_end();
bcm2835_close();
return 0;
}
回答1:
According to your comment: iTemp = volt[i]; /* uV */
, iTemp read is in micro-volts.
In order to use it in your equation, all you need to do is convert it to volts (convert it to double
or float
and multiply by (1.0/1000000.0)).
double iTempV = (double)iTemp*1.0e-6;
or:
float iTempV = (float)iTemp*1.0e-6f;
I assume the following formula converts A2D raw sample value to micro-voltage:
adc[i] = ADS1256_GetAdc(i);
volt[i] = (adc[i] * 100) / 167;
I found the following project in GitHub: https://github.com/ecao1/SEADS-Rpi/blob/master/test_ver1.c to support my claim.
来源:https://stackoverflow.com/questions/38487250/concatenating-32-bit-integers-in-c