TXT

DS18B20

By Kenneth Bennett,2014-06-12 22:40
14 views 0
DS18B20

#include < reg51.h >

    #include < intrins.h >

    unsigned char code table[]="0123456789";

    unsigned char fuhao,temph,templ; unsigned char bai,shi,ge,shifen,baifen; //È??Ö?äÁ?

    //bit flag;

sbit DQ=P3^7;

    sbit en=P2^7;

    sbit rs=P2^6;

    sbit rw=P2^5;

    void display();

    void delay(unsigned char z) {

     unsigned char j,i;

     for (i=0;i

     for (j=0;j<110;j++)

     ;

}

    void delay_50us(unsigned int t) //Ã??Î50usÑÓÊ???×î?óÎó?î13us

    {

     unsigned char j;

     for(;t>0;t--)

     for(j=19;j>0;j--)

     ;

    }

void delay_2us(unsigned char i) //Ã??Î2us??È?ÑÓÊ???×î?óÎó?î6us

    {

    while(--i);

    }

    unsigned char Ds_Reset() //?õÊ??? {

     unsigned char n;

     DQ=1;

     _nop_();

     _nop_();

     DQ=0;

     delay_50us(10); //ÑÓÊ?513us

     DQ=1;

     delay_2us(23);//ÑÓÊ?53us

     n=DQ;

     delay_50us(4);//ÑÓÊ?200us

     DQ=1;//À?ÉìÖÁ?ßµçÆ?

     return(n);

    }

unsigned char Ds_Read() //?Áº?Êý

    {

     unsigned char dat=0x00;

     unsigned char i;

     for(i=0;i<8;i++)

     { DQ=0;

     _nop_(); //ÑÓÊ?1us _nop_();

    _nop_();

     dat>>=1;

     DQ=1; //ÊÍ?Å

     _nop_();

     _nop_();

     _nop_();

    _nop_();

    _nop_();

     if(DQ)

     {dat|=0x80;}

     delay_2us(30); // ÑÓÊ?67us

     DQ=1;

     }

    return(dat);

    }

void Ds_Write(unsigned char datt)

    {

     unsigned char bitt;

     unsigned char j;

     for(j=0;j<8;j++)

     {

     bitt=datt&0x01;

     DQ=0;

     _nop_();

     _nop_(); //ÑÓÊ?2us

     if(bitt)

     {DQ=1;

     delay_2us(28);

     }

     else delay_2us(28); //ÑÓÊ?56us

     datt>>=1;

     DQ=1;

     _nop_();

     }

    }

    void Set_Tempchange() //ÎÂ?Èת??Ö?Áî {

     Ds_Reset();

     Ds_Write(0xcc);// Ìø?ý?ÁÐòºÅÁкŵÄ?Ù×?

     Ds_Write(0x44);// Æô??ÎÂ?Èת?? }

    void Read_Temp() //?Á?öÎÂ?È {

     Ds_Reset();

     Ds_Write(0xcc);// Ìø?ý?ÁÐòºÅÁкŵÄ?Ù×?

     Ds_Write(0xBE);// ?ÁRAMÊý?Ý

     templ=Ds_Read();//ÎÂ?ȵĵÍ?ËÎ?

     temph=Ds_Read();//ÎÂ?ȵÄ?ß?ËÎ?

}

    void Change_Temp() //ת??ÎÂ?È { unsigned char intn;

     temph=temph<<4;

     intn=temph+((templ&0xf0)>>4);

     shi=intn%100/10;//Ê?Î?

     ge=intn%10; //?öÎ?

     shifen=(templ&0x0f)*625/1000; }

bit lcd_bz()

    {

     bit result;

     rs =0;

     rw =1;

     en=1;

     delay(5);

     result = (bit)(P0 & 0x80);

     en=0;

     return result;

    }

void write_com(unsigned char com)

    {

     while (lcd_bz());

     en=0;

     rw=0;

     rs=0;

     P0=com;

     delay(5);

     en=1;

     delay(5);

     en=0;

    }

    void write_data(unsigned char date)

    {

     while (lcd_bz());

     en=0;

     rw=0;

     rs=1;

     P0=date;

     delay(5);

     en=1;

     delay(5);

     en=0;

    }

    void init_1602() {

     write_com(0x38);

     write_com(0x0f);

     write_com(0x06);

     write_com(0x01); }

void display()

    {

     write_com(0x80+0x40+11);

     write_data(table[shi]);

     write_data(table[ge]);

     write_data('.');

     write_data(table[shifen]);

     write_data('C'); }

    void temperature() {

     Set_Tempchange();

     delay(2);

     Read_Temp();

     delay(2);

     Change_Temp();

     delay(2);

     display(); }

    void main() {

    //EA=0;

    init_1602();

while(1)

     {

     temperature(); }

    }

Report this document

For any questions or suggestions please email
cust-service@docsford.com