Thursday, 4 August 2016

Lesson 9 Interfacing Keypad with PIC Microcontroller - MikroC

Keypad finds its application where there is need for the user to enter alpha-numeric inputs. In this tutorial, we'll consider how a matrix keypad is interfaced with a PIC microcontroller. Keypads comes in various sizes 3x3, 4x3 ,4x4. we're making use of a 4x4 matrix keypad, which is a basically a combination of push buttons in a matrix form. these arrangements allows for less i/o ports of the microcontroller to be used by the keypad.
A 4x4 keypad will require 8 i/o lines. the keypad is connected to the microcontroller (PIC18F45K22) as shown below in the Proteus Schematic:
Proteus Schematic
column pins is connected to D0-D3
Row pins is connected to D4-D7

Keypad scanning is done to determine which key is pressed. 
A high signal is sent to one of the column lines: for instance, a logic high is given to col. 1 which is connected to PORT D0, the microcontroller reads the state of the row pins PORT D4 - D7, if the D-Row (PORT D7) is high, then the  asterisk (*) button is pressed. A logic high signal is also applied to other columns through the microcontroller port and the rows are read again.

 Assuming col 2 is high, the microcontroller reads the state of the row pins PORT D4 - D7, if Row C (PORT D6) is high, then 8 button is pressed.

This procedure is repeated for all the columns continuously.
MikroC has library functions for keypad inputs, go to the MikroC help files and search for Keypad. The library routines are:
  • keypad_Init()
  • keypad_Key_Press()
  • keypad_Key_Click()
Remember to check the help files to see how to use them.

MikroC Code:

/**********************************
Project Name :
   Interfacing keypad with PIC 18F45k22
Author : Promise
Description :
   This project demonstrate how to interface A MATRIX keypad with PIC 18F45K22. The keypad is connected to PORTD, and the LCD PORTB
Test configuration:
     MCU:             PIC18F45K22
                    http://ww1.microchip.com/downloads/en/DeviceDoc/41412D.pdf
     Dev.Board:       EasyPIC7
                      http://www.mikroe.com/easypic/
     Oscillator:      16.00000 MHz
     SW:              mikroC PRO for PIC
                      http://www.mikroe.com/mikroc/pic/
 * NOTES:
     - Pull-down PORTD (PortD three-state switch).
     - Turn on LCD backlight SW4.6 (board specific)
***********************************/


unsigned short kp, count, oldstate = 0;
char txt[6];

// Keypad module connections
char  keypadPort at PORTD;
// End Keypad module connections

// LCD module connections
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;

sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
// End LCD module connections

void main() {
  count = 0;                                 // Reset counter
  Keypad_Init();                           // Initialize Keypad
  ANSELB = 0;                              // Configure AN pins as digital I/O
  ANSELD = 0;
  Lcd_Init();                              // Initialize LCD
  Lcd_Cmd(_LCD_CLEAR);                     // Clear display
  Lcd_Cmd(_LCD_CURSOR_OFF);                // Cursor off

  Lcd_Out(1, 1, "Press any Key");                 // Write message text on LCD
  Lcd_Out(2, 1, "After 2 secs");
  delay_ms(2000);
  Lcd_Cmd(_LCD_CLEAR);                     // Clear display
  
  do {
    kp = 0;                                // Reset key code variable

    // Wait for key to be pressed and released
    do
      // kp = Keypad_Key_Press();          // Store key code in kp variable
      kp = Keypad_Key_Click();             // Store key code in kp variable
    while (!kp);
   // Prepare value for output, transform key to it's ASCII value
    switch (kp) {
      //case 10: kp = 42; break;  // '*'   // Uncomment this block for keypad4x3
      //case 11: kp = 48; break;  // '0'
      //case 12: kp = 35; break;  // '#'
      //default: kp += 48;

      case  1: kp = 49; break; // 1        // Uncomment this block for keypad4x4
      case  2: kp = 50; break; // 2
      case  3: kp = 51; break; // 3
      case  4: kp = 65; break; // A
      case  5: kp = 52; break; // 4
      case  6: kp = 53; break; // 5
      case  7: kp = 54; break; // 6
      case  8: kp = 66; break; // B
      case  9: kp = 55; break; // 7
      case 10: kp = 56; break; // 8
      case 11: kp = 57; break; // 9
      case 12: kp = 67; break; // C
      case 13: kp = 42; break; // *
      case 14: kp = 48; break; // 0
      case 15: kp = 35; break; // #
      case 16: kp = 68; break; // D

    }

    Lcd_Chr(1,++count, kp);                    // Print key ASCII value on LCD

    if (count == 16) {                               // If counter variable overflow
      Lcd_Out(2, 1, "Wait for 2 secs");
      delay_ms(2000);
      count = 0;                                                 //Reset counter
      Lcd_Cmd(_LCD_CLEAR);                     // Clear display
      }

    } while (1);
}


No comments:

Post a Comment