0% found this document useful (0 votes)
2 views4 pages

LCD & Keypad

The document contains the header and implementation files for an LCD and a keypad interface in C for AVR microcontrollers. The LCD functions include initialization, command sending, data writing, clearing the display, and setting the cursor position. The keypad functions handle key press detection using a 4x3 or 4x4 matrix configuration.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views4 pages

LCD & Keypad

The document contains the header and implementation files for an LCD and a keypad interface in C for AVR microcontrollers. The LCD functions include initialization, command sending, data writing, clearing the display, and setting the cursor position. The keypad functions handle key press detection using a 4x3 or 4x4 matrix configuration.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

/*

* LCD.h
*
* Created: 3/3/2016 11:46:00 AM
* Author: Ahmad-Mustafa
*/

#ifndef LCD_H_
#define LCD_H_

#define F_CPU 1000000


#include <avr/io.h>
#include <util/delay.h>

#define RS PD4
#define RW PD5
#define E PD6
#define LCD_CPRT PORTD
#define LCD_CDDR DDRD
#define LCD_DPRT PORTC
#define LCD_DDDR DDRC

#define CLEAR_COMMAND 0x01


#define TWO_LINE_LCD_Eight_BIT_MODE 0x38
#define CURSOR_OFF 0x0C
#define CURSOR_ON 0x0E

typedef unsigned char uint8;

void lcdInit(void);
void lcdCommand(uint8 command);
void lcdData(uint8 data);
void lcdClear(void);
void lcdGoToXY(uint8 row,uint8 col);

#endif /* LCD_H_ */

/*
* LCD.c
*
* Created: 3/3/2016 11:42:46 AM
* Author: Ahmad-Mustafa
*/

#include "LCD.h"

void lcdInit(void)
{
LCD_DDDR = 0xFF; //Configure the data port as output port
LCD_CDDR |= (1<<E)|(1<<RS)|(1<<RW); //Configure the control pins(E,RS,RW) as output pins

lcdCommand(TWO_LINE_LCD_Eight_BIT_MODE); //use 2-line lcd + 8-bit Data Mode + 5*8 dot display
Mode
lcdCommand(CURSOR_OFF); //cursor off
lcdClear(); //clear LCD at the beginning
}
void lcdCommand(uint8 command)
{
LCD_CPRT &= ~(1<<RS); //Instruction Mode RS=0
LCD_CPRT &= ~(1<<RW); // write command to LCD so RW=0

LCD_CPRT |= (1<<E); //Enable LCD E=1


LCD_DPRT = command; //out the required command to the data bus D0 --> D7
_delay_us(1); //delay for processing
LCD_CPRT &= ~(1<<E); //disable LCD E=0
}

void lcdData(uint8 data)


{
LCD_CPRT |= (1<<RS); //Data Mode RS=1
LCD_CPRT &= ~(1<<RW); //write data to LCD so RW=0

LCD_CPRT |= (1<<E); //Enable LCD E=1


LCD_DPRT = data; //out the required data char to the data bus D0 --> D7
_delay_us(1); //delay for processing
LCD_CPRT &= ~(1<<E); //disable LCD E=0
}

void lcdClear(void)
{
lcdCommand(CLEAR_COMMAND);
_delay_ms(100); //delay for processing
}

void lcdGoToXY(uint8 row,uint8 col)


{
uint8 address;
/* first of all calculate the required address */
switch(row)
{
case 0:
address=col+0x80;
break;
case 1:
address=col+0xC0;
break;
case 2:
address=col+0x90;
break;
case 3:
address=col+0xD0;
break;
}
lcdCommand(address);
}
/*
* Keypad.h
*
* Created: 8/3/2016 8:10:56 PM
* Author: Ahmad-Mustafa
*/

#ifndef KEYPAD_H_
#define KEYPAD_H_

#define F_CPU 1000000


#include <avr/io.h>
#include <util/delay.h>

//typedef unsigned char uint8;

#define NC 3
#define NR 4

#define KEYPAD_PORT PORTA


#define KEYPAD_PIN PINA
#define KEYPAD_DDR DDRA

unsigned char keyPress(void);

#endif /* KEYPAD_H_ */

/*
* Keypad.h
*
* Created: 8/3/2016 8:10:56 PM
* Author: Ahmad-Mustafa
*/

#include "Keypad.h"

unsigned char keyPress(void)


{
unsigned char col,row;
while(1)
{
//Higher 4 pits as output(columns) while Lower 4 pits as input (rows)
KEYPAD_DDR = (0xF0);

for(col=0; col<NC; col++) //loop for columns


{
KEYPAD_PORT |= 0xFF; //enable the internal pull up resistors for the first 4 pins
in row pins.
KEYPAD_PORT &= (~(0b00010000<<col)); //clear the column which is the output pin in
this trace.
for(row=0; row<NR; row++) //loop for rows
{
if((KEYPAD_PIN & (1<<row)) == 0) //if the switch is press in this row
{
_delay_ms(30);
if((KEYPAD_PIN & (1<<row)) == 0) //if the switch is press in this
row
{
#if (NC == 3)
unsigned char keypad_4x3[NR][NC] =
{'1','2','3','4','5','6','7','8','9','*','0','#'};
while((KEYPAD_PIN & (1<<row)) == 0);

return keypad_4x3[row][col];

#else
unsigned char keypad_4x4[NR][NC] = {'7','8','9','/',

'4','5','6','*',

'1','2','3','-',

'C','0','=','+'};
while((KEYPAD_PIN & (1<<row)) == 0);

return keypad_4x4[row][col];

#endif
}
}
}
}
}
}

You might also like