微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > 精确到千分之一秒的单片机秒表程序(带仿真)

精确到千分之一秒的单片机秒表程序(带仿真)

时间:11-30 来源:互联网 点击:
这个秒表程序是51hei单片机开发光盘里面附带的一个程序,可精确到千分之一秒,附件里面有仿真文件和hex文件.下载地址:http://www.51hei.com/bbs/dpj-19342-1.html

#include
#include
#define uint unsigned int
#define uchar unsigned char
sbit duxa=P2^6;
sbit wexa=P2^7;
sbit k1=P1^2;
sbit k2=P1^3;
sbit k3=P1^4;
uchar temp,qian,bai,shi,ge;
uint num,a,shu;
uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
void init();
void delay(uint z);
void display(uchar num,uchar qian,uchar bai,uchar shi,uchar ge);
void main()
{
init();
while(1)
{
display(num,qian,bai,shi,ge);
if(k1==0)
{
delay(1);
TR0=0;
delay(1);
}
if(k2==0)
{
delay(1);
TR0=1;
delay(1);
}
if(k3==0)
{
delay(1);
a=0;
num=0;
TR0=1;
delay(1);

}

}
}
void init()
{
num=0;
temp=0xf0;
TMOD=0x01;
TH0=(65536-1000)/256;
TL0=(65536-1000)%256;
EA=1;
ET0=1;
TR0=1;
bai=0;
shi=0;
ge=0;
a=0;
k1=1;
k2=1;
k3=1;
}
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void timer0() interrupt 1
{
TH0=(65536-1000)/256;
TL0=(65536-1000)%256;
a++;

if(a==9999)
{
a=0;
num++;
if(num==10)
num=0;

}
shu=a;
qian=shu/1000;
bai=shu%1000/100;
shi=shu%100/10;
ge=shu%10;
}

void display(uchar num,uchar qian,uchar bai,uchar shi,uchar ge)
{
wexa=1;
P0=0xfe;
wexa=0;
P0=0;
duxa=1;
P0=table[num];
duxa=0;
P0=0xff;
delay(1);

wexa=1;
P0=0xfd;
wexa=0;
P0=0;
duxa=1;
P0=table[qian];
duxa=0;
P0=0xff;
delay(1);

wexa=1;
P0=0xfb;
wexa=0;
P0=0;
duxa=1;
P0=table[bai];
duxa=0;
P0=0xff;
delay(1);

wexa=1;
P0=0xf7;
wexa=0;
P0=0;
duxa=1;
P0=table[shi];
duxa=0;
P0=0xff;
delay(1);

wexa=1;
P0=0xef;
wexa=0;
P0=0;
duxa=1;
P0=table[ge];
duxa=0;
P0=0xff;
delay(1);
}

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top