Keyboard 带矩阵键盘和LED显示屏的8051微控制器

Keyboard 带矩阵键盘和LED显示屏的8051微控制器,keyboard,led,keil,8051,Keyboard,Led,Keil,8051,我有这样一个程序,它是有效的。我从LED显示屏上按下的按钮中获取号码。但我需要更改此程序,以便在我按*或#时在显示屏上显示最后两个按下的数字。 例如,我按“12345”。在显示屏上,我只看到最后两个数字“4 5”。我该怎么做 #include <REGX52.h> #define SEG P1 #define keypad P2 sbit r1 = P2^0; sbit r2 = P2^1; sbit r3 = P2^2; sbit r4 = P2^3; sbit c1

我有这样一个程序,它是有效的。我从LED显示屏上按下的按钮中获取号码。但我需要更改此程序,以便在我按*或#时在显示屏上显示最后两个按下的数字。 例如,我按“12345”。在显示屏上,我只看到最后两个数字“4 5”。我该怎么做

#include <REGX52.h>
#define SEG P1
#define keypad P2

sbit r1 = P2^0; 
sbit r2 = P2^1; 
sbit r3 = P2^2; 
sbit r4 = P2^3; 

sbit c1 = P2^4; 
sbit c2 = P2^5; 
sbit c3 = P2^6; 
sbit c4 = P3^7;

void scan(void);

unsigned int Display[12] = {0x0, 0x1, 0x2, 0x3,0x4,0x5,0x6,0x7,0x8,0x9};

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

void scan(void){
r1=0;
r2=r3=r4=1;

if(c1==0)
{
    while(c1==0){
        P1=Display[1];
    }
}
    if(c2==0)
    {
        while(c2==0){
            P1=Display[2];
}
}
    if(c3==0)
{
    while(c3==0){
        P1=Display[3];
    }
}
r2=0;
r1=r3=r4=1;
if(c1==0)
{
    while(c1==0){
        P1=Display[4];
    }
}
if(c2==0)
    {
        while(c2==0){
            P1=Display[5];
}
}
    if(c3==0)
{
    while(c3==0){
        P1=Display[6];
    }
}
r3=0;
r1=r2=r4=1;
if(c1==0)
{
    while(c1==0){
        P1=Display[7];
    }
}
if(c2==0)
    {
        while(c2==0){
            P1=Display[8];
}
}
    if(c3==0)
{
    while(c3==0){
        P1=Display[9];
    }
}
r4=0;
r1=r2=r3=1;
if(c2==0)
{
    while(c2==0){
        P1=Display[0];
}
    }
#包括
#定义SEG P1
#定义键盘P2
sbit r1=P2^0;
sbit r2=P2^1;
sbit r3=P2^2;
sbit r4=P2^3;
sbit c1=P2^4;
sbit c2=P2^5;
sbit c3=P2^6;
sbit c4=P3^7;
作废扫描(作废);
无符号整数显示[12]={0x0、0x1、0x2、0x3、0x4、0x5、0x6、0x7、0x8、0x9};
真空总管(真空)
{
而(1)
{
扫描();
}
}
作废扫描(作废){
r1=0;
r2=r3=r4=1;
如果(c1==0)
{
而(c1==0){
P1=显示[1];
}
}
如果(c2==0)
{
而(c2==0){
P1=显示[2];
}
}
如果(c3==0)
{
而(c3==0){
P1=显示[3];
}
}
r2=0;
r1=r3=r4=1;
如果(c1==0)
{
而(c1==0){
P1=显示[4];
}
}
如果(c2==0)
{
而(c2==0){
P1=显示[5];
}
}
如果(c3==0)
{
而(c3==0){
P1=显示[6];
}
}
r3=0;
r1=r2=r4=1;
如果(c1==0)
{
而(c1==0){
P1=显示[7];
}
}
如果(c2==0)
{
而(c2==0){
P1=显示[8];
}
}
如果(c3==0)
{
而(c3==0){
P1=显示[9];
}
}
r4=0;
r1=r2=r3=1;
如果(c2==0)
{
而(c2==0){
P1=显示[0];
}
}

现在,您不必为每个按下的按钮分配P1=Display[x],只需记住/存储按下的按钮,如下所示:

last_two_buttons[0] = last_two_buttons[1]; 
last_two_buttons[1] = Display[x]; //x here indicates the button pressed, the same way as you have been using in your code.
现在,增强scan()以检测*和#按钮

r4=0;
r1=r2=r3=1;
if(c1==0)
{
    while(c1==0){
        update_display = 1; // * button pressed
}

if(c3==0)
{
    while(c3==0){
        update_display = 1; // # button pressed
}

if(update_display)
{

    P1 = last_two_buttons[0] <<4 + last_two_buttons[1];
    update_display = 0; //reset the variables for next scan.
    last_two_buttons[0] = 0; 
    last_two_buttons[1] = 0;

}
r4=0;
r1=r2=r3=1;
如果(c1==0)
{
而(c1==0){
更新显示=1;//*按钮按下
}
如果(c3==0)
{
而(c3==0){
更新显示=1;//#按钮按下
}
如果(更新显示)
{
P1=最后两个按钮[0]
r4=0;
r1=r2=r3=1;
if(c1==0)
{
    while(c1==0){
        update_display = 1; // * button pressed
}

if(c3==0)
{
    while(c3==0){
        update_display = 1; // # button pressed
}

if(update_display)
{

    P1 = last_two_buttons[0] <<4 + last_two_buttons[1];
    update_display = 0; //reset the variables for next scan.
    last_two_buttons[0] = 0; 
    last_two_buttons[1] = 0;

}