全部文件:提取码:azxy 这一届我感觉的难点应该是温度设置时的按键处理吧。不过每个人的理解不一样,难点感觉也不一样。还是直接看题目吧。
#include "stc15f2k60s2.h"
#include "onewire.h"
typedef unsigned char u8;
typedef unsigned int u16;
sbit H1=P3^0;
sbit H2=P3^1;
sbit H3=P3^2;
sbit H4=P3^3;
sbit L1=P4^4;
sbit L2=P4^2;
sbit L3=P3^5;
u8 code duan[12]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff,0xbf};
u8 temp_setup[4];
u8 model=1;
u8 qujian=0;
u8 temp=0;
u8 temp_sx=30;
u8 temp_xx=20;
u8 key_num=0;
u8 flag=0;
u8 mi=0;
u8 num=0;
u8 i=0;
u8 count=0;
u8 led_flag=0;
void delay(u16 i)
{
while(i--);
}
void Timer0Init(void)
{
AUXR |= 0x80;
TMOD &= 0xF0;
TL0 = 0xA0;
TH0 = 0x15;
TF0 = 0;
TR0 = 1;
ET0=1;
EA=1;
}
void Service_T0() interrupt 1
{
count++;
if(count==160/(2^qujian))
{
count=0;
led_flag=~led_flag;
}
}
void Chan_hc573(chose)
{
switch(chose)
{
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;
case 0:
P2 = (P2 & 0x1f) | 0x00; break;
}
}
void Init_system()
{
Chan_hc573(5);
P0=0x00;
Chan_hc573(4);
P0=0xff;
}
void Smg_display(u8 com,u8 dat)
{
Chan_hc573(7);
P0=0xff;
Chan_hc573(6);
P0=0x01<<com;
Chan_hc573(7);
P0=dat;
}
void Close_smg()
{
Chan_hc573(6);
P0=0x00;
Chan_hc573(7);
P0=0xff;
}
void Temp_display_setup()
{
if(model==1)
{
Smg_display(0,duan[11]); delay(1000);
Smg_display(1,duan[qujian]); delay(1000);
Smg_display(2,duan[11]); delay(1000);
Smg_display(3,duan[10]); delay(1000);
Smg_display(4,duan[10]); delay(1000);
Smg_display(5,duan[11]); delay(1000);
Smg_display(6,duan[temp/10]); delay(1000);
Smg_display(7,duan[temp%10]); delay(1000);
Close_smg();
}
else if(model==2)
{
Smg_display(0,duan[11]); delay(1000);
Smg_display(1,duan[temp_setup[0]]); delay(1000);
Smg_display(2,duan[temp_setup[1]]); delay(1000);
Smg_display(3,duan[10]); delay(1000);
Smg_display(4,duan[10]); delay(1000);
Smg_display(5,duan[11]); delay(1000);
Smg_display(6,duan[temp_setup[2]]); delay(1000);
Smg_display(7,duan[temp_setup[3]]); delay(1000);
Close_smg();
}
}
void Key_down()
{
H1=0;
H2=H3=H4=L1=L2=L3=1;
if(L1==0)
{
delay(100);
if(L1==0)
{
flag=1;
while(L1==0) {Temp_display_setup();}
key_num=0;
}
}
else if(L2==0)
{
delay(100);
if(L2==0)
{
flag=1;
while(L2==0) {Temp_display_setup();}
key_num=1;
}
}
else if(L3==0)
{
delay(100);
if(L3==0)
{
flag=1;
while(L3==0) {Temp_display_setup();}
key_num=2;
}
}
H2=0;
H1=H3=H4=L1=L2=L3=1;
if(L1==0)
{
delay(100);
if(L1==0)
{
flag=1;
while(L1==0) {Temp_display_setup();}
key_num=3;
}
}
else if(L2==0)
{
delay(100);
if(L2==0)
{
flag=1;
while(L2==0) {Temp_display_setup();}
key_num=4;
}
}
else if(L3==0)
{
delay(100);
if(L3==0)
{
flag=1;
while(L3==0) {Temp_display_setup();}
key_num=5;
}
}
H3=0;
H2=H1=H4=L1=L2=L3=1;
if(L1==0)
{
delay(100);
if(L1==0)
{
flag=1;
while(L1==0) {Temp_display_setup();}
key_num=6;
}
}
else if(L2==0)
{
delay(100);
if(L2==0)
{
flag=1;
while(L2==0) {Temp_display_setup();}
key_num=7;
}
}
else if(L3==0)
{
delay(100);
if(L3==0)
{
flag=1;
while(L3==0) {Temp_display_setup();}
key_num=8;
}
}
H4=0;
H2=H3=H1=L1=L2=L3=1;
if(L1==0)
{
delay(100);
if(L1==0)
{
flag=1;
while(L1==0) {Temp_display_setup();}
key_num=9;
}
}
else if(L2==0)
{
delay(100);
if(L2==0)
{
while(L2==0) {Temp_display_setup();}
if(model==1)
{
for(i=0;i<4;i++) {temp_setup[i]=10;}
model=2; num=0;
}
else if(model==2)
{
model=1;
temp_sx=temp_setup[0]*10+temp_setup[1];
temp_xx=temp_setup[2]*10+temp_setup[3];
}
}
}
else if(L3==0)
{
delay(100);
if(L3==0)
{
while(L3==0) {Temp_display_setup();}
if(model==2)
{
for(i=0;i<4;i++) {temp_setup[i]=10; num=0;}
}
}
}
}
void Setup_temp()
{
if(model==2)
{
if(flag==1)
{
switch(key_num)
{
case 0: mi=0; break;
case 1: mi=1; break;
case 2: mi=2; break;
case 3: mi=3; break;
case 4: mi=4; break;
case 5: mi=5; break;
case 6: mi=6; break;
case 7: mi=7; break;
case 8: mi=8; break;
case 9: mi=9; break;
}
if(num<4)
{
temp_setup[num]=mi;
num++;
}
flag=0;
}
}
}
void Temp_qj()
{
if(temp<temp_xx) {qujian=0;}
else if(temp>=temp_xx&&temp<=temp_sx) {qujian=1;}
else if(temp>temp_sx) {qujian=2;}
if(qujian==0)
{
Chan_hc573(4); if(led_flag==0) {P00=0;}
Chan_hc573(5); P04=0; Chan_hc573(0);
}
else if(qujian==1)
{
Chan_hc573(4); if(led_flag==0) {P00=0;}
Chan_hc573(5); P04=0; Chan_hc573(0);
}
else if(qujian==2)
{
Chan_hc573(4); if(led_flag==0) {P00=0;}
Chan_hc573(5); P04=1; Chan_hc573(0);
}
if(temp_xx>temp_sx)
{
Chan_hc573(4); P01=0;
}
}
void main()
{
Init_system();
Timer0Init();
while(1)
{
Temp_qj();
Key_down();
Setup_temp();
Temp_display_setup();
temp=DS18B20_Read();
}
}
|