The concept of BIT Masking using ANDing and ORing Opertion in 'C'

Status
Not open for further replies.

karan123

Member level 3
Joined
Feb 2, 2009
Messages
55
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,710
8051 BIT MASKING:


I have to use same PORT (SAY P1) for BCD to 7-Segments and other for four different Switches.

My Question is:

How to mask BITS/PINS which does not cause any effect to other pins when i have to acces PORT for BCD to 7 -Segment
Decoder.

Please Clear me concept of BIT Masking using ANDing and Oring Opertion in 'C'

Thanks

Karan
 

mask concept in 8051

AND them with a "1" and OR them with a "0" ..

rgds,
IanP
 

bit masking in c

Thank For Reply...


But Please write that more clear.
...
 

masking bits

I think its OR with a "1" to turn on a bit
and AND with a "0" to turn off a bit...

for example to turn on bit 3 (P1.3)
Code:
P1 |= (1<<3); // 0000 1000 in binary, it's bit 3 at 1

to turn off the same bit 3 (P1.3)
Code:
P1 &= ~(1<<3); // 1111 0111 in binary, it's bit 3 at 0
but as far as I remember... you could use port bit definition to work on individuals bits...
Code:
#include <reg51.h>
sbit pin=P1^3; //bit P1.3 is named pin

...

{
pin=1; //to turn on...
pin=0; to turn off...
}

well that's bit masking... buuuut maybe you need to out 4 bits for the bcd decoder, leaving the other 4 bits as inputs for the switches...

let's make it the lower nibble to the bcd decoder (like a 74LS47) and the upper nibble to the 4 individual switches...

Code:
#include <reg51.h> //or someone with port definitions...
sbit SW1=P1^4; // to read the switches...
sbit SW2=P1^5;
sbit SW3=P1^6;
sbit SW4=P1^7;

/* routine to out a 4 bit bcd number..*/
void bcd_out(unsigned char bcd){
 if(bcd>9) bcd=0xF;//turn off, invalid numbers...
 P1=0xF0 | bcd; /*upper bits are inputs, leave them  with 1, lower bits are outputs... out the bcd number */
}
the PORT initial value of 0xFF makes the switches as inputs and the bcd decoder (like 7447) shut down...

this code, is for something like keil C for 8051...(maybe RIDE will work to!)

Hope it helps...
 

keil bcd c 8051

Thanks a lot...
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…