【发布时间】:2017-04-01 03:31:36
【问题描述】:
我正在编写一个代码,用户可以在其中按下与用户想知道的信息相对应的键并将其打印在 LCD 显示器上。到目前为止,我能够编写代码以在 LCD 上打印,但我无法弄清楚如何让 MCU 等待击键然后打印。 谁能建议我这样做的逻辑是什么?
微控制器:PIC18F4550; MPLAB X IDE、带有 PICkit3 的 XC8。我正在使用 Windows 7。 键盘:3x4 矩阵键盘 - MCAK304NBWB
我的代码是:
#define C1_DAT LATBbits.LATB4 //Column 1 is set portB4 as output
#define C2_DAT LATBbits.LATB5 //Column 2 is set portB5 as output
#define C3_DAT LATBbits.LATB6 //Column 3 is set portB6 as output
#define C1_DIR TRISBbits.TRISB4 //Column 1 is set portB4 as output
#define C2_DIR TRISBbits.TRISB5 //Column 2 is set portB5 as output
#define C3_DIR TRISBbits.TRISB6 //Column 3 is set portB6 as output
.....
C1_DIR = 0; //drive column 1 low
rows[0] = PORTB & 0x0f; //read all four rows1 buttons
rows[1] = 0x0f;
rows[2] = 0x0f;
DelayXLCD();
if ( (rows[0] & 0b0001) == 0) // will be zero if the "0" button is currently pressed.
putrsXLCD("key 1 is pressed");
DelayXLCD();
//break;
if ( (rows[0] & 0b0010) == 0) // will be zero if the "0" button is currently pressed.
putrsXLCD("key 4 is pressed");
DelayXLCD();
//break;
if ( (rows[0] & 0b0100) == 0) // will be zero if the "0" button is currently pressed.
putrsXLCD("key 7 is pressed");
DelayXLCD();
if ( (rows[0] & 0b1000) == 0) // will be zero if the "0" button is currently pressed.
putrsXLCD("key * is pressed");
DelayXLCD();
//break;
if ( (rows[1] & 0b0001) == 0) // will be zero if the "0" button is currently pressed.
putrsXLCD("key 2 is pressed");
DelayXLCD();
if ( (rows[1] & 0b0010) == 0) // will be zero if the "0" button is currently pressed.
putrsXLCD("key 5 is pressed");
DelayXLCD();
//break;
if ( (rows[1] & 0b0100) == 0) // will be zero if the "0" button is currently pressed.
putrsXLCD("key 8 is pressed");
//break;
if ( (rows[1] & 0b1000) == 0) // will be zero if the "0" button is currently pressed.
putrsXLCD("key 0 is pressed");
DelayXLCD();
//break;
if ( (rows[2] & 0b0001) == 0) // will be zero if the "0" button is currently pressed.
putrsXLCD("key 3 is pressed");
DelayXLCD();
//break;
if ( (rows[2] & 0b0010) == 0) // will be zero if the "0" button is currently pressed.
putrsXLCD("key 6 is pressed");
DelayXLCD();
//break;
if ( (rows[2] & 0b0100) == 0) // will be zero if the "0" button is currently pressed.
putrsXLCD("key 9 is pressed");
DelayXLCD();
//break;
if ( (rows[2] & 0b1000) == 0) // will be zero if the "0" button is currently pressed.
putrsXLCD("key # is pressed");
DelayXLCD();
Delay_s(20);
//if ( (rows[0] & rows[1] & rows[2]) == 0x0F) // will be 0x0F if all three values are 0x0F
//putrsXLCD("no keys are pressed");
//DelayXLCD();
} while((rows[0] & rows[1] & rows[2]) != 0x0F); // wait for keys release
{putrsXLCD("no keys are pressed");
Delay_s(50);
LCD_Clear();} //end of main loop
【问题讨论】:
-
您是否尝试过 google,例如按钮中断?
-
我做到了。大多数情况下,我收到的想法是,如果我不是经验丰富的人,就不要被打扰,我不是。它的替代方案是计时器。我正在调查它。只是好奇,我怎样才能将定时器用作中断?我可以同时使用 2 个计时器吗?