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.

Help with keypad+lcd using 16F877A PIC in Mikrobasic

Status
Not open for further replies.

heina

Junior Member level 3
Junior Member level 3
Joined
Dec 21, 2009
Messages
25
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
Philippines
Activity points
1,471
Hi guys. I've been looking for an example written in Mikrobasic using a keypad which will accept 4 numbers then display a message in LCD. But, so far I can't find one. I found some written in C but I'm not really familiar with that and I'm just starting out in Mikrobasic...So, I've been trying to find one which I can use.

Any help would be very much appreciated.
Thank you and God Bless.
 

Hi,
Code:
Code:
program calculator

dim LCD_RS as sbit at RB4_bit
    LCD_EN as sbit at RB5_bit
    LCD_D4 as sbit at RB0_bit
    LCD_D5 as sbit at RB1_bit
    LCD_D6 as sbit at RB2_bit
    LCD_D7 as sbit at RB3_bit

    LCD_RS_Direction as sbit at TRISB4_bit
    LCD_EN_Direction as sbit at TRISB5_bit
    LCD_D4_Direction as sbit at TRISB0_bit
    LCD_D5_Direction as sbit at TRISB1_bit
    LCD_D6_Direction as sbit at TRISB2_bit
    LCD_D7_Direction as sbit at TRISB3_bit

dim keypadPort as byte at PORTD

dim kp as byte
dim ky as byte
dim key as byte[20]
dim ks as string[20]
dim temp as word

sub procedure GlobInit
    TRISA = 0
    TRISB = 0
    LCD_Init
    LCD_Cmd(_LCD_CLEAR)
    LCD_Cmd(_LCD_CURSOR_OFF)
    Keypad_Init
    ADCON1 = 7
end sub

main:
     GlobInit
     while true
         for temp = 0 to 3
             kp = 0
             while (kp = 0)
                  kp = Keypad_Key_Click
             wend
             select case kp
                    case 1 ky = 7
                    case 2 ky = 4
                    case 3 ky = 1
                    case 4 ky = 10
                    case 5 ky = 8
                    case 6 ky = 5
                    case 7 ky = 2
                    case 8 ky = 0
                    case 9 ky = 9
                    case 10 ky = 6
                    case 11 ky = 3
                    case 12 ky = 11
                    case 13 ky = 12
                    case 14 ky = 13
                    case 15 ky = 14
                    case 16 ky = 15
             end select
             if (ky < 11) then
                key[temp] = ky
                ks[temp] = ky + 48
             end if
             if (ky > 10) then
                break
             end if
             LCD_Out(1,1,ks)
         next temp
         kp = 0
         while (kp = 0)
               kp = Keypad_Key_Click
         wend
     wend
end.
80_1263058475.jpg


Added after 1 minutes:

Hope it helps.
Let me know how it works out for you.
Tahmid.

Added after 28 seconds:

P.S. The compiler is mikroBASIC PRO for PIC v3.20.
 
OMG..Thank you so much for replying!! I'm just gonna try this and tell you about it soon as I'm done. <3 Thanks again.:)

Added after 36 minutes:

Hi Tahmid.. it compiled successfully and I was able to simulate it using Real PIC simulator but I'm not sure how it works. uhm, like when I clicked '1' another number would appear.. and when I clicked '5' number 6 would appear in the LCD. I'm sorry cuz I'm not yet familiar with how Keypad_Click() routine works...:-S

Uhm, I'm thinking more of a keypad lock thingie.Like, at first I would assign 4 numbers: 1234.and then the user will try to guess what my 4 numbers are, if he wasn't able to guess it right, a msg will appear in the lcd saying "wrong guess" and when he gets it right, "congratulations" will appear...

