【问题标题】:keystroke interrupt for PIC microcontrollerPIC单片机的击键中断
【发布时间】: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 个计时器吗?

标签: pic keypad pic18


【解决方案1】:

在这种情况下使用计时器(定期轮询键盘)并没有真正的帮助,因为它们也使用中断。您不妨深入研究并自学使用中断。基本上,中断是在某些事件发生时执行的一小段代码。

对于您的键盘,使用更改时中断。当中断触发时,读取键盘并设置一个全局信号量(标志),您的主程序可以在空闲时检查它,同时执行其他任务(例如在 main() 例程中的事件循环中。

或者,如果您没有太多其他事情可做,但不断轮询键盘输入行以查看是否有任何活动。

【讨论】:

    猜你喜欢
    • 2014-04-24
    • 2014-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多