[導(dǎo)讀][cpp] view plaincopy //---------------------------------------------------------- // 模塊名稱:DS18B20.h // 模塊功能:DS18B20 讀寫操作 //-------------------------------------------
- //----------------------------------------------------------
- // 模塊名稱:DS18B20.h
- // 模塊功能:DS18B20 讀寫操作
- //----------------------------------------------------------
- sbit DS = P1^0;
- //----------------------------------------------------------
- // 函數(shù)名稱:void DelayTmp(unsigned int count)
- // 函數(shù)功能:延時
- //----------------------------------------------------------
- void DelayTmp(unsigned int count)
- {
- unsigned int i;
- while(count)
- {
- i=200;while(i>0)i--;
- count--;
- }
- }
- //----------------------------------------------------------
- // 函數(shù)名稱:void DsReset(void)
- // 函數(shù)功能:復(fù)位
- //----------------------------------------------------------
- void DsReset(void)
- {
- unsigned int i;
- DS=0;
- i=103; while(i>0)i--;
- DS=1;
- i=4;while(i>0)i--;
- }
- //----------------------------------------------------------
- // 函數(shù)名稱:bit TmpReadBit(void)
- // 函數(shù)功能:讀取一位
- //----------------------------------------------------------
- bit TmpReadBit(void)
- {
- unsigned int i;
- bit dat;
- DS=0;i++;
- DS=1;i++;i++;
- dat=DS;
- i=8;while(i>0)i--;
- return (dat);
- }
- //----------------------------------------------------------
- // 函數(shù)名稱:unsigned char TmpRead(void)
- // 函數(shù)功能:讀取一字節(jié)
- //----------------------------------------------------------
- unsigned char TmpRead(void)
- {
- unsigned char i,j,dat;
- dat=0;
- for(i=1;i<=8;i++)
- {j=TmpReadBit();
- dat=(j<<7)|(dat>>1); }
- return(dat);
- }
- //----------------------------------------------------------
- // 函數(shù)名稱:void TmpWriteByte(unsigned char dat)
- // 函數(shù)功能:寫入一字節(jié)
- //----------------------------------------------------------
- void TmpWriteByte(unsigned char dat)
- {
- unsigned int i;
- unsigned char j;
- bit testb;
- for(j=1;j<=8;j++)
- {
- testb=dat&0x01;
- dat=dat>>1;
- if(testb) //write 1
- {
- DS=0;
- i++;i++;
- DS=1;
- i=8;while(i>0)i--;
- }
- else
- {
- DS=0; //write 0
- i=8;while(i>0)i--;
- DS=1;
- i++;i++;
- }
- }
- }
- //----------------------------------------------------------
- // 函數(shù)名稱:void TmpChange(void)
- // 函數(shù)功能:溫度轉(zhuǎn)換
- //----------------------------------------------------------
- void TmpChange(void)
- {
- DsReset();
- DelayTmp(1);
- TmpWriteByte(0xcc);
- TmpWriteByte(0x44);
- }
- //----------------------------------------------------------
- // 函數(shù)名稱:unsigned int Tmp()
- // 函數(shù)功能:獲取溫度
- //----------------------------------------------------------
- unsigned int Tmp()
- {
- float tt;
- unsigned char a,b;
- unsigned int temp;
- DsReset();
- DelayTmp(1);
- TmpWriteByte(0xcc);
- TmpWriteByte(0xbe);
- a=TmpRead();
- b=TmpRead();
- temp=b;
- temp<<=8;
- temp=temp|a;
- tt=temp*0.0625;
- temp=tt*10+0.5;
- return temp;
- }
- //----------------------------------------------------------
- // 函數(shù)名稱:void RefreshTmp()
- // 函數(shù)功能:刷新溫度
- //----------------------------------------------------------
- void RefreshTmp()
- {
- TmpChange();
- DisplayTmp(Tmp());
- }





