#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();
}
}
}
}
}
}
}
}
|