【蓝桥杯(51) STC15F2K60S2】之 “秒表(定时器完成)实验“

#include “STC15F2K60S2.H”
#include “intrins.h”

#define uchar unsigned char
uchar bLoose;
unsigned int ms;
uchar s;
uchar m;
//uchar ckey;
//uchar cKeyCode;
uchar cScanIndex;
unsigned char SMG_duanma[18]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x80,0xc6,0xc0,0x80,0x8e,0xbf,0x7f};
unsigned char SMG_weima[8] = {0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
unsigned char ScanIndex=0;
unsigned char SMG_Segcode[8];

sbit S4 = P3^3;
sbit S5 = P3^2;

void Delay500us() //@11.0592MHz
{
unsigned char i, j;

_nop_();
_nop_();
i = 6;
j = 93;
do
{
	while (--j);
} while (--i);

}

void Delay2ms() //@11.0592MHz
{
unsigned char i, j;

_nop_();
_nop_();
i = 22;
j = 128;
do
{
	while (--j);
} while (--i);

}

void SelectHC138(unsigned char channel)
{
switch(channel)
{
case 4:
P2 = P2 & 0x1f | 0x80;
break;
case 5:
P2 = P2 & 0x1f | 0xa0;
break;
case 6:
P2 = P2 & 0x1f | 0xc0;
break;
case 7:
P2 = P2 & 0x1f | 0xe0;
break;
}
}

void Display()
{
SMG_Segcode[0] = SMG_duanma[m/10];
SMG_Segcode[1] = SMG_duanma[m%10];
SMG_Segcode[2] = SMG_duanma[16];
SMG_Segcode[3] = SMG_duanma[s/10];
SMG_Segcode[4] = SMG_duanma[s%10];
SMG_Segcode[5] = SMG_duanma[16];
SMG_Segcode[6] = SMG_duanma[ms/10];
SMG_Segcode[7] = SMG_duanma[ms%10];
}

void show()
{
SelectHC138(7);
P0 = 0xff;
SelectHC138(6);
P0 = SMG_weima[cScanIndex];
SelectHC138(7);
P0 = SMG_Segcode[cScanIndex];
cScanIndex++;
cScanIndex %= 8;
Display();
Delay500us();
}

void Key_Judge()
{
if(S4 == 0)
{
Delay2ms();
if(S4 == 0){
TR0 = !TR0;
while(S4 == 0)
show();
}

}

else if(S5 == 0)
{
	Delay2ms();
		if(S5 == 0){
			ms = m = s = 0;
			while(S4 == 0)
				show();
		}
}

}

void main()
{
TMOD = 0X01;
TH0 = (65535-10000)/256;
TL0 = (65535-10000)%256;
ET0 = 1;
EA = 1;
TR0 = 1;

while(1)
{
	show();
	Key_Judge();
}

}

void INT0() interrupt 1
{
TH0 = (65535 - 10000) / 256;
TL0 = (65535 - 10000) % 256;
ms++;
if(ms == 100)
{
ms = 0;
s++;
}
if(s == 60)
{
s = 0;
m++;
}
if(m == 100)
m = 0;
}

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

world呀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值