0% found this document useful (0 votes)
4 views2 pages

Matrix Keyboard

The document is a C program for a microcontroller that initializes an LCD and a keypad. It includes functions for scanning the keypad and displaying the pressed key on the LCD. The program continuously checks for key presses and updates the LCD accordingly.

Uploaded by

tpgitspotify
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

Matrix Keyboard

The document is a C program for a microcontroller that initializes an LCD and a keypad. It includes functions for scanning the keypad and displaying the pressed key on the LCD. The program continuously checks for key presses and updates the LCD accordingly.

Uploaded by

tpgitspotify
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#pragma config FOSC = EXTRC

#pragma config WDTE = OFF


#pragma config PWRTE = OFF
#pragma config BOREN = OFF
#pragma config LVP = OFF
#pragma config CPD = OFF
#pragma config WRT = OFF
#pragma config CP = OFF

#include <xc.h>
#define _XTAL_FREQ 6000000

// Function Prototypes
void lcd_init(void);
void lcd_command(unsigned char);
void lcd_data(unsigned char);
char keypad_scan(void);

// Keypad map: row x column


const char keys[4][4] = {
{'0','1','2','3'},
{'4','5','6','7'},
{'8','9','A','B'},
{'C','D','E','F'}
};

void main(void)
{
ADCON1 = 0x06; // Make all PORTA pins digital used for Rs and Enable

// Keypad setup
TRISB = 0xF0; // RB0toRB3 as output (rows), RB4?RB7 as input (not used)
PORTB = 0x00;
TRISC = 0x0F; // RC0toRC3 as input (columns)
PORTC = 0x0F; // If using internal pullups, enable them (otherwise use
external)

// LCD setup
TRISD = 0x00; // LCD data lines D0?D7 on PORTD
TRISA = 0x00; // RA0 = EN, RA1 = RS
PORTA = 0x00;

lcd_init();

while (1)
{
char key = keypad_scan();
if (key != 0)
{
lcd_data(key);
__delay_ms(300); // Debounce delay
}
}
}

// Keypad scanning logic


char keypad_scan(void)
{
for (char row = 0; row < 4; row++)
{
PORTB = ~(1 << row) & 0x0F; // Activate one row (active LOW)
__delay_ms(100);

for (char col = 0; col < 4; col++)


{
if (!(PORTC & (1 << col))) // If column is LOW
{
while (!(PORTC & (1 << col))); // Wait until key released
return keys[row][col]; // Return the corresponding key
}
}
}
return 0; // No key press
}

// LCD Initialization
void lcd_init()
{
lcd_command(0x30);
__delay_ms(100);
lcd_command(0x30);
__delay_ms(100);
lcd_command(0x30);
__delay_ms(100);
lcd_command(0x38); // 8-bit, 2 lines, 5x7
__delay_ms(100);
lcd_command(0x0C); // Display ON, Cursor OFF
__delay_ms(100);
lcd_command(0x01); // Clear display
__delay_ms(100);
lcd_command(0x06); // Entry mode
__delay_ms(100);
}

// Send command to LCD


void lcd_command(unsigned char cmd)
{
PORTA &=0xFD; // 1111 1101 RS = 0 (command)
PORTD = cmd; // Send command
PORTA |= 0x01; //0000 0001 EN = 1
__delay_ms(100);
PORTA &= 0xFE; //1111 1110 // EN = 0
__delay_ms(100);
}

// Send data to LCD


void lcd_data(unsigned char data)
{
PORTA |=0x02; // 0000 0010 RS = 0 (command)
PORTD = data; // Send command
PORTA |= 0x01; //0000 0001 EN = 1
__delay_ms(100);
PORTA &= 0xFE; //1111 1110 // EN = 0
__delay_ms(100);
}

You might also like