#include <reg51.h>
#include<stdio.h>
unsigned char ledbuf[8];
code unsigned char ledmap[] = {0xc0, 0xf9, 0xa4, 0xb0, 0x99,
0x92, 0x82, 0xf8, 0x80, 0x90}; //8段显示
void delay(unsigned char cnt)
{
unsigned char i;
while(cnt--!=0)
for(i=100;i!=0;i--);
}
#define
tick 7554
#define T100us (256-134)
unsigned char hour,minute,second,ankey;
unsigned int c100us;
void sees()
{
ledbuf[0]=ledmap[hour/10];
ledbuf[1]=ledmap[hour%10];
ledbuf[2]=0xbf;
ledbuf[3]=ledmap[minute/10];
ledbuf[4]=ledmap[minute%10];
ledbuf[5]=0xbf;
ledbuf[6]=ledmap[second/10];
ledbuf[7]=ledmap[second%10];
}
void displayled()
{
char i;
unsigned char pos;
pos=0x01;
for(i=7;i>=0;i--)
{
P2=0x255;
P0=ledbuf[i];
P2=~pos;
delay(2);
P2=0x255;
pos<<=1;
}
}
void key()
{
if(P32==0||P33==0||P35==0)
delay(2);
if(P32==0||P33==0||P35==0)
{
if(P32==0)
{
hour++;
if(hour==24)
hour=0;
while(P32==0)
{
sees();
displayled();
}
}
if(P33==0)
{
minute++;
if(minute==60)
minute=0;
while(P33==0)
{
sees();
displayled();
}
}
if(P35==0)
{
second++;
if(second==60)
second=0;
while(P35==0)
{
sees();
displayled();
}
}
}
}
void t0int()interrupt 1
{
c100us--;
if(c100us==0)
{
c100us=tick;
second++;
if(second==60)
{
second=0;
minute++;
if(minute==60)
{
minute=0;
hour++;
if(hour==24)hour=0;
}
}
}
}
void main()
{
TMOD=0x02;
TH0=T100us;
TL0=T100us;
IE=0x82;
hour=0;
minute=0;
second=0;
c100us=tick;
TR0=1;
while(1)
{
sees();
displayled();
key();
}
}
求解key函数和void t0int()interrupt 1之间的关系?key函数不是已经实现了按键调时吗?为什么还需要时钟中断服务程序?中断函数里面写的是干嘛的?如果把key里面的调时写到void t0int()interrupt 1可以吗?刚学中断,不太懂。
一周热门 更多>