Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Keypad problem with pic18f452

Status
Not open for further replies.

LeoStar

Member level 2
Joined
Nov 19, 2014
Messages
50
Helped
0
Reputation
0
Reaction score
0
Trophy points
6
Activity points
500
Hello Everyone!

I am trying to interface a keypad with PIC18F452 and trying to make a alphanumeric keyboard with CAPSLOCK, BACKSPACE, SPACE and ENTER capabilities.
The problem is that it works well in proteus but the hardware doesnt seem to pick the pace to keep with the code.

The dimension of the keypad is 8*5 i.e. there are 40 buttons.
I am trying to work on column 1 first, which has capslock button.
The work should go on like this: On every press, an alphabet will be displayed on the 20*4 LCD and a variable should increment with it. And after every 20 presses the cursor should go to the next line but this seems to work well on proteus but not on the hardware.

Please help me. I direly have to finish this today itself. the code and the circuit diagram are attached as well.
Here is the code.
Code:
sbit LCD_RS at portb.b7;
sbit LCD_EN at portb.b6;
sbit LCD_D4 at portb.b5;
sbit LCD_D5 at portb.b4;
sbit LCD_D6 at portb.b3;
sbit LCD_D7 at portb.b2;
sbit LCD_RS_Direction at TRISb.b7;
sbit LCD_EN_Direction at TRISb.b6;
sbit LCD_D4_Direction at TRISb.b5;
sbit LCD_D5_Direction at TRISb.b4;
sbit LCD_D6_Direction at TRISb.b3;
sbit LCD_D7_Direction at TRISb.b2;
sbit S1 at portd.b0;
sbit S2 at portd.b1;
sbit S3 at portd.b2;
sbit S4 at portd.b3;
sbit S5 at portd.b4;
sbit S6 at portd.b5;
sbit S7 at portd.b6;
sbit S8 at portd.b7;
sbit C1 at portc.b0;
sbit C2 at portc.b1;
sbit C3 at portc.b2;
sbit C4 at portc.b3;
sbit C5 at portc.b4;

sbit CAPLED at porte.b0;

int aa,x,q,rr,dd;

void enter()
{
delay_ms(100);
q++;
if(q==20)
{
delay_ms(10);
x++;
if(x==1)
{
delay_ms(10);
lcd_cmd(0xC0);
}
if(x==2)
{
delay_ms(10);
lcd_cmd(0x94);
}
if(x==3)
{
delay_ms(10);
lcd_cmd(0xD4);
}
if(x==4)
{
delay_ms(10);
 Lcd_Cmd(_LCD_CLEaR);
lcd_cmd(0x80);
x=0;
}
q=0;
}
}

void col1()
{  
S1=S2=S3=S4=S5=S6=S7=S8=1;
delay_ms(160);
S1=0;
if(C1==0)
{
if(aa==1)
lcd_chr_cp('A');
else
lcd_chr_cp('a');
enter();
}
S1=1;
S2=0;
if(C1==0)
{
if(aa==1)
lcd_chr_cp('B');
else
lcd_chr_cp('b');
enter();
}
S2=1;
S3=0;
if(C1==0)
{
if(aa==1)
lcd_chr_cp('D');
else
lcd_chr_cp('d');
enter();
}
S3=1;
S4=0;
if(C1==0)
{
if(aa==1)
lcd_chr_cp('G');
else
lcd_chr_cp('g');
enter();
}
 S4=1;
S5=0;
if(C1==0)
{
if(aa==1)
lcd_chr_cp('K');
else
lcd_chr_cp('k');
enter();
}
S5=1;
S6=0;
if(C1==0)
{
delay_ms(10);
if(aa==1)
lcd_chr_cp('P');
else
lcd_chr_cp('p');
enter();
}
S6=1;
S7=0;
if(C1==0)
{
if(aa==1)
lcd_chr_cp('U');
else
lcd_chr_cp('u');
enter();
}
S7=1;
S8=0;
if(C1==0)
{
aa++;
CaPLED=1;
if(aa>1)
{
aa=0;
CaPLED=0;
}
}
}
void main()
{
ADCON1=0x06;
trisc=0x1F;
trisd=0x00;
trise=0x00;
lcd_init();
Lcd_Cmd(_LCD_CLEaR);
CaPLED=0;
delay_ms(100);
lcd_cmd(0x82);
delay_ms(100);
lcd_out_cp("INTER-ACTIVE");
delay_ms(100);
lcd_cmd(0xc2);
delay_ms(100);
lcd_out_cp("KEYBOARD");
delay_ms(100);
lcd_cmd(0x94);
delay_ms(100);
lcd_out_cp("Made by: RAJNI");
delay_ms(100);
lcd_cmd(0xd6);
delay_ms(100);
lcd_out_cp("and VARINDER");
delay_ms(100);
delay_ms(2000);
lcd_cmd(_LCD_CLEAR);
delay_ms(100);
lcd_cmd(_LCD_BLINK_CURSOR_ON);
delay_ms(110);
while(1)
{
S1=S2=S3=S4=S5=S6=S7=S8=0;
if(C1==0)
col1();
}
}
Here is the picture.


