Concatenating 32 bit integers in C

非 Y 不嫁゛ 提交于 2019-12-13 07:39:45

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!