关于DS18B20的问题

2019-03-24 19:11发布

谁能给我个51单片机的DS18B20程序,晶振是11.0592的,搞了一天都一直显示85度 此帖出自小平头技术问答
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
11条回答
tzl
2019-03-25 19:03
// 以  下  为:  DS18b20 读温度的程序
// 接口程序为: uint Read_Temperature(void);    //读回的数如果是"256"那么温度为25.6度
#define  ds18b20_out   P1DIR|= BIT7;
#define  ds18b20_in    P1DIR&=~BIT7;

#define ds18b20_out_1   P1OUT|= BIT7;    //输出1
#define ds18b20_out_0   P1OUT&=~BIT7;    //输出0
#define ds18b20_io_in      P1IN&BIT7       //判断接受的数据是1还是0
#define DQ   BIT0

//****************温度程序****************************

void delay_u(uchar x)
{
uchar y;
for(y=0;y<=x;x++);  
}
//------------------------------------------------------------------------------
void delay_d(int Us)
{
    int Count=1,i,j;
    for(i = Us; i > 0; i--){for(j = Count;j > 0;j--); }
    return;
}
//------------------------------------------------------------------------------
char DS1820_Reset(void)
{
    char presence;  // 设定管脚为输出方向
    ds18b20_out;ds18b20_out_0;
    ds18b20_out;ds18b20_out_1;
    ds18b20_out;ds18b20_out_0;
      delay_d(25);// 延时480微妙
    ds18b20_out;ds18b20_out_1;
      delay_d(3); //延时60微妙
    ds18b20_out;ds18b20_out_0;
    ds18b20_in;
    presence = (char)(ds18b20_io_in);
    delay_d(2);// 延时60微妙
    return(presence);
}
//------------------------------------------------------------------------------
char DS1820_ReadByte(void)
{
        char i,value=0,presence;
       
        for (i = 8;i > 0;i--)
        {
          value >>= 1;
            ds18b20_out;ds18b20_out_1;
            ds18b20_out;ds18b20_out_1;
            ds18b20_out;ds18b20_out_0;
            ds18b20_out;ds18b20_out_1;
            ds18b20_out_1;               // 延时1微妙       
            ds18b20_in;
            presence = (char)(ds18b20_io_in);
                if(presence) value |= 0x80;
            delay_d(2);                  // 延时60微妙
            ds18b20_out;ds18b20_out_1;
        }
            return value;
}
//------------------------------------------------------------------------------
void DS1820_WriteByte(char val)
{
        char i,nBit;
        for (i=8; i>0; i--)
        {
       ds18b20_out;ds18b20_out_1;
       ds18b20_out;ds18b20_out_1;     
       ds18b20_out_1;           // 延时1微妙
       ds18b20_out;ds18b20_out_0;
        nBit = val & 0x01;
                if (nBit)
                {ds18b20_out_1;}
                else
                {ds18b20_out_0;}
        delay_d(2);             // 延时50微妙
        ds18b20_out;ds18b20_out_1;
                 val >>= 1;
        }
        i++;i++;i++;i++;i++;// 延时5微妙
}
//------------------------------------------------------------------------------
uint Read_Temperature(void)
{
        uint th, tl,ta;
        int data;
//_DINT();
        // 复位
        DS1820_Reset();// Skip ROM
        DS1820_WriteByte(0xCC);// 开始转换
        DS1820_WriteByte(0x44);
        DS1820_Reset();
        DS1820_WriteByte(0xCC);// Read Scratch
        DS1820_WriteByte(0xBE);//读取温度数据
       
    tl=DS1820_ReadByte();
    th=DS1820_ReadByte();
    th<<=8;
    ta=tl|th;
    data=ta;
    data/=2;
    return data;
//_EINT();     
}
//****************温度程序结束************************

一周热门 更多>