As you can see the character goes to the second line itself but on the hardware nothing like this happens. Everything gets displayed but the microcontroller fails to follow the logic in the enter() functrion.

Please help meee.

- - - Updated - - -

I started with PIC16F877A but that was not even able to make the CAPSLOCK logic to work. Then I changed the microcontroller and used PIC18F452 and the CAPSLOCK logic now work but it has not been able to realize the enter() logic.
This is the onnly hurdle that I have to pass.
Please help meee.

- - - Updated - - -

I am counting on you people. This is nothing for you guys!

- - - Updated - - -

Guys check this code out. Even this doesnt work.
I am very perplexed. Please reply soon.
Code:
// Lcd pinout settings
sbit LCD_RS at Portb.b7;
sbit LCD_EN at Portb.b6;
sbit LCD_D7 at PortB.b2;
sbit LCD_D6 at PortB.b3;
sbit LCD_D5 at PortB.b4;
sbit LCD_D4 at PortB.b5;

// Pin direction
sbit LCD_RS_Direction at TRISB.B7;
sbit LCD_EN_Direction at TRISB.B6;
sbit LCD_D7_Direction at TRISB.B2;
sbit LCD_D6_Direction at TRISB.B3;
sbit LCD_D5_Direction at TRISB.B4;
sbit LCD_D4_Direction at TRISB.B5;

int aa,x,q,rr,dd;

void main() 
{
lcd_init();
lcd_cmd(_LCD_CURSOR_OFF);
lcd_cmd(0x84);
delay_ms(100);
lcd_out_cp("Varinder");
delay_ms(100);
lcd_cmd(0xc7);
delay_ms(100);
lcd_out_cp("Kaur");
lcd_cmd(0x98);
delay_ms(100);
lcd_out_cp("RAJNI");
delay_ms(100);
lcd_cmd(0xd8);
delay_ms(100);
lcd_out_cp("KAUSHAL");
delay_ms(1000);
lcd_cmd(0x80);
lcd_cmd(0x01);
while(1)
{
lcd_chr_cp('a');
delay_ms(250);
q++;
if(q==20)
{
delay_ms(50);
x++;
if(x==1)
{
delay_ms(50);
lcd_cmd(0xC0);
}
if(x==2)
{
delay_ms(50);
lcd_cmd(0x94);
}
if(x==3)
{
delay_ms(50);
lcd_cmd(0xD4);
}
if(x==4)
{
delay_ms(50);
Lcd_Cmd(_LCD_CLEaR);
lcd_cmd(0x80);
x=0;
}
q=0;
}
}
}

- - - Updated - - -

The microcontroller doesn't seem to be able to increment the variable q and check the if statement below. It just ignores it.
Whereas on Proteus, it seems to work perfectly.
Please help me soonnnn.

- - - Updated - - -

Guys look at this code, it is not even able to perform this as well.
Please suggest me something guys. I need help. Please help me.
Code:
// Lcd pinout settings
sbit LCD_RS at Portb.b7;
sbit LCD_EN at Portb.b6;
sbit LCD_D7 at PortB.b2;
sbit LCD_D6 at PortB.b3;
sbit LCD_D5 at PortB.b4;
sbit LCD_D4 at PortB.b5;

// Pin direction
sbit LCD_RS_Direction at TRISB.B7;
sbit LCD_EN_Direction at TRISB.B6;
sbit LCD_D7_Direction at TRISB.B2;
sbit LCD_D6_Direction at TRISB.B3;
sbit LCD_D5_Direction at TRISB.B4;
sbit LCD_D4_Direction at TRISB.B5;

int aa,x,queue=0,rr,dd;

void main() 
{
lcd_init();
while(1)
{
lcd_chr_cp('a');
delay_ms(250);
aa++;
if(aa==20)
{
delay_ms(1000);
lcd_cmd(0x01);
aa=0;
}
}
}
[/quote]

