標題: protues仿真DS18b20溫度顯示失敗,一直顯示0,請各位幫忙看看(程序與仿真見附件)? [打印本頁]

作者: 18258562783    時間: 2019-12-24 20:21
標題: protues仿真DS18b20溫度顯示失敗,一直顯示0,請各位幫忙看看(程序與仿真見附件)?
//Ds18b20初始化,
bit Ds18b20Init()
{
  bit i;
  DSPORT=1;
  _nop_();  
  DSPORT=0;//拉低總線
  Delaylms(500); //拉低500秒,拉低480秒~960秒即可
  DSPORT=1;//釋放總線
  Delaylms(68);//延時68秒。等待DS18b20響應
  i=DSPORT;
  Delaylms(150);
  DSPORT=1;
  _nop_();
  return (i);
}
//寫函數(shù)
void Ds18b20WriteByte(uchar dat)
{
        uchar i,j;
        for(i=0;j<8;j++)
        {
                DSPORT=0;
                Delaylms(1); //延時1us
                DSPORT=dat&0x01;
                Delaylms(77);//延時77us
                DSPORT=1;
                _nop_();
                dat>>=1;
        }
}
//讀函數(shù)
uchar Ds18b20ReadByte()
{
        uint i,j;
        uchar bi,byte;
        for(i=8;j>0;j--)
        {
                DSPORT=0;
                i++;
                DSPORT=1;
                i++;
                i++;
                bi=DSPORT;
                Delaylms(77);
                DSPORT=1;
                _nop_();
                byte=(byte>>1)|(bi<<7);       
        }
        return byte;
}

//溫度轉換指令
void Ds18b20Changetemp()
{
        Ds18b20Init();
        Delaylms(1);
        Ds18b20WriteByte(0xcc);
        Ds18b20WriteByte(0x44);                       
}
//溫度讀取指令
void Ds18b20ReadTempcom()
{
        Ds18b20Init();
        Delaylms(1);
        Ds18b20WriteByte(0xcc);
        Ds18b20WriteByte(0xbe);//讀取RAM
}
//讀取溫度數(shù)據(jù)
int Ds18b20ReadTemp()
{
        int temp=0;
        uchar tmh,tml;
        Ds18b20Changetemp();
        Ds18b20ReadTempcom();
        tml= Ds18b20ReadByte();
        tmh= Ds18b20ReadByte();
        temp=tmh;
        temp<<=8;
        temp|=tml;
        return temp;               
}


捕獲.PNG (65.14 KB, 下載次數(shù): 68)

捕獲.PNG

程序和仿真.zip

143.48 KB, 下載次數(shù): 35


作者: wulin    時間: 2019-12-24 22:58

給你改好了
DS18b20+仿真.zip (107.65 KB, 下載次數(shù): 233)


作者: 小黎    時間: 2019-12-25 09:30
你這個晶振多大的
作者: 18258562783    時間: 2019-12-26 17:00
小黎 發(fā)表于 2019-12-25 09:30
你這個晶振多大的

11.0925的,我的延時好像寫錯了,上次發(fā)的程序延時是錯的,但我改了之后還是不顯示,我去看看一樓給我改好的
作者: 18258562783    時間: 2019-12-26 18:39
wulin 發(fā)表于 2019-12-24 22:58
給你改好了

謝謝你,我去看看
作者: zack_zh    時間: 2023-5-27 01:09
wulin 發(fā)表于 2019-12-24 22:58
給你改好了

改時延了?




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