首页 > 代码库 > 4x4矩阵键盘 扫描程序

4x4矩阵键盘 扫描程序

一:不排除第四位异常处理

技术分享
uchar JuzhenkeyScan(){//    P3=0xfe;//    temp=P3;//    while(temp!=0xfe)//    {//        temp=P3;//        switch(temp)//    {//        case 0xee:num=10;//        break;//        case 0xde:num=3;//        break;//        case 0xbe:num=2;//        break;//        case 0x7e:num=1;//        break;//    }//    delayms(10);//    }    P3=0xfd;//1111 1110    temp=P3;    while(temp!=0xfd)    {    temp=P3;    switch(temp)    {        case 0xed:num=11;        break;        case 0xdd:num=6;        break;        case 0xbd:num=5;        break;        case 0x7d:num=4;        break;    }    delayms(10);    }        P3=0xfb;    temp=P3;    while(temp!=0xfb)    {    temp=P3;    switch(temp)    {        case 0xeb:num=12;        break;        case 0xdb:num=9;        break;        case 0xbb:num=8;        break;        case 0x7b:num=7;        break;    }    delayms(10);    }        P3=0xf7;    temp=P3;    while(temp!=0xf7)    {    temp=P3;    switch(temp)    {        case 0xe7:num=13;        break;        case 0xd7:num=14;        break;        case 0xb7:num=0;        break;        case 0x77:num=14;        break;    }    delayms(10);    }    return num;}
View Code

二:排除第四位异常处理

技术分享
  1 uchar keyscan()  2 {  3     P3=0xfe;  4     tempkey=P3;  5     tempkey=tempkey&0xf0;  6     while(tempkey!=0xf0)  7     {  8         delayms(5);  9         tempkey=P3; 10         tempkey=tempkey&0xf0; 11         while(tempkey!=0xf0) 12         { 13             tempkey=P3; 14             switch(tempkey) 15             { 16                 case 0xee:num=10; //A 17                 break; 18                 case 0xde:num=3; 19                 break; 20                 case 0xbe:num=2; 21                 break; 22                 case 0x7e:num=1; 23                 break; 24             } 25             while(tempkey!=0xf0) 26             { 27                 tempkey=P3; 28                 tempkey=tempkey&0xf0; 29             } 30  31         } 32     } 33  34      35      36      37             P3=0xfd; 38             tempkey=P3; 39             tempkey=tempkey&0xf0; 40             while(tempkey!=0xf0) 41                 { 42                     delayms(5); 43                     tempkey=P3; 44                     tempkey=tempkey&0xf0; 45                     while(tempkey!=0xf0) 46                     { 47                         tempkey=P3; 48                     switch(tempkey) 49                         { 50                             case 0xed:num=11;//B 51                                 break; 52                             case 0xdd:num=6; 53                                 break; 54                             case 0xbd:num=5; 55                                 break; 56                             case 0x7d:num=4; 57                                 break; 58                         } 59                     while(tempkey!=0xf0) 60                         { 61                             tempkey=P3; 62                             tempkey=tempkey&0xf0; 63                         } 64  65                     } 66                 } 67  68  69             P3=0xfb; 70             tempkey=P3; 71             tempkey=tempkey&0xf0; 72             while(tempkey!=0xf0) 73                 { 74                     delayms(5); 75                     tempkey=P3; 76                     tempkey=tempkey&0xf0; 77                     while(tempkey!=0xf0) 78                     { 79                         tempkey=P3; 80                     switch(tempkey) 81                         { 82                             case 0xeb:num=12; //C 83                                 break; 84                             case 0xdb:num=9; 85                                 break; 86                             case 0xbb:num=8; 87                                 break; 88                             case 0x7b:num=7; 89                                 break; 90                         } 91                     while(tempkey!=0xf0) 92                         { 93                             tempkey=P3; 94                             tempkey=tempkey&0xf0; 95                         } 96      97                     } 98                 } 99 100 101             P3=0xf7;102             tempkey=P3;103             tempkey=tempkey&0xf0;104             while(tempkey!=0xf0)105                 {106                     delayms(5);107                     tempkey=P3;108                     tempkey=tempkey&0xf0;109                     while(tempkey!=0xf0)110                     {111                         tempkey=P3;112                     switch(tempkey)113                         {114                             case 0xe7:num=13;115                                 break;116                             case 0xd7:num=14;117                                 break;118                             case 0xb7:num=0;119                                 break;120                             case 0x77:num=14;121                                 break;122                         }123                     while(tempkey!=0xf0)124                         {125                             tempkey=P3;126                             tempkey=tempkey&0xf0;127                         }128                     }  129                 }130 131 132 }
View Code

 

4x4矩阵键盘 扫描程序