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!)