51单片机实现秒表, 0~9、A、b、C、d、E、F,16进制秒表。
#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
uchar bai,shi;
uint a1,a2;
sbit D1=P3^0;
sbit D2=P3^1;
sbit D3=P3^2;
sbit D4=P3^3;
sbit key=P3^5;
sbit key1=P3^7;
bit j ;
uint y ;
void main()
{
TMOD=0x01;
TH0=(65536-10000)/256;
TL0=(65536-10000)%256;
EA=1;
ET0=1;
TR0=1;
bai=0;
shi=0;
while(1)
{
if(key==0)
{
j=0;
}
if(key1==0)
{
j=1; a2=0;
}
}
}
void timer0()interrupt 1
{
TH0=(65536-10000)/256;
TL0=(65536-10000)%256;
a1++;
y++;
if(a1==10) // 100
{
a1=0;
if(j==1) a2++;
if(a2>=65536) a2=0; // 65536 改 10000 是十进制 (下同)
}
D1 = 1; D2 = 1; D3 = 1; D4 = 1;
if(y==1)
{
P1=table[a2%65536/4096]; // 65536/4096 改 10000/1000
D4=0;
}
if(y==2)
{
P1=table[a2%4096/256]; // 4096/256 改 1000/100
D3=0;
}
if(y==3)
{
P1=table[a2%256/16]; // 256/16 改 100/16
D2=0;
}
if(y==4)
{
P1=table[a2%16]; // 16 改 10
D1=0;
y=0;
}
}