2017-03-23 3 views
0

J'ai un tel programme, ça marche. Je reçois le numéro du bouton appuyé sur l'écran LED. Mais j'ai besoin de changer ce programme afin qu'il montre les 2 derniers chiffres pressés sur l'affichage, quand j'appuie sur * ou #. Par exemple, j'appuie sur '1 2 3 4 5 #'. Sur l'affichage, je ne vois que deux derniers chiffres '4 5'. Comment puis-je faire ceci?Microcontrôleur 8051 avec clavier matriciel et affichage à LED

#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]; 
} 
    } 

schema

Répondre

0
unsigned int last_two_buttons[2] = {0x0, 0x0}; /* array of 2 elements to remember the last two buttons pressed. */ 

unsigned int update_display = 0; //flag to indicate if LED display needs to be updated. 

, au lieu d'attribuer P1 = Affichage [x] pour chacun le bouton pressé, vous vous souvenez simplement/magasin qui touche enfoncée dans ce tableau comme suit:

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. 

Maintenant, améliorez le scan() pour détecter les boutons * et #.

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; 

} 

L'hypothèse est que, si l'utilisateur appuie sur un seul bouton, disons 5 # puis, nous exposerons 0 et 5 comme les deux derniers boutons enfoncé.

Espérons que cela aide.