#include #define GET_KEY P1
#define GET_LED P0
sbit LSA=P2^2;
sbit LSB=P2^3;
sbit LSC=P2^4;
unsigned char code DIG_CODE[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71}; //“-”的段码是0x40
unsigned char flag=0,power=0; //flag 数字键加1; power表示电源键是否按下
unsigned char Keyvalue,Keystate;
unsigned char fenh,fenl,miaoh,miaol,huo;
void delay(unsigned int u)
{ unsigned char i;
for(;u>0;u--)
for(i=50;i>0;i--);
}
void Key_Scan()
{ GET_KEY=0x0f;
if(GET_KEY!=0x0f)
{ delay(15);
if(GET_KEY!=0x0f)
{ Keystate=1; //有键按下
GET_KEY=0x0f; //测试列
switch(GET_KEY)
{
case(0X07):Keyvalue=0;break;
case(0X0b):Keyvalue=4;break;
case(0X0d): Keyvalue=8;break;
case(0X0e):Keyvalue=12;break;
}
GET_KEY=0xf0; //测试行
switch(GET_KEY)
{
case(0X70):Keyvalue=Keyvalue+3;break;
case(0Xb0):Keyvalue=Keyvalue+2;break;
case(0Xd0): Keyvalue=Keyvalue+1;break;
case(0Xe0):Keyvalue=Keyvalue;break;
}
while(GET_KEY!=0xf0); //松手检测
if(Keyvalue==0x0f)
power++;
else flag++;
}
}
}
void init_t0()
{TH0=(65536-20000)/256;
TL0=(65536-20000)%256; //定时20ms
EA=1;
ET0=1;
TMOD=0x01;
}
void display1() //LED动态显示函数
{ unsigned char i;
for(i=0;i<8;i++)
{ switch(i)
{ case 0: LSC=0;LSB=0;LSA=0;GET_LED=DIG_CODE[fenh];break;
case 1: LSC=0;LSB=0;LSA=1;GET_LED=DIG_CODE[fenl];break;
case 2: LSC=0;LSB=1;LSA=0;GET_LED=0x40;break;
case 3: LSC=0;LSB=1;LSA=1;GET_LED=0x40;break;
case 4: LSC=1;LSB=0;LSA=0;GET_LED=DIG_CODE[miaoh];break;
case 5: LSC=1;LSB=0;LSA=1;GET_LED=DIG_CODE[miaol];break;
case 6: LSC=1;LSB=1;LSA=0;GET_LED=DIG_CODE[1];break;
case 7: LSC=1;LSB=1;LSA=1;GET_LED=DIG_CODE[huo];break;
}
delay(1);
GET_LED=0x00;
}
}
void display2()
{ unsigned char i;
for(i=0;i<8;i++)
{ switch(i)
{ case 0: LSC=0;LSB=0;LSA=0;break;
case 1: LSC=0;LSB=0;LSA=1;break;
case 2: LSC=0;LSB=1;LSA=0;break;
case 3: LSC=0;LSB=1;LSA=1;break;
case 4: LSC=1;LSB=0;LSA=0;break;
case 5: LSC=1;LSB=0;LSA=1;break;
case 6: LSC=1;LSB=1;LSA=0;break;
case 7: LSC=1;LSB=1;LSA=1;break;
}
GET_LED=0x00;
delay(1);
}
}
void main()
{unsigned char temp;
Keystate=0;
init_t0();
TR0=0;//定时器初始化
while(1)
{ Key_Scan();
temp=Keyvalue;
if(Keystate==1) //有键按下
{ if(temp==0x0f)
{
if(power==1)
{ huo=0;
fenh=0;
fenl=0;
miaoh=0;
miaol=0;
} //电源打开
if(power==2)
{
power=0;
} //电源关掉
}
if(flag==1)
{
if(temp>=0&&temp<=5)
fenh=temp;
else flag--;
}
if(flag==2)
{
if(temp>=0&&temp<=9)
fenl=temp;
else flag--;
}
if(flag==3)
{
if(temp>=0&&temp<=5)
miaoh=temp;
else flag--;
}
if(flag==4)
{
if(temp>=0&&temp<=9)
miaol=temp;
else flag--;
}
if(flag==5)
{
if(temp>=0x0a&&temp<=0x0c)
{
huo=temp;
TR0=1;
flag=0;
// 先输入定时时间,然后按火候键开始定时
}
else flag--;
}
}
// 开始显示
if(power==1) display1();
if(power==0) display2();//电源关闭,灯灭,重新开始输入、定时
}
}
void timer0() interrupt 1
{ static unsigned char count=0;
TH0=(65536-20000)/256;
TL0=(65536-20000)%256;
count++;
if(count==50) //1ms定时到
{miaol--;
count=0;
}
if(miaol==0xff)
{ miaoh--;
miaol=9;
}
if(miaoh==0xff)
{ fenl--;
miaoh=5;
}
if(fenl==0xff)
{ fenh--;
fenl=9;
}
if(fenh==0xff)
{ TR0=0;
fenh=0;
fenl=0;
miaoh=0;
miaol=0;
power=0; //定时时间到,关电源
}
}
如果觉得《51单片机微波炉c语言程序 基于51单片机的微波炉控制C源程序.doc》对你有帮助,请点赞、收藏,并留下你的观点哦!