#include<reg52.h>
#include<stdio.h>
#include "18b20.h"
#include "1602.h"
#include "delay.h"sbit key0=P1^4;
sbit key1=P1^5;
sbit key4=P1^6;
sbit led0=P1^1;
sbit led1=P1^2;
sbit led2=P1^3;
sbit motor=P2^2;
sbit motor1=P2^3;
void scankey();
bit ReadFlag; //定义读时间标志
char an=0;
unsigned char jiemian=0;
void Init_Timer0(void); //定时器初始化
int temp,tempH=60,tempL=30;
void UART_Init(void) //串口中断
{
SCON = 0x50; // SCON: 模式 1, 8-bit UART, 使能接收
TMOD |= 0x20; // TMOD: timer 1, mode 2, 8-bit 重装
TH1 = 0xFD; // TH1: 重装值 9600 波特率 晶振 11.0592MHz
TR1 = 1; // TR1: timer 1 打开
//EA = 1; //打开总中断
//ES = 1; //打开串口中断
TI=1;
}
void main (void)
{
float wen;
char displaywen[16];//定义显示区域临时存储数组
unsigned char flag=0; //正常温度的显示
LCD_Init(); //初始化液晶
DelayMs(200); //延时有助于稳定
LCD_Clear(); //清屏
Init_Timer0();
UART_Init();
Lcd_User_Chr(); //写入自定义字符
wendu();
DelayMs(200); //延时有助于稳定
wendu();
DelayMs(200); //延时有助于稳定
wendu();
DelayMs(200); //延时有助于稳定
while (1)
{
scankey();
if(ReadFlag==1)
{
ReadFlag=0;
temp=wendu();
wen=(float)temp*0.0625; //读取温度
temp>>=4; //确保切换高、低温时准确
sprintf(displaywen,"Temp % 7.3f",wen);
LCD_Write_String(0,0,displaywen); //显示第二行
LCD_Write_Char(13,0,0x01); //写入温度右上角点
LCD_Write_Char(14,0,'C'); //写入字符C
}
if(temp>=tempL&&temp<=tempH)
{flag=0;led0=0;led2=1;led1=1;motor=1;motor1=1;}
else if(temp>=tempH)
{flag=1;led1=0;led0=1;led2=1;motor1=0;motor=1;}
else if(temp<=tempL)
{flag=2;led2=0;led0=1;led1=1;motor=0;motor1=1;}
switch(jiemian)
{
case 0:switch(flag)
{ case 0:sprintf(displaywen,"H.%3d L.%3d ",tempH, tempL); //正常显示状态
LCD_Write_String(0,1,displaywen);
break;
case 1:LCD_Write_String(0,1," shangxian tempH ");
break; //高于高温状态
case 2:LCD_Write_String(0,1," xiaxian tempL ");
break; //低于低温状态 } break;
case 1:sprintf(displaywen,"+h.%3d l.%3d ",tempH,tempL);
LCD_Write_String(0,1,displaywen);break;
case 2:sprintf(displaywen,"h.%3d +l.%3d ",tempH,tempL);
LCD_Write_String(0,1,displaywen);break;
case 3:sprintf(displaywen,"-h.%3d l.%3d ",tempH,tempL);
LCD_Write_String(0,1,displaywen);break;
case 4:sprintf(displaywen,"h.%3d -l.%3d ",tempH,tempL);
LCD_Write_String(0,1,displaywen);break;
}
}
}
void Init_Timer0(void) //定时器
{
TMOD |= 0x01; //使用模式1,16位定时器,使用"|"符号可以在使用多个定时器时不受影响
//TH0=0x00; //给定初值
//TL0=0x00;
EA=1; //总中断打开
ET0=1; //定时器中断打开
TR0=1; //定时器开关打开
}
void Timer0_isr(void) interrupt 1
{
static unsigned int num;
TH0=(65536-2000)/256; //重新赋值 2ms
TL0=(65536-2000)%256;
num++;
if(num==1000)
{
num=0;
ReadFlag=1; //读标志位置1
}
}void scankey() //按键控制区域
{
if(key0==0)
{
DelayMs(10);
if(key0==0)
{
if(jiemian==1)tempH+=10; //上限温度的加的标志位
else if(jiemian==2)tempL+=10;
}
while(!key0);
}
else if(key1==0)
{
DelayMs(10);
{
if(key1==0)
{
if(jiemian==3)tempH-=10; //下限温度的加的标志位
else if(jiemian==4)tempL-=10;
}
while(!key1);
}
}
else if(key4==0) //切换界面
{
DelayMs(10);
{
if(key4==0)
{
if(jiemian==0)jiemian=1;
else if(jiemian==1)jiemian=2;
else if(jiemian==2)jiemian=3;
else if(jiemian==3)jiemian=4;
else if(jiemian==4)jiemian=0;
}
while(!key4);
}
}
}
|