decimal input on portB


Closed Thread
Results 1 to 9 of 9
  1. #1
    Join Date
    Apr 2015
    Posts
    4

    Question decimal input on portB

    Hello from french wine town !

    I'm newbie : it's first post here and I work on my second program

    Thanks to Dogan's books and this forum to show what can be done with pbp.

    Today I would like to divide portB inputs in two groups of 4 bits and get the two numbers in order to use them in branch loop.

    Help would be appreciated.

    have a nice day

  2. #2
    Join Date
    Mar 2003
    Location
    Commerce Michigan USA
    Posts
    1,166


    Did you find this post helpful? Yes | No

    Default Re: decimal input on portB

    Sancerre, I would:
    1. read the port of choice into a single BYTE size variable.
    2. AND port read variable with $F0 and place into separate variable to separate the upper nibble. (4 bits)
    3. AND port read variable with $0F and place into separate variable to separate the lowerr nibble. (4 bits)
    There you have it. I am assuming you are talking about BCD when you are talking about decimal?
    Dave Purola,
    N8NTA
    EN82fn

  3. #3
    Join Date
    Jan 2013
    Location
    Texas USA
    Posts
    229


    Did you find this post helpful? Yes | No

    Default Re: decimal input on portB

    One way to accomplish this is this way.
    This will copy the upper 4 bits of the value of PORTB into a new byte variable in the lower 4 bit positions and will copy the lower 4 bits of the value of PORTB into another byte variable in the lower 4 bit positions.

    Code:
    myPortB var byte
    HighNib var byte
    LowNib  var byte
    
    
    myPortB = PortB
    HighNib = myPortB >> 4   'Right shift myPortB 4 bits, (moves bits 7:4 to bits 3:0 and then set bits 7:4 to 0)
    LowNib  = myPortB & %00001111   'Mask off bits 7:4 to 0, Only get bits 3:0
    
    So if PortB = %10010110 '$96
    Then 
    HighNib = %00001001 '$09
    LowNib  = %00000110 '$06
    Last edited by Tabsoft; - 7th April 2015 at 18:49. Reason: Clarification
    Regards,
    TABSoft

  4. #4
    Join Date
    Apr 2015
    Posts
    4


    Did you find this post helpful? Yes | No

    Question Re: decimal input on portB

    Hi all,

    Decimal or Hex ? It's depend of BRANCH syntax. Can I write something like
    BRANCH PORTB [dog, cat,fish] ???


    For nibbles, I can choose between
    initial:
    TRISB = 111111
    SWVAR VAR BYTE
    SWVAR1 VAR BYTE
    SWVAR2 VAR BYTE

    code1:
    SWVAR = PORTB
    SWVAR1 = SWVAR & $0F
    SWVAR2 = SWVAR & $F0

    or code2:
    SWVAR1 = SWVAR >> 4 'Right shift myPortB 4 bits, (moves bits 7:4 to bits 3:0 and then set bits 7:4 to 0)
    SWVAR2 = SWVAR & %00001111 'Mask off bits 7:4 to 0, Only get bits 3:0

    switches will be active when pulling down... Do I change something in my code ?

    Thanks for your time. Have a nice day. Oleg
    Last edited by Sancerre; - 8th April 2015 at 07:54.

  5. #5
    Join Date
    Jan 2013
    Location
    Texas USA
    Posts
    229


    Did you find this post helpful? Yes | No

    Default Re: decimal input on portB

    What version of PBP are you using?
    Regards,
    TABSoft

  6. #6
    Join Date
    Apr 2015
    Posts
    4


    Did you find this post helpful? Yes | No

    Default Re: decimal input on portB

    Bonjour

    Quote Originally Posted by Tabsoft View Post
    What version of PBP are you using?
    PBP 2,5.

    Have a nice day.

  7. #7
    Join Date
    Jan 2013
    Location
    Texas USA
    Posts
    229


    Did you find this post helpful? Yes | No

    Default Re: decimal input on portB

    Yes, you could use the BRANCH command as you have described, "BRANCH PORTB [dog, cat,fish]".
    That being said, I don't think I would use it that way.

    The BRANCH command has the following syntax, "BRANCH Index,[Label{,Label...}]"

    One issue in using the BRANCH command this way is that using a byte variable or Port Alias for the "Index" means you can have 256 possible values (0-255).
    Which might require you to create 256 Labels and add them to the Label list in the BRANCH command.
    The "Index" is just a number pointing to the list of "Labels" in the BRANCH command. E.g. BRANCH Index, [Label_0, Label_1, Label_2, ...]
    If Index = 0 then the program will Branch to Label_0, if Index = 1 then the program will Branch to Label_1, etc.

    If you are using the value of PORTB, there can be up to 256 different values which would make for one very long Branch command.
    "BRANCH Index,[Label_0, Label_1,... Label_255]"

    If it were me I would do something like this.

    Code:
    myPortB var byte
    
    myPortB = PORTB
    
    if myPortB = 1 then GOTO Label_1
    if myPortB = 2 then GOTO Label_2
    
    etc.
    
    Label_1:
        'Do something
        
    Label_2:
        'Do something
    This gives you the power to pick and choose which values of PORTB you want to act on.

    You could also replace the GOTO portion above with GOSUB, which will jump to the Label_x, then at the end of your subroutine (Label_x) the "Return"
    command will cause the program to return back to the next instruction after the GOSUB.

    Code:
     
    if myPortB = 1 then GOSUB Label1
    if myPortB = 2 then GOSUB Label2
    etc.
    
    Label_1:
        'Do something
        return
        
    Label_2:
        'Do something
        return
    You could similarly use the Select Case command.
    Regards,
    TABSoft

  8. #8
    Join Date
    Aug 2003
    Posts
    985


    Did you find this post helpful? Yes | No

    Default Re: decimal input on portB

    This wouldn’t be needed if shift shifts in zero values which I think it does:
    Code:
    SWVAR2 = SWVAR & %00001111 'Mask off bits 7:4 to 0, Only get bits 3:0

  9. #9
    Join Date
    Apr 2015
    Posts
    4


    Did you find this post helpful? Yes | No

    Default Re: decimal input on portB

    Hi all,

    thanks for your valuable help. Following your advices, my code can look like :

    TRISB = %11111111
    TRISA = %00000
    SWVAR VAR BYTE
    SWVAR1 VAR BYTE
    SWVAR2 VAR BYTE

    SWVAR = PORTB
    SWVAR1 = SWVAR >> 4 'Right shift myPortB 4 bits, (moves bits 7:4 to bits 3:0 and then set bits 7:4 to 0)
    SWVAR2 = SWVAR & %00001111 'Mask off bits 7:4 to 0, Only get bits 3:0

    IF SWVAR1 <>0 THEN GOTO SUITE

    if SWVAR2 = 1 then GOTO sound1
    if SWVAR2 = 2 then GOTO sound2
    if SWVAR2 = 3 then GOTO sound3
    ...
    ENDIF

    SUITE:
    if SWVAR1 = 1 then GOTO sound1
    if SWVAR1 = 2 then GOTO sound2
    if SWVAR1 = 3 then GOTO sound3
    ...

    sound1:
    FREQOUT PORTA.0,4000,500

    sound2:
    FREQOUT PORTA.0,4000,1000

    sound3:
    FREQOUT PORTA.0,4000,1500

    sound4:
    FREQOUT PORTA.0,4000,2000

    Have a nice day.
    Oleg

Similar Threads

  1. Decimal variable input with HSERIN command
    By pxidr84 in forum mel PIC BASIC Pro
    Replies: 8
    Last Post: - 28th April 2013, 14:14
  2. PORTB.3 Input not working as expected.
    By BobEdge in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 5th March 2013, 09:58
  3. Replies: 6
    Last Post: - 12th March 2011, 13:11
  4. portb input problems
    By lockjawz in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 15th February 2011, 15:18
  5. Decimal value
    By leonel in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 7th April 2005, 16:39

Members who have read this thread : 1

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