0

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;
}
  • 2
    What is an actual number? Is the math the problem here? Is it that you, don't know how to calculate the number from the `iTemp`? – Iharob Al Asimi Jul 20 '16 at 17:42
  • its that the number is displayed correctly using the printf settings they have. However I need to create a variable that acutally is that number so that I can use it in my equation – Josh Jul 20 '16 at 17:47
  • Does `iTemp` contain 1186392 at the moment? If it does, conversion could be done via division by `1000000.0` – struct Jul 20 '16 at 17:53
  • `while((ADS1256_Scan() == 0));` has an empty body. If this is intended, It would be more explicit to write it `while((ADS1256_Scan() == 0)) continue;` – chqrlie Jul 20 '16 at 18:20
  • Your right, I was over complicating it. Thanks for you help – Josh Jul 20 '16 at 18:25
  • @chqrlie right, even the semicolon in the next line or an actual empty body block `{}` is better but it's odd that you didn't mention the extraneous parentheses. – Iharob Al Asimi Jul 20 '16 at 19:08
  • @iharob: the extra parentheses are redundant but harmless, what is more troublesome is the closing curly brace 4 rows from the bottom of the code fragment... – chqrlie Jul 20 '16 at 19:16
  • @Josh: **please indent the code correctly!** As posted, it is very confusion ans inconsistent. – chqrlie Jul 20 '16 at 19:18

1 Answers1

0

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.

Rotem
  • 30,366
  • 4
  • 32
  • 65