Setting 2 ports in one step?

General discussion relating to the library modules supplied with the compiler

Moderators: David Barker, Jerry Messina

Post Reply
User avatar
JWinters
Posts: 106
Joined: Mon Feb 04, 2008 4:56 pm
Location: North Carolina, USA
Contact:

Setting 2 ports in one step?

Post by JWinters » Fri Jul 03, 2009 3:26 am

I'm interfacing PORTA and PORTD to a 16-bit wide address bus. Currently I'm setting the address bits with the following:

Code: Select all

Sub SetAddress(myAddress as Word)
    PortA = Address.Byte0
    PortD = Address.Byte1
End Sub
Is there a more efficient way to do this? With of all the structure, union and alias abilities of this compiler, I'm sure there is a way to do it all in one step.

It would be nice to have 2-byte wide object that set all the bits when I assign it a new value. I can't figure out how to do it though.

Jerry Messina
Swordfish Developer
Posts: 1470
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Post by Jerry Messina » Fri Jul 03, 2009 12:37 pm

Jason,

I don't think there's too much you can do to improve on this. The problem with trying to use a structure/union is that the PORTA and PORTD addresses aren't contiguous (although some people on this forum have come up with some pretty ingenious code)

If you're looking to reduce execution time, you could always just declare the sub inline. If you're willing to use two bytes in the call, you could reduce this even more, but that's getting you further away from your goal of using a word-sized object.

Code: Select all

dim w as word

// standard version
sub setaddr(addr as word)
    portA = addr.byte0
    portD = addr.byte1
end sub

// standard version inlined
inline sub setaddr_il(addr as word)
    portA = addr.byte0
    portD = addr.byte1
end sub

// faster version using two bytes
inline sub set_addr(addrh as portD, addrl as portA)
end sub

// overloaded two byte version
inline sub set_addr(addr as word)
    set_addr(addr.byte1, addr.byte0)
end sub

w = $1234

// standard
setaddr($1234)
setaddr(w)

// standard inline
setaddr_il($1234)
setaddr_il(w)

// faster two byte version
set_addr($12, $34)
set_addr(w>>8, w)
set_addr(w.byte1, w.byte0)

// overloaded two byte version (same as standard inline)
set_addr($1234)
set_addr(w)


Post Reply