ZeroSkillz 11 Posted February 13, 2011 Share Posted February 13, 2011 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: NOT 1101 1010 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) OR 0001 0001 ( 1101 0001 <- RESULT Another example: 1100 001 (a) OR 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) AND 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: 1101 0001 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: 0100 0000 NOT BIT6 (~BIT6) Looks like this: 1011 1111 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. Happy coding! ZeroSkillz bluehash, GeekDoc, Alexis and 6 others 9 Quote Link to post Share on other sites
TheDirty 33 Posted February 13, 2011 Share Posted February 13, 2011 Very nice. You might want to mention the difference between these bitwise operators as opposed to the logic AND, OR, and NOT. &&, ||, and ! Quote Link to post Share on other sites
bluehash 1,581 Posted February 13, 2011 Share Posted February 13, 2011 Very nice. One upvote for you. Quote Link to post Share on other sites
gatesphere 45 Posted February 14, 2011 Share Posted February 14, 2011 Very nice breakdown! Quote Link to post Share on other sites
cris11368 0 Posted September 23, 2011 Share Posted September 23, 2011 You have on Idea how much I want to thank you. I took C++ and was never taught what any of these operations were. I bought the Launchpad kit because I have been interested in learning micro-controllers for a while now and the price just made it difficult to resist. I have literally spent days trying to figure out what all those symbols meant in the example code. Thank you. Quote Link to post Share on other sites
ike 53 Posted September 23, 2011 Share Posted September 23, 2011 Toggle, toggle on the wall Who is missing something small? ZeroSkillz tell us more about: P1OUT ^=BIT6; Quote Link to post Share on other sites
Alexis 0 Posted December 8, 2011 Share Posted December 8, 2011 muchas gracias!! hace d Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.