摘要:本文将带你了解C语言程序设计之10s的秒表(代码实例),希望本文对大家C语言有所帮助。
本文将带你了解C语言程序设计之10s的秒表(代码实例),希望本文对大家C语言有所帮助。
/* 名称:10s 的秒表
说明:首次按键计时开始,https://www.jiakang021.com再次按键暂停,第三次按键清零。
*/
#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
sbit K1=P3^7;
uchar
i,Second_Counts,Key_Flag_Idx;
bit Key_State;
uchar
DSY_CODE[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
//延时
void DelayMS(uint ms)
{
uchar t;
while(ms--) for(t=0;t<120;t++);
}
//处理按键事件
void Key_Event_Handle()
{
if(Key_State==0)
{
Key_Flag_Idx=(Key_Flag_Idx+1)%3;
switch(Key_Flag_Idx)
{
case 1:EA=1;ET0=1;TR0=1;break;
case 2:EA=0;ET0=0;TR0=0;break;
case 0:P0=0x3f;P2=0x3f;i=0;Second_Counts=0;
}
}
}
//主程序
void main()
{
P0=0x3f; //显示 00
P2=0x3f;
i=0;
Second_Counts=0;
Key_Flag_Idx=0; //按键次数(取值 0,1,2,3)
Key_State=1; //按键状态
TMOD=0x01; //定时器 0 方式 1
TH0=(65536-50000)/256; //定时器 0:15ms
TL0=(65536-50000)%256;
while(1)
{
if(Key_State!=K1)
{
DelayMS(10);
Key_State=K1;
Key_Event_Handle();
}
}
}
/T0 中断函数
void DSY_Refresh() interrupt 1
{
TH0=(65536-50000)/256; //恢复定时器 0 初值
TL0=(65536-50000)%256;
if(++i==2)
{
i=0;
Second_Counts++; //50ms*2=0.1s 转换状态
P0=DSY_CODE[Second_Counts/10];
P2=DSY_CODE[Second_Counts%10];
if(Second_Counts==100) Second_Counts=0; //满 100(10s)后显示 00
}
}
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号