piclist 2003\02\22\054410a >
Thread: Pin won't turn off properly
face BY : Russell McMahon email (remove spam text)


   Why you should NEVER use read-modify-write on an SX or rely on port

> I think it's fair to say that adding NOPs is "a" solution. If you look at
> the first 3 pictures here
> http://home.clear.net.nz/pages/joecolquitt/sx_pins2.html

No (alas) - it's only fair to say that adding NOPs is a "solution" 'in this
ie not in every case.

> That said, the code is working with setb and clrb instructions. If each
> pin is allowed time to rise or fall to the intended level and is not
> to interference by other port instructions then I see nothing wrong with
> using them

I know you know the following, but others with less experience may well not,
so I'll spell it out.

There are two distinct forms of this problem and the above only deals with
one of them.
The WHOLE problem is that the port read instruction reads what the actual
real world levels are and NOT what you may expect them to be as a result of
program action. When the actual and expected values are the same then there
is no problem.

The actual and expected values can differ for two reasons.

i)    As above - there is a delay in actual pin level response due to
dynamic loading (eg load capacitance etc). If you read the pin before the
capacitance has had time to charge/discharge then you may get the wrong
The solution above cures this.

ii)    There is a static load which holds the output pin permanently at one
or other level regardless of what value you write to the port. NOTE
CAREFULLY - the port may work entirely correctly as an output in such cases.
But no number of nops or intervening instructions will make it work in a
read-modify-write mode because the port will ALWAYS read as having one

A real world example should make this clearer.

Imagine an npn  transistor used to drive a lamp load with the transistor
driven by the port pin.
Transistor emitter to ground.
Transistor collector to lamp.
Other side of lamp to Vcc supply or where-ever.
Now imagine that the transistor base is driven directly by a port pin with
no drive resistor. This is very bad practice but will work.
When port pin is low the base is low and transistor is off.
Read the port pin and you will see a logic low, as you would expect.

Now drive the port pin high with eg a setb instruction.
The transistor Vbe junction will clamp at about 0.7v. The SX will try to
drive it higher but will not succeed. The SX will current limit at 20+ mA
into the base. This is a naughty thing to do but will probably not hurt the
SX or most transistors.
The SX will "think" that there "should" be a logic 1 on the pin as it has
sent a high level. The transistor will agree - it WILL see a high level and
it WILL be turned on.
BUT if you read the SX pin the 0.7v level due to the transistor base-emitter
diode clamp will (probably) mean that the pin appears as a logic low.

The answer: As noted elsewhere there is an easy solution which should be
used as the standard solution UNLESS you really really need the extra speed
and slightly fewer instructions and really really understand what you are
Use a "shadow port " instead of the port. The shadow port is simply a RAM
location that you write to INSTEAD of writing to the real port. Whenever you
need to write to the port, instead write to the shadow port and then copy
the shadow port to the real port. Input pins can be read directly from the
real port.

The shadow port adds slight extra delay and code overhead but ALWAYS works
as intended. Use it wherever possible and you won't be sorry.

SOME microprocessors (eg AVR) have ports with TWO data registers per port.
One is used to write the output data to and can also be used to read the
data that you have written. The other is used to read the actual port pin
level. This is a generally superior arrangement but is slightly more
expensive to implement. It has no real disadvantages for programming.

       Russell McMahon

http://www.piclist.com hint: The list server can filter out subtopics
(like ads or off topics) for you. See http://www.piclist.com/#topics
<026c01c2da5e$b9e57980$2301a8c0@Paradise> 7bit

See also: www.piclist.com/techref/ubicom/devices.htm?key=sx
Reply You must be a member of the piclist mailing list (not only a www.piclist.com member) to post to the piclist. This form requires JavaScript and a browser/email client that can handle form mailto: posts.
Subject (change) Pin won't turn off properly

month overview.

new search...