- - - Updated - - -

Here is the updated post.
Now can you please help me Brother!

- - - Updated - - -

Come on guys please help me.
 

Hi,

It's really difficult to go through your code because
* it is not formatted
* it is not documented


Klaus
 

Guys look at the last code at least that should work> But no. It doesn't.
The character keeps getting displayed.
First on the first line then third line then second line and then fourth and sooo onnn.

- - - Updated - - -

Can you please tell me how to format it and document it?
I will do it at once brother.

- - - Updated - - -

I did put it in the code tags. and updated the post for you

- - - Updated - - -

I willl catch up in an hour.
 

Hi,

First on the first line then third line then second line and then fourth and sooo onnn.

This typically happens when you don't set the display position....just writing one character after the other.
Read display documentation. Especially values of cursor positions.

Klaus
 
Brother please look at the last code.
In that a variable aa is being incremented on every iteration, when it becomes 19 it should command LCD to display the character from the second line.
Why doesn't this happen? it happens on the proteus though.
Any idea?

- - - Updated - - -

Not in the last code, sorry, in the second code, look at the variable q which is incremented everytime.

- - - Updated - - -

And it doesnt even clear the display as per the third code.
It just keeps displaying the character. WHy is that happening?
Why doesnt it keep the track the value of the variable and perform the appropriate action?
Please help me.
I will be available now continuously,

- - - Updated - - -

I will repeat again. it does work perfectly in the proteus simulation, but the hardware just refuses to work as directed.

- - - Updated - - -

Now look at this code, it keeps making the LCD display the character only in the first line.
Code:
// Lcd pinout settings
sbit LCD_RS at Portb.b7;
sbit LCD_EN at Portb.b6;
sbit LCD_D7 at PortB.b2;
sbit LCD_D6 at PortB.b3;
sbit LCD_D5 at PortB.b4;
sbit LCD_D4 at PortB.b5;

// Pin direction
sbit LCD_RS_Direction at TRISB.B7;
sbit LCD_EN_Direction at TRISB.B6;
sbit LCD_D7_Direction at TRISB.B2;
sbit LCD_D6_Direction at TRISB.B3;
sbit LCD_D5_Direction at TRISB.B4;
sbit LCD_D4_Direction at TRISB.B5;

int aa=1,x=0,q=0,rr,dd;

void main() 
{
lcd_init();
lcd_cmd(0x80);
while(1)
{
lcd_chr(rr,q,'a');
delay_ms(200);
q++;
if(q==21)
{
delay_ms(50);
x++;
if(x==1)
{
rr=2;
delay_ms(50);
lcd_cmd(0xC0);
}
if(x==2)
{
rr=3;
delay_ms(50);
lcd_cmd(0x94);
}
if(x==3)
{
rr=4;
delay_ms(50);
lcd_cmd(0xD4);
}
if(x==4)
{
rr=1;
delay_ms(50);
 Lcd_Cmd(_LCD_CLEaR);
lcd_cmd(0x80);
x=0;
}
q=1;
}
}
}

- - - Updated - - -

Now I am specifying the position to the LCD with the instruction:

lcd_chr(rr,q,'a');

Should not it work now?
It completely ignores the value of rr and doesnt check the if statements with x in them>

- - - Updated - - -

Please help me guys.

- - - Updated - - -

Here is an update, the last code has started working. i can see a ray of hope now.

- - - Updated - - -

The last code works but when i put the same thing in this code it doesn't. Hve a look.
Code:
sbit LCD_RS at portb.b7;
sbit LCD_EN at portb.b6;
sbit LCD_D4 at portb.b5;
sbit LCD_D5 at portb.b4;
sbit LCD_D6 at portb.b3;
sbit LCD_D7 at portb.b2;
sbit LCD_RS_Direction at TRISb.b7;
sbit LCD_EN_Direction at TRISb.b6;
sbit LCD_D4_Direction at TRISb.b5;
sbit LCD_D5_Direction at TRISb.b4;
sbit LCD_D6_Direction at TRISb.b3;
sbit LCD_D7_Direction at TRISb.b2;
sbit S1 at portd.b0;
sbit S2 at portd.b1;
sbit S3 at portd.b2;
sbit S4 at portd.b3;
sbit S5 at portd.b4;
sbit S6 at portd.b5;
sbit S7 at portd.b6;
sbit S8 at portd.b7;
sbit C5 at portc.b0;
sbit C4 at portc.b1;
sbit C3 at portc.b2;
sbit C2 at portc.b3;
sbit C1 at portc.b4;

