數字溫度報警器:我是根據自己的單片機改的,數碼管是連接鎖存器的。調試程序的時候顯示TL0未定義。請大家幫我看看這么回事,謝謝啦。
#define <reg51.h>
#define uint unsigned int
#define uchar unsigned char
#define SET P3^0
#define ADD P3^1
#define DEC P3^2
#define BEEP P2^3
#define ALAM P1^0
#define DQ P2^2
#define SEG P0
#define count 50000
#define TH_M1(65636-count)/256
#define TL_M1(65636-count)%256
bit shanshuo_st;
bit beep_st;
sbit DIAN=P0^5;
sbit bit_select=P2^7;
sbit seg_select=P2^6;
uchar x=0;
signed char shangxian=38;
signed char xiaxian=5;
unsigned char seg_data[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
unsigned char bit_data[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
//
void Delay_DS18B20(int num)
{
while(num--);
}
//
void Init_DS18B20(void)
{
unsigned char X=0;
DQ=1;
Delay_DS18B20(8);
DQ=0;
Delay_DS18B20(80);
DQ=1;
Delay_DS18B20(14);
x=DQ;
Delay_DS18B20(20);
}
//
unsigned char 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_DS18B20(4);
}
return(dat);
}
//
void WriteOneChar(unsigned char dat)
{
unsigned char i=0;
for(i=8;i>0;i--)
{
DQ=0;
DQ=dat&0x01;
Delay_DS18B20(5);
DQ=1;
dat>>=1;
}
}
//
unsigned int ReadTemperature(void)
{
unsigned char a=0;
unsigned char b=0;
unsigned int i=0;
float tt=0;
Init_DS18B20();
WriteOneChar(0xcc);
WriteOneChar(0x44);
Init_DS18B20();
WriteOneChar(0xcc);
WriteOneChar(0xbe);
a=ReadOneChar();
b=ReadOneChar();
t=b;
t<<=8;
t=t|a;
tt=t*0.0625;
t=tt*10+0.5;
return(t);
}
//
void Delay(unit num)
{
while(--num);
}
//
void InitTimer(void)
{
TMOD=0x01;
TH0=TH_M1;
TL0=TL_M1;
}
//
void check_wendu(void)
{
uint a,b,c;
c=temperature();
a=c/100;
b=c/10-a*10;
m=c/10;
n=c-a*100-b*10;
if(m<0)
{
m=0;
n=0;
}
if(m>99)
{
m=99;
n=9;
}
}
//
void Disp_init(void)
{
SEGPORT=0xff
seg_select=1;
P0=0x7f;
seg_select=0;
SEGPORT=0xff
bit_select=1;
P0=0xfe;
bit_select=0;
delay(100);
seg_select=1;
P0=0x7f;
seg_select=0;
bit_select=1;
P0=0xfd;
bit_select=0;
delay(100);
seg_select=1;
P0=0x7f;
seg_select=0;
bit_select=1;
P0=0xfb;
bit_select=0;
delay(100);
seg_select=1;
P0=0x7f;
seg_select=0;
bit_select=1;
P0=0xf7;
bit_select=0;
delay(100);
P0=0xff;
}
//
void Disp_Temperature(void)
{
SEGPORT=0xff
seg_select=1;
P0=seg_date[n];
seg_select=0;
SEGPORT=0xff
bit_select=1;
P0=0xfe;
bit_select=0;
delay(200);
seg_select=1;
P0=seg_date[m%10];
seg_select=0;
bit_select=1;
P0=0xfd;
bit_select=0;
delay(200);
seg_select=1;
P0=seg_data[m/10]|0x80;
seg_select=0;
bit_select=1;
P0=0xfb;
bit_select=0;
delay(200);
seg_select=1;
P0=0x39;
seg_select=0;
bit_select=1;
P0=0xf7;
bit_select=0;
Delay(200);
P2=0xff;
}
//
void Disp_alarm(uchar baojing)
seg_select=1;
P0=0x39;
seg_select=0;
bit_select=1;
P0=0xfe;
bit_select=0;
Delay(100);
P2=0xff;
seg_select=1;
P0=seg_data[baojing%10];
seg_select=0;
bit_select=1;
P0=0xfd;
bit_select=0;
Delay(100);
P2=0xff;
seg_select=1;
P0=seg_data[baojing/10];
seg_select=0;
bit_select=1;
P0=0xfb;
bit_select=0;
Delay(100);
P2=0xff;
seg_select=1;
if(set_st==1)P0=0x76;
else if(set_st==2)P0=0x38;
seg_select=0;
bit_select=1;
P0=0xf7;
bit_select=0;
Delay(100);
P2=0xff;
//
void Alarm()
{
if(x>=10)
{
beep_st=~beep_st;
x=0;
if((m>shangxian&&beep_st==1)||(m<xiaxian&&beep_st==1))
{
BEEP=0;
ALARM=0;
}
else
{
BEEP=1;
ALARM=1;
}
}
//
void main(void)
{
uint z;
InitTimer();
EA=1;
TR0=1;
ET0=1;
IT0=1;
IT1=1;
check_wendu();
check_wendu();
for(z=0;z<300;z++)
{
Disp_init();
}
while(1)
{
if(SET==0)
{
Delay(2000);
do{}while(SET==0);
set_st++;
x=0;
shanshuo_st=1;
if(set_st>2)set_st=0;
}
if(set_st==0)
{
EX0=0;
EX1=0;
check_wendu();
Disp_Temperature();
Alarm();
}
else if(set_st==1)
{
BEEP=1;
ALARM=1;
EX0=1;
EX1=1;
if(x>=10)
{
shanshuo_st=~shanshuo_st;
x=0;
}
if(shanshuo_st)
{
Disp_alarm(shangxian);
}
}
else if(set_st==2)
{
BEEP=1;
ALARM=1;
EX0=1;
EX1=1;
if(x>=10)
{
shanshuo_st=~shanshuo_st;
x=0;
}
if(shanshuo_st)
{
Disp_alarm(xiaxian);
}
}
}
}
//
void timer0(void)interrupt 1
{
TH0=TH_M1;
TL0=TL_M1;
x++;
}
//
void int0(void) interrupt 0
{
EX0=0;
if(DEC==0&&set_st==1)
{
do{
Disp_alarm(shangxian);
}
while(DEC==0);
shangxian--;
if(shangxia<xiaxian)shangxian=xiaxian;
}
else if(DEC==0&&set_st==2)
{
do{
Disp_alarm(xiaxian);
}
while(DEC==0);
xiaxian--;
if(xiaxian<0)xiaxian=0;
}
}
//
void int1(void) interrupt 2
{
EX1=0;
if(ADD==0&&set_st==1)
{
do{
Disp_alarm(shangxian);
}
while(ADD==0);
shangxian++;
if(shangxian>>99)shangxian=99;
}
else if(ADD==0&&set_st==2)
{
do{
Disp_alarm(xiaxian);
}
while(ADD==0);
xiaxian++;
if(xiaxian>shangxian)xiaxian=shangxian;
}
}
|