標(biāo)題: DS18B20源碼和proteus仿真 [打印本頁]

作者: 霸王別墨跡    時間: 2016-10-25 14:36
標(biāo)題: DS18B20源碼和proteus仿真

仿真和源碼下載:
自尊版--最后調(diào)試.rar (50.08 KB, 下載次數(shù): 26)

#include<reg51.h>
#define uint unsigned int
#define uchar unsigned char
uchar xs[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xbf,0xc6,0xbf};
uchar location[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
sbit DQ=P3^4;
float c;
uchar flag;
void delay(uchar t)
{
        while(t--);
}
void delay1(uint a)
{
        uint i,j;
        for(i=0;i<a;i++)
                for(j=0;j<120;j++);
}
void init_ds18b20()
{
        uchar n;
        DQ=1;
        delay(8);
        DQ=0;
        delay(110);              
        DQ=1;
        delay(25);
        n=DQ;
        delay(4);
}
void write_byte(uchar dat)
{
        uchar i;
        for(i=0;i<8;i++)
        {
                DQ=0;
                DQ=dat&0x01;
                delay(5);
                DQ=1;
                dat=dat>>1;
        }
        delay(5);
}
uchar read_byte()
{
        uchar i,value=0x00;
        for(i=0;i<8;i++)
        {
                DQ=0;
                DQ=1;        
                value=value>>1;
                delay(3);
                if(DQ)
                value=value|0x80;        
        
                delay(4);
        }
        return value;
}
uchar readtemperature()
{
        uchar a,b,t;
        init_ds18b20();
        write_byte(0xcc);
        write_byte(0x44);
        delay1(20);
        init_ds18b20();
        write_byte(0xcc);
        write_byte(0xbe);
        a=read_byte();
        b=read_byte();
        c=0x0f&a;
        c*=0.0625;
        a>>=4;
        b<<=4;
        t=b+a;
        if(t>128)
        {
                t=~t+1;
                flag=1;
        }
        else
                flag=0;
        return t;
}
void display(uchar wei,uchar dat)
{
        P2=location[wei];               
        if(wei==5)
                P0=xs[dat]&0x7f;
        else
                P0=xs[dat];
}
void main()
{
        uchar temp;
        while(1)
        {               
                temp=readtemperature();
                if(flag==1)
                {
                        display(3,12);
                        delay1(5);
                }
                display(4,temp/10);
                delay1(5);
                display(5,temp%10);
                delay1(5);
                display(6,(int)(c*10)%10);
                delay1(5);
                display(7,11);
                delay1(5);
        }
}


作者: 玄玄凱凱    時間: 2017-4-19 19:31
希望能用。,老鐵




歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1