標(biāo)題: 求大神看一下這段溫度顯示程序有沒有錯(cuò)誤 [打印本頁]
作者: wshixinren 時(shí)間: 2017-12-5 22:34
標(biāo)題: 求大神看一下這段溫度顯示程序有沒有錯(cuò)誤
#include<AT89X52.h>
#include<intrins.h>
#define uint unsigned int
#define uchar unsigned char
uchar max_int=0x00,max_dot=0x00,min_int=0x00,min_dot=0x00;
bit s=0,s1=0;
#include"ds18b20.h"
#include"keyscan.h"
#include"display.h"
/***********************主函數(shù)************************/
void main()
{
beer=0;
led=1;
IT1=1;
EX1=0;
EA=1;
timer1_init(0);
get_temperature(1);
while(1)
{
keyscan();
get_temperature(0);
keyscan();
display(temp,temp_d*0.625);
alarm();
keyscan();
}
}
/********************************************************************
* 程序名; __ds18b20_h__
* 功 能: DS18B20的c51編程頭文件
* 編程者:ZPZ
* 編程時(shí)間:2009/10/2
* 說 明:用到的全局變量是:無符號(hào)字符型變量temp(測(cè)得的溫度整數(shù)部分),temp_d
* (測(cè)得的溫度小數(shù)部分),標(biāo)志位f(測(cè)量溫度的標(biāo)志位‘0’表示“正溫度”‘1’表
* 示“負(fù)溫度”),標(biāo)志位f_max(上限溫度的標(biāo)志位‘0’表示“正溫度”、‘1’表
* 示“負(fù)溫度”),標(biāo)志位f_min(下限溫度的標(biāo)志位‘0’表示“正溫度”、‘1’表
* 示“負(fù)溫度”),標(biāo)志位w(報(bào)警標(biāo)志位‘1’啟動(dòng)報(bào)警‘0’關(guān)閉報(bào)警)。
*********************************************************************/
#ifndef __ds18b20_h__
#define __ds18b20_h__
#define uint unsigned int
#define uchar unsigned char
sbit DQ= P2^3;
sbit beer=P3^0;
sbit led=P3^1;
uchar temp=0; //溫度的整數(shù)部分
uchar temp_d=0; //溫度的小數(shù)部
uchar n;
bit f=0,f_max=0,f_min=0;w=0;
/***********************延時(shí)子函數(shù)************************/
void ds18b20_delayus(uint t)
{
while(t--);
}
void ds18b20_delayms(uint t)
{
uint i,j;
for(i=t;i>0;i--)
for(j=120;j>0;j--);
}
/******************ds18b20初始化函數(shù)*********((*********/
void ds18b20_init() // DS18B20初始化
{
DQ=1;
DQ=0; //控制器向DS18B20發(fā)低電平脈沖
ds18b20_delayus(30); //延時(shí)480μs
DQ=1; //控制器拉高總線,
while(DQ); //等待DS18B20拉低總線,在60-240μs之間
ds18b20_delayus(20); //延時(shí),等待上拉電阻拉高總線
DQ=1; //提升數(shù)據(jù)線,準(zhǔn)備數(shù)據(jù)傳輸;
}
/******************ds18b20字節(jié)讀函數(shù)******************/
uchar ds18b20_read() //DS18B20 字節(jié)讀取
{
uchar i;
uchar d = 0;
DQ = 1; //準(zhǔn)備讀;
for(i=8;i>0;i--)
{
d >>= 1; //低位先發(fā);
DQ = 0;
_nop_();
_nop_();
_nop_();
DQ = 1; //必須寫1,否則讀出來的將是不預(yù)期的數(shù)據(jù);
if(DQ) //在12us處讀取數(shù)據(jù);
d |= 0x80;
ds18b20_delayus(10);
}
return d;
}
/******************ds18b20字節(jié)寫函數(shù)******************/
void ds18b20_write(uchar d) // ds18b20字節(jié)寫
{
uchar i;
for(i=8;i>0;i--)
{
DQ=0;
_nop_();
_nop_();
_nop_();
DQ=d&0x01;
ds18b20_delayus(5);
DQ=1;
d >>= 1;
}
}
/*********************獲取溫度函數(shù)**********************/
void get_temperature(bit f) //得到整數(shù)的溫度值
{
uchar a=0,b=0,c=0,d=0;
uint i;
ds18b20_init(); //DS18B20初始化
ds18b20_write(0xcc); //向DS18B20發(fā)SKIP ROM命令
ds18b20_write(0x44); //啟動(dòng)DS18B20進(jìn)行溫度轉(zhuǎn)換,結(jié)果存入內(nèi)部RAM
ds18b20_delayms(1);
ds18b20_init(); //DS18B20初始化
ds18b20_write(0xcc); //向DS18B20發(fā)SKIP ROM命令
ds18b20_write(0xbe); //讀內(nèi)部RAM中9字節(jié)的內(nèi)容命令
a=ds18b20_read(); //讀內(nèi)部RAM (LSB)
b=ds18b20_read(); //讀內(nèi)部RAM (MSB)
if(f==1)
{
max_int=ds18b20_read(); //讀內(nèi)部RAM (LSB)
min_int=ds18b20_read();
}
if((max_int&0x80)==0x80)
{f_max=1;max_int=(max_int-0x80);}
if((min_int&0x80)==0x80)
{f_min=1;min_int=(min_int-0x80);}
i=b;
i>>=4;
if (i==0)
{
f=0; //i為0,正溫度,設(shè)立正溫度標(biāo)記
temp=((a>>4)|(b<<4)); //整數(shù)部分
a=(a&0x0f);
temp_d=a; //小數(shù)部分
}
else
{
f=1; //i為1,負(fù)溫度,設(shè)立負(fù)溫度標(biāo)記
a=~a+1;
b=~b;
temp=((a>>4)|(b<<4)); //整數(shù)部分
a=(a&0x0f); //小數(shù)部分
temp_d=a;
}
}
void store_t()
{
if(f_max==1)
max_int=max_int+0x80;
if(f_min==1)
min_int=min_int+0x80;
ds18b20_init(); //DS18B20初始化
ds18b20_write(0xcc); //向DS18B20發(fā)SKIP ROM命令
ds18b20_write(0x4e);
ds18b20_write(max_int);
ds18b20_write(min_int);
ds18b20_write(0xff);
ds18b20_init(); //DS18B20初始化
ds18b20_write(0xcc); //向DS18B20發(fā)SKIP ROM命令
ds18b20_write(0x48);
}
/**********************溫度超限報(bào)警函數(shù)***********************/
void alarm()
{
if(f_max==0)
{
if(f_min==0)
{
if(f==0)
{
if((temp+temp_d*0.0625)<=min_int||(temp+temp_d*0.0625)>=max_int)
{w=1;TR1=1;}
if((temp+temp_d*0.0625)<max_int&&(temp+temp_d*0.0625)>min_int)
{w=0;}
}
if(f==1){w=1;TR1=1;}
}
if(f_min==1)
{
if(f==0)
{
if((temp+temp_d*0.0625)>=max_int)
{w=1;TR1=1;}
if((temp+temp_d*0.0625)<max_int )
{w=0;}
}
if(f==1)
{
if((temp+temp_d*0.0625)>=min_int)
{w=1;TR1=1;}
if((temp+temp_d*0.0625)<min_int )
{w=0;}
}
}
}
if(f_max==1)
{
if(f_min==1)
{
if(f==1)
{
if((temp+temp_d*0.0625)<=max_int||(temp+temp_d*0.0625)>=min_int)
{w=1;TR1=1;}
if((temp+temp_d*0.0625)<min_int&&(temp+temp_d*0.0625)>max_int)
{w=0;}
}
if(f==0){w=1;TR1=1;}
}
}
}
#endif
/**********************************************************************
* 程序名; __keyscan_H__
* 功 能: ds18b20鍵盤頭文件,通過鍵盤設(shè)定設(shè)定上下限報(bào)警溫度
* 編程者:ZPZ
* 編程時(shí)間:2009/10/2
**********************************************************************/
#ifndef __keyscan_H__
#define __keyscan_H__
sbit key1=P2^2;
sbit key2=P2^1;
sbit key3=P2^0;
sbit key4=P3^3;
uchar a=0,i=0;
bit k4=0,v=0,v1=0,v2=0;
/***************************讀鍵盤延時(shí)子函數(shù)**************************/
void keyscan_delay(uint z)
{
uint i,j;
for(i=z;i>0;i--)
for(j=120;j>0;j--);
}
/****************************溫度調(diào)節(jié)函數(shù)******************************/
int temp_change(int count,bit f)
{
if(key2==0)
{
keyscan_delay(10);
if(key2==0)
{
if(f==0)
{
count++;
if(a==1){if(count>125) count=125;}
if(a==2){if(count>125) count=125;}
}
if(f!=0)
{
count++;
if(a==1){if(count>55) count=55;}
if(a==2){if(count>55) count=55;}
}
}
while(key2==0);
keyscan_delay(10);
}
if(key3==0)
{
keyscan_delay(10);
if(key3==0)
{
count--;
if(a==1){if(count<0) count=0;}
if(a==2){if(count<0) count=0;}
}
while(key3==0);
keyscan_delay(10);
}
return count;
}
/*****************************讀鍵盤函數(shù)******************************/
void keyscan()
{
if(key1==0)
{
keyscan_delay(10);
if(key1==0)
{
TR1=1;
k4=1;
v=1;
i++;
if(i>2){i=0;TR1=0;k4=0;v=0;store_t();get_temperature(1);}
switch(i)
{
case 0:a=0;break;
case 1:a=1;break;
case 2:a=2;break;
default:break;
}
}
while(key1==0);
keyscan_delay(10);
}
if(a==1&&v==1)
{led=0;max_int=temp_change(max_int,f_max);}
else if(a==2&&v==1)
{led=1;min_int=temp_change(min_int,f_min);}
else;
if(k4==1)
{
if(key4==0)
{
keyscan_delay(5);
if(key4==0)
{
if(a==1)
{if(max_int>55) f_max=0;else f_max=~f_max;}
if(a==2)
{if(min_int>55) f_max=0;else f_min=~f_min;}
}
while(key4==0);
keyscan_delay(10);
}
}
if(v==0)
{
if(key2==0)
{
keyscan_delay(10);
if(key2==0)
{
a=1;
TR1=1;
s1=1;
}
while(key2==0);
keyscan_delay(10);
}
if(key3==0)
{
keyscan_delay(10);
if(key3==0)
{
a=2;
TR1=1;
s1=1;
}
while(key3==0);
keyscan_delay(10);
}
if(v1==1)
{a=0;v1=0;TR1=0;}
}
}
#endif
/**********************************************************************
* 程序名; __ds18b20_display_H__
* 功 能: ds18b20數(shù)碼管動(dòng)態(tài)顯示頭文件,通過定時(shí)器0延時(shí)實(shí)現(xiàn)數(shù)碼管動(dòng)態(tài)顯示
* 編程者:ZPZ
* 編程時(shí)間:2009/10/2
**********************************************************************/
#ifndef __ds18b20_display_H__
#define __ds18b20_display_H__
#define uint unsigned int
#define uchar unsigned char
sbit wei1=P2^4;
sbit wei2=P2^5;
sbit wei3=P2^6;
sbit wei4=P2^7;
uchar num=0;
uchar code temperature1[]={ 0x3f,0x06,0x5b,0x4f,0x66,
0x6d,0x7d,0x07,0x7f,0x6f};
uchar code temperature2[]={ 0xbf,0x86,0xdb,0xcf,0xe6,
0xed,0xfd,0x87,0xff,0xef};
uchar code temperature3[]={ 0x00,0x80,0x40,0x76,0x38};
/***********************延時(shí)子函數(shù)************************/
void display_delay(uint t)
{
uint i,j;
for(i=t;i>0;i--)
for(j=20;j>0;j--);
}
/**************************定時(shí)器1初始化函數(shù)***************************/
void timer1_init(bit t)
{
TMOD=0x10;
TH0=0x3c;
TL0=0xb0;
EA=1;
ET1=1;
TR1=t;
}
/**************************定時(shí)器1中斷函數(shù)*****************************/
void timer1() interrupt 3
{
TH0=0x3c;
TL0=0xb0;
num++;
if(num<5) {s=1;if(w==1){beer=1;led=1;}else{beer=1;led=1;}}
else {s=0;if(w==1){beer=0;led=0;}else{beer=1;led=1;}}
if(num>25)
{
num=0;
s1=0;
v1=1;
}
}
/***********************調(diào)節(jié)選擇函數(shù)************************/
void selsct_1(uchar f,uchar k)
{
if(f==0)
{
if(k/100==0) P0=temperature3[0];
else P0=temperature1[k/100];
}
if(f==1)
{
if(k%100/10==0) P0=temperature3[0];
else P0=temperature3[2];
}
}
void selsct_2(bit f,uchar k)
{
if(f==0)
{
if((k/100==0)&&(k%100/10==0))
P0=temperature3[0];
else P0=temperature1[k%100/10];
}
if(f==1)
{
if(k%100/10==0) P0=temperature3[2];
else P0=temperature1[k%100/10];
}
}
/***********************顯示函數(shù)************************/
void display(uchar t,uchar t_d)
{
uchar i;
for(i=0;i<4;i++)
{
switch(i)
{
case 0:
if(a==0){selsct_1(f,t);}
if(a==1)
{
if(s==0) selsct_1(f_max,max_int);
else P0=temperature3[0];
if(s1==1) selsct_1(f_max,max_int);
}
if(a==2)
{
if(s==0) selsct_1(f_min,min_int);
else P0=temperature3[0];
if(s1==1) selsct_1(f_min,min_int);
}
wei2=0;
wei3=0;
wei4=0;
wei1=1;
break;
case 1:
if(a==0){selsct_2(f,t);}
if(a==1)
{
if(s==0) selsct_2(f_max,max_int);
else P0=temperature3[0];
if(s1==1) selsct_2(f_max,max_int);
}
if(a==2)
{
if(s==0) selsct_2(f_min,min_int);
else P0=temperature3[0];
if(s1==1) selsct_2(f_min,min_int);
}
wei1=0;
wei3=0;
wei4=0;
wei2=1;
break;
case 2:
if(a==0){P0=temperature2[t%10];}
if(a==1)
{
if(s==0) P0=temperature2[max_int%10];
else P0=temperature3[0];
if(s1==1) P0=temperature2[max_int%10];
}
if(a==2)
{
if(s==0) P0=temperature2[min_int%10];
else P0=temperature3[0];
if(s1==1) P0=temperature2[min_int%10];
}
wei1=0;
wei2=0;
wei4=0;
wei3=1;
break;
case 3:
if(a==0){P0=temperature1[t_d];}
if(a==1)
{
if(s==0)
P0=temperature1[0];
else P0=temperature3[0];
if(s1==1) P0=temperature1[0];
}
if(a==2)
{
if(s==0)
P0=temperature1[0];
else P0=temperature3[0];
if(s1==1) P0=temperature1[0];
}
wei1=0;
wei2=0;
wei3=0;
wei4=1;
break;
}
display_delay(16);
}
}
#endif
作者: HelloPan111 時(shí)間: 2017-12-6 15:53
這么長的程序,沒幾個(gè)人會(huì)看的,你還是直接說遇到什么問題吧
歡迎光臨 (http://www.torrancerestoration.com/bbs/) |
Powered by Discuz! X3.1 |