DS18B20温度传感器使用指南

 对于单片机中 DS18B20温度传感器的时序读写

#include <STC15F2K60S2.H>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int    

uchar code tab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,\
                                    0x40,0x79,0x1a,0x30,0x19,0x12,0x02,0x78,0x00,0x10,0xbf,0xff};
uchar yi,er,san,si,wu,liu,qi,ba,num,dat,tt;
sbit DQ=P1^4;
long wendu;
void Delayms(int ms);
void Delay500us();
void Delay80us();
void Allinit(void);
void Display1(uchar yi,uchar er);
void Display2(uchar san,uchar si);
void Display3(uchar wu,uchar liu);
void Display4(uchar qi,uchar ba);

void Init_DS18B20(void);
void DS18B20_witeByte(unsigned char dat);
unsigned char DS18B20_readByte(void);
long TemperGet(void);

void main(void)
{
    void Allinit();
    yi=21;er=21;san=21;si=11;wu=21;liu=21;qi=21;ba=21;
//    Init_DS18B20();
//    DS18B20_witeByte(0xCC);//工作指令
//    DS18B20_witeByte(0x44);   
    
    while(1)
    {    
        
//        if(Init_DS18B20();==0)
//        {
//            yi=10;
//        }
        

        wendu=TemperGet();
//        yi=wendu/10;
//        er=wendu%10;    
        yi=wendu/100000;
        er=wendu%100000/10000+10;
        san=wendu%10000/1000;
        si=wendu%1000/100;
        wu=wendu%100/10;
        liu=wendu%10;        
        
        
        Display1(yi,er);
        Display2(san,si);
        Display3(wu,liu);
        Display4(qi,ba);
        
    }
}

void Init_DS18B20(void)//温度传感器初始化
{
    DQ=0;
    Delay500us();
    DQ=1;
    Delay500us();
}

void DS18B20_witeByte(unsigned char dat)
{
    unsigned char i=0;
    for(i=0;i<8;i++)
    {
        DQ=0;
        DQ=dat&0x01;//1100 1100&0000 0001=0000 0000
        Delay80us();
        DQ=1;
        dat>>=1;//0110 0110 //0011 0011
    }
    Delay80us();
}
unsigned char DS18B20_readByte(void)
{
    unsigned char i=0;
    unsigned char dat=0;
    for(i=0;i<8;i++)
    {    
        DQ=0;
        dat>>=1;
//        _nop_();
        DQ=1;
        if(DQ==1)
        {
            dat|=dat|0x80;//0000 0000|1000 0000=1000 0000
        }
        Delay80us();
    
    }
    return dat;
    
}
long TemperGet(void)
{
    unsigned char low;
    unsigned char    high;
    long temp;
    Init_DS18B20();//初始化
                                    //ROM操作
    DS18B20_witeByte(0xCC);//工作指令
    DS18B20_witeByte(0x44);
    Delay500us();//等待温度转换完成
    
    Init_DS18B20();//初始化
                                    //ROM操作
    DS18B20_witeByte(0xCC);//工作指令
    DS18B20_witeByte(0xBE);
    
    low=DS18B20_readByte();
    high=DS18B20_readByte();
    

//    temp=high<<4;//0000 1010 ;1010 0000
//    temp=temp|(low>>4);//1010 0110;0000 1010 1010 1010
    temp=(high&0x0f);
    temp<<=8;
    temp=temp|low;
    temp=temp*625;
    return temp;
}    

void Display1(uchar yi,uchar er)
{
    P2=0xc0;//打开控制数码管位选的573
    P0=0x01;//选中所有数码管
    P2=0xE0;//打开控制数码管段选的573
    P0=tab[yi];//
    Delayms(1);
    
    P2=0xc0;//打开控制数码管位选的573
    P0=0x02;//选中所有数码管
    P2=0xE0;//打开控制数码管段选的573
    P0=tab[er];//
    Delayms(1);    
}
void Display2(uchar san,uchar si)
{
    P2=0xc0;//打开控制数码管位选的573
    P0=0x04;//选中所有数码管
    P2=0xE0;//打开控制数码管段选的573
    P0=tab[san];//
    Delayms(1);
    
    P2=0xc0;//打开控制数码管位选的573
    P0=0x08;//选中所有数码管
    P2=0xE0;//打开控制数码管段选的573
    P0=tab[si];//
    Delayms(1);    
}
void Display3(uchar wu,uchar liu)
{
    P2=0xc0;//打开控制数码管位选的573
    P0=0x10;//选中所有数码管
    P2=0xE0;//打开控制数码管段选的573
    P0=tab[wu];//
    Delayms(1);
    
    P2=0xc0;//打开控制数码管位选的573
    P0=0x20;//选中所有数码管
    P2=0xE0;//打开控制数码管段选的573
    P0=tab[liu];//
    Delayms(1);    
}
void Display4(uchar qi,uchar ba)
{
    P2=0xc0;//打开控制数码管位选的573
    P0=0x40;//选中所有数码管
    P2=0xE0;//打开控制数码管段选的573
    P0=tab[qi];//
    Delayms(1);
    
    P2=0xc0;//打开控制数码管位选的573
    P0=0x80;//选中所有数码管
    P2=0xE0;//打开控制数码管段选的573
    P0=tab[ba];//
    Delayms(1);    
}

void Allinit(void)//初始化
{
    P2=0xa0;//打开控制蜂鸣器的573
    P0=0x00;//关闭蜂鸣器继电器
    
    P2=0xc0;//打开控制数码管位选的573
    P0=0xff;//选中所有数码管
    P2=0xE0;//打开控制数码管段选的573
    P0=0xff;//关闭所有数码管
    
    P2=0x80;//打开控制LED的573
    P0=0xff;//关闭所有LED
}

void Delayms(int ms)
{
    int i,j;
    for(i=0;i<ms;i++)
    for(j=845;j>0;j–);
}    
void Delay500us()        //@11.0592MHz
{
    unsigned char i, j;

    _nop_();
    _nop_();
    i = 6;
    j = 93;
    do
    {
        while (–j);
    } while (–i);
}
void Delay80us()        //@11.0592MHz
{
    unsigned char i, j;

    _nop_();
    i = 1;
    j = 217;
    do
    {
        while (–j);
    } while (–i);
}

 

物联沃分享整理
物联沃-IOTWORD物联网 » DS18B20温度传感器使用指南

发表评论