Can you help me do it because I have no idea how to do it.. :-(

A million thanks..

Heina.
 

O, I see the problem.
The keypad you used is different from that in the schematic.
My keypad has '7' as A1, '5' as B2, '0' as D2 and so on. Hope you understand how I said this.
Can you give me the picture/image of your keypad.
The keypad library is such such, pressing:
A1 returns 1
B1 returns 2
C1 returns 3
D1 returns 4
A2 returns 5
B2 returns 6
C2 returns 7
D2 returns 8
A3 returns 9
B3 returns 10
C3 returns 11
D3 returns 12
A4 returns 13
B4 returns 14
C4 returns 15
D4 returns 16
And the returned value is kp in my program. So just change the code accordingly, or just give me a schematic/picture of your keypad, ie in which order nos 1, 2, 3, etc are arranged. Hope you understood.
Tahmid.
 

    heina

    Points: 2
    Helpful Answer Positive Rating
Hi Tahmid,


Here is an image of my keypad:
kp.jpg


hmm.. the image got smaller..anyways, the numbers are arranged in this manner:


1 2 3 A
4 5 6 B
7 8 9 C
* 0 # D

I tried changing the code like this but still won't work right :


Code:
 select case kp
                    case 1 ky = 1       
                    case 2 ky = 4       
                    case 3 ky = 7       
                    case 4 ky = -6
                    case 5 ky = 2       
                    case 6 ky = 5       
                    case 7 ky = 8       
                    case 8 ky = 0       
                    case 9 ky = 3       
                    case 10 ky =6      
                    case 11 ky =9       
                    case 12 ky = -13
                    case 13 ky = 17       'A 
                    case 14 ky = 18      'B 
                    case 15 ky = 19      'C 
                    case 16 ky = 20      'D 
             end select

I don't know if there's still something that I have to configure in my simulation or I just did it wrong. I noticed that you added 48 in each of the return value if it's less than 10 :
Code:
if (ky < 11) then
                key[temp] = ky
                ks[temp] = ky + 48
             end if

So, i changed the variable type of ky, key and ks to Integer so that they can accept negative values because the return value of '*' is -6 and for '#' is -13.

Hm.. What is my problem? Whenever I press a key at the first row sometimes they all display some other number but also in that row. Example : I pressed '1'. Instead of '1', number 3 will appear.


Many Thanks..

Sincerely,

Heina
 

Hi,
I added 48 to convert it to ASCII. This only works for numbers between 0 to 9.
Now I just provided ASCII without complicating matters.
Anyway, here's the new code:
Code:
program calculator

dim LCD_RS as sbit at RB4_bit
    LCD_EN as sbit at RB5_bit
    LCD_D4 as sbit at RB0_bit
    LCD_D5 as sbit at RB1_bit
    LCD_D6 as sbit at RB2_bit
    LCD_D7 as sbit at RB3_bit

    LCD_RS_Direction as sbit at TRISB4_bit
    LCD_EN_Direction as sbit at TRISB5_bit
    LCD_D4_Direction as sbit at TRISB0_bit
    LCD_D5_Direction as sbit at TRISB1_bit
    LCD_D6_Direction as sbit at TRISB2_bit
    LCD_D7_Direction as sbit at TRISB3_bit

dim keypadPort as byte at PORTD

dim kp as byte
dim ky as char
dim key as byte[20]
dim ks as string[20]
dim temp as word

sub procedure GlobInit
    TRISA = 0
    TRISB = 0
    LCD_Init
    LCD_Cmd(_LCD_CLEAR)
    LCD_Cmd(_LCD_CURSOR_OFF)
    Keypad_Init
    ADCON1 = 7
end sub

main:
     GlobInit
     for temp = 0 to 3
         kp = 0
         while (kp = 0)
               kp = Keypad_Key_Click
         wend
         select case kp
             case 1 ky = "1"
             case 2 ky = "4"
             case 3 ky = "7"
             case 4 ky = "*"
             case 5 ky = "2"
             case 6 ky = "5"
             case 7 ky = "8"
             case 8 ky = "0"
             case 9 ky = "3"
             case 10 ky = "6"
             case 11 ky = "9"
             case 12 ky = "#"
             case 13 ky = "A"
             case 14 ky = "B"
             case 15 ky = "C"
             case 16 ky = "D"
         end select
         ks[temp] = ky
         LCD_Out(1,1,ks)
     next temp
     while true
     wend
end.
Hope you notice the difference and understand it.
And also that it came to help you.
Let me know how it works.
Tahmid.
 

Hi Tahmid!

It's almost there!:-D So I followed the old schematic and just changed the code but whenever I click a number.. for example '1'. Sometimes it displays '1' in the LCD and when I clicked it again, it now displays '2', then sometimes '3' or '4'. It displays something on the same row. o_0.

oh, and the while loop. I think you forgot to place it above the code
Code:
for temp = 0 to 3
. I noticed it only loops 4 times so I figured it should be there.

hm..I wonder why it doesn't work right on me.
It should display what I've clicked, right? regardless if I clicked it again and again?

Many Thanks.

Sincerely,

Heina
 

Hi,
I don't get your problem. The code works fine for me.
I changed the code and included the for loop so that it only accepts the input 4 times. Then the program stops and does nothing.
Do one thing, copy whatever I wrote, the whole thing, create a new project in mikroBASIC, paste my code there and then build the project using F9 key or from the project menu.
It should work fine. It works 100% ok for me.
 

Hi Tahmid,

I created a new project and copy+pasted the code. I don't know why I still get the same results as before. Whenever I click 1, 2 would appear..x_x

which pic simulator do you use? I don't know if I missed configuring something with REAL PIC Simulator and I'm still figuring out how to simulate it using Oshon's PIC Simulator ide just to try it out with another simulator... x_x.. gosh..

Trillion Thanks...

Sincerely,

Heina
 

Hi,
I had the project built on hardware earlier. I just burned the 16F877A with the PICKit2 and placed it in the circuit.
You could try using Proteus.

Added after 57 seconds:

You should try it as hardware if possible.
 

Hi Tahmid!!

great news.. After a lot of experimentation, it worked on pic simulator ide, the one that I just started using. I just had to configure it right and observe for a few moments. We'll be making the hardware soon. Thank you so much for all the help. I hope you will never get tired in helping people like me.
Until next time.

Sincerely,

Heina
 

heina said:
Hi Tahmid!!

great news.. After a lot of experimentation, it worked on pic simulator ide, the one that I just started using. I just had to configure it right and observe for a few moments. We'll be making the hardware soon. Thank you so much for all the help. I hope you will never get tired in helping people like me.
Until next time.

Sincerely,

Heina
can u share it project with us........
sorce code + hex code + circuit diagram....
HOPE...
U SHARE IT......
 

heina said:
Hi guys. I've been looking for an example written in Mikrobasic using a keypad which will accept 4 numbers then display a message in LCD. But, so far I can't find one. I found some written in C but I'm not really familiar with that and I'm just starting out in Mikrobasic...So, I've been trying to find one which I can use.

Any help would be very much appreciated.
Thank you and --- Bless.

Hey man if you are beginner use mikroc Software help you will easily learn
PIC programming readimade libraries are there in code
 

sorry, may i ask, since the proteus software is given the program. but as i run in MPlab, its all fail. how? im stucked.
pls. help me..
 

thanks pal! BUT... =(
hmm.. trying to run, but failed. i inserted the ori programs in it and run. successfully compiled. but when use it for simulation, it seems nothing.
is there any clue? i nvr use microbasic b4. mayb there is a miss there.

pls..
 

microBasic lo..
im trying to simulate the proteus sample p16, calculator.
but what happen is that, it said, the function is not define, and the file cant be open.
im lost..

have u try out b4? im stil new in MicroBasic. hope U, could help me..

pls..
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top