蓝桥杯 定时器 数码管 秒表

#include <STC15F2K60S2.H>


sbit HC138_A=P2^5;
sbit HC138_B=P2^6;
sbit HC138_C=P2^7;
sbit buzz_1= P0^6;
sbit jdq_2= P0^4;

sbit s7=P3^0;//独立按键
sbit s6=P3^1;

unsigned char code SMG_duanma[18]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x80,0xc6,0xc0,0x86,0x8e,0xbf,0x7f};//有第零个位置
//unsigned char yu=1;
unsigned char Sec,Min,Hour;
void DelaySMG(unsigned int num) 

     unsigned int i; 
     while(num--) 
     for(i=0; i<628; i++); 

void Init_HC138(unsigned char n)
{
     switch (n)
     {
         case 4:
              HC138_A=0;
              HC138_B=0;
              HC138_C=1;
         break;
         case 5:
              HC138_A=1;
              HC138_B=0;
              HC138_C=1;
         break;
         case 6:
              HC138_A=0;
              HC138_B=1;
              HC138_C=1;
         break;
         case 7:
              HC138_A=1;
              HC138_B=1;
              HC138_C=1;
         break;
     }
}

void led_disp()
{
    
    Init_HC138(4);
    
    P0=0xff;
    
}

void close_buzz()
{
    Init_HC138(5);
    
    buzz_1=0;//0是关闭
    jdq_2=0;//0是关闭
}

void smg_show_bit(unsigned char dat,unsigned char pos)
{
    Init_HC138(6);//数码管的位置
    P0=0x01<<pos;
    Init_HC138(7);//数码管的内容
    P0=dat;    
}

void smg_dynamic()
{
    //    unsigned char i;
    //    for(i=0;i<8;i++)
    //    {
    //    
    //        smg_show_bit(SMG_duanma[i],i);
    //        Delay(1) ;
    //        
    //    }
    //    
    smg_show_bit(SMG_duanma[Hour/10],0);
    DelaySMG(1) ;
    smg_show_bit(SMG_duanma[Hour%10],1);
    DelaySMG(1) ;
    smg_show_bit(SMG_duanma[16],2);
    DelaySMG(1) ;
    smg_show_bit(SMG_duanma[Min/10],3);
    DelaySMG(1) ;
    
    smg_show_bit(SMG_duanma[Min%10],4);
    DelaySMG(1) ;
    smg_show_bit(SMG_duanma[16],5);
    DelaySMG(1) ;
    smg_show_bit(SMG_duanma[Sec/10],6);
    DelaySMG(1) ;
    smg_show_bit(SMG_duanma[Sec%10],7);
    DelaySMG(1) ;
}

void delay(unsigned char t)//注意这有两个延时函数,一个延时数码管,一个整体延时
{
    while(t--)
    {
        smg_dynamic();
    }
}

/*****************************************************************/
void Timer1Init(void)        //1毫秒@12.000MHz
{
    AUXR &= 0xBF;        //定时器时钟12T模式
    TMOD &= 0x0F;        //设置定时器模式
    TL1 = 0x18;        //设置定时初始值
    TH1 = 0xFC;        //设置定时初始值
    TF1 = 0;        //清除TF1标志
    TR1 = 1;        //定时器1开始计时
    
    ET1 = 1; // 允许定时器 1 中断
    EA = 1; // 允许系统中断
}

void ServiceTimer1()  interrupt 3
{
    
    static unsigned int T1Count;
    T1Count++;
    
    if(T1Count>=1000)
    {
       T1Count=0;
       Sec++;
      if(Sec>=60)
     {
        Sec=0;
        Min++;
        if(Min>=60)
        {
           Min=0;
           Hour++;
           if(Hour>=24)
           {
            Hour=0;
           }
        }
     }
        
    }            
}

void Scankey()
{
    if(s7==0)
    {
       delay(10);
       if(s7==0)
       {
          TR1 = ~TR1;
       }
    }
    if(s6==0)
    {
       delay(10);
       if(s6==0)
       {
          Sec=0;
          Min=0;
          Hour=0;
       }
    }
}

/*****************************************************************/

void main()
{    
     close_buzz();
     led_disp();
     Timer1Init();
     while(1)
     {
          smg_dynamic();    
          Scankey();
     }
}
 

这里提供一份蓝桥杯单片机数码管定时器动态扫描的参考代码,供大家参考。 ```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、付费专栏及课程。

余额充值