|
楼主 |
发表于 2023-11-10 17:05:42
|
显示全部楼层
计时器 电子时钟已做完 中断基本可以理解
下面是电子时钟代码
#include<Reg52.h>
sbit key1=P3^1;
sbit key2=P3^0;
sbit key3=P3^2;
sbit key4=P3^3;
char a[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
int n=0;
int cc = 0;
int T1MS = 65536-50000;
int hour;
int minute;
int second;
void disp_time(int hour, int minute, int second);
void init_interrupt();
void delay(int i)
{
int x,y;
for(x=0;x<i;x++)
for(y=0;y<255;y++);
}
void disp_time(int hour, int minute, int second)
{
P0 = 0x00;
P2=0xFF;//111(1 11)11
P0=a[hour/10];
delay(2);
P0 = 0x00;
P2=0xFB;//111(1 10)11
P0=a[hour%10];
delay(2);
P0 = 0x00;
P2=0xF3;//111(1 00)11
P0=a[minute/10];
delay(2);
P0 = 0x00;
P2=0xEF;//111(0 11)11
P0=a[minute%10];
delay(2);
P0 = 0x00;
P2=0xE7;//111(0 01)11
P0=a[second/10];
delay(2);
P0 = 0x00;
P2=0xE3;//111(0 00)11
P0=a[second%10];
delay(2);
}
void main()
{
init_interrupt();
while(1)
{
disp_time(hour,minute,second);
if(key1==0)
{
delay(2);
if(key1==0)
{
hour+=1;
}
while(key1==0);
}
if(key2==0)
{
delay(2);
if(key2==0)
{
minute+=1;
}
while(key2==0);
}
if(key3==0)
{
delay(2);
if(key3==0)
{
second+=1;
}
while(key3==0);
}
if(key4==0)
{
delay(2);
if(key4==0)
{
hour=0;
minute=0;
second=0;
}
}
}
}
void init_interrupt()
{
TMOD = 0x00;
TL0 = T1MS%256;
TH0 = T1MS/256;
TR0= 1;
ET0 = 1;
EA = 1;
}
void T0_interrupt() interrupt 1
{
TL0 = T1MS%256;
TH0 = T1MS/256;
cc++;
if (cc>=80)
{
cc = 0;
second++;
}
if(second==60)
{
second=0;
minute+=1;
}
if(minute==60)
{
minute=0;
hour+=1;
}
if(hour==24)
{
hour=0;
minute=0;
second=0;
}
} |
|