2301_78458652 2023-06-18 19:09 采纳率: 100%
浏览 34
已结题

设计并制作一个电路,具备密码功能,有三个按键,连续按下K1两次,K2一次,K3-次,数码管显示1,其余无论何种情况均显示0。

img

设计并制作一个电路,具备密码功能,有三个按键,连续按下K1两次,K2一次,K3-次,数码管显示1,其余无论何种情况均显示0。

  • 写回答

3条回答 默认 最新

  • Minuw 2023-06-18 20:22
    关注

    上次做了一个一样的

    // 包含头文件和函数声明等
    #include <reg51.h>
    #define LED P2
    //循环计数器 
    unsigned char Cnt_K1 = 0, Cnt_K2 = 0, Cnt_K3 = 0;
    sbit K1 = P1 ^ 0;
    sbit K2 = P1 ^ 1;
    sbit K3 = P1 ^ 2;
    int state = 0;
    void delay(unsigned int t)
    {
        while (t--);
    }
    void display(int num)
    {
        static const char  table[] = { 0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f };
        LED = table[num];
    }
    
    //延时函数-1ms 
    void Delay1ms()
    {
        unsigned char a, b, c;
        for (c = 45; c > 0; c--)
            for (b = 10; b > 0; b--)
                for (a = 10; a > 0; a--);
    }
    
    void main() {
        
        while (1) {
    
            if (K1 == 0)
            {
                Cnt_K1++;
                state = 1;
                Delay1ms();
                if (Cnt_K1 > 2)
                {
                    Cnt_K1 = 0;
                    state = 0;
                }
                
            }
            if (K2 == 0 && state == 1)
            {
                Cnt_K2++;
                state = 2;
                Delay1ms();
                if (Cnt_K2 > 1)
                {
                    Cnt_K2 = 0;
                    state = 0;
                }
            
            }
            if (K3 == 0 && state == 2)
            {
                Cnt_K3++;
                state = 3;
                Delay1ms();
                if (Cnt_K3 > 1)
                {
                    Cnt_K3 = 0;
                    state = 0;
                }
        
            }
            //连续按下K1两次,K2一次,K3一次,数码管显示1
            if (Cnt_K1 == 2 && Cnt_K2 == 1 && Cnt_K3 == 1 && state == 3)
            {
                display(1);
            }
            else
            {
                display(0);
            }
        }
    }
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 6月26日
  • 已采纳回答 6月18日
  • 创建了问题 6月18日