//********************************************************************************************************
//名称:按键控制分区器的分区打开与关闭
//********************************************************************************************************
//说明:按下K1时分区1打开,再次按下K1时分区1关闭;其它分区雷同.
//********************************************************************************************************
#include
#define uchar unsigned char
#define uint unsigned int
sbit ch1 = P1^7;
sbit ch2 = P1^6;
sbit ch3 = P1^5;
sbit ch4 = P1^4;
sbit ch5 = P1^3;
sbit k1 = P3^0;
sbit k2 = P3^1;
sbit k3 = P3^2;
sbit k4 = P3^4;
sbit k5 = P3^5;
//********************************************************************************************************
//延时
//********************************************************************************************************
void Delay_ms(uint ms)
{
uchar t;
while(ms--) for(t = 0; t < 120; t++ );
}
void keys_Scan1()
{
k1=0;
k2=1;
k3=1;
k4=1;
k5=1;
Delay_ms(20);
if(k5==0)
{
while(k5==0);
ch1 = ~ch1;
Delay_ms(20);
}
if(k4==0)
{
while(k4==0);
ch3 = ~ch3;
Delay_ms(20);
}
if(k2==0)
{
while(k2==0);
ch4 = ~ch4;
Delay_ms(20);
}
}
void keys_Scan2()
{
k1=1;
k2=0;
k3=1;
k4=1;
k5=1;
Delay_ms(20);
if(k5==0)
{
while(k5==0);
ch5 = ~ch5;
Delay_ms(20);
}
if(k4==0)
{
while(k4==0);
ch2 = ~ch2;
Delay_ms(20);
}
}
//********************************************************************************************************
//主程序
//********************************************************************************************************
void main()
{
P1=0xFF;
P3=0xFF;
while(1)
{
keys_Scan1();
Delay_ms(20);
keys_Scan2();
Delay_ms(20);
}
}