Bit twiddling the port config registers...

Jim G.Jim G. Posts: 27
As I continue using the 1-Wire stuff I am trying to develop a more
general purpose library of functions to include in a project.· This means that
I need to be able to turn "tristate" on & off for a single bit without knowing·how the
other·ports bits are configured.

I have not seen a way to read the port config registers, or set/clear bits on them...

In the past I have kept a global "image" variable for the config registers, tweak that
value, then write it to the config register, but I would prefer to avoid the global image thing.
One thing that would make this possible is to create a library of port interface routines that would
keep a "private" image that only they would use.··Any part of the program that needs to mess
with the ports·would have to use the library routines...

What I may end up doing is keeping one 1-Wire bit set as an output, and simply wiring
a reverse-bias diode in series with the port pin to prevent it from driving the bus high,
and reading the 1-Wire bus with a separate port bit that is always set as an input.
However I would then have a 2-wire system, not 1.

Any ideas will be appreciated!



  • 3 Comments sorted by Date Added Votes
  • Paul BakerPaul Baker Posts: 6,351
    edited February 2005 Vote Up0Vote Down
    Ok heres some tricks I use:

    ·OR is a universal "set", set a register to $00 then setting the bits which I wish to set, then ORing it with·the destination·register that I wish to set bits in.

    AND is a universal "clear", set a register to $FF then clearing the bits which I want to clear, then ANDing it with the destination·register that I want to clear bits in.

    XOR is a universal "toggle", set a register to $00 the setting bits which I want to change thier state to thier opposite value, then XORing it with the destination register I want to toggle bits in.

    Using these methods, only those bits which you want to change will be affected.

    The problem is the SX28 and lower do not provide a method of reading the port configuration·registers (as you have already surmised), so your stuck with keeping some for of readable copy of the registers (AND, OR and XOR require reading the destination register) in the SX48/52 modes $0C-$0F provide access to the values in those registers, but this requires coping them out, doing the above function then copying them back in using mode $1C-$1F.

    So given that this advice likely will not aid your problem,
    I looked at the secret instructions to see if any of them would help, but it doesn't appear any will.

  • James NewtonJames Newton Posts: 329
    edited February 2005 Vote Up0Vote Down
    Watch out using OR, AND, XOR, bit set / clear, etc... see:·for the dreaded RMW trap.

    The "global image variable" Jim used is the preferred method and is often called a "port shadow" register.

    The idea of a port access library is great! It still requires a shadow register, but keeping it in a bank is valuable.

    The problem with both of these "safe" approaches is that they take cycles. A global shadow takes twice the time to set a bit, for example, compared to a simple SETB RA.1. A port library will require MANY cycles for the same thing.

    One thing you can do is carefully arrange the pins per thier use. For example, if you have one pin that you need to very quickly set and clear, you can try to place that pin on a port where the other pins are used only for input. This avoids RMW problems with the other pins and as long as you don't use XOR to toggle that pin, you should be ok.

    James Newton, Host of
    [][/url] 1-619-652-0593 fax:1-208-279-8767
    SX FAQ / Code / Tutorials / Documentation: Pick faster!

Sign In or Register to comment.