| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1138 人关注过本帖
标题:DS18B20温度检测器
只看楼主 加入收藏
小白程序
Rank: 1
等 级:新手上路
帖 子:33
专家分:0
注 册:2022-2-24
结帖率:100%
收藏
 问题点数:0 回复次数:0 
DS18B20温度检测器
程序代码:
#include <REGX51.H>
#include <intrins.h>
#include "display.h"
void delay5us(unsigned char x)
{
    unsigned char i;
    while(x--){
    _nop_();
    i = 11;
    while (--i);}
}
sbit DQ=P3^7;
bit DS18B20chushihua()
{
    bit i;
    DQ=0;
    delay5us(96);
    DQ=1;
    delay5us(16);
    i=DQ;
    delay5us(80);
    return i;
}

void writeDS18B20(unsigned char xdat)
{
    unsigned char i;
    for(i=0;i<8;i++){
        DQ=0;
        delay5us(1);
        DQ=xdat&0x01;
        delay5us(12);
        DQ=1;
        xdat=xdat>>1;
    }
}

unsigned char readDS18B20()
{
    unsigned char i,rdat;
    for(i=0;i<8;i++){
        rdat=rdat>>1;
        DQ=0;
        delay5us(1);
        DQ=1;
        delay5us(1);
        if(DQ==1){
            rdat=rdat|0x80;
            delay5us(12);
            DQ=1;}

 }
    return rdat;
}

unsigned char L;
unsigned int H;
unsigned int temp;
void main()
{
    while(1){
        DS18B20chushihua();
        writeDS18B20(0xcc);
        writeDS18B20(0x44);
        DS18B20chushihua();
        writeDS18B20(0xcc);
        writeDS18B20(0xbe);
        L=readDS18B20();
        H=readDS18B20();
        H=(H<<8)|L;
        if((H&0xf800)==0xf800)
            H=!H+1;
        temp=H*0.0625;
        display_s((double)temp);
}
    }        

    

调用函数:#include <REGX51.H>
#include "delay.h"
sbit wei1=P2^1;
sbit wei2=P2^3;
sbit wei3=P2^5;
sbit wei4=P2^7;
//********????,????,???????********
unsigned char code table[]={
// 0,   1,  2,   3,   4,   5,   6,   7,   8,   9,   A,   B,   C,   D,   E,  F   
0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};


//********????,????,???????********
unsigned char code table1[]={
//0.,  1., 2.,  3.,  4.,  5.,  6.,  7.,  8.,  9.,  A.,  B.,  C.,  D.,  E.,  F.  
0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10,0x08,0x03,0x46,0x21,0x06,0x0e};
void display_s(double x)
{
    unsigned int yi,er,san,si;
    yi=(unsigned int)(x)/10;
    er=(unsigned int)(x)%10;
    san=(unsigned int)(x*10)%10;
    si=12;
    while(1){
    P0=table[yi];
    wei1=0;
    delay(2);
    wei1=1;
    P0=table1[er];
    wei2=0;
    delay(2);
    wei2=1;
    P0=table[san];
    wei3=0;
    delay(2);
    wei3=1;
    P0=table[si];
    wei4=0;
    delay(2);
    wei4=1;}
}
protuse仿真图:
图片附件: 游客没有浏览图片的权限,请 登录注册

求解:为什么返回0

[此贴子已经被作者于2023-6-14 21:37编辑过]

搜索更多相关主题的帖子: char unsigned delay include int 
2023-06-04 19:30
快速回复:DS18B20温度检测器
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.033254 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved