找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開始

搜索
查看: 1410|回復(fù): 2
打印 上一主題 下一主題
收起左側(cè)

單片機(jī)矩陣和獨(dú)立按鍵的結(jié)合但不知道哪里出錯(cuò)還是實(shí)現(xiàn)不了退格清零,麻煩各位幫忙...

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
#include <regx52.h>
#include <intrins.h>

#define uint unsigned int
#define uchar unsigned char
sbit beep=P2^3;
sbit DU = P2^6;
sbit WE = P2^7;
sbit key_s2 = P3^0;
uchar num;
uchar KeyValue = 20;
uchar temp;

uchar  code tabel[]= {
//0                1         2     3     4     5     6     7     8
0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F,
//9     A     B           C         D           E         F                H         L         
0x6F, 0x77, 0x7C, 0x39, 0x5E, 0x79, 0x71, 0x76, 0x38,
//n           u          -          ??
0x37, 0x3E, 0x40, 0x00 };


void delay(uint z)
{
        uint x,y;
        for(x = z; x > 0; x--)
                for(y = 114; y > 0 ; y--);                 
}

void KeyScan()
{
        
        P3 = 0XF0;
        if(P3 != 0XF0)
        {
                delay(10);
                if(P3 != 0XF0)
                {
                        switch(P3)
                        {
                                case 0xe0:        KeyValue = 0;        break;
                                case 0xd0:        KeyValue = 1;        break;
                                case 0xb0:        KeyValue = 2;        break;
                                case 0x70:        KeyValue = 3;        break;
                        }
                        P3 = 0X0F;
                        switch(P3)
                        {
                                case 0x0e:        KeyValue = KeyValue;        break;
                                case 0x0d:        KeyValue = KeyValue + 4;        break;
                                case 0x0b:        KeyValue = KeyValue + 8;        break;
                                case 0x07:        KeyValue = KeyValue + 12;        break;
                        }
                        while(P3 != 0X0F);        
                }
        }
        P3 = 0XFF;
        if(P3 != 0XFF)
        {
                delay(10);
                if(P3 != 0XFF)
                {
                        switch(P3)
                        {
                                case 0xfe:        KeyValue = 16;        break;
                                case 0xfd:        KeyValue = 17;        break;
                                case 0xfb:        KeyValue = 18;        break;
                                case 0xf7:        KeyValue = 19;        break;
                        }
                        while(P3 != 0XFF);               
                }        
        }

}


void Template()
{
        
        
        if(KeyValue==0)
        {
               
                num=0;
               
        }
        else if(KeyValue==1)
        {
               
                num=1;

        }
        else if(KeyValue==2)
        {
               
                num=2;
        }
        else if(KeyValue==3)
        {
               
                num=3;
        }
        else if(KeyValue==4)
        {
               
                num=4;
        }
        else if(KeyValue==5)
        {
               
                num=5;
        }
        else if(KeyValue==6)
        {
               
                num=6;
        }
        else if(KeyValue==7)
        {
               
                num=7;
        }
        else if(KeyValue==8)
        {
               
                num=8;
        }
        else if(KeyValue==9)
        {
               
                num=9;
        }
        
        else if(KeyValue==10)
        {
               
                num=10;
        }
        else if(KeyValue==11)
        {
                num=11;
        }
        else if(KeyValue==12)
        {
                num=12;
        }
        else if(KeyValue==13)
        {
                num=13;
        }
        else if(KeyValue==14)
        {
                num=14;
        }
        
        else if(KeyValue==15)
        {
                num=15;
        }
        
        
        if(KeyValue==16)
        {
               
                if(num!=9)  num++;
                if(4<KeyValue&&KeyValue<9)
                {
                        temp=_crol_(temp,1);
                        P1=temp;
                        
                        beep=~beep;
                        delay(500);
                }
        }
        else if(KeyValue=17)
        {
               
                num=num-1;  
                if(num==0);num=0;
        }
        else if(KeyValue=18)
        {
               
                num=num+1;  
                if(num>9)num=9;
        }
        else if(KeyValue=19)
        {
               
                num=0;   
        }

  KeyValue=49;
}


void main()
{
        temp=0xf0;
        P1=temp;
        
        WE = 1;
        P0 = 0XFE;
        WE = 0;

        DU = 1;
        while(1)
        {
                KeyScan();
                Template();
                P0 = tabel[num];
               
         
        }
}
分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

沙發(fā)
ID:161164 發(fā)表于 2022-11-13 17:34 | 只看該作者

回復(fù)

使用道具 舉報(bào)

板凳
ID:123289 發(fā)表于 2022-11-14 15:25 | 只看該作者
矩陣的各個(gè)鍵都是等價(jià)的,關(guān)鍵是按下后你程序如何執(zhí)行!
退格:清除上次鍵操作的結(jié)果,將光標(biāo)(指針)停留在上次的鍵位上。
這就要求你記住每次操作的鍵位置。
清零:將當(dāng)前位置上的東東清0 。
也要求你對(duì)每次操作的鍵位置有記錄,還要求你對(duì)清0有定義。
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表