Truncated match.
PICList
Thread
'to invert a single bit'
1998\11\18@143426
by
David Wong
Hello,
Does anyone know of a low instruction count method of inverting a
single bit in a register. For example inverting bit 0 in the PortB
register.
Thanks
DW
1998\11\18@144500
by
Dave Johnson
>Does anyone know of a low instruction count method of inverting a
>single bit in a register. For example inverting bit 0 in the PortB
>register.
I'm making this up as I go, but what about:
movlw 0x01
xorwf portb
Does that work? Any bits except for bit 0 are preserved (if it's a 0
xored w/ 0, you get 0; if it's a 1 xored w/ 0, you get 1), and bit 0 is
toggled ( a 1 xored w/ 1 is 0, a 0 xored w/ 1 is 1). Right?
Dave Johnson
1998\11\18@170406
by
Chip Weller
|
Hi David,
>Hello,
> Does anyone know of a low instruction count method of inverting a
>single bit in a register. For example inverting bit 0 in the PortB
>register.
A word of caution, changing a bit in an I/O port is inherently different
than changing any other bit. All methods to change a bit (even using the bsf
or bcf instructions) will first read the register, modify it, and then write
it back out. When an I/O port is read you don't read what is programmed on
an output line, but what is being read on an input line. As the output
transistor supplies about 100 ohms of resistance it is possible that
external loading or noise can cause a different value to be read than is
programmed. This can cause bits other than the one you are toggling to be
changed, or the one you are attempting to toggle to not change.
To avoid this problem I map the output register values into an internal RAM
location. By modifying the internal RAM value and then writing it out this
problem is avoided. Example:
PortBMap equ 20 ; define a map for the RAM.
; code fragment to invert bit 0 of PORTB, using PortBMap.
movlw 1 ; toggle the lowest bit.
xorwf PortBMap,f ; in the map.
movf PortBMap,w ; now copy the map
movwf PORTB ; to PORTB.
Chip
1998\11\18@183639
by
Andy Kunz
>movlw 0x01
>xorwf portb
Right.
Andy
==================================================================
Andy Kunz - Statistical Research, Inc. - Westfield, New Jersey USA
==================================================================
1998\11\19@034558
by
Dr. Imre Bartfai
|
On Wed, 18 Nov 1998, Dave Johnson wrote:
> >Does anyone know of a low instruction count method of inverting a
> >single bit in a register. For example inverting bit 0 in the PortB
> >register.
> I'm making this up as I go, but what about:
>
> movlw 0x01
> xorwf portb
>
> Does that work? Any bits except for bit 0 are preserved (if it's a 0
> xored w/ 0, you get 0; if it's a 1 xored w/ 0, you get 1), and bit 0 is
> toggled ( a 1 xored w/ 1 is 0, a 0 xored w/ 1 is 1). Right?
>
> Dave Johnson
>
>
It's o.k. However, one can get problem if try to invert a port bit
directly. Why? Because of all port modifying operations (except
outputting) are read-modify-write.
If you have for instance a relay and you pull it down, you emit a bit 0
for it. Then if you xor another bit in that port, the level on the relay
pin will be read. However it will bit 1 because of a to-be-pulled down
relay is tied to Vdd. So if you emit the read bit unchanged, it will
change the previous state. The solution? I use shadow registers,
and maintain their contents.
Imre
More... (looser matching)
- Last day of these posts
- In 1998
, 1999 only
- Today
- New search...