PIC16F88 swaping bit status in PORTB

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!

Reply to
ilmvmastm
Loading thread data ...

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 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
Reply to
Tim Wescott

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

Reply to
Paul E. Schoen

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

Reply to
Bob Eld

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.