#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
#define CLOSE P2 &= 0x1f
#define y6 P2=P2&0x1f,P2=P2|0xc0
#define y7 P2=P2&0x1f,P2=P2|0xe0
#define y5 P2=P2&0x1f,P2=P2|0xa0
#define y4 P2=P2&0x1f,P2=P2|0x80
void nao();
sbit d=P0^6;
sbit k1=P3^0;
sbit k2=P3^1;
sbit k3=P3^2;
sbit k4=P3^3;
uint num=0,sum=0,miao=55,fen=0,shi=0,yue=11,ri=19,miao1=0,fen1=1,shi1=0,miao2=0;
unsigned char table[]={
0xc0,0xf9,0xa4,0xb0,
0x99,0x92,0x82,0xf8,0x80,0x90};
unsigned char colork[]={0xc0,0xf9,0xa4,0xb0,
0x99,0x92,0x82,0xf8,0x80,0x90};
uchar flag=0;
void time(uint n)
{
int i,j;
for(i=n;i>0;i--)
for(j=150;j>0;j--);
}
void feng() // 关闭蜂鸣器
{
y5;
d=0;
CLOSE;
}
void xiang() // 响铃 1
{
y5;
d=1;
CLOSE;
}
void xiang1() // 响铃 2
{
xiang();
time(500);
feng();
}
void xiang2() // 响铃 3
{
xiang();
time(500);
feng();
time(200);
xiang();
time(300);
feng();
time(200);
xiang();
time(100);
feng();
}
void display() // 时间显示
{
CLOSE;
P0=0x24; //0010 0100
P2=0xc0; //y6
CLOSE;
P0=0xbf;
P2=0xe0;
time(2); //y7
CLOSE; //suozhu shuzi
CLOSE;
P0=0x80; //1000 0000
P2=0xc0; //y6
CLOSE;
P0=table[miao%10];
P2=0xe0;
time(2); //y7
CLOSE; //suozhu shuzi
CLOSE;
P0=0x40; //0100 0000
P2=0xc0; //y6
CLOSE;
P0=table[miao/10];
P2=0xe0;
time(2); //y7
CLOSE; //suozhu shuzi
CLOSE;
P0=0x10; //0001 0000
P2=0xc0; //y6
CLOSE;
P0=table[fen%10];
P2=0xe0;
time(2); //y7
CLOSE; //suozhu shuzi
CLOSE;
P0=0x08; //0000 1000
P2=0xc0; //y6
CLOSE;
P0=table[fen/10];
P2=0xe0;
time(2); //y7
CLOSE; //suozhu shuzi
CLOSE;
P0=0x02; //0000 0010
P2=0xc0; //y6
CLOSE;
P0=table[shi%10];
P2=0xe0;
time(2); //y7
CLOSE; //suozhu shuzi
CLOSE;
P0=0x01; //0000 0001
P2=0xc0; //y6
CLOSE;
P0=table[shi/10];
P2=0xe0;
time(2); //y7
CLOSE; //suozhu shuzi
nao();
}
void naozhong() // 闹钟显示
{
CLOSE;
P0=0x80; //1000 0000
P2=0xc0; //y6
CLOSE; // suozhu weizhi
P0=table[miao2%10];
P2=0xe0; //y7
CLOSE; //suozhu shuzi
time(1);
CLOSE;
P0=0x40; //0100 0000
P2=0xc0; //y6
CLOSE; //suozhu weihzi
P0=table[miao2/10];
P2=0xe0; //y7
CLOSE; //suozhu shuzi
time(1);
CLOSE;
P0=0x01;
y6; // 0000 0001
CLOSE;
P0=table[shi1%10];
y7;
CLOSE;
time(4);
CLOSE;
P0=0x02;
y6; // 0000 0010
CLOSE;
P0=table[shi1/10];
y7;
CLOSE;
time(4);
CLOSE;
P0=0x04;
y6; // 0000 0100
CLOSE;
P0=0xbf; // 1011 1111
y7;
CLOSE;
time(4);
CLOSE;
P0=0x08;
y6; // 0000 1000
CLOSE;
P0=table[fen1/10];
y7;
CLOSE;
time(4);
CLOSE;
P0=0x10;
y6; // 0001 0000
CLOSE;
P0=table[fen1%10];
y7;
CLOSE;
time(4);
}
void play0()
{
uint temp;
P3=0xfe; // 检测第一行按键
//将第一行线置为低电平,其余全为高电平 1111 1110
temp=P3;
temp=temp&0xf0; //0xfe & 0xf0 1111 1110 & 1111 0000 = 1111 0000
if(temp!=0xf0)
{
time(10);
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0) //再次确认按键是否被按下,避免上次为抖动
{
temp=P3;
switch(temp) // 判断按键
{
case 0xee: //1110 1110
feng(); //关闹铃
break;
}
while(temp!=0xf0) // 等待按键释放
{
time(4);
feng();
temp=P3;
temp=temp&0xf0; // 将按键复原
}
}
}
}
void play1() // 调整时间
{
fen++;
if(fen==60)
{
fen=0;
shi++;
if(shi==24)
{
shi=0;
}
}
}
void play2() // 暂停时间
{
while(!0x7e);
TR0=~TR0;
}
void day() // 日期显示
{
CLOSE;
P0=0x01;
y6; // 0000 0001
CLOSE;
P0=table[yue%10];
y7;
CLOSE;
time(4);
CLOSE;
P0=0x02;
y6; // 0000 0010
CLOSE;
P0=table[yue/10];
y7;
CLOSE;
time(4);
CLOSE;
P0=0x04;
y6; // 0000 0100
CLOSE;
P0=0xbf; // 1011 1111
y7;
CLOSE;
time(4);
CLOSE;
P0=0x08;
y6; // 0000 1000
CLOSE;
P0=table[ri/10];
y7;
CLOSE;
time(4);
CLOSE;
P0=0x10;
y6; // 0001 0000
CLOSE;
P0=table[ri%10];
y7;
CLOSE;
time(4);
}
void play4() // 闹钟自定义
{
fen1++;
if(fen1==60)
{
fen=0;
shi1++;
if(shi1==24)
{
shi1=0;
}
}
}
void play5() // 日期自定义
{
ri++;
if(ri==31)
{
ri=1;
yue++;
if(yue==12)
{
yue=1;
}
}
}
void play6() // 进入闹钟界面
{
int temp;
temp=P3;
while(temp==P3)
naozhong();
}
void play7() // 进入日期界面
{
int temp;
temp=P3;
while(temp==P3)
day();
}
void play3() // 自定义闹铃
{
miao2++;
if(miao2==3)
miao2=0;
}
void major() // 矩阵按键扫描
{
uint temp;
P3=0xfe; // 检测第一行按键
//将第一行线置为低电平,其余全为高电平 1111 1110
temp=P3;
temp=temp&0xf0; //0xfe & 0xf0 1111 1110 & 1111 0000 = 1111 0000
if(temp!=0xf0)
{
time(10);
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0) //再次确认按键是否被按下,避免上次为抖动
{
temp=P3;
switch(temp) // 判断按键
{
case 0xee: //1110 1110
feng(); //关闹铃
break;
case 0xde:
play1(); // 调整时间 // 1101 1110
break;
case 0xbe:
play4(); //调整闹钟时间
break;
case 0x7e:
play7(); //0111 1110 进入riqi界面
break;
}
while(temp!=0xf0) // 等待按键释放
{
temp=P3;
temp=temp&0xf0; // 将按键复原
}
}
}
P3=0xfd; // 检测第二行按键 1111 1101
// 将第二行置为低电平,其余全为高电平
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
time(10);
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0) // 再次确认按键是否被按下,避免上次为抖动
{
temp=P3;
switch(temp) // 判断按键
{
case 0xed:
play2(); // 暂停时间
break;
case 0xdd:
play5(); // 日期自定义
break;
case 0xbd:
play6(); //进入闹钟界面
break;
case 0x7d:
play3(); // 自定义闹铃
break;
}
while(temp!=0xf0) //等待按键释放
{
temp=P3;
temp=temp&0xf0;
}
}
}
}
void nao() // 启用闹铃
{
if(fen==fen1&&shi==shi1&&miao==flag)
{
if(miao2==0)
xiang();
else if(miao2==1)
xiang1();
else if(miao2==2)
xiang2();
}
}
void main()
{
feng();
TMOD=0x01; // 设置定时器0为工作方式1(M1M0为01)
// TMOD是定时器的工作方式寄存器,确定工作方式和功能。
TH0=(65536-50000)/256; // 装初值11.0592M晶振定时50ms数为45872
TL0=(65536-50000)%256; //
EA=1; // 开总中断
ET0=1; // 开定时器0中断
TR0=1; // 启动定时器0
while(1) // 程序停止在这里不断扫描数码管的同时等待中断发生
{
display();
// play0();
major();
}
}
void TO_time() interrupt 1
{
TH0=(65536-50000)/256; //重装初值
TL0=(65536-50000)%256;
sum++; //num每加1次判断一次是否到20次
if(sum==20) //如果到了20次,说明1秒时间到
{
sum=0;
miao++;
if(miao==60)
{
miao=0;
fen++;
if(fen==60)
{
fen=0;
shi++;
if(shi==24)
{
shi=0;
ri++;
if(ri==31)
{
ri=0;
yue++;
{
if(yue==12)
{
yue=1;
nao();
}
}
}
}
}
}
}
}
|