在C中串联32位整数 [英] Concatenating 32 bit integers in C

查看:146
本文介绍了在C中串联32位整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在一个项目中,该项目将使用热敏电阻作为温度传感器,并使用树莓派将这些信息显示在gui上.但是,我目前仍停留在模数转换上.使用我正在使用的waveshare广告板上的示例代码,设法获得要显示的电压,但是随后我需要在分压器方程式中使用此vout来获得热敏电阻的电阻,因此我不知道该怎么做.实际上使用 32位整数iTemp 变量并正确地对其进行转换,以便其在控制台上显示的实际数字.当前,带有itemp的2条打印行打印出的数字为(1.186 391 V).是正确的,但我需要将其转换为实际数字,然后将其插入我的分压器方程式中.附:我在打印语句中包括了代码的一部分. 任何帮助将不胜感激.

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.

代码:

 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;
}

推荐答案

根据您的评论:iTemp = volt[i]; /* uV */,iTemp读数以微伏特为单位. 为了在方程式中使用它,您要做的就是将其转换为伏特(将其转换为doublefloat并乘以(1.0/1000000.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;

或:

float iTempV = (float)iTemp*1.0e-6f;

我假设以下公式将A2D原始样本值转换为微电压:

I assume the following formula converts A2D raw sample value to micro-voltage:

adc[i] = ADS1256_GetAdc(i);
volt[i] = (adc[i] * 100) / 167;

我在GitHub上找到了以下项目: https://github. com/ecao1/SEADS-Rpi/blob/master/test_ver1.c 来支持我的主张.

I found the following project in GitHub: https://github.com/ecao1/SEADS-Rpi/blob/master/test_ver1.c to support my claim.

这篇关于在C中串联32位整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