I'm a total n00b, and from what I have seen, there are a lot of us here so I figured I'd share what I've been learning and maybe help some others along the way! While running demos and looking at the code that makes them tick you will come across things like this:
P1OUT |= BIT6
P1OUT &= ~BIT6
And to a n00b this looks like gibberish! Lets break it down and learn! - as taught to me by my mentor & friend Sven (Thanks bub!).
There are three basic functions in play here AND; OR and NOT. Lets start with NOT.
1101 1010 <- binary representation of a byte (8bits)
What NOT does is INVERT the bits:
0010 0101 <- RESULT
What was ON is now OFF (and vice versa)
Now, lets look at OR. While NOT operates on ONE value, OR (as well as AND) operate on TWO values and combines them in a way to create a result. OR sets all the bits that are in either (a) OR (.
1100 0000 (a)
0001 0001 (
1101 0001 <- RESULT
1100 001 (a)
1101 000 (
1101 001 <- RESULT (same as the first result)
If a bit at a certain position is set to 1 in either the first (a) OR the second ( var, the bit in the result will be set to 1 also.
Now, lets look at AND. The difference between OR and AND is that the bit in the result will be set only if BOTH the first (a) and the second ( var are set.
1100 0001 (a)
1000 0100 (
1000 0000 <-RESULT
Only the topmost bit (BIT7) is set in (a) AND (, so only the topmost bit is set in the result.
SO - NOW WHAT!? well, in C we don't write OR, AND and NOT...we use symbols.
& is AND
| is OR
~ is NOT
Lets look at a line of code:
P1OUT =P1OUT &~BIT6
What this line of code does is MASK bit number 6. It takes what is currently in P1OUT then does an AND with EVERYTHING except BIT6 and writes it back to P1OUT. Whatever was written to P1OUT still be there EXCEPT for BIT6, which will be set to zero. We can make this line shorter:
P1OUT &= ~BIT6
On a LaunchPad it turns off the Green LED located at 1.6 - ISN'T THAT GREAT TO KNOW!?
When you want to change a bit in a port, you CANNOT access just that one bit. You can only write a WHOLE byte to memory, which effects all 8 port bits. So, since you do not want to change the other bits you need to read the current state, leaving all the other bits unchanged by using AND with ALL bits except the one you want to clear (BIT6).
Lets say that P1OUT looks like this:
BIT0, BIT4, BIT6 and BIT7 are set (Zero is rightmost Seven is topmost). Now you create your mask by doing a NOT on a single BIT (BIT6). BIT6 Looks like this:
NOT BIT6 (~BIT6) Looks like this:
Now, if you do an AND operation with this MASK all bits will remain unchanged except BIT6 which will always result in zero. You write the result back to the port byte and you have only affected ONE bit (BIT6) while all the others remain the unchanged.
SO - HOW DO I TURN BIT6 ON!?
You just OR the port with the desired bit:
P1OUT |= BIT6
If BIT6 is already set, there is no change. If BIT6 was not set it is now. On a Launchpad the green LED would turn ON or STAY ON.
Just for fun:
P1OUT &= ~(BIT6|BIT0)
This line of code will turn OFF BOTH LED's on a Launchpad.