赐予我力量吧 2023-02-12 16:25 采纳率: 100%
浏览 39
已结题

关于#单片机#的问题:用这个代码写出来矩阵键盘完全控制不了LED

用这个代码写出来矩阵键盘完全控制不了LED,想知道为什么。
矩阵键盘肯定没坏,已经试验过了。

#include "reg52.h"

sfr P4 = 0xc0;

sbit R1 = P3^0;
sbit R2 = P3^1;
sbit R3 = P3^2;
sbit R4 = P3^3;

unsigned char code SMG_duanma[18]=
    {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,
        0x80,0x90,0x88,0x80,0xc6,0xc0,0x86,0x8e,0xbf,0x7f}; 
    
sbit C4 = P3^4;
sbit C3 = P3^5;
sbit C2 = P4^2;
sbit C1 = P4^4;

void SelectHC573(unsigned char channel)
{
    switch(channel)
    {
        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 DisplayKeyNum(unsigned char value)
{
    SelectHC573(6);
    P0 = 0x01;
    SelectHC573(6);
    P0 = value;
}

unsigned char key_num;
void ScanKeyMulti()
{
    R1 = 0;
    R2 = R3 = R4 = 1;
    C1 = C2 = C3 = C4 = 1;
    if(C1 == 0)
    {
        while(C1 == 0);
        key_num = 0;
        DisplayKeyNum(SMG_duanma[key_num]);
    }
    else if(C2 == 0)
    {
     while(C2 == 0);
        key_num = 1;
        DisplayKeyNum(SMG_duanma[key_num]);
    }
    else if(C3 == 0)
    {
     while(C3 == 0);
        key_num = 2;
        DisplayKeyNum(SMG_duanma[key_num]);
    }
    else if(C4 == 0)
    {
     while(C4 == 0);
        key_num = 3;
        DisplayKeyNum(SMG_duanma[key_num]);
    }
    R2 = 0;
    R1 = R3 = R4 = 1;
    C1 = C2 = C3 = C4 = 1;
    if(C1 == 0)
    {
        while(C1 == 0);
        key_num = 4;
        DisplayKeyNum(SMG_duanma[key_num]);
    }
    else if(C2 == 0)
    {
     while(C2 == 0);
        key_num = 5;
        DisplayKeyNum(SMG_duanma[key_num]);
    }
    else if(C3 == 0)
    {
     while(C3 == 0);
        key_num = 6;
        DisplayKeyNum(SMG_duanma[key_num]);
    }
    else if(C4 == 0)
    {
     while(C4 == 7);
        key_num = 3;
        DisplayKeyNum(SMG_duanma[key_num]);
    }
    R3 = 0;
    R2 = R1 = R4 = 1;
    C1 = C2 = C3 = C4 = 1;
    if(C1 == 0)
    {
        while(C1 == 0);
        key_num = 8;
        DisplayKeyNum(SMG_duanma[key_num]);
    }
    else if(C2 == 0)
    {
     while(C2 == 0);
        key_num = 9;
        DisplayKeyNum(SMG_duanma[key_num]);
    }
    else if(C3 == 0)
    {
     while(C3 == 0);
        key_num = 10;
        DisplayKeyNum(SMG_duanma[key_num]);
    }
    else if(C4 == 0)
    {
     while(C4 == 0);
        key_num = 11;
        DisplayKeyNum(SMG_duanma[key_num]);
    }
    R4 = 0;
    R2 = R3 = R1 = 1;
    C1 = C2 = C3 = C4 = 1;
    if(C1 == 0)
    {
        while(C1 == 0);
        key_num = 12;
        DisplayKeyNum(SMG_duanma[key_num]);
    }
    else if(C2 == 0)
    {
     while(C2 == 0);
        key_num = 13;
        DisplayKeyNum(SMG_duanma[key_num]);
    }
    else if(C3 == 0)
    {
     while(C3 == 0);
        key_num = 14;
        DisplayKeyNum(SMG_duanma[key_num]);
    }
    else if(C4 == 0)
    {
     while(C4 == 0);
        key_num = 15;
        DisplayKeyNum(SMG_duanma[key_num]);
    }
}

void main()
{
    while(1)
    {
        ScanKeyMulti();
    }
}



img


```

  • 写回答

2条回答 默认 最新

  • 佛大深夜学习 2023-02-14 00:35
    关注

    以下是我的回答,希望能点击一下采纳,让我有继续回答下去的动力
    https://blog.csdn.net/weixin_63568691/article/details/129019521

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 2月22日
  • 已采纳回答 2月14日
  • 修改了问题 2月12日
  • 创建了问题 2月12日

悬赏问题

  • ¥20 管道轴向耦合水击问题
  • ¥60 补全networkx TODO部分。
  • ¥15 有内推吗,云计算linux运维方向
  • ¥30 sort cuteSV.vcf by bcftools用IGV可视化出现报错
  • ¥100 SOS!对STK中导出的天体图像进行质心提取有没有人做过啊
  • ¥15 python 欧式距离
  • ¥15 运行qteasy报错
  • ¥15 遗传算法解决有工序顺序约束的大规模FJSP问题
  • ¥15 企业消防水炮塔设计方案
  • ¥20 WORKBENCH网格划分