#include<reg52.h>
sbit beep=P2^2;
sbit dula=P2^7;
sbit wela=P2^6;
sbit mode=P3^4;
sbit up=P3^5;
sbit down=P3^6;
sbit alarm=P3^7;
#define uint unsigned int
#define uchar unsigned char
uchar temp,temp1,temp2,aa,h,h1,m,m1,s,s1,key,t;//显示部分定义
uchar mmode,uup,ddown,aalarm;//记录键盘获取的数据
uchar code table[] = {0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x90, 0x88, 0x83, 0xc6, 0xa1, 0x86, 0x8e};
void delay(uint z) //延时
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void keyscan() //键盘扫描
{
while(1)
{
P3=0xfe;
t=P3;
t=t&0xf0;
if(t!=0xf0)
{
delay(10);
if(t!=0xf0)
{
t=P3;
switch(t)
{
case 0xee:key=0;break;
case 0xde:key=1;break;
case 0xbe:key=2;break;
case 0x7e:key=3;break;
}
while(t!=0xf0)
{
t=P3;
t=t&0xf0;
beep=0;
}
beep=1;
P1=0xfe;
}
}//第一行键盘扫描
P3=0xfd;
t=P3;
t=t&0xf0;
if(t!=0xf0)
{
delay(10);
if(t!=0xf0)
{
t=P3;
switch(t)
{
case 0xed:key=4;break;
case 0xdd:key=5;break;
case 0xbd:key=6;break;
case 0x7d:key=7;break;
}
while(t!=0xf0)
{
t=P3;
t=t&0xf0;
beep=0;
}
beep=1;
}
}//第二行键盘扫描
P3=0xfb;
t=P3;
t=t&0xf0;
if(t!=0xf0)
{
delay(10);
if(t!=0xf0)
{
t=P3;
switch(t)
{
case 0xeb:key=8;break;
case 0xdb:key=9;break;
case 0xbb:key=10;break;
case 0x7b:key=11;break;
}
while(t!=0xf0)
{
t=P3;
t=t&0xf0;
beep=0;
}
beep=1;
}
}//第三行键盘扫描
P3=0xf7;
t=P3;
t=t&0xf0;
if(t!=0xf0)
{
delay(10);
if(t!=0xf0)
{
t=P3;
switch(t)
{
case 0xe7:key=12;break;
case 0xd7:key=13;break;
case 0xb7:key=14;break;
case 0x77:key=15;break;
}
while(t!=0xf0)
{
t=P3;
t=t&0xf0;
beep=0;
}
beep=1;
}
}//第四行键盘扫描
}
}
void init() //初始化
{
wela=0;
dula=0;
temp=0;
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
ET0=1;
TR0=1;
}
void display(uchar h,uchar h1,uchar m,uchar m1,uchar s,uchar s1) //LED显示
{
dula=1;
P0=table[h];
dula=0;
P0=0x00;
wela=1;
P0=0x01;
wela=0;
delay(1);
dula=1;
P0=table[h1];
dula=0;
P0=0x00;
wela=1;
P0=0x02;
wela=0;
delay(1);
dula=1;
P0=0xBF;
dula=0;
P0=0x00;
wela=1;
P0=0x04;
wela=0;
delay(1);
dula=1;
P0=table[m];
dula=0;
P0=0x00;
wela=1;
P0=0x08;
wela=0;
delay(1);
dula=1;
P0=table[m1];
dula=0;
P0=0x00;
wela=1;
P0=0x10;
wela=0;
delay(1);
dula=1;
P0=0xBF;
dula=0;
P0=0x00;
wela=1;
P0=0x20;
wela=0;
delay(1);
dula=1;
P0=table[s];
dula=0;
P0=0x00;
wela=1;
P0=0x40;
wela=0;
delay(1);
dula=1;
P0=table[s1];
dula=0;
P0=0x00;
wela=1;
P0=0x80;
wela=0;
delay(1);
}
void main() //主程序
{
init();
while(1)
{
if(aa==20)
{
aa=0;
temp++;
if(temp==60)
{
temp=0;
temp1++;
}
if(temp1==60)
{
temp1=0;
temp2++;
}
if(temp2==24)
{
temp2=0;
}
s1=temp%10;
s=temp/10;
m1=temp1%10;
m=temp1/10;
h1=temp2%10;
h=temp2/10;
}
display(h,h1,m,m1,s,s1);
}
}
void timer0() interrupt 1 //中断1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
aa++;
}
要实现的目标就是当按下独立键盘的第一个键时,开始设置时钟。按下独立键盘第二个键时,停止设置时钟。
矩阵键盘第一个设置小时模式,第一列第二行增加小时数,第一列第三行减少小时数。然后以此类推。求各位大侠帮忙。单片机是探索的。。 |