can anyone help me to find out what is the problem of the code? It is successfully built but cant show the number of keypad when i press. I'm using MPLab IDE with HiTechC compiler.
Code:
#include <htc.h>
#include <pic.h>
#include <stdio.h>
__CONFIG(0x3F72)
#define RS RD2 //LCD
#define EN RD3
#define D4 RD4
#define D5 RD5
#define D6 RD6
#define D7 RD7
#define R1 RC0 //Row1
#define R2 RC1 //Row2
#define R3 RC2 //Row3
#define R4 RC3 //Row4
#define C1 RC4 //Column1
#define C2 RC5 //Column2
#define C3 RC6 //Column3
#define C4 RC7 //Column4
#define kyp_PORT PORTC
#define kyp_PORT_Dir TRISC
#define FOSC
#define BAUD_RATE 9.6
#define BAUD_VAL (char)(FOSC/(16*BAUD_RATE))-1;
#define _XTAL_FREQ 8000000 //set the crystal oscillator frquency
#include "lcd.h"
void main(){
TRISD = 0x00; //LCD
TRISC = 0xF0; //keypad
EN = 1;
OPTION_REG &= 0x7F;
lcd_init();
lcd_clear();
void keypad_init();
char keypad_read();
char keypad_wait();
char key_get;
__delay_ms(100);
while(1){
lcd_cmd(0x00);
lcd_cmd(0x01);
__delay_ms(5);
SetCursor(1,0);
WriteString("Enter: ");
__delay_ms(5);
SetCursor(2,0);
for(int j=0;j<17;j++){
key_get = keypad_wait();
WriteChar(key_get);
}
}
}
char keypad_read(void){
R1 = 0; //Row1
R2 = 1;
R3 = 1;
R4 = 1;
__delay_us(30);
if(C1 == 0){ return '1';}
if(C2 == 0){ return '2';}
if(C3 == 0){ return '3';}
if(C4 == 0){ return 'A';}
R1 = 1; //Row2
R2 = 0;
R3 = 1;
R4 = 1;
__delay_us(30);
if(C1 == 0){ return '4';}
if(C2 == 0){ return '5';}
if(C3 == 0){ return '6';}
if(C4 == 0){ return 'B';}
R1 = 1; //Row3
R2 = 1;
R3 = 0;
R4 = 1;
__delay_us(30);
if(C1 == 0){ return '7';}
if(C2 == 0){ return '8';}
if(C3 == 0){ return '9';}
if(C4 == 0){ return 'C';}
R1 = 1; //Row4
R2 = 1;
R3 = 1;
R4 = 0;
__delay_us(30);
if(C1 == 0){ return '*';}
if(C2 == 0){ return '0';}
if(C3 == 0){ return '#';}
if(C4 == 0){ return 'D';}
return 0xFF;
}
char keypad_wait(){
char pressed_key = 0xFF;
do{
pressed_key = keypad_read();
}
while (pressed_key == 0xFF);
while (keypad_read() != 0xFF);
return pressed_key;
}
void kyp_init(){
TRISC = 0x00;
PORTC = 0xFF;
kyp_PORT = 0x00;
kyp_PORT_Dir = 0xF0;
OPTION_REG = 0x7F;
__delay_ms(30);
}