您的位置 首页 软件

51单片机秒表准确计时

#includeREG51.H>#includeintrins.h#defineuintunsignedint#defineucharunsignedcharuintsecond;codeuns

#include
#include”intrins.h”
#define uint unsigned int
#define uchar unsigned char
uint second ;
code unsigned char table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x00};
uchar code DIS_BIT[10]={0xff,0xfe,0xfd,0xfc,0xfb,0xfa,0xf9,0xf8};
uchar count=0;
/*void delay(int j) //delay 1 ms
{
uchar i;
while(j–)
for(i=1;i<=250;i++)
{
_nop_();
_nop_();
_nop_();
_nop_();
}
} */
void init(void)
{
TMOD=0x01;
TH0=(65536-50000)/256; //预先 设定定时器 初值,50毫秒
TL0=(65536-50000)%256;
EA=1; //敞开 总中止
ET0=1; //敞开定时器 0 中止
TR0=1; //定时器 0 发动计数
}
void display(uint result)
{
P2=table[result/1000];
P1=DIS_BIT[3];
//delay(900);
P2=table[result%1000/100];
P1=DIS_BIT[2];
//delay(900);
P2=table[result%1000%100/10];
P1=DIS_BIT[1];
//delay(900);
P2=table[result%1000%100%10];
P1=DIS_BIT[0];
//delay(900);
}
void main()
{
init();
while(1)
{
display(second);
}
}
void time0(void) interrupt 1
{
count++; //50ms太短,闪耀频率太快
if(count==20) //用变量 count 延长到100ms,依据自己的需求,调整参数或许定时器初始值来改成时间量
{
count=0;
second++;
if(second>59)
{
second=0;
}
}
}

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/qianrushi/ruanjian/266676.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部