DS18B20温度格式转换

来源:互联网 发布:excel中一行数据求和 编辑:程序博客网 时间:2024/05/17 07:03
#include <stdio.h>#include <stdint.h>/* DS18B20温度格式转换 */uint8_t tempIntPart[3];uint8_t tempDecPart[4];float convertToFloat(int16_t temperature) {    float temp;    temp = (float)temperature;    temp /= 16.0f;    return temp;}void splitIntPart(int16_t temperature) {    uint8_t i;    if (temperature & 0x8000)        temperature = -temperature;    temperature >>= 4;    for (i = 0; i < 3; i++) {   //逆向存        tempIntPart[i] = temperature % 10;        temperature /= 10;    }}void splitDecPart(int16_t temperature) {    uint8_t decPart;    if (temperature & 0x8000)        temperature = -temperature;    decPart = (uint8_t)(temperature & 0x000F);    tempDecPart[0] = (decPart * 10) >> 4;    tempDecPart[1] = ((decPart * 100) >> 4) % 10;    tempDecPart[2] = ((decPart * 1000) >> 4) % 10;    tempDecPart[3] = ((decPart * 10000) >> 4) % 10;}int main() {    int16_t temperature = 0x00AB, i;    printf("%f\n", convertToFloat(temperature));    splitIntPart(temperature);    splitDecPart(temperature);    for (i = 2; i >= 0; i--)        printf("%hhd", tempIntPart[i]);    printf("\n");    for (i = 0; i < 4; i++)        printf("%hhd", tempDecPart[i]);    printf("\n");    return 0;}

0 0