# include & ltmath.h & gt//Keil庫
# include & ltstdio.h & gt//Keil庫
# include & ltINTRINS。H & gt
#定義uchar無符號字符
#定義uint無符號整數
#從左到右定義顯示1 0x Fe//數碼管1
#定義顯示2 0x FD//數碼管2從左到右
#定義顯示3 0x FB//數碼管3從左到右
#定義顯示4 0xf 7//數碼管4從左到右
#定義顯示5 0x ef//數碼管5從左到右
#定義顯示6 0x df//數碼管6從左到右
#定義顯示7 0x BF//數碼管7從左到右
#定義顯示器80x7f//數碼管8從左到右
sbit led=p1^5;
sbit dq=p3^6;//根據實際情況設置
sbit setth=p1^0;//設置th
sbit up=p1^1;//添加
sbit down=p3^2;//減號
sbit beep = p3^3;
uchar tempint,f,bb,tempth,temptl,th,tl,beepflag//溫度的整數部分和小數部分
int tempdf,c;
code unsigned char ledmap[]={0xc0,0xf9,0xa4,0xb0,0x99,
0x92,0x82,0xf8,0x80,0x90,0x BF };
code unsigned char led map 1[]= { 0x 40,0x79,0x24,0x30,
0x19,0x12,0x02,0x78,0x00,0x 10 };
//7段數碼管0 ~ 9位數字* * *正顯示代碼和負位代碼(最後壹位)
void set _ ds 18b 20();//初始化DS18B20子程序。
void get _ temperature();//獲取溫度子程序
void read _ ds 18b 20();//讀取DS18B20子程序
void write_ds18b20(uchar命令);//將1字節子程序寫入DS18B20。
無效延遲(uchar計數);//延遲計數毫秒子程序
void disp_temp(tempint,tempdf);//顯示溫度子程序
/* * * * * * * * * * * * * * * * * * * * * * * *初始化DS18B20子程序* * * * * * * * * * * * * * * * * * * * * *
//****************************************************************************
void set_ds18b20()
{
while(1)
{
uchar延遲,標誌;
flag = 0;
DQ = 1;
延遲= 1;
while(-delay);
DQ = 0;//數據線設置為低電平。
延時= 250;
while(-delay);//保持低電平500us。
DQ = 1;//數據線設置為高電平。
延遲= 30;
while(-delay);//保持高電平60us。
While(DQ==0) //判斷DS18B20是否發出低電平信號。
{
延時= 210;//DS18B20響應,延時420us。
while(-delay);
If(DQ) //DS18B20成功發出高電平初始化,並返回。
{
flag = 1;//DS18B20初始化成功標誌
//初始化成功LED標誌
打破;
}
}
If(flag) //初始化成功,然後延時480us,時序要求。
{
延時= 240;
while(-delay);
打破;
}
}
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
//****************************************************************************
Void get_temperature() //溫度轉換和溫度采集子程序
{
set _ ds 18b 20();//初始化DS18B20
write _ ds 18b 20(0x cc);//發送命令跳過ROM匹配。
write _ ds 18b 20(0x 44);//發送溫度轉換命令
//delay ms(750);
disp_temp(tempint,tempdf);//顯示溫度,等待AD轉換。
set _ ds 18b 20();
write _ ds 18b 20(0x cc);//發送命令跳過ROM匹配。
write _ ds 18b 20(0x be);//發出溫度讀數命令。
read _ ds 18b 20();//將讀取的溫度數據保存到tempint和tempdf。
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
//****************************************************************************
void read_ds18b20()
{
uchar delay,I,j,k,temp,temph,templ
j = 4;//讀取2位字節數據
做
{
for(I = 8;我& gt0;I-)//以8位讀取壹個字節。
{
temp & gt& gt=1;//讀取1位,右移1位。
DQ = 0;//數據線設置為低電平。
延遲= 1;
while(-delay);
DQ = 1;//數據線設置為高電平。
延遲= 4;
while(-delay);//延遲8us
If(DQ) //讀取1位數據。
temp | = 0x80
延遲= 25;//讀取1位數據後延遲50us。
while(-delay);
}
如果(j==4)
templ = temp//讀取的第壹個字節存儲在templ中。
如果(j==3)
temph = temp//讀取的第二個字節存儲在temph中。
如果(j==2)
tempth = temp//讀取的第三個字節存儲tempth的值。
如果(j==1)
temptl = temp//讀取的第四個字節存儲tempth TL的值。
} while(-j);
f = 0;
如果((temph & amp0xf8)!=0x00) //如果溫度為負,則處理二進制補碼。
{
f = 1;//負溫度f設置1。
temph = ~ temph
templ = ~ templ
k = templ+1;
templ = k;
if(k & gt;255)
{
temph++;
}
}
tempdf = templ & amp0x0f//將讀取的數據轉換為溫度值,整數部分存儲在tempint中,小數部分存儲在tempdf中。
c =(tempdf * 625);
tempdf = c;
templ & gt& gt=4;//
temph & lt& lt=4;//轉換為字節溫度
tempint = temph | templ//兩個字節合並成壹個字節。
if(tempint & gt;th | tempint = = th | tempint & lttl|tempint==tl)
beepflag = 1;
其他
beepflag = 0;
}
/* * * * * * * * * * * * * * * * * * * * * *編寫DS18B20子程序* * * * * * * * * * * * * * * * * * * * * *
//****************************************************************************
void write_ds18b20(uchar命令)
{
烏查爾延遲,我;
for(I = 8;我& gt0;I-)//逐位寫入壹個字節的數據。
{
DQ = 0;//數據線設置為低電平。
延遲= 6;//延時12us
while(-delay);
DQ =指揮與控制;0x 01;//將數據放在數據線上
延遲= 25;//延遲50us
while(-delay);
command = command & gt& gt1;//準備發送下壹位數據
DQ = 1;//發送壹位數據後,數據線置高電平。
}
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
//****************************************************************************
void disp_temp( tempint,tempdf)
{
烏查爾坦因特、坦因特爾、坦因拜、十分、百分、葛粉、千分;
if (bb==1)
{
tempin Bai = th/100;
tempint =(th % 100)/10;
temp intl = th % 10;//整數模
葛粉= 0/1000;
十分= 0% 1000/100;
百分= 0% 100/10;
千分= 0% 10;//十進制模數
}
else if(bb = = 2)
{ tempin Bai = TL/100;
tempinth =(TL % 100)/10;
temp intl = TL % 10;//整數模
葛粉= 0/1000;
十分= 0% 1000/100;
百分= 0% 100/10;
千分= 0% 10;//十進制模數
}
其他
{
tempin Bai = tempint/100;
tempint =(tempint % 100)/10;
temp intl = tempint % 10;//整數模
葛粉= tempdf/1000;
shifen = tempdf % 1000/100;
baifen = tempdf % 100/10;
qianfen = tempdf % 10;//十進制模數
}
如果(f==0)
{
P0 = display 1;//符號位
P2 = led map[0];
}
其他
{
P0 = display 1;//符號位
P2 = led map[10];
}
戴萊姆斯(2);
P0 = display2
P2 = led map[tempin Bai];//開壹百個名額
戴萊姆斯(2);
P0 =顯示3;
P2 = led map[tempint];//開十席
戴萊姆斯(2);
P0 = display4
P2 = led map 1[temp intl];//開座
戴萊姆斯(2);
P0 = display5
P2 = led map[葛粉];//開倉
戴萊姆斯(2);
P0 = display6
P2 = led map[shifen];//開放十分鐘
戴萊姆斯(2);
P0 = display7
P2 = led map[百分];//開放百分位數
戴萊姆斯(2);
P0 = display8
P2 = led map[qianfen];//打開千分之壹
}
//*******************************************************************/
//*蜂鳴器響起*/
//*******************************************************************/
void beep_st()
{
if (beepflag==1)
{ beep = 0;
delayms(1000);
嗶聲= 1;
}
其他
嗶聲= 1;
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
//****************************************************************************
void delay ms(uchar count)//延遲計數ms子程序
{
uchar i,j;
做
{
for(I = 5;我& gt0;我-)
for(j = 98;j & gt0;j-);
} while(-count);
}
//****************************************************************************
void keyscan()//鍵盤掃描
{
如果(設置==0)
{
delay ms(1);
}
如果(設置==0)
{
b b++;
而(!setth);//這裏循環不是0=1。
}
if(bb==1)
{
if(up==0)
{
delay ms(1);
}
if(up==0)
{
disp_temp(th,tempdf);
if(th & lt;125)
{ th++;}
而(!Up)//不是0=1
{
disp_temp(th,tempdf);
}
}
if(向下==0)
{
delay ms(1);
}
if(向下==0)
{
disp_temp(th,tempdf);
如果(th!=0) //不等於0則為真。
{
th-;
而(!Down)//Down是(不是0=1)用於循環執行。
{
disp_temp(th,tempdf);
}
}
}
}
如果(bb==2)
{
if(up==0)
{
delay ms(1);
}
if(up==0)
{
disp_temp(tl,tempdf);
if(TL & lt;125)
{ tl++;}
而(!Up)//不是0=1
{
disp_temp(th,tempdf);
}
}
if(向下==0)
{
delay ms(1);
}
if(向下==0)
{
disp_temp(th,tempdf);
如果(tl!=0) //不等於0則為真。
{
TL-;
而(!Down)//Down是(不是0=1)用於循環執行。
{
disp_temp(tl,tempdf);
}
}
}
}
如果(bb==3)
{ bb = 0;
set _ ds 18b 20();//初始化DS18B20
write _ ds 18b 20(0x cc);//發送命令跳過ROM匹配。
write _ ds 18b 20(0x4e);//發送溫度轉換命令
write _ ds 18b 20(th);//寫TH 3
write _ ds 18b 20(TL);
//write _ ds 18b 20(0x7f);
set _ ds 18b 20();//初始化DS18B20
write _ ds 18b 20(0x cc);//發送命令跳過ROM匹配。
write _ ds 18b 20(0x 48);
}
}
void main()
{ set _ ds 18b 20();
write _ ds 18b 20(0x cc);//發送命令跳過ROM匹配。
write _ ds 18b 20(0x be);//發出溫度讀數命令。
read _ ds 18b 20();//要讀出的數據
th = tempth//將TH讀取到MCU
tl = temptl
SP = 0x60//設置堆棧指針
bb = 0;
beepflag = 0;
while(1)
{
key scan();
get _ temperature();//獲取溫度
disp_temp(tempint,tempdf);//顯示溫度
beep _ ST();
}
}