我正在为C语言编写PIC单片机的代码。我有一个超过4位小数的浮点值。我想将它舍入到小数点后1位。四舍五入C中没有内置函数的浮点值
但问题是,我不能使用C语言提供的圆形,底板,细胞函数。
有没有其他的方法来做到这一点数学?
UPDATE 1:
我想显示连接到PIC微控制器的一个LCD的温度。我正在模拟温度传感器在-40 * C到50 * C的范围内线性地提供0-5V。
为此,我使用线性插值来确定温度。值显示在LCD上。这里是我的功能:
float Temp;
void Read_Temp()
{
ADCON0 &= 0xC5; //Clearing channel selection bits
ADCON0 |= 0<<3; //Setting channel 0 selection bits
__delay_ms(2);
while(GO_nDONE);
unsigned int adc_value = ((ADRESH<<8)+ADRESL);
Temp = (adc_value * (90.0/1023.0)) - 40.0; // to show in celcius
if(TempMode == 'F') // to show in farenheit
Temp = (Temp*1.8)+32;
}
我正在使用库来连接液晶显示器。显示浮点值的函数有此评论的头文件在它旁边写着:
/* LCD_display_float: Displays a floating point numerical value on the LCD
* This function take the parameter 'value' and displays it to the number of
* decimal places specified by the parameter 'dplaces'. NOTE: The function
* is limited to displaying 4 significant figures due to headroom limitations
* of the 24 bit float type in MPLAB X. Attempting to display more than 4
* significant figures will lead to the string "ERR" being displayed. */
void LCD_display_float (float value, unsigned char dplaces);
所以,当我模仿我的代码,ERR写在LCD。我应该如何解决这个问题?
如果你们需要lcd_display_float()的定义,我也可以上传。
如果该值不超过整数的范围,则可以将通常的乘以100,截断,根据lsd修改,除以100.如果是,则甚至可以这样做,但提取尾数将会比较棘手,更不用说把它放回去。但是,为什么你在微控制器上使用浮点运算? – StoryTeller
'sprintf'和'atof'是否可用? –
因为浮点是二进制的,所以实际上不能舍入到小数点后1位。这里没有0.1的确切表示。根据你想要做的事情,最好四舍五入到最接近的0.125。 –