im using MPLab IDE with HiTech C Compiler
//lcd.h file------------------------------------------------------------
#ifndef __LCD
#define RS RC2
#define EN RC3
#define D4 RC4
#define D5 RC5
#define D6 RC6
#define D7 RC7
#endif
void lcd_port(char d) //function to
{
if(d & 1) //if w equal 1 (0001)
D4 = 1; //set D4 data bit to 1
else //else
D4 = 0; //set D4 data bit to 0
if(d & 2) //if w equal 2 (0010)
D5 = 1; //set D5 data bit to 1
else //else
D5 = 0; //set D5 data bit to 0
if(d & 4) //if w equal 4 (0100)
D6 = 1; //set D6 data pin to 1
else //else
D6 = 0; //set D6 data bit to 0
if(d & 8) //if w equal 8 (1000)
D7 = 1; //set D7 data pin to 1
else //else
D7 = 0; //set D7 data bit to 0
}
void lcd_cmd(char k) //able the LCD sending command
{
RS = 0; //able sending commond
lcd_port(k); //get data from lcd_port(a)
EN = 0; //enable it
__delay_ms(4); //delay
EN = 1; //disable
}
lcd_clear() //clear LCD display
{
lcd_cmd(0);
lcd_cmd(1); //Clears all display
//and returns the cursor
//to the home position
}
void SetCursor(char row, char col) //set cursor of the LCD
{
char temp,z,y; //declare temp, z, y
if(row == 1) //if row equal 1
{
temp = 0x80 + col; //0x80 add col and store to temp
z = temp>>4; //shift temp right 4 bit and store in z
y = (0x80+col) & 0x0F; // Start Cursor From First Line
lcd_cmd(z); //call command function
lcd_cmd(y); //call command function
}
else if(row == 2) //if row equal 2
{
temp = 0xC0 + col; //0x80 add col and store to temp
z = temp>>4; //shift temp right 4 bit and store in z
y = (0xC0+col) & 0x0F; // Start Cursor From Second Line
lcd_cmd(z); //call command function
lcd_cmd(y); //call command function
}
}
/* Power initialization to prepare LCD
* to read/write 8 bits as 2 4-bit nibbles */
void lcd_init() //Intilize LCD in 4-Bit Mode
{
lcd_port(0x00); //clear display
__delay_ms(20); //delay
lcd_cmd(0x03); //Special Sequence:Write Function Set.
__delay_ms(20); //delay
lcd_cmd(0x03); //Special Sequence:Write Function Set.
__delay_ms(20); //delay
lcd_cmd(0x03); //Special Sequence:Write Function Set.
//write 3 time 0x03 for the LCD change interface to 4 bit
lcd_cmd(0x02); //set to 4 bit operation
lcd_cmd(0x02); //set the function set for 4 bit
lcd_cmd(0x08); //set the function set for 4 bit
lcd_cmd(0x00); //Display On
lcd_cmd(0x0C); //Display On
lcd_cmd(0x00); //Set entry mode
lcd_cmd(0x06); //Set entry mode
}
void WriteChar(char c) //write character data to LCD
{
char temp1,y1; //declare temp and y
temp1 = c&0x0F; //Mask higher 4 bits
y1 = c&0xF0; //Mask Lower 4 Bits
RS = 1; //wan data input
lcd_port(y1>>4); //Data transfer, shift 4 bit right
EN = 0;
__delay_ms(5); //Send Enable Signal to LCD
EN = 1;
lcd_port(temp1); //data transfer to function port
EN = 0;
__delay_ms(5); //Send Enable Signal to LCD
EN = 1;
}
void WriteString(char *str) //convert string to character
{
for(int i=0; str!='\0'; i++) //loop the string last character
WriteChar(str); //call write char function
}
//keypad.h file-------------------------------------------------------------------------
#define R1 RC0
#define R2 RC1
#define R3 RC2
#define R4 RC3
#define C1 RC4
#define C2 RC5
#define C3 RC6
#define C4 RC7
#define Keypad_PORT PORTC
#define Keypad_PORT_Dir TRISC
//char KeypadEntry(void);
delay(){
for (int i=0; i<250; i++){
NOP();
}
}
void InitKeypad(void){
Keypad_PORT = 0x00; // Set Keypad port pin values zero
Keypad_PORT_Dir = 0xF0; // Last 4 pins input, First 4 pins output
// Enable weak internal pull up on input pins
}
char READ_SWITCHES(void)
{
R1 = 0; R2 = 1; R3 = 1; R4 = 1; //Test Row A
if (C1 == 0)
{ delay();
while (C1==0);
return '1';
}
if (C2 == 0) { delay(); while (C2==0); return '2'; }
if (C3 == 0) { delay(); while (C3==0); return '3'; }
if (C4 == 0) { delay(); while (C4==0); return 'A'; }
R1 = 1; R2 = 0; R3 = 1; R4 = 1; //Test Row B
if (C1 == 0) { delay(); while (C1==0); return '4'; }
if (C2 == 0) { delay(); while (C2==0); return '5'; }
if (C3 == 0) { delay(); while (C3==0); return '6'; }
if (C4 == 0) { delay(); while (C4==0); return 'B'; }
R1 = 1; R2 = 1; R3 = 0; R4 = 1; //Test Row C
if (C1 == 0) { delay(); while (C1==0); return '7'; }
if (C2 == 0) { delay(); while (C2==0); return '8'; }
if (C3 == 0) { delay(); while (C3==0); return '9'; }
if (C4 == 0) { delay(); while (C4==0); return 'C'; }
R1 = 1; R2 = 1; R3 = 1; R4 = 0; //Test Row D
if (C1 == 0) { delay(); while (C1==0); return '*'; }
if (C2 == 0) { delay(); while (C2==0); return '0'; }
if (C3 == 0) { delay(); while (C3==0); return '#'; }
if (C4 == 0) { delay(); while (C4==0); return 'D'; }
return 'n'; // Means no key has been pressed
}
char GetKey(void){
{
char key = 'n'; // Assume no key pressed
while(key=='n') // Wait untill a key is pressed
key = READ_SWITCHES(); // Scan the keys again and again
return key; //when key pressed then return its value
}
}
//main.c file---------------------------------------------------------------------------------------------------------------
#include <htc.h>
#include <pic.h>
#include "keypad.h"
__CONFIG (0x3F3A);
//just follow the datasheet of PIC16F877A and 1602 LCD to define the pin number
#define RS RC2
#define EN RC3
#define D4 RC4
#define D5 RC5
#define D6 RC6
#define D7 RC7
#define _XTAL_FREQ 8000000 //set the crystal oscillator frquency
#include "lcd.h"
void main(){
char Key = 'n'; // Variable to store pressed key value
// Initialize Keypad pins
TRISC = 0xF0; //set Port C as output
//TRISD = 0xFF;
//TRISB=0xF0;
PORTC=0;
EN = 1;
ADCON1 = 0XFF;
OPTION_REG &= 0x7F;
lcd_init(); //Intilize LCD in 4-Bit Mode
lcd_clear(); //clear LCD display
InitKeypad();
while(1)
{
Key = GetKey();
SetCursor(1,0); //set the LCD to display at first row first column
WriteString("Hello"); //display name in first line
SetCursor(2,0);
WriteString(key); ERROR!
}
}
//lcd.h file------------------------------------------------------------
#ifndef __LCD
#define RS RC2
#define EN RC3
#define D4 RC4
#define D5 RC5
#define D6 RC6
#define D7 RC7
#endif
void lcd_port(char d) //function to
{
if(d & 1) //if w equal 1 (0001)
D4 = 1; //set D4 data bit to 1
else //else
D4 = 0; //set D4 data bit to 0
if(d & 2) //if w equal 2 (0010)
D5 = 1; //set D5 data bit to 1
else //else
D5 = 0; //set D5 data bit to 0
if(d & 4) //if w equal 4 (0100)
D6 = 1; //set D6 data pin to 1
else //else
D6 = 0; //set D6 data bit to 0
if(d & 8) //if w equal 8 (1000)
D7 = 1; //set D7 data pin to 1
else //else
D7 = 0; //set D7 data bit to 0
}
void lcd_cmd(char k) //able the LCD sending command
{
RS = 0; //able sending commond
lcd_port(k); //get data from lcd_port(a)
EN = 0; //enable it
__delay_ms(4); //delay
EN = 1; //disable
}
lcd_clear() //clear LCD display
{
lcd_cmd(0);
lcd_cmd(1); //Clears all display
//and returns the cursor
//to the home position
}
void SetCursor(char row, char col) //set cursor of the LCD
{
char temp,z,y; //declare temp, z, y
if(row == 1) //if row equal 1
{
temp = 0x80 + col; //0x80 add col and store to temp
z = temp>>4; //shift temp right 4 bit and store in z
y = (0x80+col) & 0x0F; // Start Cursor From First Line
lcd_cmd(z); //call command function
lcd_cmd(y); //call command function
}
else if(row == 2) //if row equal 2
{
temp = 0xC0 + col; //0x80 add col and store to temp
z = temp>>4; //shift temp right 4 bit and store in z
y = (0xC0+col) & 0x0F; // Start Cursor From Second Line
lcd_cmd(z); //call command function
lcd_cmd(y); //call command function
}
}
/* Power initialization to prepare LCD
* to read/write 8 bits as 2 4-bit nibbles */
void lcd_init() //Intilize LCD in 4-Bit Mode
{
lcd_port(0x00); //clear display
__delay_ms(20); //delay
lcd_cmd(0x03); //Special Sequence:Write Function Set.
__delay_ms(20); //delay
lcd_cmd(0x03); //Special Sequence:Write Function Set.
__delay_ms(20); //delay
lcd_cmd(0x03); //Special Sequence:Write Function Set.
//write 3 time 0x03 for the LCD change interface to 4 bit
lcd_cmd(0x02); //set to 4 bit operation
lcd_cmd(0x02); //set the function set for 4 bit
lcd_cmd(0x08); //set the function set for 4 bit
lcd_cmd(0x00); //Display On
lcd_cmd(0x0C); //Display On
lcd_cmd(0x00); //Set entry mode
lcd_cmd(0x06); //Set entry mode
}
void WriteChar(char c) //write character data to LCD
{
char temp1,y1; //declare temp and y
temp1 = c&0x0F; //Mask higher 4 bits
y1 = c&0xF0; //Mask Lower 4 Bits
RS = 1; //wan data input
lcd_port(y1>>4); //Data transfer, shift 4 bit right
EN = 0;
__delay_ms(5); //Send Enable Signal to LCD
EN = 1;
lcd_port(temp1); //data transfer to function port
EN = 0;
__delay_ms(5); //Send Enable Signal to LCD
EN = 1;
}
void WriteString(char *str) //convert string to character
{
for(int i=0; str!='\0'; i++) //loop the string last character
WriteChar(str); //call write char function
}
//keypad.h file-------------------------------------------------------------------------
#define R1 RC0
#define R2 RC1
#define R3 RC2
#define R4 RC3
#define C1 RC4
#define C2 RC5
#define C3 RC6
#define C4 RC7
#define Keypad_PORT PORTC
#define Keypad_PORT_Dir TRISC
//char KeypadEntry(void);
delay(){
for (int i=0; i<250; i++){
NOP();
}
}
void InitKeypad(void){
Keypad_PORT = 0x00; // Set Keypad port pin values zero
Keypad_PORT_Dir = 0xF0; // Last 4 pins input, First 4 pins output
// Enable weak internal pull up on input pins
}
char READ_SWITCHES(void)
{
R1 = 0; R2 = 1; R3 = 1; R4 = 1; //Test Row A
if (C1 == 0)
{ delay();
while (C1==0);
return '1';
}
if (C2 == 0) { delay(); while (C2==0); return '2'; }
if (C3 == 0) { delay(); while (C3==0); return '3'; }
if (C4 == 0) { delay(); while (C4==0); return 'A'; }
R1 = 1; R2 = 0; R3 = 1; R4 = 1; //Test Row B
if (C1 == 0) { delay(); while (C1==0); return '4'; }
if (C2 == 0) { delay(); while (C2==0); return '5'; }
if (C3 == 0) { delay(); while (C3==0); return '6'; }
if (C4 == 0) { delay(); while (C4==0); return 'B'; }
R1 = 1; R2 = 1; R3 = 0; R4 = 1; //Test Row C
if (C1 == 0) { delay(); while (C1==0); return '7'; }
if (C2 == 0) { delay(); while (C2==0); return '8'; }
if (C3 == 0) { delay(); while (C3==0); return '9'; }
if (C4 == 0) { delay(); while (C4==0); return 'C'; }
R1 = 1; R2 = 1; R3 = 1; R4 = 0; //Test Row D
if (C1 == 0) { delay(); while (C1==0); return '*'; }
if (C2 == 0) { delay(); while (C2==0); return '0'; }
if (C3 == 0) { delay(); while (C3==0); return '#'; }
if (C4 == 0) { delay(); while (C4==0); return 'D'; }
return 'n'; // Means no key has been pressed
}
char GetKey(void){
{
char key = 'n'; // Assume no key pressed
while(key=='n') // Wait untill a key is pressed
key = READ_SWITCHES(); // Scan the keys again and again
return key; //when key pressed then return its value
}
}
//main.c file---------------------------------------------------------------------------------------------------------------
#include <htc.h>
#include <pic.h>
#include "keypad.h"
__CONFIG (0x3F3A);
//just follow the datasheet of PIC16F877A and 1602 LCD to define the pin number
#define RS RC2
#define EN RC3
#define D4 RC4
#define D5 RC5
#define D6 RC6
#define D7 RC7
#define _XTAL_FREQ 8000000 //set the crystal oscillator frquency
#include "lcd.h"
void main(){
char Key = 'n'; // Variable to store pressed key value
// Initialize Keypad pins
TRISC = 0xF0; //set Port C as output
//TRISD = 0xFF;
//TRISB=0xF0;
PORTC=0;
EN = 1;
ADCON1 = 0XFF;
OPTION_REG &= 0x7F;
lcd_init(); //Intilize LCD in 4-Bit Mode
lcd_clear(); //clear LCD display
InitKeypad();
while(1)
{
Key = GetKey();
SetCursor(1,0); //set the LCD to display at first row first column
WriteString("Hello"); //display name in first line
SetCursor(2,0);
WriteString(key); ERROR!
}
}