PIC单片机的击键中断

时间:2017-03-29 11:22:32

标签: pic keypad pic18

我正在编写一个代码,用户可以按下与用户想要知道的信息对应的密钥并将其打印在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

1 个答案:

答案 0 :(得分:0)

在此上下文中使用计时器(定期轮询键盘)并没有真正帮助,因为它们也使用中断。您也可以深入研究并教自己使用中断。基本上,中断是在某些事件发生时执行的SHORT代码。

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

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