calculator-like code entry with matrix keypad and display


Closed Thread
Results 1 to 36 of 36

Hybrid View

  1. #1
    Join Date
    May 2008
    Location
    Italy
    Posts
    825


    Did you find this post helpful? Yes | No

    Default

    Wondered if you had disappeared or given up on me :-)
    No Dennis, I had a long hard day at work!

    Did you read also my added code?

    Al.
    All progress began with an idea

  2. #2


    Did you find this post helpful? Yes | No

    Default something strange...

    Hi Al ...

    Sorry to hear about the long hard work day :-( it was a public holiday in my country .. so it felt like a weekend :-)

    I tried the code and checked the lines in red.

    I excitedly (stupidly I gues) just copied and pasted it in .... :-)
    Well it didn't I now only ever see a 0 in hyperterminal for any number keyed in following the #

    Time to debug :-) , if you see anything obvious just let me know and I will test.. will keep you posted

    Kind regards

    Dennis

  3. #3


    Did you find this post helpful? Yes | No

    Default bugs...

    Al..
    There is some odd behaviour now ... :-(
    Here's updated code with added display options and cosmetics for testing.
    So far # must be pressed twice and 126 is displayed for any number (1,2 or 3 digits) ??
    Check clearing of var's and counter , will continue with tests, I'm willing to bet it's something really small causing the problem
    Kind regards
    Dennis

    Code:
    'keypad capture code begins here
              
    'variables begin here
            
    Array var Byte [3] 'byte array holds keypresses
    A0 var byte 'counter/index
    B0 var Byte 'working sum of bytes
    KeyPress Var Byte ' will contain the ascii character of the key pressed passed from myvar
    myvar var byte 'holds keypress byte
    digit var byte 'check the number of bytes entered
    
    start:
    A0=0 'set index to 0
    
    Ini: 'main loop
    
    lcdout $fe,1
    lcdout "enter number"
    @ READKEYPAD _myvar  'read keypress variable and place in myvar
      
      LOOKUP myvar,[0,"123A456B789C*0#D"],myvar 'use lookup table to diplay proper keypress
      keypress = myvar 'pass the variable to solve strange character problem
     ' will contain the ascii character of the key pressed
      
      lcdout keypress
      pause 1000
      lcdout $fe,1  
    
    If KeyPress = "*" then goto retry
    
    If KeyPress = "#" then 'if # conditions begin here
    
    if A0=2 then 'if counter is 2 then sum all bytes
    B0= (Array[0]*100)+(Array[1]*10)+Array[2] 'byte sum
    lcdout "3 digits ",dec B0,$0d,$0a
    pause 1000
    lcdout $fe,1
    HSEROUT ["here is the total ",dec B0,$0d,$0a]  'send result to pc com port 
    endif
    
    if A0=1 then
    B0= (Array[0]*10)+Array[1]
    lcdout "2 digits " ,dec B0,$0d,$0a
    pause 1000
    lcdout $fe,1
    HSEROUT ["here is the total ",dec B0,$0d,$0a]
    endif
    
    if A0=0 then
    B0= Array[0]
    lcdout "1 digit " ,dec B0,$0d,$0a
    pause 1000
    lcdout $fe,1
    HSEROUT ["here is the total ",dec B0,$0d,$0a]
    endif
    
    A0=0
    Array[0]=0
    Array[1]=0
    Array[2]=0
    ENDIF
    
    If A0>2 then Ini
    
    Array[A0]=KeyPress -48
    A0=A0+1
    goto Ini
    
    retry:
    lcdout "retry"
    pause 1000
    lcdout $fe,1
    A0=0
    myvar=0
    goto ini
    goto start
    end
    Last edited by Dennis; - 16th December 2009 at 20:42.

  4. #4
    Join Date
    May 2008
    Location
    Italy
    Posts
    825


    Did you find this post helpful? Yes | No

    Default

    Dennis, I think the bug was a missing goto. (see code added in red)

    Al.

    Code:
    'keypad capture code begins here
              
    'variables begin here
            
    Array var Byte [3] 'byte array holds keypresses
    A0 var byte 'counter/index
    B0 var Byte 'working sum of bytes
    KeyPress Var Byte ' will contain the ascii character of the key pressed passed from myvar
    myvar var byte 'holds keypress byte
    digit var byte 'check the number of bytes entered
    
    start:
    A0=0 'set index to 0
    
    Ini: 'main loop
    
    lcdout $fe,1
    lcdout "enter number"
    @ READKEYPAD _myvar  'read keypress variable and place in myvar
      
      LOOKUP myvar,[0,"123A456B789C*0#D"],myvar 'use lookup table to diplay proper keypress
      keypress = myvar 'pass the variable to solve strange character problem
     ' will contain the ascii character of the key pressed
      
      lcdout keypress
      pause 1000
      lcdout $fe,1  
    
    If KeyPress = "*" then goto retry
    
    If KeyPress = "#" then 'if # conditions begin here
    
    if A0=2 then 'if counter is 2 then sum all bytes
    B0= (Array[0]*100)+(Array[1]*10)+Array[2] 'byte sum
    lcdout "3 digits ",dec B0,$0d,$0a
    pause 1000
    lcdout $fe,1
    HSEROUT ["here is the total ",dec B0,$0d,$0a]  'send result to pc com port 
    endif
    
    if A0=1 then
    B0= (Array[0]*10)+Array[1]
    lcdout "2 digits " ,dec B0,$0d,$0a
    pause 1000
    lcdout $fe,1
    HSEROUT ["here is the total ",dec B0,$0d,$0a]
    endif
    
    if A0=0 then
    B0= Array[0]
    lcdout "1 digit " ,dec B0,$0d,$0a
    pause 1000
    lcdout $fe,1
    HSEROUT ["here is the total ",dec B0,$0d,$0a]
    endif
    
    A0=0
    
    GOTO Ini
    
    ENDIF
    
    If A0>2 then Ini
    
    Array[A0]=KeyPress -48
    A0=A0+1
    goto Ini
    
    retry:
    lcdout "retry"
    pause 1000
    lcdout $fe,1
    A0=0
    myvar=0
    goto ini
    goto start
    end
    Al.
    All progress began with an idea

  5. #5


    Did you find this post helpful? Yes | No

    Default i found the fix !!

    Al...

    First issue is solved :-)
    A0=A0-1 was needed :-) this was causing the # to be included in the array :-)
    So to solve that problem just step the counter back by 1 :-)
    I placed it here in the code :-)
    Code:
    If KeyPress = "#" then 'if # conditions begin here
       A0=A0-1
    That fixed incorrect numbers issue.
    So first time I power up the PIC , I enter a number (1,2 or 3 digits in size) and end with a # and whammo the correct number appears on screen :-) YAY
    The problem comes in when as you enter number two .. and three etc ...
    each displayed number is out !
    It's because the variable does not get cleared ( not sure which one yet)
    I noticed that last night (early hours of this morning :-)) and created a retry label which clears the var's and array..particularly myvar << which i think is the culprit !

    Haven't tested your goto change yet ....stand by for feedback, will do it asap :-)
    Al .. I just noticed you also removed the array clearance , why ? Is it because of the goto ?
    Kind regards

    Dennis
    Last edited by Dennis; - 16th December 2009 at 21:14. Reason: PS

  6. #6


    Did you find this post helpful? Yes | No

    Default don't GOTO :-)

    Al...

    Goto does not fix the problem
    With goto in place and your code fro your last post in place unchanged...
    after PIC is powered up, first number I enter is 1 and it displays as 10 (10X keypress)the number 20 is 140 ..weird !!
    Ok will implement my changes and sort through it now and feedback asap :-)
    Dennis

  7. #7


    Did you find this post helpful? Yes | No

    Default Do GOTO :-)

    Al ......

    You rock !! Woooohoooo :-)

    Ok GOTO was the bug !
    for clearing the var's
    A0=A0-1 is solution for strange numbers appearing after first keypress.
    this problem is caused by the # value ( and the A,B,C,D keys would cause the same issue)

    I no longer need to run the code through the retry label anymore ..yay less code! :-)

    Any further thoughts mods etc ?

    Dennis

Similar Threads

  1. a little help with keypad on 18f4520 please
    By Dennis in forum mel PIC BASIC Pro
    Replies: 17
    Last Post: - 24th November 2009, 20:38
  2. Keypad input test
    By Kalind in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 8th October 2008, 04:00
  3. Need help in matrix keypad coding
    By rano_zen06 in forum mel PIC BASIC Pro
    Replies: 15
    Last Post: - 24th May 2008, 13:16
  4. LCD display not working properly
    By dilpkan in forum mel PIC BASIC Pro
    Replies: 14
    Last Post: - 2nd February 2008, 07:43
  5. Keypad entry?
    By TONIGALEA in forum General
    Replies: 3
    Last Post: - 8th August 2004, 16:10

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts