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
PortB output using mask problem
Moderator: Benj
-
- Matrix Staff
- Posts: 9521
- Joined: Sat May 05, 2007 2:27 pm
- Location: Northamptonshire, UK
- Has thanked: 2585 times
- Been thanked: 3815 times
Re: PortB output using mask problem
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
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
Martin
-
- Flowcode v5 User
- Posts: 118
- Joined: Thu Sep 17, 2009 1:30 pm
- Has thanked: 3 times
- Been thanked: 11 times
Re: PortB output using mask problem
Hi Martin
You are absolutely correct!.....Completely forgot about that. Bits for bits
and bytes for ports
Thank you for your complete and lengthy answer!
Mark
You are absolutely correct!.....Completely forgot about that. Bits for bits
and bytes for ports

Thank you for your complete and lengthy answer!
Mark