Hobby Electronics Basics PIC16F88 swaping bit status in PORTB

Bookmark this page:  YahooMyWeb Yahoo!  Google Google  Windows Live Favorites Windows Live  del.icio.us del.icio.us  digg digg  Add to Netscape Netscape
Subject Author Date
PIC16F88 swaping bit status in PORTB ilmvmastm 08-26-08
Posted by ilmvmastm on August 26, 2008, 2:22 pm
Please log in for more thread options


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!

Posted by Tim Wescott on August 26, 2008, 3:18 pm
Please log in for more thread options


ilmvmastm wrote:
> 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

That's absolutely positively not C. It doesn't even match the
bastardized versions that I've seen implemented for small processors.

_Assuming_ the right bitfield definition for PORTB, you _could_ use
something like

PORTB.bits.BIT3 = ~PORTB.bits.BIT3; or
PORTB.bits.BIT3 = PORTB.bits.BIT3 ^ 1;

Both of these would work, even with a strictly ANSI-C compliant compiler.

> 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!

But you need to do this byte-wise, which means that in C you'd do
something like

PORTB ^= 0x08; // exclusive-or bit 3

You'd do the same in assembler: read the port, xor with 08h, then write
it back to the port. How to do that in the 35 easy-to-learn
instructions that you get with a PIC is up to you.

--

Tim Wescott
Wescott Design Services
http://www.wescottdesign.com

Do you need to implement control loops in software?
"Applied Control Theory for Embedded Systems" gives you just what it says.
See details at http://www.wescottdesign.com/actfes/actfes.html

Posted by Paul E. Schoen on August 26, 2008, 4:03 pm
Please log in for more thread options



> 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



Posted by Bob Eld on August 26, 2008, 8:26 pm
Please log in for more thread options



> 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



Similar ThreadsPosted
PIC16F88 swaping bit status in PORTB August 26, 2008, 2:22 pm
how it could be connected to the PIC16F88 microcontroller? November 6, 2006, 5:44 pm
Status Indicator System November 27, 2006, 9:03 pm