Skip to main content

keypad Interfacing with PIC18 microcontroller

Keypad Interfacing with PIC18 microcontroller

KEYPAD INTERFACING

Keypad and LCDs are the most widely used input/output devices and a basic understanding of them is essential. In this Post, we first discuss keypad fundamentals, along with key press detection and key identification mechanisms.

Then we show how a keypad is interfaced to a PIC18.





Interfacing the keypad to the PIC18
At lowest level, keypads are arranged in a matrix of rows and columns, PIC controller uses two 8 bit ports to entertain 8 x 8 key matrix to connect it.  

Connection establish between row and column whenever a key is pressed, in any other case connection considered to be open between row and column. 

Generally, In personal computers keyboards, there is also a  microcontroller that can handle hardware and software interfacing of the keyboard.
 
In a simple way ,Read only memory of microcontroller utilized to store the code to scan the keys constantly and detect which rows and column in contact, pass it to motherboard for further action.   

Hence, to write code for keypad interfacing we must need two processes: (a) key press detection, and (b) key identification. 

Scanning method for key press detection

Normally, scanning process is used in key press detection. To achieve the desired results, microcontroller reads the column after applying ground to all rows. If the logic levels read from the columns are all high means no key has been pressed and the process repeats until a key press is detected. 

If one of the column bits has a logic level zero, however, this indicates  that a key press exist. Next, microcontroller needs the code to identify the pressed key, this time microcontroller grounds only first row and reads the column and check the status of data , if all high means no key in that row is pressed, process goes on  for remaining rows. When the row is detected in which key has been presses, code will find the relevant column. 

Don't worry about the above complex coding ,miKroC library for keypad can easily solve our problem.

Keypad Library in miKroC

The mikroC PRO for PIC provides a library for working with 4x4 keypad. The library routines can also be used with 4x1, 4x2, or 4x3 keypad. For connections explanation see Proteus simulation at the bottom of this post.

Keypad_Init

       
Prototype:
void Keypad_Init(void);

Returns:
Nothing.

Description:
Initializes port for working with keypad.

Requires:
Global variable :
  • keypadPort - Keypad port
must be defined before using this function.

Example:
// Keypad module connections
char keypadPort at PORTD;
// End of keypad module connections
...
Keypad_Init();

Keypad_Key_Press


Prototype:
char Keypad_Key_Press(void);

Returns:
The code of a pressed key (1..16).
If no key is pressed, returns 0.

Description:
Reads the key from keypad when key gets pressed.

Requires:
Port needs to be initialized for working with the Keypad library, see Keypad_Init.

Example:
char kp;
...
kp = Keypad_Key_Press();

Keypad_Key_Click


Prototype:
char Keypad_Key_Click(void);

Returns:
The code of a clicked key (1..16).
If no key is clicked, returns 0.

Description:
Call to Keypad_Key_Click is a blocking call: the function waits until some key is pressed and released. When released, the function returns 1 to 16, depending on the key. If more than one key is pressed simultaneously the function will wait until all pressed keys are released. After that the function will return the code of the first pressed key.

Requires:
Port needs to be initialized for working with the Keypad library, see Keypad_Init.

Example:
char kp;
...
kp = Keypad_Key_Click();

Proteus Simulation for Keypad Simulation






miKroC Code for Keypad Interfacing With PIC18f4580


 unsigned short kp;

// 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(){
  ADCON1 = 0xff;
  CMCON = 0x7;
  Keypad_Init();
  Lcd_Init();
  Lcd_Cmd(_LCD_CLEAR);
  Lcd_Cmd(_LCD_CURSOR_OFF);
  Lcd_Out(1, 1, "Key Pressed:");


  do {
    kp = 0;
    do
      kp = Keypad_Key_Click();
    while (!kp);
    switch (kp) {


      case  1: kp = 49; break; // 1
      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(2, 10, kp);
  } while (1);
}







CSS Code for keypad Interfacing 









#include "flex_lcd.c"
#include "KBD.c"
 char k;
void main()
{

   setup_adc_ports(NO_ANALOGS|VSS_VDD);
   setup_adc(ADC_OFF|ADC_TAD_MUL_0);
   setup_psp(PSP_DISABLED);
   setup_spi(SPI_SS_DISABLED);
   setup_wdt(WDT_OFF);
   setup_timer_0(RTCC_INTERNAL);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);
//Setup_Oscillator parameter not selected from Intr Oscillator Config tab

   // TODO: USER CODE!!

lcd_init();
   kbd_init();

   lcd_putc("\fReady...\n");
  

   while (TRUE) {
      k=kbd_getc();
      if(k!=0)
       if(k=='*')
          lcd_putc('\f');
        else
          lcd_putc(k);
        
   }
}


                    Friends for further learning about PIC18 checkout 

Comments

Popular posts from this blog

How to use SPI Debugger

  SPI Debugger Hi Friends, in this post, we will discuss how to use SPI Debugger available in proteus for serial peripheral interface (SPI) which is a built-in feature of PIC microcontroller to communicate and data exchange between PIC and other devices. SPI Debugger Introduction to SPI Guys, SPI can be considered as a programmable shift  register, The SPI module is a synchronous serial I/O port that shifts a serial bit stream of variable length and data rate between the PIC and other peripheral devices. Here “synchronous” means that the data transmission is synchronized to a clock signal. To avoid further delay, let's see some important connections and different configurations to effectively use SPI Debugger. How to use SPI Debugger in MASTER & SLAVE configuration Select the virtual instrument and from the instrument, list selects "SPI Debugger". SPI Debugger Insert two SPI Debugger on the working area, select any one of them,

PIC18f4580 Timer0 calculation using miKroC , CSS and proteus

PIC18f4580 TIMER PROGRAMMING The PIC18f4580 has four timers. they're named as Timers zero, one, two, and three. they will be used either as timers to come up with a time delay or as counters to count events happening outside the microcontroller. First, we see however Timers zero is employed to come up with time delays. Every timer wants a clock pulse to tick. The clock supply will be internal or external. If we have a tendency to use the inner clock supply, then 1/ fourth of the frequency of the crystal oscillator on the OSC1 and OSC2 pins (Fosc/4) is fed into the timer. Therefore, it's used for time delay generation and for that reason is termed a timer. By selecting the external clock choice, we have a tendency to feed pulses through one among the PIC18's pins: this is often known as a counter. GIF  taken from  https://exploreembedded.com Basic registers of the timer Majority of t timers in 18F are 16 bits wide. Because the PIC 18 has an 8- bit ar

Frequency Counter using Interrupts PIC18f4580 Project

Frequency Counter  using PIC18f458 0 Proje ct This POST describes the construction of small frequency counter with a cheap PIC18f4580 microcontroller with 16 x 2 LCD.   Prerequisites: PIC18F4580 TIMER Programming. PIC18F4580 COUNTER Programming. PIC18F4580 Interfacing with 16x2 LCD. PIC18F4580 Interrupts Programming  ( we will cover in this POST) Concept: Frequency  is the number of occurrences of a repeating event per unit time. in our case we will measure a number of clocks generated by clock source per unit time. In this project, LCD is used to display the frequency and PIC timer 1 to measure the input signal and Timer0 to generate an indication that one second has gone. System software utilizes Timer-1 in the 16-bit counter mode to count the input signal and overflows of the counter are added to provide the aggregate count in multiples of 65536. Totaling the existing value of the counter at the conclusion provides the full count.