#include "reg51.h"
#include "LCD1602.h"
#include "stdio.h"
#include "DS18B20.h"
#include <intrins.h>
#define uchar unsigned char //宏定義
#define uint unsigned int
uchar code table1[] = "0123456789012345"; //定義數組
uchar code table2[] = "abcdefghABCDEFGH";
uchar *str1 = table1; //定義指針,把數組的首地址賦給它
uchar *str2 = table2; //數組名表示數組首元素的地址
uchar Temperature_Disp1[] = "abcdefghABCDEFGH";
uchar Temperature_Disp2[] = "abcdefghABCDEFGH";
unsigned char Key_Value;
uint Temperature;
uint Temperature_Max = 40;
uint Temperature_Min = 20;
sbit L0=P1^0;
sbit L1=P1^1;
sbit L2=P1^2;
sbit L3=P1^3;
sbit Beep=P2^4;
sbit DQ=P2^3; //數據傳輸線接單片機的相應的引腳
sbit LED_Green=P2^5;
sbit LED_Red_Max=P2^6;
sbit LED_Red_Min=P2^7;
sbit motor=P3^0;
void delay_Key(unsigned int time) //延時子程序//
{
unsigned int i;
for(i=0;i<time;i++)
{}
}
unsigned char Key_Scan(void)
{
unsigned char Key_Value = 11;
P1=0xef; //行掃描初值11101111//
if (L0==0) Key_Value = 0;
if (L1==0) Key_Value = 1;
if (L2==0) Key_Value = 2;
if (L3==0) Key_Value = 3;
delay_Key(5000);
P1=0xdf; //行掃描初值11011111//
if (L0==0) Key_Value = 4;
if (L1==0) Key_Value = 5;
if (L2==0) Key_Value = 6;
if (L3==0) Key_Value = 7;
delay_Key(5000);
P1=0xbf; //行掃描初值10111111//
if (L0==0) Key_Value = 8;
if (L1==0) Key_Value = 9;
if (L2==0) Key_Value = 10;
if (L3==0) Key_Value = 11;
delay_Key(5000);
P1=0x7f; //行掃描初值//
if (L0==0) Key_Value = 12;
if (L1==0) Key_Value = 13;
if (L2==0) Key_Value = 14;
if (L3==0) Key_Value = 15;
delay_Key(5000);
return Key_Value;
}
void Key_Proc()
{
Key_Value = Key_Scan();
switch(Key_Value)
{
case 0:
Temperature_Max++;
break;
case 1:
Temperature_Max--;
break;
case 2:
Temperature_Min++;
break;
case 3:
Temperature_Min--;
break;
case 4:
Temperature_Max = 40;
Temperature_Min = 20;
Beep = 1;
break;
}
}
void Display(unsigned char *Temperature_Disp1,unsigned char *Temperature_Disp2)
{
Lcd_Show(0,0); //定位 從1行的第1個位置開始顯示
while(*Temperature_Disp1 !='\0') //判斷是否顯示完字符串
{
LcdWrDat(*Temperature_Disp1++) ; // 把字符串一個一個放到相應位置
}
Lcd_Show(0,1); //定位 從2行的第1個位置開始顯示
while(*Temperature_Disp2!='\0')
{
LcdWrDat(*Temperature_Disp2++) ;
}
}
void Temperature_Proc()
{
Temperature = (uint)ReadTemperature();
sprintf(Temperature_Disp1,"Max:%dC Min:%dC",Temperature_Max,Temperature_Min);
sprintf(Temperature_Disp2,"Now:%dC ",Temperature);
Display(Temperature_Disp1,Temperature_Disp2);
if(Temperature > Temperature_Max)
{
Beep = 1;
LED_Red_Max = 0;
LED_Red_Min = 1;
LED_Green = 1;
motor = 1;
}
else if((Temperature <= Temperature_Max)&&(Temperature >= Temperature_Min))
{
LED_Red_Max = 1;
LED_Red_Min = 1;
Beep = 0;
LED_Green = 0;
motor = 0;
}
else if(Temperature < Temperature_Min)
{
Beep = 1;
LED_Red_Max = 1;
LED_Red_Min = 0;
LED_Green = 1;
motor = 0;
}
}
void main(void)
{
Beep = 0;
Temperature = (uint)ReadTemperature();
motor = 0;
Delay500ms();
Lcd_Init();
while(1)
{
Key_Proc();
Temperature_Proc();
}
}
|