PDA

View Full Version : Assign 2 variables for 1 port..



oldtoddler
- 26th February 2006, 08:19
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

Dave
- 26th February 2006, 13:12
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

oldtoddler
- 26th February 2006, 15:00
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

Dave
- 28th February 2006, 12:13
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

oldtoddler
- 1st March 2006, 09:20
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);