#include "reg52.h"
sbit S4 = P3^3;
sbit S5 = P3^2;
unsigned char fen = 0;
unsigned char miao = 0;
unsigned char t_005ms = 0;
void delay(unsigned char t)
{
while(t--);
}
unsigned char code table[] =
{
0xc0,//0
0xf9,//1
0xa4,//2
0xb0,//3
0x99,//4
0x92,//5
0x82,//6
0xf8,//7
0x80,//8
0x90,//9
0x88,//A
0x83,//B
0xc6,//C
0xa1,//D
0x86,//E
0x8e,//F
0xbf,//-
0x7f//.
};
void SelectHC573(unsigned char n)
{
switch(n)
{
case 4:
P2 = (P2&0x1f) | 0x80;
break;
case 5:
P2 = (P2&0x1f) | 0xa0;
break;
case 6:
P2 = (P2&0x1f) | 0xc0;
break;
case 7:
P2 = (P2&0x1f) | 0xe0;
break;
}
}
void display(unsigned char val,unsigned char pos)
{
SelectHC573(6);
P0 = 0x01<<pos;
SelectHC573(7);
P0 = table[val];
}
void showtime()
{
display(t_005ms%10,7);//个0.05ms
delay(500);
display(t_005ms/10,6);//十
delay(500);
display(16,5);
delay(500);
display(miao%10,4);//个秒
delay(500);
display(miao/10,3);//十
delay(500);
display(16,2);
delay(500);
display(fen%10,1);//个分
delay(500);
display(fen/10,0);//十
delay(500);
}
//========定时器相关函数==============
void InitTimer0()
{
TMOD = 0x01;
TH0 = (65535 - 50000)/256;
TL0 = (65535 - 50000)%256;
ET0 = 1;
EA = 1;
TR0 = 1;
}
void Timer0() interrupt 1
{
TH0 = (65535 - 50000)/256;
TL0 = (65535 - 50000)%256;
t_005ms++;
if(t_005ms == 20)
{
miao++;
t_005ms = 0;
if(miao == 60)
{
fen++;
miao = 0;
}
if(fen == 99)
{
fen = 0;
}
}
}
//==============按键扫描======================
void scan()
{
if(S4 == 0) //启动暂停
{
delay(100);
if(S4 == 0)
{
TR0 = ~TR0;
while(S4 == 0)
{
showtime();
}
}
}
if(S5 == 0) //清零
{
delay(100);
if(S5 == 0)
{
t_005ms = 0;
fen = 0;
miao = 0;
while(S5 == 0)
{
showtime();
}
}
}
}
void main()
{
InitTimer0();
while(1)
{
scan();
showtime();
}
}
标签:P2,break,定时器,09,unsigned,拓展,char,case,0x1f From: https://www.cnblogs.com/Unclesundada/p/17011052.html