a little help with keypad on 18f4520 please


Closed Thread
Results 1 to 18 of 18

Hybrid View

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


    Did you find this post helpful? Yes | No

    Default

    I want to be able to type in a number like 124 and end it with a #
    So this number (the actual number 124) can be transmitted.
    If you could imagine a cellphone or cordless landline...
    As you type in the numbers on the keypad you can see them on the screen and then you press dial or possibly C(clear or *) to backspace one digit.
    And when you are happy with the number on screen you press dial (in my case #) then your number is dialled (in my case TX'd)
    So that means that now the keypad works correctly, and when you press the wanted key you obtain the correct caracter.

    To acheive what you want, you will need an additional array and a few variables:

    The array will retain the numbers you key in and a variable should keep track of the number of time you have pressed the keys.

    Here a snippet



    Code:
    Ratain         var byte
    Ret_Char     var byte [10]
    Ret_Count   var byte
    Ret_Prnt      var byte
    Ret_Clst       var byte
    
    Ret:Prnt = "#"
    Ret_Clst = "*"
    
    Loop:
    Ret_Char[Ret_Count]=Key_Char ' here you transfer the char of the key pressed
    
    If Retain = 0 then ' Retain is a flag then if set will do your job 
    Hserout [Ret_Char[Ret_Count]]
    else
    
    If Ret_Char[Ret_Count] = Ret_Clst then ' if key = "*" then step-back counter
    Ret_Count = Ret_Count - 1
    goto Clear_Count
    endif
    
    If  Ret_Char[Ret_Count] = Ret_Prnt then ' if key = "#" then send string
    Hserout [str Ret_Char \ Ret_count-1]
    RetCount = 0
    goto Clear_Count
    endif
    Ret_Count = Ret_Count + 1
    Clear_Count:
    endif
    return
    I set the array Ret_Char to 10 elements, if you need more then adjiust it for your need. The snippet is not tested so some adjustment could be necessary.

    Al.
    Last edited by aratti; - 21st November 2009 at 20:02.
    All progress began with an idea

  2. #2


    Did you find this post helpful? Yes | No

    Default almost spot on !!

    Al

    :-)
    WOW thank you ... wasn't expecting you to code it for me :-)

    The array idea is perfect for capturing the keypresses and also the option to loop until we see a # or *

    The code snippet raises a few questions though....

    On receipt of a # it sends off a string of characters (those encountered before the #..not so ?
    For example I key in 134#
    First keypress to first variable in the array [1]
    Second to the second variable [3]
    Third to third variable [4]

    Then the # is encountered and we do an hserout

    Will we send a total of a 3 character string using hserout (in other words 24bits)

    Apologies if I have the workings of the code wrong ...I am not clued up on the workings of hserout yet but I am reading up.

    Once again any suggestions or guidance would be appreciated.

    Kind regards

    Dennis

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


    Did you find this post helpful? Yes | No

    Default

    Then the # is encountered and we do an hserout

    Will we send a total of a 3 character string using hserout (in other words 24bits)

    Hserout [str Ret_Char \ Ret_count-1], will send out a string of Ret_count-1 bytes, so "#" will not be sent

    You could use SEROUT2

    SEROUT2,pin,baud,[str Ret_Char \ Ret_count-1]

    or you could use SEROUT

    For A0 = 1 to Ret_count-1
    Serout pin,baud,[Ret_Char [A0]]
    NEXT A0



    Al.
    Last edited by aratti; - 21st November 2009 at 20:05.
    All progress began with an idea

  4. #4


    Did you find this post helpful? Yes | No

    Default Thanks again ...

    Thanks for hserout clairfying the code and the option for serout2.

    Let me re-phrase that last question
    "Then the # is encountered and we do an hserout

    Will we send a total of a 3 character string using hserout (in other words 24bits)"

    So it will be 24 bits ... 134 (0000001 11000000 01000000) as opposed to the 8 bit number 134 (10000110)

    I am after a single 8 bit number , at the end of the key capture
    (or more depending on whether the number keyed in is larger than 255- perhaps I should cap it at 255).

    So perhaps I need to do a little math in the array, perhaps (+) OR-ing ?

  5. #5


    Did you find this post helpful? Yes | No

    Default not compiling and not sure why ?

    Hi Al

    How are you ?

    I am experimenting with your code snippet .. here's what I have so far.
    Code:
    'keypad variables for array keypress capture begin here 
            Retain         var byte
            Ret_Char     var byte [10]
            Ret_Count   var byte
            Ret_Prnt      var byte
            Ret_Clst       var byte
            Ret_Prnt = "#"
            Ret_Clst = "*"
            'keypad capture code begins here
            Ret_Char[Ret_Count]=datatx ' here you transfer the char of the key pressed
    
            If Retain = 0 then ' Retain is a flag then if set will do your job 
            For A0 = 1 to Ret_count -1
            Serout portc.6,T2400,baud,[Ret_Char [A0]]
            NEXT A0
    
            lcdout datatx 'display the variable on the LCD
            pause 1000
            lcdout $fe,1          'clear lcd screen
            else
    
            If Ret_Char[Ret_Count] = Ret_Clst then ' if key = "*" then step-back counter
            Ret_Count = Ret_Count - 1
            goto Clear_Count
            endif
    
            If  Ret_Char[Ret_Count] = Ret_Prnt then ' if key = "#" then send string
            For A0 = 1 to Ret_count -1
            Serout portc.6,T2400,[Ret_Char [A0]]
            NEXT A0
    
            lcdout datatx 'display the variable on the LCD
            pause 1000
            lcdout $fe,1          'clear lcd screen
            RetCount = 0
            goto Clear_Count
            endif
            Ret_Count = Ret_Count + 1
            Clear_Count:
            endif
    goto start
    As i try to compile I encounter an error here
    For A0 = 1 to Ret_count -1

    Not quite sure why ?

    Do you think I could attain the same thing with either a case select statement or a do...while loop ?
    So if I were using a do ... while (or while wend) for example
    do
    keep allowing keypress
    While
    key_press_variable is not equal to '#" 'until we see #

    then serout

    Kind regards
    Dennis

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


    Did you find this post helpful? Yes | No

    Default

    ...... As i try to compile I encounter an error here
    For A0 = 1 to Ret_count -1
    Not quite sure why ?
    Simply because variable A0 has not been declared! Add A0 var Byte to your code.

    While wend loop could work as well, if properly structured.

    Remember you should place two limits: Ret_Count cannot be greater then the array dimension and # command should be ignored if Ret_Count = 0.

    Al.
    Last edited by aratti; - 24th November 2009 at 08:02.
    All progress began with an idea

  7. #7


    Did you find this post helpful? Yes | No

    Default haha

    Al

    I feel a little stupid for missing that obvious one :-)

    Will make the change and feedback asap :-)

    Stay tuned :-)

    And once again thanks for the reply and help :-)

    Kind regards
    Dennis

Similar Threads

  1. 4x4 keypad Help
    By aaliyah1 in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 5th October 2010, 16:34
  2. Universal Keypad System
    By aratti in forum Code Examples
    Replies: 3
    Last Post: - 18th January 2009, 13:06
  3. Keypad input test
    By Kalind in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 8th October 2008, 04:00
  4. Need help in matrix keypad coding
    By rano_zen06 in forum mel PIC BASIC Pro
    Replies: 15
    Last Post: - 24th May 2008, 13:16
  5. Inconsistent output on a 4x4 matrix keypad
    By markcadcam in forum mel PIC BASIC Pro
    Replies: 12
    Last Post: - 24th November 2006, 03:54

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