Page 1 of 1

PortB output using mask problem

Posted: Mon Nov 25, 2013 1:30 pm
by MarkW
Hello

I have a problem with the mask output function for Port B on a 18F66K22.
I have selected RB1 to RB5 in the mask to output there only. If i run the program
on the chip (Internal osc / 16mhz) it will not turn on/off the leds on that port.

If i drive the port output in single bit mode then it works ok.

Attached is FC5 short test program. It will flash RB1 ok as single bit (10X), but the
next loop will not output anything(mask function).

Any help?

Thanx

Mark

Re: PortB output using mask problem

Posted: Mon Nov 25, 2013 1:58 pm
by medelec35
Hi Mark
Pin B0 will only be high when value sent to portB is 1
Pin B1 will only be high when value sent to portB is 2
Pin B2 will only be high when value sent to portB is 4
Pin B3 will only be high when value sent to portB is 8
Etc.


So to get both B1 and B2 high you will need to send 2 + 4 = 6

If you send 1 to port, then this will only set b0 if masked.
Since it's not, no pins connected to port B will be high

To toggle pin B2 without affecting other bits for example you will need to send:
Variable = Variable XOR (1<<2)
The 2 on the right is the bit you're interested in.

To set and clear bits without affecting other bits (taken from one of my posts):
(Variable & (1 << bit)) <> 0 // to Check if bit of variable is set
or
(Variable & (1 << bit)) = 0 // to Check if bit of variable is Clear

So if you want to see if bit 2 is Clear:
If (Variable & (1 << 2)) = 0 then it's clear

If you want to see if bit 2 is Set:
If (Variable & (1 << 2)) <> 0 then it's set


Also if you want to set a bit:
Variable = Variable |(1<<bit)

To clear a bit:
Variable = Variables &~(1<<bit)



Martin

Re: PortB output using mask problem

Posted: Tue Nov 26, 2013 10:02 am
by MarkW
Hi Martin

You are absolutely correct!.....Completely forgot about that. Bits for bits
and bytes for ports :roll:

Thank you for your complete and lengthy answer!

Mark