蓝桥杯定时器综合(仅供自己参考)

#include "reg52.h"

sbit YMQ_A=P2^5;
sbit YMQ_B=P2^6;
sbit YMQ_C=P2^7;

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

unsigned char min=0;
unsigned char sec=0;
unsigned char msec50=0;

unsigned char code SMG_Duanma[18]=
{0xc0,0xf9,0xa4,0xb0,0x99,0x92,
0x82,0xf8,0x80,0x90,0x88,0x83,
0xc6,0xc0,0x86,0x8e,0xbf,0x7f};

void Delay(unsigned int t)
{
    while(t--);
}
void InitHC138(unsigned char n)
{
    switch(n)
    {
        case 4:
            YMQ_C=1;
            YMQ_B=0;
            YMQ_A=0;
        break;
        case 5:
            YMQ_C=1;
            YMQ_B=0;
            YMQ_A=1;
        break;
        case 6:
            YMQ_C=1;
            YMQ_B=1;
            YMQ_A=0;
        break;
        case 7:
            YMQ_C=1;
            YMQ_B=1;
            YMQ_A=1;
        break;
    }
}

void DisplaySMG_Bit(unsigned char value,unsigned char pos)
{
    InitHC138(6);
    P0=0x01<<pos-1;
    InitHC138(7);
    P0=value;
}

void DisplayTime()
{
    DisplaySMG_Bit(SMG_Duanma[msec50%10],8);
    Delay(500);
    DisplaySMG_Bit(SMG_Duanma[msec50/10],7);
    Delay(500);
    DisplaySMG_Bit(SMG_Duanma[16],6);
    Delay(500);
    DisplaySMG_Bit(SMG_Duanma[sec%10],5);
    Delay(500);
    DisplaySMG_Bit(SMG_Duanma[sec/10],4);
    Delay(500);
    DisplaySMG_Bit(SMG_Duanma[16],3);
    Delay(500);
    DisplaySMG_Bit(SMG_Duanma[min%10],2);
    Delay(500);
    DisplaySMG_Bit(SMG_Duanma[min/10],1);
    Delay(500);
}
//====定时器相关函数======
void InitTimer0()
{
    TMOD=0x01;
    TH0=(65535-50000)/256;
    TL0=(65535-50000)%256;
    EA=1;
    ET0=1;
    TR0=0;
}

void ServiceTimer0() interrupt 1
{
    TH0=(65535-50000)/256;
    TL0=(65535-50000)%256;
    msec50++;
    if(msec50==20)
    {
        sec++;
        msec50=0;
        if(sec==60)
        {
            min++;
          sec=0;
        }
        if(min==99)
        {
            min=0;
        }
    }
}
//=======================

//====按键相关函数======
void ScanKeys()
{
    if(S4==0)  //秒表启动与暂停
    {
        Delay(100);
        if(S4==0)
        {
            while(S4==0);
            {
                DisplayTime();
            }
            TR0=~TR0;
            Delay(100);
        }
    }
    if(S5==0)  //秒表清零
    {
        Delay(100);
        if(S5==0)
        {
            while(S5==0);
            {
                DisplayTime();
            }
            msec50=0;
            sec=0;
            min=0;
            Delay(100);
        }
    }
}
//=======================

void main()
{
    InitTimer0();
    while(1)
    {
      DisplayTime();
        ScanKeys();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这里提供一份蓝桥杯单片机数码管定时器动态扫描的参考代码,供大家参考。 ```c #include <reg51.h> //头文件 // 数码管位选信号 unsigned char code LED_W[] = {0xFE, 0xFD, 0xFB, 0xF7, 0xEF, 0xDF, 0xBF, 0x7F}; // 数码管段选信号 unsigned char code LED_D[] = { 0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F, 0x77, 0x7C, 0x39, 0x5E, 0x79, 0x71 }; // 定时器中断服务函数 void Timer0_ISR() interrupt 1 { static unsigned char count = 0; // 计数器 static unsigned char index = 0; // 位选信号索引,表示当前显示的是哪一位数码管 count++; // 计数器加1 if (count >= 100) { // 计数器达到100时,表示100ms已经过去了 count = 0; // 计数器清零 P0 = LED_W[index]; // 先将位选信号输出到P0口 P2 = LED_D[index]; // 再将对应的段选信号输出到P2口 index = (index + 1) % 8; // 索引加1,循环显示8个数码管 } } void main() { TMOD = 0x01; // 定时器0工作在模式1,16位定时器 TH0 = 0xFC; // 定时器初值,定时1ms TL0 = 0x66; ET0 = 1; // 允许定时器0中断 EA = 1; // 允许总中断 TR0 = 1; // 启动定时器0 while(1); // 主函数空循环,等待中断触发 } ``` 该代码中,通过定时器0产生1ms的中断,然后在中断服务函数中动态扫描8个数码管。其中,`LED_W`为数码管的位选信号,`LED_D`为数码管的段选信号。在每次中断服务函数中,先输出位选信号,再输出对应的段选信号,就可以实现数码管的动态扫描了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值