蓝桥杯双定时器数码管按键无敌写法

#include <REGX52.H>
#include "intrins.H"
sfr P4 = 0XC0;
sbit P4_4 = P4^4;
sbit P4_2 = P4^2;
code unsigned char Table1[] = 
{
0xc0, //0
0xf9, //1
0xa4, //2
0xb0, //3
0x99, //4
0x92, //5
0x82, //6
0xf8, //7
0x80, //8
0x90, //9
0x88, //A
0x83, //b
0xc6, //C
0xa1, //d
0x86, //E
0x8e //F
};
unsigned char Table2[] = {0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff };
unsigned char key_c = 0;
unsigned char flag =1;
unsigned char knum = 0;
unsigned char flag_c = 0;
void Delay20ms()        //@12.000MHz
{
    unsigned char i, j, k;

    _nop_();
    _nop_();
    i = 1;
    j = 234;
    k = 113;
    do
    {
        do
        {
            while (--k);
        } while (--j);
    } while (--i);
}

void Mode_Select(unsigned char Mode)
{
    switch(Mode)
    {
        case 4: P2 &= 0X1F; P2 |= 0X80;break; 
        case 5: P2 &= 0X1F; P2 |= 0XA0;break;
        case 6: P2 &= 0X1F; P2 |= 0XC0;break;
        case 7: P2 &= 0X1F; P2 |= 0XE0;break;
    }
    
}
void Sys_Init()
{
    P0 = 0XFF;
    Mode_Select(4);
    P2 = 0x1f;
    
    P0 = 0X00;
    Mode_Select(5);
    P2 = 0x1f;
}

//数码管
void Nixie(unsigned char loc)
{
    P0 = 0XFF;
    Mode_Select(7);
    P2 = 0x1f;
    
    P0 = 0x01 << loc - 1;
    Mode_Select(6);
    P2 = 0x1f;
    
    P0 = Table2[loc];
    Mode_Select(7);
    P2 = 0x1f;
    
    
}
void Nixie_S(unsigned char location , number)
{
    Table2[location] = Table1[number];
    
}
void Nixie_Loop()
{
    static unsigned char flag =1;
    Nixie(flag);
    flag++;
    if(flag > 8)
    {
        flag =1;
        
    }
        
    
}
//矩阵键盘
unsigned char key()
{
    unsigned char num  = 0;
    

    
    
    P3 = 0XFF;
    P4 = 0XFF;
    P3_2 = 0;
    if(P4_4 == 0) {num = 9;}
    if(P4_2 == 0) {num = 10;}
    if(P3_5 == 0) {num = 11;}
    if(P3_4 == 0) {num = 12;}

    
    P3 = 0XFF;
    P4 = 0XFF;
    P3_3 = 0;
    if(P4_4 == 0) {num = 13;}
    if(P4_2 == 0) {num = 14;}
    if(P3_5 == 0) {num = 15;}
    if(P3_4 == 0) {num = 16;}

    return num;
}
void Key_Loop()
{
    static unsigned char now , last;
    last = now;
    now = key();
    if(last == 10 && now == 0)
    {
        knum = 10;
    }
    if(last == 10 && now == 10)
    {
        key_c =1;
    }
    else
    {
        key_c = 0;
        
    }
    
}
unsigned char  Key_S()
{
    unsigned char tep = 0;
    tep = knum;
    knum = 0;
    return tep;
    
}
//如果要实现长按键
void Key_Dispose()
{
    unsigned char Data = 0;
    Data = Key_S();
    if(Data == 10)
    {
        if(flag_c ==1)
        {
            flag_c = 0;
            flag++;
        }
    }
    
    
}

//定时器
void Timer1Init(void)        //100微秒@11.0592MHz
{
    TMOD &= 0x0F;        //设置定时器模式
    TMOD |= 0x10;        //设置定时器模式
    TL1 = (65535 - 1000) % 256;
    TH1 = (65535 - 1000) / 256;
    EA = 1;
    ET1 = 1 ;
    TF1 = 0;        //清除TF1标志
    TR1 = 1;        //定时器1开始计时
}

void main()
{
    Sys_Init();
    Timer1Init();
    while(1)
    {
        Key_Dispose();
        Nixie_S(2,flag);
    }
    
}

void Timer1_Routine() interrupt 3
{
    static unsigned int count = 0,count_k = 0,count_c =0;
    TL1 = (65535 - 1000) % 256;
    TH1 = (65535 - 1000) / 256;
    count++;
    count_k++;
    if(key_c)
    {
        count_c++;
    }
    else
    {
        count_c = 0;
    }
    if(count >= 2)
    {
        Nixie_Loop();
        count = 0;
        
    }
    if(count_k >= 20)
    {
        count_k = 0;
         Key_Loop();
    }
    
    if(count_c >= 1000)
    {
        count_c  = 0;
        flag_c = 1;
        
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值