Truncated match.
PICList
Thread
'inverting a single bit in C'
1999\04\14@105156
by
David Wong
If I want to invert a single bit in C how do I do that. For example I want
to invert RA3.
I tried
RA3 = !RA3;
But that doesn't seem to work.
What am I doing wrong.
DW
1999\04\14@111746
by
Philip Restuccia
David Wong wrote:
>
> If I want to invert a single bit in C how do I do that. For example I want
> to invert RA3.
>
> I tried
>
> RA3 = !RA3;
Try:
RA3 ^= 0x04;
or
RA3 = RA3 ^ 0x04;
-- Phil
--
Philip Restuccia
Senior Principal Software Engineer
Periphonics Corporation
spam_OUTphilip.restucciaTakeThisOuT
peri.com
**********************************************************************
This email and any files transmitted with it are confidential and
intended solely for the use of the individual or entity to whom they
are addressed. If you have received this email in error please notify
systems administration at .....postmasterKILLspam
@spam@peri.com.
This footnote also confirms that this email message has been scanned for
the presence of commonly detectable virus-types.
**********************************************************************
1999\04\14@112519
by
Andy Kunz
At 10:48 AM 4/14/99 -0400, you wrote:
>If I want to invert a single bit in C how do I do that. For example I want
>to invert RA3.
>
>I tried
>
>RA3 = !RA3;
I would do it this way:
#define bit3 0b00001000
PORTA = PORTA ^ bit3;
The "right" way would be to use a shadow register for the update, just
writing the changed value to the port:
UINT8 shadow_RA; // Always use this to write to the port
shadow_RA ^= bit3;
PORTA = shadow_RA;
Andy
==================================================================
Montana Design - http://www.montanadesign.com - Electronics & Model Boats
==================================================================
1999\04\14@215305
by
Byron A Jeff
>
> If I want to invert a single bit in C how do I do that. For example I want
> to invert RA3.
>
> I tried
>
> RA3 = !RA3;
>
> But that doesn't seem to work.
Bit selection isn't a standard C operation. As shown in other posts you
use the XOR operator on the whole register.
I'm in the processing of developing yet another HLL for microcontrollers
named NPCI. I addressed the issue by adding a bit selection operator.
In NPCI this is done by:
porta:3 = !porta:3;
Which is very similar to what you thought it should originally be.
Unfortunately NPCI isn't in a usable state right now because of a design
flaw for local variables in parameters. Hope to fix in the next few months.
Just a comment.
BAJ
1999\04\14@232521
by
Peter Homann
I'm using the CCS compiler and it works for me. Below is a fragment of code
for inverting the bit driving a piezo buzzer. The piezo is driven from pin
RA1 of a 16F84.
0000 00083 .................... #define PIEZO
a_port.a1 // Piezo beeper pin
0000 01005 .................... PIEZO = !(PIEZO);
// Toggle the output pin.
012E 3002 01006 MOVLW 02
012F 0685 01007 XORWF 05,F
Regards,
Peter.
---
Peter Homann Email: peterh
KILLspamadacel.com.au
Adacel Technologies Ltd _/_/_/ _/_/_/ _/ Phone: +61 3 9596 2991
250 Bay St, Brighton _/ _/ _/ _/ Fax: +61 3 9596 2960
Victoria 3186 _/_/_/ _/ _/ Home: +61 3 9555 5603
AUSTRALIA _/ _/ _/ _/_/_/Mobile: 0414-494578
------------------------------------------------------------------------
|{Original Message removed}
1999\04\16@141712
by
Brian Scearce
On Wed, 14 Apr 1999 13:33:54 -0400 Ralph Stickley <.....rstickleyKILLspam
.....DATALUX.COM>
shared some of Paul Britton's code for turning a bit number (0-7)
into a bit mask (1, 2, 4, 8, 16...128). It was nine instructions
long, and isosynchronous.
My code is less clever, but faster:
;----------
; TWO_POWER: two raised to the power of W
;----------
TWO_POWER:
#ifdef SAFE
ANDLW 0x7
#endif
ADDWF PCL, F
RETLW B'00000001'
RETLW B'00000010'
RETLW B'00000100'
RETLW B'00001000'
RETLW B'00010000'
RETLW B'00100000'
RETLW B'01000000'
RETLW B'10000000'
So, to invert a single bit:
MOVF bit_to_invert, W
CALL TWO_POWER
XORWF word_to_have_its_bit_inverted
Paul Britton's code is easier to inline, but in the code I've
written, I've found that the function is used often enough that I
usually want to turn it into a subroutine anyway.
Brian
1999\04\20@161358
by
John Payson
|
|If I want to invert a single bit in C how do I do that. For example I want
|to invert RA3.
|I tried
|RA3 = !RA3;
|But that doesn't seem to work.
If you have RA3 declared to be a bit variable at [5.3], then the code
you have written should work. Basically, there are four ways to invert
a bit [CCS syntax shown, but similar technique should work in all comp-
ilers]:
/* Method 1 */
#byte PORTA = 0x05
#define RA3mask = 0x08
...
PORTA ^= RA3mask;
...
/* Method 2 */
#bit RA3 = 0x05.3
...
RA3 = !RA3;
...
/* Method 3 */
#bit RA3 = 0x05.3
...
RA3 ^= 1;
...
/* Method 4*/
#bit RA3 = 0x05.3
...
if (RA3)
RA3 = 0;
else
RA3 = 1;
...
Note that methods (1) and (4) have clear unambiguous counterparts in
machine code, and the machine code version of (1) is smaller than (4).
Methods (2) and (3) will probably be evaluated the same as (1) or (4)
depending upon the compiler.
Note also that if you do something 'creative' like...
#bit RA3 = 0x05.3
#bit foo = 0x05.3
...
RA3 = !foo;
...
...you should not necessarily expect the compiler to produce 'correct'
code since it may not see the interaction between 'foo' and 'RA3', and
could rewrite that expression as
...
RA3 = 0;
if (!foo)
RA3 = 1;
...
which would indeed be the fastest possible code for a 'normal' bit ass-
ignment. Note, btw, that assigning boolean quantities to I/O ports may
cause undesired behavior if brief changes in port state 'matter'; in
some cases where the changes don't cause problems, they can even be handy
as a means of seeing [on a scope] where the bit-assignments take place.
Interesting, no?
More... (looser matching)
- Last day of these posts
- In 1999
, 2000 only
- Today
- New search...