?? 溫度測試與輸入顯示.c
字號:
#include<at89x51.h>
#include<absacc.h>
#include<intrins.h>
#define uchar unsigned char
uchar idata com1,com2; //鍵盤掃描用
uchar fu; //測量溫度用
int n;
sbit DQ =P3^0;//定義通信端口
uchar code LED[] ={0xc0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};//0123456789
uchar code LED1[]={0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10};//0-9的帶小數點的段選碼
uchar code tab[] ={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};//P2選通數組
int b[4]={0,0,0,0}; //測量溫度緩存數組
int c[4]={0,0,0,0}; //設定溫度緩存數組
//延時函數
void delay(unsigned int i)
{
while(i--);
}
//初始化函數
Init_DS18B20(void)
{
unsigned char x=0;
DQ=1; //DQ復位
delay(8); //稍做延時
DQ=0; //單片機將DQ拉低
delay(80); //精確延時 大于 480us
DQ=1; //拉高總線
delay(14);
x=DQ; //稍做延時后 如果x=0則初始化成功 x=1則初始化失敗
delay(20);
}
//讀一個字節
ReadOneChar(void)
{
unsigned char i=0;
unsigned char dat = 0;
for (i=8;i>0;i--)
{
DQ=0; // 給脈沖信號
dat>>=1;
DQ=1; // 給脈沖信號
if(DQ)
dat|=0x80;
delay(4);
}
return(dat);
}
//寫一個字節
WriteOneChar(unsigned char dat)
{
unsigned char i=0;
for (i=8; i>0; i--)
{
DQ=0;
DQ=dat&0x01;
delay(5);
DQ=1;
dat>>=1;
}
delay(4);
}
//讀取溫度
float ReadTemperature(void)
{
float ttt;
unsigned char a=0;
unsigned char b=0;
unsigned int t=0;
float tt=0;
Init_DS18B20(); //初始化函數DS18B20
WriteOneChar(0xCC); // 跳過讀序號列號的操作
WriteOneChar(0x44); // 啟動溫度轉換
Init_DS18B20();
WriteOneChar(0xCC); //跳過讀序號列號的操作
WriteOneChar(0xBE); //讀取溫度寄存器等(共可讀9個寄存器) 前兩個就是溫度
a=ReadOneChar();
b=ReadOneChar();
//a=0xeb; -33.3125(0x215)
//b=0xfd;
t=b;
t<<=8;
t=t|a;
fu=b;
fu=fu&0x08;
if(fu!=0)
{t=~t+1;}
tt=t*0.0625; //25.0625
ttt=tt;
return(ttt);
}
uchar key()
{
uchar i;
uchar com;
com1=0x00;
com2=0x00;
delay(100); //消除鍵盤抖動 延時10ms
P1=0xf0;
if(P1!=0xf0)
{
com1=P1;
P1=0x0f;
com2=P1;
}
P1=0xf0;
while(P1!=0xf0);
com=com1|com2;
if(com==0xee)i=0;
if(com==0xed)i=1;
if(com==0xeb)i=2;
if(com==0xe7)i=3;
if(com==0xde)i=4;
if(com==0xdd)i=5;
if(com==0xdb)i=6;
if(com==0xd7)i=7;
if(com==0xbe)i=8;
if(com==0xbd)i=9;
if(com==0xbb)i=10;
if(com==0xb7)i=11;
if(com==0x7e)i=12;
if(com==0x7d)i=13;
if(com==0x7b)i=14;
if(com==0x77)i=15;
return(i);
}
void zhd() interrupt 0
{ uchar j;
uchar g;
//key();
j=key();
if(j==10)
{
n=0;
for(g=0;g<4;g++)
{
c[g]=0;
}
P2=0x00;
}
else
{
c[n]=j;
n=n+1;
}
}
void main()
{
EA=1;
EX0=1;
IT0=1;
while(1)
{
float show;
unsigned int test,set;
int m,n;
int j;
show=ReadTemperature();//讀溫度
m=show;//25
b[3]=m/10;//十位
b[2]=m%10;//個位
n=((show-m)*100+0.5);
b[1]=n/10;//十分位
b[0]=n%10;//百分位
P2=0;
for(j=0;j<4;j++)
{
P2=tab[j];
if(j==2)P0=LED1[b[j]];
else P0=LED[b[j]];
delay(50);
}
P1=0xf0;
for(j=4;j<8;j++)
{
P2=tab[j];
if(j==5)P0=LED1[c[j-4]];
else P0=LED[c[j-4]];
delay(50);
}
test=b[3]*1000+b[2]*100+b[1]*10+b[0];
set =c[0]*1000+c[1]*100+c[2]*20+c[3];
if(test>set)
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -