|
新手纠结很多天了,求解答出来!90c52多功能电子时钟,有秒表,闹钟功能,用4*3键盘
[复制链接]
#include
#define uint unsigned int
#define uchar unsigned char
// P0段显 P2位显
sbit beep=P1^5;
uint hour=19,minute=58,second=48;
uint num,num1;
uchar temp0,temp1,temp2,temp3;
uint hour_shi,hour_ge,minute_shi,minute_ge,second_shi,second_ge;
uint key1=1,key2=1,key3=1,key4=1,key5=1,key6=1,key7=1,key8=1,key9=1,key10=1,key11=1,key12=1;
uint clock_minute,clock_second,clock_minute_shi,clock_minute_ge,clock_second_shi,clock_second_ge;
uint stopwatch_minute,stopwatch_second,stopwatch_minute_shi,stopwatch_minute_ge,stopwatch_second_shi,stopwatch_second_ge;
uint watch_minute,watch_second,watch_minute_shi,watch_minute_ge,watch_second_shi,watch_second_ge;
uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
void delay(uint z) //延时
{
int x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void display_hour_shi() //显示小时十位
{
hour_shi=hour/10;
P2=0xfe;
P0=table[hour_shi];
delay(1);
}
void display_hour_ge() //显示小时十位
{
hour_ge=hour%10;
P2=0xfd;
P0=table[hour_ge];
delay(1);
}
void display_minute_shi() //显示分钟十位
{
minute_shi=minute/10;
P2=0xfb;
P0=table[minute_shi];
delay(1);
}
void display_minute_ge() //显示分钟个位
{
minute_ge=minute%10;
P2=0xf7;
P0=table[minute_ge];
delay(1);
}
void display_second_shi() //显示秒钟十位
{
second_shi=second/10;
P2=0xef;
P0=table[second_shi];
delay(1);
}
void display_second_ge() //显示秒钟个位
{
second_ge=second%10;
P2=0xdf;
P0=table[second_ge];
delay(1);
}
void display_time() //显示时间
{
display_hour_ge();
display_hour_ge();
display_minute_shi();
display_minute_ge();
display_second_shi();
display_second_ge();
}
void display_clock_time() //显示闹钟时间
{
clock_minute_shi=clock_minute/10;
P2=0xfb;
P0=table[clock_minute_shi];
delay(1);
clock_minute_ge=clock_minute%10;
P2=0xf7;
P0=table[clock_minute_ge];
delay(1);
clock_second_shi=clock_second/10;
P2=0xef;
P0=table[clock_second_shi];
delay(1);
clock_second_ge=clock_second%10;
P2=0xdf;
P0=table[clock_second_ge];
delay(1);
}
void display_stopwatch_time() //秒表显示
{
stopwatch_minute_shi=stopwatch_minute/10;
P2=0xfb;
P0=table[stopwatch_minute_shi];
delay(1);
stopwatch_minute_ge=stopwatch_minute%10;
P2=0xf7;
P0=table[stopwatch_minute_ge];
delay(1);
stopwatch_second_shi=stopwatch_second/10;
P2=0xef;
P0=table[stopwatch_second_shi];
delay(1);
stopwatch_second_ge=stopwatch_second%10;
P2=0xdf;
P0=table[stopwatch_second_ge];
delay(1);
}
void ring() //蜂鸣器报警
{
beep=0;
delay(100);
beep=1;
}
void silent() //蜂鸣器安静
{
beep=1;
delay(100);
beep=0;
}
void function() //功能行
{
uchar temp1;
P3=0xbf;
temp1=P3;
temp1=temp1&0xf0;
if(temp1!=0xf0)
{
delay(100);
temp1=P3;
temp1=temp1&0xf0;
if(temp1!=0xf0)
{
temp1=P3;
switch(temp1)
{case 0xb7: key9=0;break;
case 0xbb: key10=0;break;
case 0xbd: key11=0;break;
case 0xbe: key12=0;break;}
}
while(temp1!=0xf0)
{
temp1=P3;
temp1=temp1&0xf0;
}
}
}
void timedisplay() //调节显示时间
{
uchar temp2;
P3=0xdf;
temp2=P3;
temp2=temp2&0xf0;
if(temp2!=0xf0)
{
delay(100);
temp2=P3;
temp2=temp2&0xf0;
if(temp2!=0xf0)
{
temp2=P3;
switch(temp2)
{case 0xd7: key1=0;break;
case 0xdb: key2=0;break;
case 0xdd: key3=0;break;
case 0xde: key4=0;break;}
}
while(temp2!=0xf0)
{
temp2=P3;
temp2=temp2&0xf0;
}
}
}
void clockdisplay() //调节闹钟时间
{
uchar temp3;
P3=0xef;
temp3=P3;
temp3=temp3&0xf0;
if(temp3!=0xf0)
{
delay(100);
temp3=P3;
temp3=temp3&0xf0;
if(temp3!=0xf0)
{
temp3=P3;
switch(temp3)
{case 0xe7: key5=0;break;
case 0xeb: key6=0;break;
case 0xed: key7=0;break;
case 0xee: key8=0;break;}
}
while(temp3!=0xf0)
{
temp3=P3;
temp3=temp3&0xf0;
}
}
}
void clock_open() //开启闹钟
{
if(key9==0)
{ display_clock_time();
delay(5);
if(key9==0)
{
while(key9!=1)
{
display_clock_time();
clock_minute=0;
clock_second=0;
if(clock_minute==minute&&clock_second==second)
{
ring();
temp0=P3;
while(temp0!=0xff)
silent();
}
}
}
}
}
void stopwatch_open() //开启秒表
{
if(key10==0)
{ display_stopwatch_time();
delay(5);
if(key10==0)
{
while(key10!=1)
{
display_stopwatch_time();
stopwatch_minute=0;
stopwatch_second=0;
}
}
}
}
void stopwatch_close() //秒表停止
{
if(key11==0)
{ display_stopwatch_time();
delay(5);
if(key11==0)
{
while(key11!=1)
{
display_stopwatch_time();
watch_minute=stopwatch_minute;
watch_second=stopwatch_second;
watch_minute_shi=watch_minute/10;
P2=0xfb;
P0=table[watch_minute_shi];
delay(1);
watch_minute_ge=watch_minute%10;
P2=0xf7;
P0=table[watch_minute_ge];
delay(1);
watch_second_shi=watch_second/10;
P2=0xef;
P0=table[watch_second_shi];
delay(1);
watch_second_ge=watch_second%10;
P2=0xdf;
P0=table[watch_second_ge];
delay(1);
}
}
}
}
void reset() //复位
{
if(key12==0)
{ display_time();
delay(5);
if(key12==0)
{
while(key12!=1)
{
display_time();
}
}
}
}
void minute_add() //显示时间分钟加1
{
if(key1==0)
{ display_time();
delay(5);
if(key1==0)
{
while(key1!=1)
{
display_time();
minute++;
if(minute==60)
minute=0;
}
}
}
}
void minute_minus() //显示时间分钟减1
{
if(key2==0)
{ display_time();
delay(5);
if(key2==0)
{
while(key2!=1)
{
display_time();
minute--;
if(minute==0)
minute=60;
}
}
}
}
void second_add() //显示时间秒钟加1
{
if(key3==0)
{ display_time();
delay(5);
if(key3==0)
{
while(key3!=1)
{
display_time();
second++;
if(second==60)
second=0;
}
}
}
}
void second_minus() //显示时间秒钟减1
{
if(key4==0)
{ display_time();
delay(5);
if(key4==0)
{
while(key4!=1)
{
display_time();
second--;
if(second==0)
second=60;
}
}
}
}
void clock_minute_add() //显示闹钟分钟加1
{
if(key5==0)
{ display_clock_time();
delay(5);
if(key5==0)
{
while(key5!=1)
{
display_clock_time();
clock_minute++;
if(clock_minute==60)
clock_minute=0;
}
}
}
}
void clock_minute_minus() //显示闹钟分钟减1
{
if(key6==0)
{ display_clock_time();
delay(5);
if(key6==0)
{
while(key6!=1)
{
display_clock_time();
clock_minute--;
if(clock_minute==0)
clock_minute=60;
}
}
}
}
void clock_second_add() //显示闹钟秒钟加1
{
if(key7==0)
{ display_clock_time();
delay(5);
if(key7==0)
{
while(key7!=1)
{
display_clock_time();
clock_second++;
if(clock_second==60)
clock_second=0;
}
}
}
}
void clock_second_minus() //显示闹钟秒钟减1
{
if(key8==0)
{ display_clock_time();
delay(5);
if(key8==0)
{
while(key8!=1)
{
display_clock_time();
clock_second--;
if(clock_second==0)
clock_second=60;
}
}
}
}
main() //主函数
{
hour=10,minute=48,second=52;
TMOD=0x01;
TH0=(65536-45872)/256;
TL0=(65536-45872)%256;
TH1=(65536-45872)/256;
TL1=(65536-45872)%256;
EA=1;
ET0=1;
TR0=1;
ET1=1;
TR1=1;
beep=1;
while(1)
{ display_time();
function();
timedisplay();
clockdisplay();
clock_open();
stopwatch_open();
stopwatch_close();
reset();
minute_add();
minute_minus();
second_add();
second_minus();
clock_minute_add();
clock_minute_minus();
clock_second_add();
clock_second_minus();
}
}
void T0_time() interrupt 1 //显示时间中断函数
{
TH0=(65536-45872)/256;
TL0=(65536-45872)%256;
num++;
if(num==20)
{
num=0;
second++;
if(second==60)
{
second=0;
minute++;
if(minute==60)
{
minute=0;
hour++;
if(hour==60)
hour=0;
}
}
}
}
void T1_time() interrupt 3 //秒表时间中断函数
{
TH1=(65536-45872)/256;
TL1=(65536-45872)%256;
num1++;
if(num1==20)
{
num1=0;
stopwatch_second++;
if(stopwatch_second==60)
{
stopwatch_second=0;
stopwatch_minute++;
if(stopwatch_minute==60)
{
stopwatch_minute=0;
}
}
}
}
|
|