calculator-like code entry with matrix keypad and display


Closed Thread
Results 1 to 36 of 36

Hybrid View

  1. #1


    Did you find this post helpful? Yes | No

    Default I'm all for keeping it simple !

    Joe S and Al thanks so much for the reply !!

    And both your codes snippets work like a charm !

    I love the DIG command ...very cool !

    They certainly did simplify mine somewhat :-)

    OK so here's the deal so far.
    The HSEROUT line is working well !
    Each decimal digit is displayed correctly and in the correct order!
    So a number like 123 gets sent and displayed correctly :-)

    Now what remains is what I have been on about since the beginning of this thread and that is the following.

    I need to get each keypress stored in an array and then 'JOIN' (either by adding them or some other method ) them up into one number..that's why I went off on a tangent in the last post.
    All I would like to do is be able to send one bye or word containing the actual number keyed in.
    I would like to be able to key in the number 123 (and see it on screen) and then send it as the actual number 123 decimal or binary or hex ...just not as 3 separate bytes. Consider the same thing being done with a bank of 8 dip switches ... I set the dip switches to reflect the number 123 (bin=11110111 and hex =A3) and that's what I would read in and send.
    But this seems to be rather difficult to do using a keypad not so ?

    Again any suggestions etc would be greatly appreciated

    Kind regards
    Dennis

  2. #2
    Join Date
    Jan 2009
    Location
    California, USA
    Posts
    323


    Did you find this post helpful? Yes | No

    Default

    How about something like this:


    keypressed var byte ' latest keypad entry
    mynumber var word 'your number, built up from multiple key presses

    ' each time a key is pressed, do this:

    mynumber = (mynumber * 10) + keypressed
    Maybe?


    steve
    Last edited by Byte_Butcher; - 15th December 2009 at 21:55. Reason: still learning to spell...

  3. #3


    Did you find this post helpful? Yes | No

    Default Steve...

    Tried that ... it yeilds undesirable results .. and with each keypress a different value appears in MCS serial tool window :-(
    Code:
    the numbers are 34113
    the numbers are 13500
    the numbers are 3978
    the numbers are 39830
    the numbers are 5134
    This is probably because the number is continuously multiplied each time the program loops at each keypress ... and the word value aslo can only store two bytes (16 bits)
    This is how I tried your suggestion
    Code:
    'end of variables
        
               Pause 2000       ' Wait for LCD to startup
    Start:
    
      @ READKEYPAD _myvar  'read keypress variable and place in myvar
      
      LOOKUP myvar,[0,"123A456B789C*0#D"],myvar 'use lookup table to display proper keypress
               
                   
                        
    
                       lcdout myvar
                       lcdout $fe,1
                       pause 1000
    mynumber = (mynumber * 10) + myvar 
    lcdout "my number ", dec mynumber
    hserout ["the numbers are " ,dec mynumber,$0d,$0a]
    The plan is along the right lines though!

    1.Check keypress to see if it's valid (0-9,A,B,C,D)
    2.Store each vaild keypress eg. 1,2,3 (number is 123) <<<<<<<<<<<THIS IS WHERE I HAVE A PROBLEM DON'T KNOW HOW TO WORK WITH THE ARRAY!
    3.Check for # character which denotes a valid input
    4.Take first character and (LSB) and multiply it by 1(byte 1)
    5.Take next character and multiply by 10 (byte 2)
    6.Take next character and multiply by 100 (byte 3)
    7. Now add them up (in this example 100+20+3 = 123)
    8.Check that number is valid ... <255 since it must be max one byte (8 bits)
    123 is valid since it's less than 255
    9.Display and send the number

    Those are the steps I need to go through :-)

    Hope that helps

    Kind regards
    Dennis

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


    Did you find this post helpful? Yes | No

    Default

    Dennis tray this snippet:

    Code:
    Array var Byte [3]
    A0 var byte
    B0 var Byte
    KeyPress Var Byte ' will contain the ascii character of the key pressed
    
    A0=0
    
    Ini:
    If KeyPress = "#" then
    B0= (Array[0]*100)+(Array[1]*10)+Array[2]
    HSEROUT [B0]
    A0=0
    ENDIF
    
    If A0>2 then Ini
    
    Array[A0]=KeyPress -48
    A0=A0+1
    goto Ini
    Al.
    All progress began with an idea

  5. #5


    Did you find this post helpful? Yes | No

    Default Wow !

    Al .... it's sort of working :-) YAY at least some light at last :-)

    I tried your code snippet :-) and it sort of works :-)

    You can now enter digits, each keypress shows up on the LCD , a # value denotes a send and a * denotes a clear and retry.

    Ok so when I enter 123# , the correct number is sent to the PC com port and arrives perfectly at MCS serial tool window! YAY
    In fact any 3 digit number within the range of 100 to 255 works well.
    Anything larger is obviously too large for the var byte value so shows up as the number less 256 which means it is behaving correctly since I only want a max of one byte (0 to 255).
    For example I enter 455# ,MCS serial tool shows the number as 199 (455-256) .

    So the problem now is how to get numbers which are less than 3 digits to work.
    For example if I enter the number 99 I get 227 in MCS serial tool
    Another example is the number 1 (single digit) shows up as 195

    I am trying to figure out why but can't see it as yet.

    Another thing is if you do send a number larger that 255 or something less than 3 digits , the next few times , any valid 3 digit number entered is incorrectly displayed ...still looking to slove that one!
    I added a retry: label .. which runs when you enter the * key. It clears the vars send pointer to beginning of the main loop :-)


    If you have any updates or suggestions I would gladly try them

    Please find the current (semi-working) code attached below

    Kind regards

    Dennis
    Code:
    myvar var byte 'var output from keypress
    Array var Byte [3] 'array variable to hold 3 keypresses
    A0 var byte 'counter/index
    B0 var Byte 'sum of digits
    KeyPress Var Byte 'holds each keypress as it happens
               Pause 2000       ' Wait for LCD to startup
    
      
       A0=0   'index = 0
    
    Ini:
    
    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 = "a" or "b" or "c" or "d" then goto numbers
    if keypress = "*" then goto retry
    If KeyPress = "#" then    'check input keypress for a # value
    B0= (Array[0]*100)+(Array[1]*10)+Array[2]'multiply each bit in the array by the correct column value
    'if B0 > 255 then goto overflow 'test for overflow B0>255
    HSEROUT ["here is the total ",dec B0,$0d,$0a]  'send result to pc com port 
     
    A0=0
     
    ENDIF
    
    If A0>2 then retry 'Ini
    
    Array[A0]=KeyPress -48
    A0=A0+1
    
    goto ini
    
    numbers: 'tell person to enter only numbers
    lcdout "numbers only"
    pause 1000
    A0=0
    myvar=0
    goto ini
    
    retry: 'is the * key is pressed then prompt for a retry and clear A0 and myvar
    lcdout "retry"
    pause 1000
    lcdout $fe,1
    A0=0
    myvar=0
    goto ini
    
    overflow: 'tell person to enter 0 to 255 only
    lcdout $fe,1
    lcdout "0 to 255 ONLY!"
    pause 1000
    A0=0
    myvar=0
    
    goto ini

  6. #6
    Join Date
    Nov 2005
    Location
    Bombay, India
    Posts
    970


    Did you find this post helpful? Yes | No

    Default

    Just some minor modifications to your code

    Code:
      
    myvar var byte 'var output from keypress
    Array var Byte [3] 'array variable to hold 3 keypresses
    A0 var byte 'counter/index
    B0 var Byte 'sum of digits
    KeyPress Var Byte 'holds each keypress as it happens
               Pause 2000       ' Wait for LCD to startup
    
      
       A0=0   'index = 0
    
    Ini:
    
    lcdout $fe,1
    lcdout "enter number"
    B0 = 0                             ' start with 0 in the sum of digits
    @ READKEYPAD _myvar  'read keypress variable and place in myvar
      b0 = b0*10+_myvar     ' add in the new number
    
    
      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 = "a" or "b" or "c" or "d" then goto numbers
    if keypress = "*" then goto retry
    If KeyPress = "#" then    'check input keypress for a # value
    'if B0 > 255 then goto overflow 'test for overflow B0>255
    HSEROUT ["here is the total ",dec B0,$0d,$0a]  'send result to pc com port 
     
    A0=0
     
    ENDIF
    
    If A0>2 then retry 'Ini
    
    Array[A0]=KeyPress -48
    A0=A0+1
    
    goto ini
    
    numbers: 'tell person to enter only numbers
    lcdout "numbers only"
    pause 1000
    A0=0
    myvar=0
    goto ini
    
    retry: 'is the * key is pressed then prompt for a retry and clear A0 and myvar
    lcdout "retry"
    pause 1000
    lcdout $fe,1
    A0=0
    myvar=0
    goto ini
    
    overflow: 'tell person to enter 0 to 255 only
    lcdout $fe,1
    lcdout "0 to 255 ONLY!"
    pause 1000
    A0=0
    myvar=0
    
    goto ini

  7. #7
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default

    Hi Dennis,
    You might find this thread useful, check the zip files LLoyd posted.
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

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, 21:38
  2. Keypad input test
    By Kalind in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 8th October 2008, 05:00
  3. Need help in matrix keypad coding
    By rano_zen06 in forum mel PIC BASIC Pro
    Replies: 15
    Last Post: - 24th May 2008, 14:16
  4. LCD display not working properly
    By dilpkan in forum mel PIC BASIC Pro
    Replies: 14
    Last Post: - 2nd February 2008, 08:43
  5. Keypad entry?
    By TONIGALEA in forum General
    Replies: 3
    Last Post: - 8th August 2004, 17: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