我有这样的程序,它有效。我从LED显示屏上的按下按钮获得数字。但是当我按*或#时,我需要更改此程序,以便在显示屏上显示最后2个按下的数字。 例如,我按'1 2 3 4 5#'。在显示器上,我只看到最后两个数字'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];
}
}
答案 0 :(得分: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.
现在,您不必为每个按下的按钮分配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;
}
这里的假设是,如果用户只按下一个按钮,比如5#,那么我们将显示0和5作为最后两个按下的按钮。
希望这有帮助。