Assign 2 variables for 1 port..


Closed Thread
Results 1 to 5 of 5
  1. #1
    oldtoddler's Avatar
    oldtoddler Guest

    Smile Assign 2 variables for 1 port..

    This might be a fag or at least a newby question.. o:-)
    How do I divide one port into "2 variables" ?
    It works fine if I need all 8 bits, but I only need 3 or 4 and I'd hate to waste any port pins..

    This is what I'm after:

    BUSD VAR PORTD
    A.0 VAR BUSD.0 (A has been defined as BYTE)
    A.1 VAR BUSD.1
    A.2 VAR BUSD.2
    A.3 VAR BUSD.3
    B.0 VAR BUSD.4 (B has been defined as BYTE)
    B.1 VAR BUSD.5
    B.2 VAR BUSD.6
    B.3 VAR BUSD.7
    ...
    LET A=10 (sets PortD pins 0..3 into %1010 = "10")
    LET B=12 (sets PortD pins 4..7 into %1100 = "12")

    any ideas?
    LET BUSD = 202 (%11001010) works fine but it ain't practical way to do it..

    thnx adv..

    /MRa
    Last edited by oldtoddler; - 26th February 2006 at 08:29.

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


    Did you find this post helpful? Yes | No

    Default

    oldtoddler, This is how I would approch it:

    BUSD VAR PORTD
    A VAR BYTE
    B VAR BYTE

    LET A=10 (sets A bits 0..3 into %1010 = "10")
    LET B=12 (sets B bits 0..3 into %1100 = "12")

    BUSD = (B << 4) + A

    Dave Purola,
    N8NTA

  3. #3
    oldtoddler's Avatar
    oldtoddler Guest


    Did you find this post helpful? Yes | No

    Thumbs up

    Quote Originally Posted by Dave
    oldtoddler, This is how I would approch it:

    BUSD = (B << 4) + A

    Dave Purola,
    N8NTA
    Thnx a lot Dave, my binary operations "department" needs little training... o:-)

    Excellent, this will work fine for me.
    For curiosity, what will happen if A>%1111 - does it have any effect ? (+ operand)

    /MRa

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


    Did you find this post helpful? Yes | No

    Default

    oldtoddler, You should mask variable A for $1111 as adding it to B would be incorrect as far as binary. You should use something like A=A & %00001111 or A=A MIN 15. These statements will limit variable A to a maximum value of 15 or binary %00001111. You could just add the mask as such:
    BUSD = (B << 4) + (A & 15)

    Dave Purola,
    N8NTA

  5. #5
    oldtoddler's Avatar
    oldtoddler Guest


    Did you find this post helpful? Yes | No

    Thumbs up

    Quote Originally Posted by Dave
    You could just add the mask as such:

    BUSD = (B << 4) + (A & 15)

    N8NTA
    Neat! Getting better all the time..
    Thnx Dave, I'm getting the idea..

    --
    a = (b>c) ? (d=e,d+f) : (g=h,g+i);

Similar Threads

  1. Variables and port pins
    By PeterReed in forum mel PIC BASIC
    Replies: 2
    Last Post: - 22nd September 2009, 14:01
  2. cant get my Lcd to work using Pic18f452
    By civicgundam in forum General
    Replies: 13
    Last Post: - 7th October 2007, 00:13
  3. Help with sound command in 2 programs
    By hyperboarder in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 5th July 2007, 20:36
  4. Microcontroller with 2 way paging application problem
    By oneohthree in forum mel PIC BASIC Pro
    Replies: 30
    Last Post: - 20th April 2007, 17:27
  5. pins in arrays?
    By scorpion in forum mel PIC BASIC Pro
    Replies: 16
    Last Post: - 17th September 2005, 08:18

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