#include <AT89X52.H> unsigned char dispbitcnt; //数码管位码扫描变量
unsigned char second; //秒变量
unsigned char minite; //分变量
unsigned char hour; //时变量
unsigned char tcnt; //秒信号产生变量
unsigned char mstcnt; //扫描时间变量 const unsigned char dispcode[10]={0xc0,0xf9,0xa4,0xb0,0x99,<br> 0x92,0x82,0xf8,0x80,0x90};
//0123456789段码
const unsigned char dispbitcode[6]={0xfe,0xfd,0xfb,0xf7,<br> 0xef,0xdf};
//123456位码
unsigned char dispbuf[6]={0,0,0,0,0,0};
//缓冲数组
//**************TMR0中断数码管扫描程序*********************
void Timer0() interrupt 1
{ P2=dispcode[dispbuf[dispbitcnt]];//段码
P1=dispbitcode[dispbitcnt];//位码
dispbitcnt++;
if(dispbitcnt==6)
{dispbitcnt=0;<br> }
tcnt++; //中断产生秒信号
if(tcnt==244)
{tcnt=0;<br> second++;<br> if(second==60)<br> {second=0;<br> minite++;<br> if(minite==60)<br> {minite=0;<br> hour++;<br> if(hour==24)<br> {hour=0;<br> }
}
}
dispbuf[4]=second%10; //得秒钟的低位,送缓冲数组
dispbuf[5]=second/10; //得秒钟的高位,送缓冲数组
dispbuf[2]=minite%10; //得分钟的低位,送缓冲数组
dispbuf[3]=minite/10; //得分钟的高位,送缓冲数组
dispbuf[0]=hour%10; //得时钟的低位,送缓冲数组
dispbuf[1]=hour/10; //得时钟的高位,送缓冲数组
}
TMOD=0x01;
TL0=0x10;
TH0=0xf0; }
///****************1ms延时程序******************
void delay_1ms(unsigned char i)
{
unsigned char j;
for(i=0;i<144;i++)
for(j=0;j<144;j++);
} ///****************按键检测*********************
void key_scan(void)
{
while(1)
{
if(P3_0==0)
{
delay_1ms(20);
if(P3_0==0)
{second++;<br> if(second==60)<br> {second=0;<br> }
dispbuf[4]=second%10;
dispbuf[5]=second/10;
while(P3_0==0);
}
}
if(P3_1==0)
{
delay_1ms(20);
if(P3_1==0)
{
minite++;
if(minite==60)
{minite=0;<br> }
dispbuf[2]=minite%10;
dispbuf[3]=minite/10;
while(P3_1==0);
}
}
if(P3_2==0)
{
delay_1ms(20);
if(P3_2==0)
{
hour++;
if(hour==24)
{
hour=0;
}
}
dispbuf[0]=hour%10;
dispbuf[1]=hour/10;
while(P3_2==0);
}
}
}
//*************主程序********************
void main(void)
{
TMOD=0x01;TL0=0x10;
TH0=0xf0;
while(1)
{
dispbitcnt=0;P1=0x00; //开段码
P2=0x00; //开位码
delay_1ms(200); //延时
delay_1ms(200); //延时
delay_1ms(200); //延时
delay_1ms(200); //延时
P2=0xff; //关位码EA=1;
ET0=1;
TR0=1;
key_scan();
}
}
|