Maker Pro
Maker Pro

PIC16F88 swaping bit status in PORTB

I

ilmvmastm

Hello every one!
I have a microcontroler PIC16F88. I want to make some simple
applications with it, but for some applications I need to swap the
value of one bit in one of two ports (PORTA or PORTB).
For example:
If PORTB bit 3 is set I want to reset it. And if PORTB bit 3 is reset
I want to set it.
In main idea i want to make logical NOT for bit 3 or antoher bit in
ports.
In programing language C this thing is :
PORTB,3 != PORTB,3
but I can't inplement this in my assembler program.

If someone understand my idea can you help me for this?
Thank you for your attention!
 
P

Paul E. Schoen

ilmvmastm said:
Hello every one!
I have a microcontroler PIC16F88. I want to make some simple
applications with it, but for some applications I need to swap the
value of one bit in one of two ports (PORTA or PORTB).
For example:
If PORTB bit 3 is set I want to reset it. And if PORTB bit 3 is reset
I want to set it.
In main idea i want to make logical NOT for bit 3 or antoher bit in
ports.
In programing language C this thing is :
PORTB,3 != PORTB,3
but I can't inplement this in my assembler program.

If someone understand my idea can you help me for this?
Thank you for your attention!

The way to toggle a bit is by using the XOR instruction. In your case, use:

MOVLW b'00001000' ;Set bit 3 high
XORWF PORTB, F ;Toggle bit 3 of PORTB

You might need to be careful of the read/modify/write problem that can
cause unexpected results under some conditions. You need to make sure the
port output is actually what you expect it to be, so if there is a heavy
capacitive load, or if the port is open drain, you might be safer using a
shadow file register for operations and writing the results to the port, as
follows:

BDATA RES 1 ;Reserve 1 byte for PORTB data

CLRF BDATA ;Initialize
MOVF BDATA, W
MOVWF PORTB ;Write the intended data to the port

MOVF BDATA, W ;Move BDATA to W register
XORLW b'00001000' ;Toggle bit 3 of BDATA
MOVWF PORTB ;Write the intended data to the port

If you have some pins of PORTB as inputs, you will need to read them and
then adjust the BDATA register accordingly. This can be done by masking
appropriate bits and using logical AND/OR operations to set or clear them.

Paul
 
B

Bob Eld

ilmvmastm said:
Hello every one!
I have a microcontroler PIC16F88. I want to make some simple
applications with it, but for some applications I need to swap the
value of one bit in one of two ports (PORTA or PORTB).
For example:
If PORTB bit 3 is set I want to reset it. And if PORTB bit 3 is reset
I want to set it.
In main idea i want to make logical NOT for bit 3 or antoher bit in
ports.
In programing language C this thing is :
PORTB,3 != PORTB,3
but I can't inplement this in my assembler program.

If someone understand my idea can you help me for this?
Thank you for your attention!

Paul's XOR method is probably the best but here is another way using bit
manipulation. It does take five steps though.

BTFSC PORTB,3 ;if port b 3 is "1"
GOTO $+3 ;jump forward 3 steps
BSF PORTB,3 ;else if "0", set it
GOTO $+2 ;and jump out
BCF PORTB,3 ;if 3rd step, clear it
 
Top