欧美极品高清xxxxhd,国产日产欧美最新,无码AV国产东京热AV无码,国产精品人与动性XXX,国产传媒亚洲综合一区二区,四库影院永久国产精品,毛片免费免费高清视频,福利所导航夜趣136
標(biāo)題:
DS18B20驅(qū)動程序
[打印本頁]
作者:
asdf1545
時間:
2017-3-21 16:53
標(biāo)題:
DS18B20驅(qū)動程序
微信截圖.png
(14.93 KB, 下載次數(shù): 56)
下載附件
2017-3-21 16:53 上傳
#include<reg52.h>
code unsigned char seg7code[11]={0x3f,0x06,0x5b,0x4f,0x66,
0x6d,0x7d,0x07,0x7f,0x6f,0x40}; //顯示段碼
void Delay(unsigned int tc) //顯示延時程序
{while( tc != 0 )
{unsigned int i;
for(i=0; i<100; i++);
tc--;}
}
sbit TMDAT =P3^1; //DS18B20的數(shù)據(jù)輸入/輸出腳DQ,根據(jù)情況設(shè)定
unsigned int sdata;//測量到的溫度的整數(shù)部分
unsigned char xiaoshu1;//小數(shù)第一位
unsigned char xiaoshu2;//小數(shù)第二位
unsigned char xiaoshu;//兩位小數(shù)
bit fg=1; //溫度正負(fù)標(biāo)志
void dmsec (unsigned int count) //延時部分
{
unsigned char i;
while(count--)
{for(i=0;i<115;i++);}
}
void tmreset (void) //發(fā)送復(fù)位
{
unsigned char i;
TMDAT=0; for(i=0;i<103;i++);
TMDAT = 1; for(i=0;i<4;i++);
}
bit tmrbit (void) //讀一位//
{
unsigned int i;
bit dat;
TMDAT = 0;
i++;
TMDAT = 1;
i++; i++; //微量延時 //
dat = TMDAT;
for(i=0;i<8;i++);
return (dat);
}
unsigned char tmrbyte (void) //讀一個字節(jié)
{
unsigned char i,j,dat;
dat = 0;
for (i=1;i<=8;i++)
{ j = tmrbit(); dat = (j << 7) | (dat >> 1); }
return (dat);
}
void tmwbyte (unsigned char dat) //寫一個字節(jié)
{
unsigned char j,i;
bit testb;
for (j=1;j<=8;j++)
{ testb = dat & 0x01;
dat = dat >> 1;
if (testb)
{ TMDAT = 0; //寫0
i++; i++;
TMDAT = 1;
for(i=0;i<8;i++); }
else
{ TMDAT = 0; //寫0
for(i=0;i<8;i++);
TMDAT = 1;
i++; i++;}
}
}
void tmstart (void) //發(fā)送ds1820 開始轉(zhuǎn)換
{ tmreset(); //復(fù)位
dmsec(1); //延時
tmwbyte(0xcc); //跳過序列號命令
tmwbyte(0x44); //發(fā)轉(zhuǎn)換命令 44H,
}
void tmrtemp (void) //讀取溫度
{
unsigned char a,b;
tmreset (); //復(fù)位
dmsec (1); //延時
tmwbyte (0xcc); //跳過序列號命令
tmwbyte (0xbe); //發(fā)送讀取命令
a = tmrbyte (); //讀取低位溫度
b = tmrbyte (); //讀取高位溫度
if(b>0x7f) //最高位為1時溫度是負(fù)
{a=~a; b=~b+1; //補(bǔ)碼轉(zhuǎn)換,取反加一
fg=0; //讀取溫度為負(fù)時fg=0
}
sdata = a/16+b*16; //整數(shù)部分
xiaoshu1 = (a&0x0f)*10/16; //小數(shù)第一位
xiaoshu2 = (a&0x0f)*100/16%10;//小數(shù)第二位
xiaoshu=xiaoshu1*10+xiaoshu2; //小數(shù)兩位
}
void DS18B20PRO(void)
{ tmstart();
//dmsec(5); //如果是不斷地讀取的話可以不延時 //
tmrtemp(); //讀取溫度,執(zhí)行完畢溫度將存于TMP中 //
}
void Led()
{
if(fg==1) //溫度為正時顯示的數(shù)據(jù)
{ P2=P2&0xef;
P0=seg7code[sdata/10]; //輸出十位數(shù)
Delay(8); P2=P2|0xf0; P2=P2&0xdf;
P0=seg7code[sdata%10]|0x80; //輸出個位和小數(shù)點(diǎn)
Delay(8); P2=P2|0xf0; P2=P2&0xbf;
P0=seg7code[xiaoshu1]; //輸出小數(shù)點(diǎn)后第一位
Delay(8); P2=P2|0xf0; P2=P2&0x7f;
P0=seg7code[xiaoshu2]; //輸出小數(shù)點(diǎn)后第二位
Delay(4); P2=P2|0xf0;
}
if(fg==0) //溫度為負(fù)時顯示的數(shù)據(jù)
{ P2=P2&0xef;
P0=seg7code[11]; //負(fù)號
Delay(8); P2=P2|0xf0; P2=P2&0xdf;
P0=seg7code[sdata/10]|0x80; //輸出十位數(shù)
Delay(8); P2=P2|0xf0; P2=P2&0xbf;
P0=seg7code[sdata%10]; //輸出個位和小數(shù)點(diǎn)
Delay(8); P2=P2|0xf0; P2=P2&0x7f;
P0=seg7code[xiaoshu1]; //輸出小數(shù)點(diǎn)后第一位
Delay(4); P2=P2|0xf0;
}
}
main()
{fg=1;
while(1)
{
DS18B20PRO();
Led();
}
}
復(fù)制代碼
歡迎光臨 (http://m.raoushi.com/bbs/)
Powered by Discuz! X3.1