结合键盘,中断和数码管相关知识,实现一个精度为0.01s的秒表,并用数码管前四位显示。详细要求如下: 1)利用定时器/计数器T0的工作方式1 2)秒表的变化范围是0.00s~59.99s (必须有小数点) 3)用独立键盘或矩阵键盘控制秒表的启动和停止
时间: 2024-02-12 14:06:25 浏览: 36
好的,下面是一个基于定时器/计数器T0、中断和数码管的精度为0.01s的秒表,使用矩阵键盘控制启动和停止:
```
#include <reg51.h>
#define uint unsigned int
#define uchar unsigned char
uchar code table[]={"0123456789."};
uchar mm,ss,ms;
uchar flag_start=0,flag_stop=0;
void delayms(uint xms)
{
uint i,j;
for(i=xms;i>0;i--)
for(j=110;j>0;j--);
}
void init()
{
TMOD=0x01;
TH0=0;
TL0=0;
TR0=1;
ET0=1;
EA=1;
}
void display()
{
uchar i;
uchar a,b;
a=ms%10;
b=ms/10;
P2=0x80;
P0=table[b];
delayms(1);
P2=0x81;
P0=table[a];
delayms(1);
a=ss%10;
b=ss/10;
P2=0x82;
P0=table[b];
delayms(1);
P2=0x83;
P0=table[a];
delayms(1);
}
void timer0() interrupt 1
{
TH0=(65536-1000)/256;
TL0=(65536-1000)%256;
if(flag_start==1 && flag_stop==0)
{
ms++;
if(ms==100)
{
ms=0;
ss++;
if(ss==60)
{
ss=0;
mm++;
if(mm==60)
{
mm=0;
}
}
}
}
display();
}
void keyscan()
{
uchar keyvalue;
P1=0xff;
if(P1!=0xff)
{
delayms(10);
if(P1!=0xff)
{
keyvalue=P1;
P1=0xff;
switch(keyvalue)
{
case 0xfe: //start
flag_start=1;
flag_stop=0;
break;
case 0xfd: //stop
flag_start=0;
flag_stop=1;
break;
}
}
}
}
void main()
{
init();
while(1)
{
keyscan();
}
}
```
在此程序中,使用了定时器/计数器T0进行计时,使用数码管进行显示。在主函数中,调用了init()函数进行初始化,然后通过一个while循环来保持程序运行。在keyscan()函数中,使用矩阵键盘来控制秒表的启动和停止。在timer0()中断服务函数中,当启动标志flag_start为1时,才进行计时和显示。最后,在display()函数中,将当前的时间进行显示。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)