|
#include <stc15f2k60s2.h>
sbit R1 = P3^0;
sbit R2 = P3^1;
sbit R3 = P3^2;
sbit R4 = P3^3;
sbit C1 = P4^4;
sbit C2 = P4^2;
sbit C3 = P3^5;
sbit C4 = P3^4;
unsigned char code LedChar[] = {
0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,
0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E,0xBF,0x7F
};
unsigned char LedBuff[8] = {
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF
};
unsigned int num = 3525; //接收按键数字
unsigned char max = 0,min = 0;
unsigned char temp; //18B20温度接收位
unsigned char mode_temp = 0;
unsigned int delay_Led = 0; //Led闪烁时间间隔
bit mode_set = 0; // 0 显示温度 1 设置温度
bit flag_Led = 0; // Led闪烁标志位
void Timer0Init(void); //1毫秒@12.000MHz
void InitSystem();
void ConfigSMG();
void ScanKeys();
void Led_light();
void Setmode_Temp();
extern bit Start18B20(); //18B20初始化
extern unsigned char Get18B20Temp(); //获取温度
void main()
{
InitSystem();
Timer0Init();
while(1)
{
Start18B20();
Setmode_Temp();
ConfigSMG();
ScanKeys();
}
}
/******HC573选择函数*******/
void Select74HC573(unsigned char channel)
{
switch(channel)
{
case 4:P2 = (P2&0x1F)|0x80;break;
case 5:P2 = (P2&0x1F)|0xA0;break;
case 6:P2 = (P2&0x1F)|0xC0;break;
case 7:P2 = (P2&0x1F)|0xE0;break;
default:P2 = (P2&0x1F)|0x00;break;
}
}
/*******关闭LED和蜂鸣器、继电器******/
void InitSystem()
{
Select74HC573(5);
P0 = 0x00;
Select74HC573(4);
P0 = 0xFF;
Select74HC573(0);
}
/*******数码管显示******/
void ConfigSMG()
{
if(mode_set==0) //显示温度
{
temp = Get18B20Temp();
LedBuff[0] = LedChar[16];
LedBuff[1] = LedChar[mode_temp];
LedBuff[2] = LedChar[16];
LedBuff[3] = 0xFF;
LedBuff[4] = 0xFF;
LedBuff[5] = 0xFF;
LedBuff[6] = LedChar[temp/10];
LedBuff[7] = LedChar[temp%10];
}
else //设置温度范围
{
LedBuff[0] = LedChar[16];
LedBuff[1] = LedChar[max/10];
LedBuff[2] = LedChar[max%10];
LedBuff[3] = 0xFF;
LedBuff[4] = 0xFF;
LedBuff[5] = LedChar[16];
LedBuff[6] = LedChar[min/10];
LedBuff[7] = LedChar[min%10];
}
}
/*******按键配置函数*******/
void Delay1ms() //@12.000MHz
{
unsigned char i, j;
i = 12;
j = 169;
do
{
while (--j);
} while (--i);
}
void ScanKeys()
{
R1 = 0;
R2 = R3 = R4 = 1;
C1 = C2 = C3 = C4 = 1;
if(C1==0) //按键0
{
Delay1ms();
if(C1==0)
{
if(mode_set)
{
num = num *10 + 0;
if(num>9999)
num = 0;
}
while(C1==0)
{
ConfigSMG();
}
}
}
else if(C2==0) //按键1
{
Delay1ms();
if(C2==0)
{
if(mode_set)
{
num = num *10 + 1;
if(num>9999)
num = 1;
}
while(C2==0)
{
ConfigSMG();
}
}
}
else if(C3==0) //按键2
{
Delay1ms();
if(C3==0)
{
if(mode_set)
{
num = num *10 + 2;
if(num>9999)
num = 2;
}
while(C3==0)
{
ConfigSMG();
}
}
}
R2 = 0;
R1 = R3 = R4 = 1;
C1 = C2 = C3 = C4 = 1;
if(C1==0) //按键3
{
Delay1ms();
if(C1==0)
{
if(mode_set)
{
num = num *10 + 3;
if(num>9999)
num = 3;
}
while(C1==0)
{
ConfigSMG();
}
}
}
else if(C2==0) //按键4
{
Delay1ms();
if(C2==0)
{
if(mode_set)
{
num = num *10 + 4;
if(num>9999)
num = 4;
}
while(C2==0)
{
ConfigSMG();
}
}
}
else if(C3==0) //按键5
{
Delay1ms();
if(C3==0)
{
if(mode_set)
{
num = num *10 + 5;
if(num>9999)
num = 5;
}
while(C3==0)
{
ConfigSMG();
}
}
}
R3 = 0;
R1 = R2 = R4 = 1;
C1 = C2 = C3 = C4 = 1;
if(C1==0) //按键6
{
Delay1ms();
if(C1==0)
{
if(mode_set)
{
num = num *10 + 6;
if(num>9999)
num = 6;
}
while(C1==0)
{
ConfigSMG();
}
}
}
else if(C2==0) //按键7
{
Delay1ms();
if(C2==0)
{
if(mode_set)
{
num = num *10 + 7;
if(num>9999)
num = 7;
}
while(C2==0)
{
ConfigSMG();
}
}
}
else if(C3==0) //按键8
{
Delay1ms();
if(C3==0)
{
if(mode_set)
{
num = num *10 + 8;
if(num>9999)
num = 8;
}
while(C3==0)
{
ConfigSMG();
}
}
}
R4 = 0;
R1 = R2 = R3 = 1;
C1 = C2 = C3 = C4 = 1;
if(C1==0) //按键9
{
Delay1ms();
if(C1==0)
{
if(mode_set)
{
num = num *10 + 9;
if(num>9999)
num = 9;
}
while(C1==0)
{
ConfigSMG();
}
}
}
else if(C2==0) //设置按键
{
Delay1ms();
if(C2==0)
{
mode_set = ~mode_set;
while(C2==0)
{
ConfigSMG();
}
}
}
else if(C3==0) //清除按键
{
Delay1ms();
if(C3==0)
{
if(mode_set)
{
num = 0;
}
while(C3==0)
{
ConfigSMG();
}
}
}
}
/******LED闪烁设置*******/
void Led_light()
{
Select74HC573(4);
if(mode_temp==0)
{
delay_Led = 800;
if(flag_Led)
{
flag_Led = 0;
P0 = 0xFE; //打开LED1
}
else
{
P0 = 0xFF; //关闭LED1
}
}
else if(mode_temp==1)
{
delay_Led = 400;
if(flag_Led)
{
flag_Led = 0;
P0 = 0xFE; //打开LED1
}
else
{
P0 = 0xFF; //关闭LED1
}
}
else if(mode_temp==2)
{
delay_Led = 200;
if(flag_Led)
{
flag_Led = 0;
P0 = 0xFE; //打开LED1
}
else
{
P0 = 0xFF; //关闭LED1
}
}
Select74HC573(0);
}
/******温度区间设置******/
void Setmode_Temp()
{
max = num/100;
min = num%100;
if(max>min)
{
if(temp<min)
{
mode_temp = 0;
EA = 0;
Select74HC573(5);
P0 = 0x00; //关闭继电器
Select74HC573(0);
EA = 1;
}
else if(temp>max)
{
mode_temp = 2;
EA = 0;
Select74HC573(5);
P0 = 0x10; //打开继电器
Select74HC573(0);
EA = 1;
}
else
{
mode_temp = 1;
EA = 0;
Select74HC573(5);
P0 = 0x00; //关闭继电器
Select74HC573(0);
EA = 1;
}
Led_light();
}
else //设置温度错误
{
Select74HC573(4);
P0 = 0xFD; //LED2常亮
Select74HC573(0);
P0 = 0xFF;
}
}
/*******数码管动态刷新*******/
void ScanSMG()
{
static unsigned char i = 0;
P0 = 0xFF;
Select74HC573(6);
P0 = 0x01<<i;
Select74HC573(7);
P0 = LedBuff[i++];
if(i>=8)
i = 0;
Select74HC573(0);
}
/*******定时器0配置******/
void Timer0Init(void) //1毫秒@12.000MHz
{
AUXR &= 0x7F; //定时器时钟12T模式
TMOD &= 0xF0; //设置定时器模式
TL0 = 0x18; //设置定时初值
TH0 = 0xFC; //设置定时初值
TF0 = 0; //清除TF0标志
TR0 = 1; //定时器0开始计时
ET0 = 1;
EA = 1;
}
void InterruptTimer0() interrupt 1
{
static unsigned int cnt = 0;
ScanSMG();
cnt++;
if(cnt>=delay_Led)
{
flag_Led = 1;
cnt = 0;
}
}
|
|