📄 简易秒表设计.c
字号:
//MCU:AT89S51
//晶振:12M
#include "AT89X51.H"
unsigned char code numcode[]={0xc0,0xf9,0xa4,0xb0,0x99,
0x92,0x82,0xf8,0x80,0x90,
0xbf,0xff
};//数字0~9及"-"" "共阳数码管代码
unsigned char dispbuff[8]={0,0,10,0,0,11,11,11};
unsigned char code bitcode[]={0xfe,0xfd,0xfb,0xf7,
0xef,0xdf,0xbf,0x7f}; //数码管位选代码
unsigned char dd_second; //百分之一秒
unsigned char second; //秒
unsigned char key_value;
unsigned int tcount;
unsigned int ttcount;
unsigned char disp_bit_count;
/********1ms延时子程序***********/
delay_nms(unsigned int n)
{
unsigned int i;
unsigned char j;
for(i=0;i<n;i++)
for(j=0;j<120;j++)
; //空操作
}
void main(void)
{
unsigned char i;
unsigned char go_flag; //开始秒表计时标志,0表示未开始,
//显示"00-00",1表示开始秒表计时。
TMOD=0x02; //使用定时器0,选择方式2(常数自动重装的8位定时器)
TH0=0x06; //保存数值,用于自动重装
TL0=0x06; //定时250uS初值
// TR0=1; //开定时器0
ET0=1; //开定时器0溢出中断
EA=1; //开总中断
dd_second=0;
second=0;
while(1)
{
if(P2_0==0)
{
delay_nms(10);
if(P2_0==0)
{
key_value++;
switch(key_value)
{
case 1:
TH0=0x06;
TL0=0x06;
TR0=1;
go_flag=1;
break;
case 2:
TR0=0;
go_flag=0;
break;
case 3:
key_value=0;
second=0;
dd_second=0;
go_flag=0;
break;
}
while(P2_0==0);
}
}
if(go_flag==0)
{
dispbuff[0]=dd_second%10;//百分之一秒
dispbuff[1]=dd_second/10;//十分之一秒
dispbuff[3]=second%10; //秒个位
dispbuff[4]=second/10; //秒十位
for(i=0;i<8;i++)
{
P0=0XFF;
P0=numcode[dispbuff[i]];
P1=bitcode[i];
delay_nms(10);
}
}
}
}
/*********250uS中断服务程序*************/
void t0(void) interrupt 1 using 0
{
tcount++;
ttcount++;
if(ttcount==8)
{
ttcount=0;
dispbuff[0]=dd_second%10;//百分之一秒
dispbuff[1]=dd_second/10;//十分之一秒
dispbuff[3]=second%10; //秒个位
dispbuff[4]=second/10; //秒十位
P0=numcode[dispbuff[disp_bit_count]];
P1=bitcode[disp_bit_count];
disp_bit_count++;
if(disp_bit_count==8)
disp_bit_count=0;
}
if(tcount==40) //10mS到
{
tcount=0;
if(dd_second==100)
{
dd_second=0;
second++;
if(second==100)
second=0;
}
else
dd_second++;
}
}
/*********程序到此完成ok!!!!*****************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -