
;******************************************************************************
;*标题: 东哥单片机学习网 DG-51/AVR-V1.0开发板演示程序--秒表计C程序
;*文件: DG002.asm
;*版本: 1.0
;*作者: 东哥开发网 - 东哥
;*邮箱: picavr@163.com QQ: 279068079
;*标题: 东哥单片机学习网 DG-51/AVR-V1.0开发板演示程序--秒表计C程序
;*文件: DG002.asm
;*版本: 1.0
;*作者: 东哥开发网 - 东哥
;*邮箱: picavr@163.com QQ: 279068079
;*网站: http://www.picavr.com
;*****************************************************************************
;*****************************************************************************
;*: 描述:
;*: 秒表计C程序 按K1启动键计时,按K2停止计时,按K3清除计时。
;*: 秒表计C程序 按K1启动键计时,按K2停止计时,按K3清除计时。
;*:
;******************************************************************************
;******************************************************************************
;* 【版权】 Copyright(C)东哥单片机学习网 www.picavr.com All Rights Reserved
;* 【声明】 此程序仅用于学习与参考,引用请注明版权和作者信息!
;******************************************************************************
//秒表程序
//秒表程序
#define XMS 2000 /* 定时中断间隔 12MHz,1uS */
#include <reg51.h>
#include <stdio.h>
#define uchar unsigned char
#include <reg51.h>
#include <stdio.h>
#define uchar unsigned char
sbit KEY1 =P3^3; /* 启动键 */
sbit KEY2 =P3^4; /* 停止键 */
sbit KEY3 =P3^5; /* 清除键*/
sbit KEY2 =P3^4; /* 停止键 */
sbit KEY3 =P3^5; /* 清除键*/
bit stop;
char buf[4]={0,0,0,0};
char low[4]={0xfe,0xfd,0xfb,0xf7};
char disppoint;
uchar ms;
char buf[4]={0,0,0,0};
char low[4]={0xfe,0xfd,0xfb,0xf7};
char disppoint;
uchar ms;
code char seg[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90}; /* 显示段表*/
void delayms(int t)
{
uchar i;
while(t--)
{
i=100;
while(i--);
}
}
{
uchar i;
while(t--)
{
i=100;
while(i--);
}
}
void put_num(char c)
{
P0=seg[c&0x0f];
}
{
P0=seg[c&0x0f];
}
void timer0( ) interrupt 1 using 1 /*定时器中断程序*/
{
EA=0;
//显示:
TH0=-(XMS/256);
TL0=-(XMS%256);
disppoint&=3;
put_num(buf[disppoint]);
//if(disppoint==2)P0&=~0x20; /* 小数点*/
P1=low[disppoint];
disppoint++;
if(stop){EA=1;return;}
//----
ms--;
if(ms==0)
{
ms=100;
buf[3]++;
if(buf[3]>9)
{buf[3]=0;
buf[2]++;
if(buf[2]>9)
{buf[2]=0;
buf[1]++;
if(buf[1]>9)
{buf[1]=0;
buf[0]++;
if(buf[0]>9)buf[0]=0;
}
}
}
}
EA=1;
}
{
EA=0;
//显示:
TH0=-(XMS/256);
TL0=-(XMS%256);
disppoint&=3;
put_num(buf[disppoint]);
//if(disppoint==2)P0&=~0x20; /* 小数点*/
P1=low[disppoint];
disppoint++;
if(stop){EA=1;return;}
//----
ms--;
if(ms==0)
{
ms=100;
buf[3]++;
if(buf[3]>9)
{buf[3]=0;
buf[2]++;
if(buf[2]>9)
{buf[2]=0;
buf[1]++;
if(buf[1]>9)
{buf[1]=0;
buf[0]++;
if(buf[0]>9)buf[0]=0;
}
}
}
}
EA=1;
}
main()
{
TMOD=0x01;
TH0=-(XMS/256); /* 5ms */
TL0=-(XMS%256);
TR0=1;
EA=1;
{
TMOD=0x01;
TH0=-(XMS/256); /* 5ms */
TL0=-(XMS%256);
TR0=1;
EA=1;
while(1)
{
EA=1;
ET0=1;
if(!KEY1 && stop!=0){stop=0;delayms(100);}
if(!KEY2 && stop==0){stop=1;delayms(100);}
if(!KEY3)
{buf[0]=buf[1]=buf[2]=buf[3]=0;
stop=1;
delayms(100);}
}
}
{
EA=1;
ET0=1;
if(!KEY1 && stop!=0){stop=0;delayms(100);}
if(!KEY2 && stop==0){stop=1;delayms(100);}
if(!KEY3)
{buf[0]=buf[1]=buf[2]=buf[3]=0;
stop=1;
delayms(100);}
}
}
好极了