基于51单片机的数字秒表的设计
(2)外部中断1服务程序
外部中断1服务程序结合外部P3.3键实现数字秒表的停止功能。
流程图如下所示。
本地图片,请重新上传
(3)定时器T0中断服务程序
当T0一处后,向CPU发出中断请求信号。CPU跳转到定时中断程序执行,具体流程如下。
2.3程序清单
#include
unsigned int data table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; //显示码值
unsigned int i,j,k,l,count;
void delay(unsigned int z) //延时程序
{
unsigned int x,y;
for(x=z;x!=0;x--)
for(y=110;y!=0;y--);
}
void main()
{
TMOD=0x01; //设置定时器为模式1
TH0=(65536-1000)/256; //给定时器赋定时初值
TL0=(65536-1000)%256;
EA=1; //开中断
EX0=1;
EX1=1;
ET0=1;
IT0=1; //设置外部中断位脉冲边沿触发方式
IT1=1;
while(1) // 数码显示
{
P2=0x03;
P0=table[i];
delay(1);
P2=0x02;
P0=table[j];
delay(1);
P2=0x01;
P0=(table[k]+0x80);
delay(1);
P2=0x00;
P0=table[l];
delay(1);
};
}
void ex0() interrupt 0 //外部中断0
{
TR0=1; //开定时器,开始计数
}
void ex1() interrupt 2 //外部中断0
{
TR0=0; //停止计数
}
void timer0() interrupt 1 //定时器T0溢出中断
{
TH0=(65536-1000)/256; //重装计数初值
TL0=(65536-1000)%256;
count++; //溢出中断次数加一
if(count==10)
{
count=0;
i++; //溢出10次,0.01s位加一
if(i==10)
{
i=0; //0.01s位到10了,清零,0.1s位加一
j++;
if(j==10)
{
j=0; //0.1s位到10了,清零,1s位加一
k++;
if(k==10)
{
k=0; //1s位到10了,清零,10s位加一
l++;
}
}
}
}
}
评论