sbit CAPLED at porte.b0;

int aa,x,q=0,rr=0,dd;
void enter();
void col1();

void enter()
{
delay_ms(200);
q++;
if(q==21)
{
delay_ms(50);
x++;
if(x==1)
{
rr=2;
delay_ms(50);
lcd_cmd(0xC0);
}
if(x==2)
{
rr=3;
delay_ms(50);
lcd_cmd(0x94);
}
if(x==3)
{
rr=4;
delay_ms(50);
lcd_cmd(0xD4);
}
if(x==4)
{
rr=1;
delay_ms(50);
 Lcd_Cmd(_LCD_CLEaR);
lcd_cmd(0x80);
x=0;
}
q=1;
}
}

void col1()
{  
S1=S2=S3=S4=S5=S6=S7=S8=1;
delay_ms(160);
S1=0;
if(C1==0)
{
if(aa==1)
lcd_chr(rr,q,'A');
else
lcd_chr(rr,q,'a');
enter();
}
S1=1;
S2=0;
if(C1==0)
{
if(aa==1)
lcd_chr(rr,q,'B');
else
lcd_chr(rr,q,'b');
enter();
}
S2=1;
S3=0;
if(C1==0)
{
if(aa==1)
lcd_chr(rr,q,'D');
else
lcd_chr(rr,q,'d');
enter();
}
S3=1;
S4=0;
if(C1==0)
{
if(aa==1)
lcd_chr(rr,q,'G');
else
lcd_chr(rr,q,'g');
enter();
}
 S4=1;
S5=0;
if(C1==0)
{
if(aa==1)
lcd_chr(rr,q,'K');
else
lcd_chr(rr,q,'k');
enter();
}
S5=1;
S6=0;
if(C1==0)
{
delay_ms(10);
if(aa==1)
lcd_chr(rr,q,'P');
else
lcd_chr(rr,q,'p');
enter();
}
S6=1;
S7=0;
if(C1==0)
{
if(aa==1)
lcd_chr(rr,q,'U');
else
lcd_chr(rr,q,'u');
enter();
}
S7=1;
S8=0;
if(C1==0)
{
aa++;
CaPLED=1;
if(aa>1)
{
aa=0;
CaPLED=0;
}
}
}
void main()
{
ADCON1=0x06;
trisc=0x1F;
trisd=0x00;
trise=0x00;
lcd_init();
Lcd_Cmd(_LCD_CLEaR);
CaPLED=0;
delay_ms(100);
lcd_cmd(0x82);
delay_ms(100);
lcd_out_cp("INTER-ACTIVE");
delay_ms(100);
lcd_cmd(0xc2);
delay_ms(100);
lcd_out_cp("KEYBOARD");
delay_ms(100);
lcd_cmd(0x94);
delay_ms(100);
lcd_out_cp("Made by: RAJNI");
delay_ms(100);
lcd_cmd(0xd6);
delay_ms(100);
lcd_out_cp("and VARINDER");
delay_ms(100);
delay_ms(2000);
lcd_cmd(_LCD_CLEAR);
delay_ms(100);
lcd_cmd(_LCD_BLINK_CURSOR_ON);
delay_ms(110);
while(1)
{
S1=S2=S3=S4=S5=S6=S7=S8=0;
if(C1==0)
col1();
}
}

- - - Updated - - -

A brief update, S1 S2, .... S8 are the rows and the C1, is the column.
Please help me , I am almost there brother.

- - - Updated - - -

Guys it keeps displaying the character in the first line only on every btton press, the position in the line gets incremented but not the line itself.
I know its quite out of the box here but the problem is minor not big.
Please help me.
 

I don't recognize language you use, but to set column value you should use Lat not Port, and then read Port
 

Brother, its pic microcontroller and I am programming using MikroC. I guess you are not familiar with it.

- - - Updated - - -

Dont bother about it anymore. I am marking this thread as solved. Thank you all for your precious time.
 

My program is ready.

I just need one help. I am uploading the C file here. I want to get hex file from it.
I have compiled it using MIKROC PRO PIC v5.61 and it gives me errors while simulating it. The error that comes is
ATTEMPT TO EXECUTE INVALID OPCODE.

I think its due to the hex file.

I am uploading the C file, please give the hex file from it.
Thanks in advance.

View attachment CODE.rar

- - - Updated - - -

This is the last thing that i need.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top