蓝桥杯基础学习记录——定时器实现秒表功能

这个首先列一个框架。

  1. 首先头文件,main函数,数码管段码的定义,HC138(或573)部分的代码以及分配S4S5的地址都是我们通过看题后立马就能知道要用到的东西。以及我们在第二步时会注意到这个地方要求我们根据分-秒-毫秒的格式显示,所以还需要定义三个变量,使其每一次定时后数值都能够变化。
    #include <REGX52.H>
    
    sbit S4 = P3^3;
    sbit S5 = P3^2;
    unsigned char code smgduanma[18]={
    0xC0,0xF9,0xA4,0xB0,0x99,0x92,
    0x82,0xF8,0x80,0x90,0x88,0x80,
    0xc6,0xc0,0x86,0x8e,0xbf,0x7f
    };
    unsigned char t_fen=0;
    unsigned char t_s=0;
    unsigned char t_ms=0;
    void SELECT_HC138(unsigned char n)
    	{
    
      switch(n)
    {
    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 main()
    {
    while(1)
    {
    
    }
    }
  2. 需要数码管显示,所以要有Display函数,通过pos位置和显示值value控制。以及调用Display函数用于显示时间的函数DisplayTime。
    void Display(unsigned char value,unsigned char pos)
    {
    	SELECT_HC138(6);
    	P0 = 0x01 << pos;
    	SELECT_HC138(7);
    	P0 = value;
    
    }
    
    
    
    void DisplayTime()
    {
    	Display(smgduanma[t_ms/10],6);
    	Delay(500);
    	Display(smgduanma[t_ms%10],7);
    	Delay(500);
    	Display(smgduanma[16],5);
    	Delay(500);
    	Display(smgduanma[t_s/10],3);
    	Delay(500);
    	Display(smgduanma[t_s%10],4);
    	Delay(500);
    	Display(smgduanma[16],2);
    	Delay(500);
    	Display(smgduanma[t_fen/10],0);
    	Delay(500);
    	Display(smgduanma[t_fen%10],1);
    	Delay(500);
    }
    

  3. 需要独立按键部分,所以要编写一个ScanKey函数实现当S4S5按下时要做的操作。
    void ScanKey()
    {
    	if(S4==0)//秒表暂停/启动
    		{
    			Delay(100);
    			if(S4==0)
    				{
    					
    					TR0=~TR0;
    					while(S4==0)
    					{
    					DisplayTime();
    					}
    			}
    		
    	}
      if(S5 == 0)//秒表清零
    		{
    			Delay(100);
    			if(S5==0)
    				{
    					
    					t_ms=0;
    					t_s=0;
    					t_fen=0;
    					while(S5==0)
    					{
    					DisplayTime();
    					}
    					
    			}
    		
    	}
    }
    

  4. 需要定时器模块,这里分为两个函数,一个是初始化函数InitTimer,另一个是中断服务函数SeverTimer0。
    void initTimer()
    {
    	TMOD=0X01;
    	TH0 = (65535 - 50000) / 256;
    	TL0 = (65535 - 50000) % 256;
      ET0=1;
    	EA=1;
    	TR0=1;
    
    }
    void ServiceTimer0() interrupt 1
    {
    	TH0 = (65535 - 50000) / 256;
    	TL0 = (65535 - 50000) % 256;
      t_ms++;
    	if(t_ms == 20)
    {
      t_s++;
    	t_ms = 0;
    	if(t_s == 60)
    	{
    	 t_fen++;
    	 t_s = 0;
    	}
    	if(t_fen == 99)
    	{
    		t_fen=0;
    	
    	}
    }
    
    }
    

  5. 将这些函数放到main函数里循环。
    
    void main()
    {
    	initTimer();
    	while(1)
    	{
      
    	DisplayTime();
    	ScanKey();
    	}
    
    }

  • 63
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值