double time() //你要的函数
{ doubule ti;
int time;
TMOD=0x10;//设置定时器1为工作方式2
succeed_flag=0; //定时器溢出赋初值0
EA=1; //打开全局中断控制
EX0=1;
EX1=1; //打开外部中断0,1中断
TH1=0; //机器周期高位
TL1=0; //机器周期低位
TF1=0; //定时器1溢出标志位
if(succeed_flag==1)//如果定时器未溢出,计算时间
{
time=timeH256+timeL;
ti=time0000109;
}
return ti;
}
void exter() interrupt 0 //当接受到脉冲时进行计数中断
{
timeH=TH1;
timeL=TL1;
TR1=0;
succeed_flag=1;
TH1=0; //机器周期高位
TL1=0;
TR1=1; //开启定时器1
}
void exter() interrupt 1 //当接受到脉冲时进行计数中断
{
timeH=TH1;
timeL=TL1;
TR1=0;
succeed_flag=1;
TH1=0; //机器周期高位
TL1=0; //机器周期低位
TL1=0;
TR1=1; //开启定时器1
}
void T1_time() interrupt 3 //定时器1的终端序号是3
{
TH1=0;
TL1=0;
succeed_flag=0;
}
可能有点语法错误,自己改吧
完整的程序,装上就可以用了。
/
秒表程序,P0接段位,P2接数码管数位。采用中断精准及时。
/
#include <AT89X55H>
#define uc unsigned char
uc s,min,h,time,ms;
uc temp[]={0,0,0,0,0,0,0,0};
void display()
{
uc displaycode[10]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
uc i,k;
k=0x01;
for(i=0;i<8;i++)
{
P2=~0;
P0=displaycode[temp[i]];
P2=k;
k=k<<1;
}
P2=~0;
}
void main()
{
if(time==100)
{
s++;
time=0;ms=0;
if(s>59)
{
s=0;min++;
if(min>59)
{
min=0;
h++;
if(h>23)h=0;
}
}
}
temp[0]=h/10;
temp[1]=h%10;
temp[2]=min/10;
temp[3]=min%10;
temp[4]=s/10;
temp[5]=s%10;
temp[6]=ms/10;
temp[7]=ms%10;
while(1)
{
display();
}
TMOD=0x01;
TH0=(65536-10000)/256;
TL0=(65536-10000)%256;
ET0=1;
EA=1;
IT0=1;
EX0=1;
IT1=1;
EX1=1;
display();
}
void time0() interrupt 0
{
TR0=1;s=0;min=0;h=0;
}
void time1() interrupt 2
{
TR0=0;
}
void time2() interrupt 1
{
TH0=(65536-10000)/256;
TL0=(65536-10000)%256;
ms++;
time++;
}
以上就是关于51单片机利用两个外部中断和一个定时器,计算两个中断时间间隔的的程序。急求急求!!!全部的内容,包括:51单片机利用两个外部中断和一个定时器,计算两个中断时间间隔的的程序。急求急求!!!、51单片机里2个外中断触发计时器开始和结束,将所记得的时间显示出来的程序怎么写啊、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)