Skip to main content

The future of AI in business and its potential to transform industries

Interfacing 4x3 Keypad with PIC18F4520 controller

Interfacing 4x3 Keypad with PIC18F4520 controller

nterfacing keypad with PIC18F4520

The keypad is one of the most widely used peripheral devices, from our academics projects to industrial use whenever user interface is required we remember the keypad for our embedded systems. Hence learning how to interface keypad to different-different controllers becomes important.

In this article, we will see how to interface a 4x3 keypad to the PIC18F4520 controller.

We will cover the topics on the following points:-

  • About the project
  • Selecting the required components
  • Circuit diagram
  • Writing the code for the controller
  • Uploading the HEX file to the controller
  • Simulating the project

About the project

In this project, the keypad is connected to Port B and the data pins are connected to the Port D of the PIC controller. Data controlling pin of the LCD are connected to pin 3.0, pin 3.1, pin 3.2 of port C.

In this simple project initially, the LCD display will print the name of this blog page "Code somenplus" and then it will ask to enter the digits from the keypad. Whatever digit you will press it will display on the LCD screen.

Software tools

  • Proteus professional 8 (project simulation)
  • MP Lab IDE (writing code for the controller)

Selecting the required components

For the completion of this project simulation, the components that required are:

  • PIC18F420 controller
  • LM016L 16x2 LCD display
  • 4x3 Phone keypad

Circuit diagram

The circuit diagram for the simulation is given below

keypad interfacing circuit diagram

Please make sure all the pins are connected properly.

Writing the code for the controller

The code for the controller is given below

#include <xc.h>

#define _XTAL_FREQ 20000000


#define rs RC0

#define rw RC1

#define en RC2


#define col0 RB0

#define col1 RB1

#define col2 RB2

#define row0 RB4

#define row1 RB5

#define row2 RB6

#define row3 RB7


void lcd_data(unsigned char data);

void lcd_cmd(unsigned char command);

void lcd_string(const unsigned char *string, unsigned char length);

void lcd_initialize();

void keypad();


void main()

{

    TRISD=0;

    TRISC=0;

    TRISB=0xF0;

    

    lcd_initialize();

    

    lcd_cmd(0x80);

       lcd_string("Code Somenplus",14);

       __delay_ms(2000);

    

       lcd_cmd(0x01);

       lcd_cmd(0x80);

       lcd_string("Enter Digits",12);

       lcd_cmd(0xc0);

    

    while(1)

    {

       keypad();

        

    }

}


void lcd_data(unsigned char data)

{

    PORTD=data;

    rs=1;

    rw=0;

    en=1;

    __delay_ms(5);

    en=0;

}


void lcd_cmd(unsigned char command)

{

    PORTD=command;

    rs=0;

    rw=0;

    en=1;

    __delay_ms(5);

    en=0;

}


void lcd_string(const unsigned char *string, unsigned char length)

{

    unsigned char i;

    for(i=0;i<length;i++)

    {

        lcd_data(string[i]);

    }

}


void lcd_initialize()

{

    lcd_cmd(0x38);

    lcd_cmd(0x06);

    lcd_cmd(0x0c);

    lcd_cmd(0x01);

}


void keypad()

{

    col0=1;

    col1=0;

    col2=0;

    

    if(row0==1)

    {

        lcd_data('1');

        while(row0==1);  

    }

    

    if(row1==1)

    {

        lcd_data('4');

        while(row1==1);

    }

    

    if(row2==1)

    {

        lcd_data('7');

        while(row2==1);   

    }

    

    if(row3==1)

    {

        lcd_data('*');

        while(row3==1);  

    }

    

    col0=0;

    col1=1;

    col2=0;

    

    if(row0==1)

    {

        lcd_data('2');

        while(row0==1);

    }

    

    if(row1==1)

    {

        lcd_data('5');

        while(row1==1);

    }

    

    if(row2==1)

    {

        lcd_data('8');

        while(row2==1); 

    }

    

    if(row3==1)

    {

        lcd_data('0');

        while(row3==1);  

    }

    

    col0=0;

    col1=0;

    col2=1;

    

    if(row0==1)

    {

        lcd_data('3');

        while(row0==1);

    }

    

    if(row1==1)

    {

        lcd_data('6');

        while(row1==1);

    }

    

    if(row2==1)

    {

        lcd_data('9');

        while(row2==1);

    }

    

    if(row3==1)

    {

        lcd_data('#');

        while(row3==1);    

    }

}

After writing the code on MP lab IDE all you need to compile the code and build a hex file for the controller.

Uploading the HEX file to the controller

For uploading the HEX file all you need to double click on the controller and select the HEX file you created.

uploading hex file to PIC controller

After uploading the HEX file don't forget to change the clock frequency from 4MHz to 20MHz.

Simulate the project

For simulating the project all you need to click the play button given on the bottom left of the proteus simulator.

Video


Popular posts from this blog

What is machine learning and it's types?

 What is machine learning(ML)? Machine learning is a subset of artificial intelligence. Machine learning enables computers or machines to make data-driven decisions rather than being explicitly programmed for a certain task. These programs or algorithms are designed in a way that they learn and improve over time when are exposed to new data. Examples:- 1. Product recommendations   While checking for a product did you noticed when it recommends a product similar to what you are looking for? or did you noticed "the person bought this product also bought this" combination of products? How are they doing this recommendation? This is machine learning. 2. Email spam and malware filtering  There are a number of spam filtering approaches that email clients use.  To ascertain that these spam filters are continuously updated they are powered by machine learning. 3. Online customer support A number of websites nowadays offer the option to chat with customer support representati...

PIC18 Timer programming in C

The PIC18 timer is divided into 4 types Timer 0 Timer 1 Timer 2 Timer 3 PIC18 timers can be used to generate a time delay or as a counter to count external event happening outside the microcontroller. In this article, we will see how to generate a time delay by programming the PIC18 timer. Timer 0 The timer 0 module has the following features Software is scalable as an 8 bit or 16-bit timer/counter. Readable and writable Dedicated 8 bit software programmable Prescaler Clock source selectable to be internal or external Edge select for external clock Register required for Timer 0 Control register Each timer has a control register called TCON to set the various timer operation modes. T0CON is an 8-bit register used for control of timer 0. TOCON TMR0ON (Timer0 on/off control bit)                                       1 = Enable timer 0                ...

Interface relay with PIC18 microcontroller

Hi... today we will talk about another important component used in the embedded domain called a relay. Relays are electric switch which uses electromagnetism to either form or breaks the existing circuits. With the help of a relay, you can trigger a high voltage operation by a low voltage input signal. Relay is a highly versatile component that is as effective in a complex circuit as in a simple circuit. In this article, we will talk about how to interface relay with PIC18 controller. We will cover the topics in the following points: About the interfacing task Software tools used Required components Circuit diagram Code for controller Upload the HEX file Run simulation About the interfacing task In the interface relay task, with the help of a push-button, we will trigger the relay to control the lighting of the bulb which is of higher voltage. When we press the push button the microcontroller will trigger the relay to change its state, when the relay changes its state from normally clo...