2012年4月25日 星期三

DIY - PIC: PIC18F4550 PS/2 鍵盤中斷程式 (一百二十七)

DIY - PIC PIC18F4550 PS/2 鍵盤中斷程式 (一百二十七)

PS/2 鍵盤 Data Line 接上 RB3 Clock Line 接上 RB2這次使用了 18F4550 PORTB RB2 作為按鍵中斷 I/O,每當鍵盤由 High 變成 Low便會觸發中斷執行程式而程式是會接收 RB3的數據時,會暫時停止 RB2中斷服務,直至將 Data 的數據全部接收並將數據全部顯示在 LCD 屏上。

  PIC18F4550鍵盤中斷程式流程圖
▼主程式
void main(void)
{

    // Set up RB2 Interrupt
    INTCON2bits.RBPU = 0;       // Enable Pull up resistors at PORTB
    INTCON2bits.INTEDG2 = 0;    // Ext INT2 1=Rising Edge,0=Falling Edge
           
    LATBbits.LATB2 = 1;             // Latch 1 to RB2
    LATBbits.LATB3 = 1;             // Latch 0 to RB3
    PS2KeyInit();               // Init PS2 Parameters

    // Set up RB2 Interrupt
    RCONbits.IPEN      = 1;     // Enable INT Priority
    INTCON3bits.INT2IP = 1;     // Set RB2 High INT2 1=High,0=Low
    INTCON3bits.INT2IE = 1;     // Enable RB2 INT2 1=Enable,0=Disable
    INTCONbits.PEIE    = 1;     // Enable Peripheral INT
    INTCONbits.GIE     = 1;     // Enable Global INT

    // Check the PS2 Keyboard BAT After RST~320ms Max=500ms
        for (timeout=0;timeout<50;timeout++){
                Delay10KTCYx(5);        // 10,000x(4 x 0.05us)=2ms*5=10ms
                if(PS2KeyHit()){
                        itoa(timeout,disp_temp);
                        putsXLCD(disp_temp);
                        putrsXLCD(" ");                      
                        PS2Key_Char = PS2KeyGetKey();
                        timeout=254;
                        }     
                }
    // Check the PS2 Keyboard BAT 0xAA/0xFC Code
                if (PS2Key_Char == 0xAA){
                        itoa(PS2Key_Char,disp_temp);
                        putsXLCD(disp_temp);
                        putrsXLCD(" READY");
                } else putrsXLCD(" ERROR");


while(1) {
timeout++;

                if(timeout>150){
                                timeout=0;
                                LcdSetLine2();
                                putrsXLCD("                ");
                                LcdSetLine2();
                }



                if(PS2KeyHit()){
                    PS2Key_Char = PS2KeyGetKey();
                temp=(PS2Key_Char & 0xF0);

                temp=(temp>>4)+48;
                if (temp>57) temp=temp+7;
                disp_temp[0]=temp;disp_temp[1]='\0';
                putsXLCD(disp_temp);

                temp=(PS2Key_Char & 0x0F)+48;
                        if (temp>57) temp=temp+7;
                disp_temp[0]=temp;disp_temp[1]='\0';
                            putsXLCD(disp_temp);
                putrsXLCD("");
                }



                LATAbits.LATA0 = 0;                     // RA0=0 LED=OFF Scope=50ms
                Delay10KTCYx(10);                     // 10,000 x 1 x (4 x 0.05us) = 2ms
                                                       
                LATAbits.LATA0 = 1;                     // RA0=1 LED=ON Scope=1
                Delay10KTCYx(10);                     // 10,000 x 1 x (4 x 0.05us) = 2ms
                                                           // Scope = 2ms
}
       
} // Main End

▼中斷副程式
void low_isr()
{

    INTCON3bits.INT2IE = 0;     // Disable RB2 INT Flag
    INTCON3bits.INT2IF = 0;     // Clear RB2 INT Flag

    PS2KeyIntHandler();         // PS2 Key INT Handler

    INTCON3bits.INT2IE = 1;     // Enable RB2 INT2 1=Enable,0=Disable
    INTCONbits.GIE     = 1;     // Enable All INT

}

PS/2 鍵盤接上控制板

鍵盤執行 BAT,完成後,數據 0xAA = 170
 測試 BWS 按鍵:
  B鍵數據 32 F0 32

W鍵數據 1D F0 1D

S鍵數據 1B F0 1B

沒有留言:

張貼留言