ds18b20 驱动测试程序

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/ioctl.h>
#include <fcntl.h>

#define DS18B20 "/dev/ds18b20"

int main()
{
    int fd;
    char buf[2],temp[7];
        unsigned char a=0;
    unsigned int b=0;
    unsigned int t=0;
    unsigned char TempH,TempL;
    if((fd = open(DS18B20, O_RDONLY)) < 0){
        printf("error in open %s",DS18B20);
        return -1;
    }
    read(fd,buf,0);
    a=buf[0];
    b=buf[1];
    b <<= 8;
    t = a + b;
    if(t&0x8000) {
        t = ~t;
        t = t+1;
        temp[0] = '-';
    } else {
        temp[0] = ' ';
    }
    TempH = t >> 4;
        TempL = t & 0x0F;
        TempL = TempL*6/10;
    temp[1] = TempH/100 + '0';
    temp[2] = (TempH%100)/10 + '0';
    temp[3] = (TempH%100)%10 + '0';
    temp[4] = '.';
    temp[5] = (TempL & 0x0F)*6/10 + '0';
    temp[6] = 0;
    /*printf("%d %d\n",buf[1],buf[0]);*/
    printf("the temprature is %s C.\n",temp);
    close(fd);    
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值