Thứ Năm, 29 tháng 11, 2018

Giao tiếp ma trận phím 4x4

Giới thiệu

Bàn phím được sử dụng làm thiết bị đầu vào để đọc phím được người dùng nhấn và xử lý nó.
Bàn phím 4x4 bao gồm 4 hàng và 4 cột. Công tắc được đặt giữa các hàng và cột. Một phím bấm thiết lập kết nối giữa hàng và cột tương ứng giữa công tắc được đặt.
Để đọc báo chí quan trọng, chúng ta cần phải cấu hình các hàng như đầu ra và cột làm đầu vào.
Các cột được đọc sau khi áp dụng các tín hiệu cho các hàng để xác định có hay không một phím được nhấn và nếu được bấm, phím nào sẽ được bấm.

Bàn phím 4x4

 Sơ đồ
Giao diện bàn phím 4x4 với PIC18F4550 

Thí dụ

Ở đây, chúng ta sẽ giao diện bàn phím 4x4 với PIC18F4550 và sẽ hiển thị nhấn một phím trên LCD16x2.
LCD16x2 được sử dụng ở đây ở chế độ 4 bit.

Chương trình

/*
 * Keypad Interfacing with PIC18F4550
 * 
 */

#include <pic18f4550.h>
#include "Configuration_Header_File.h"
#include "16x2_LCD_4bit_File.h"

unsigned char keyfind();  /* function to find pressed key */

#define write_port LATB  /* latch register to write data on port */
#define read_port PORTB  /* PORT register to read data of port */
#define Direction_Port TRISB
unsigned char col_loc,rowloc,temp_col;

unsigned char keypad[4][4]= {'7','8','9','/',
                             '4','5','6','*',
                             '1','2','3','-',
                             ' ','0','=','+'};

void main() 
{
    char key;
    OSCCON = 0x72;

    RBPU=0;  /* activate pull-up resistor */
    LCD_Init();  /* initialize LCD16x2 in 4-bit mode */
    LCD_String_xy(0,0,"Press a Key");
    LCD_Command(0xC0);  /* display pressed key on 2nd line of LCD */
    while(1)
    {
        key = keyfind();  /* find a pressed key */
        LCD_Char(key);  /* display pressed key on LCD16x2 */
    }
    
}

unsigned char keyfind()
{      
    
    Direction_Port = 0xf0;  /* PORTD.0-PORTD.3 as a Output Port and PORTD.4-PORTD.7 as a Input Port*/
    unsigned char temp1;
   
        write_port = 0xf0;  /* Make lower nibble as low(Gnd) and Higher nibble as High(Vcc) */
      do
      {
        do
        {
          
            col_loc = read_port & 0xf0;  /* mask port with f0 and copy it to col_loc variable */   
        
        }while(col_loc!=0xf0);  /* Check initially at the start there is any key pressed*/ 
         col_loc = read_port & 0xf0;  /* mask port with f0 and copy it to col_loc variable */  
      }while(col_loc!=0xf0);
      
      MSdelay(50);
        write_port = 0xf0;  /* Make lower nibble as low(Gnd) and Higher nibble as High(Vcc) */
        do
        { do
            
            {
               
                col_loc = read_port & 0xf0;
            }while(col_loc==0xf0);  /* Wait for key press */
         col_loc = read_port & 0xf0;
        }while(col_loc==0xf0);  /* Wait for key press */
               
        MSdelay(20);
         
        col_loc = read_port & 0xf0;
               
   
    
    while(1)
    {
        write_port  = 0xfe;  /* make Row0(D0) Gnd and keep other row(D1-D3) high */
        col_loc = read_port & 0xf0;         /* Read Status of PORT for finding Row */
        temp_col=col_loc;
        if(col_loc!=0xf0)
        {
            rowloc=0;  /* If condition satisfied get Row no. of key pressed */
            while(temp_col!=0xf0)  /* Monitor the status of Port and Wait for key to release */
            {
                temp_col = read_port & 0xf0;  /* Read Status of PORT for checking key release or not */    
            }
            break;
        }
        
        write_port = 0xfd;  /* make Row1(D1) Gnd and keep other row(D0-D2-D3) high */
        col_loc = read_port & 0xf0;  /* Read Status of PORT for finding Row */
        temp_col=col_loc;
        if(col_loc!=0xf0)
        {
            rowloc=1;  /* If condition satisfied get Row no. of key pressed*/
            while(temp_col!=0xf0)  /* Monitor the status of Port and Wait for key to release */
            {
                temp_col = read_port & 0xf0;  /* Read Status of PORT for checking key release or not */
            }
            break;
        }
        
        write_port = 0xfb;  /* make Row0(D2) Gnd and keep other row(D0-D1-D3) high */
        col_loc = read_port & 0xf0;  /* Read Status of PORT for finding Row*/
        temp_col=col_loc;
        if(col_loc!=0xf0)
        {
            rowloc=2;  /* If condition satisfied get Row no. of key pressed */
            while(temp_col!=0xf0)  /* Wait for key to release */
            {
                temp_col = read_port & 0xf0;  /* Read Status of PORT for checking key release or not */
            }
            break;
        }
        
        write_port = 0xf7;  /* make Row0(D3) Gnd and keep other row(D0-D2) high */
        col_loc = read_port & 0xf0;  /* Read Status of PORT for finding Row */
        temp_col=col_loc;
        if(col_loc!=0xf0)
        {
            rowloc=3;  /* If condition satisfied get Row no. of key pressed */
            while(temp_col!=0xf0)  /* Wait for key to release */
            {
                temp_col = read_port & 0xf0;  /* Read Status of PORT for checking key release or not */
            }
            break;
        }
        
    }

    
    while(1)
    {
        
        if(col_loc==0xe0)
        {
             return keypad[rowloc][0];  /* Return key pressed value to calling function */            
        }
        else 
            if(col_loc==0xd0)
        {
            return keypad[rowloc][1];  /* Return key pressed value to calling function */   
        }
        else
            if(col_loc==0xb0)
        {
            return keypad[rowloc][2];  /* Return key pressed value to calling function */
        }
        else
            
        {
            return keypad[rowloc][3];  /* Return key pressed value to calling function */           
        }    
    }
    
   MSdelay(300);     
 
}



Không có nhận xét nào:

Đăng nhận xét