how to do multiple outputs 1 instruction

Recommended Posts

THis should be a no brainer for you guys but I tried it at home and it didn't work. Lets say that I want to set 2 output pins on my PIC16f84 hight. Can I do this in one single isntruction or would I need 2 instruction.

exsample,

set_high_portb(0x60) ;\* is this legal or would I have to do*/

set_high_portb(5)

set_high_portb(6)

Share on other sites

Just use a logical AND

easy example to set the second and the eights Pin of PortB to high:

void main(void)

{

PORTB = PORTB & 10000010b; // =130dec = 82hex (0x82 in c2c)

}

Share on other sites

Hi

just try

void main(void)

{

PORTB = PORTB & 10000010b; // =130dec = 82hex (0x82 in c2c)

}

Share on other sites

Hi

just try this approach with a logical and

void main(void)

{

PORTB = PORTB & 10000010b; // =130dec = 82hex (0x82 in c2c)

}

Share on other sites

THanks guys. Interesting though, everybody gave the same example. Did the answer came from a text book?

Share on other sites

it was always my..., the formum has not been actualized real-time.. ::

Share on other sites

wait a minute. lets say I do as you guys say.

portb = portb & 0x82

the output of portb should be 10000010

now lets say later on I call a function and in the function I have the following comand

portb = portb & 0x86

The question is the following. Is the output of portb going to be equal 0x86 (10000110) or is it going to be (0x86 & 0x82) which as I understand it it will result in 0x82?

Share on other sites

If portb originaly = 0x00 then the output would be 0x00 if you want to set specific bits and leave the rest unchanged then perform an OR function on them.

PORTB = PORTB | 0x82;

If you want to change the port totally then set the port equal to the value that you want.

PORTB = 0x82;

If you need to clear specific bits and leave others unchanged then AND them with the inverted value of the bits you wish to set. Lets say you want to clear the bits that where set originally, PORTB = PORTB|0x82, then perform the following PORTB = PORTB & ~(0x82).

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.