Sie sind auf Seite 1von 3

OneNote Online

1 of 3

https://onenote.officeapps.live.com/o/onenoteframe.aspx?Fi=SDE6D29...

Bitwise opera ons


08 August 2014 11:46

When you use "|=" it means that you are se ng registers.


When you use "&=" it means that you are rese ng registers.
eg:
x= x & 0x01; is same as
x &= 0x01;

____----------------------------------------------------------------------------------------------------------------

P1DIR |= BIT0;

With this instruc on, we set the P1.0 pin (which is connected to the red led on the LaunchPad) to the
output direc on. In the MSP430 architecture, P1DIR is a 8-bit register that controls the i/o direc on of
the Port 1 pins. If you set a bit to 0, it is congured as an input, otherwise it is an output. The BIT0
constant is simply the 0x01 hex number, so you do this :
P1DIR before 00000000+
BIT0 00000001=
P1DIR a er 00000001
This way, all the Port1 pins are set to input direc on, except for P1.0 which is our led.
-----------------------------------------------------------------------------------------------------------------------

Bit manipula on in the C programming language


C has direct support for bitwise opera ons that can be used for bit manipula on. In the following
examples, n is the index of the bit to be manipulated within the variable bit_fld, which is an
unsigned char being used as a bit eld. Bit indexing begins at 0, not 1. Bit 0 is the least signicant
bit.
a.Set a bit
bit_d |= (1 << n)
b.Clear a bit
bit_d &= ~(1 << n)
c.Toggle a bit
bit_d ^= (1 << n)
d.Test a bit
bit_d & (1 << n)

Bit set, reset, toggle and masking in Microcontrollers


Posted on June 13, 2014 by Manpreet Singh Minhas
Well I would like your concepts on bit set and reset opera ons to be clear. No ma er which language
you use be it assembly or embedded c, you will always require to set and reset register bits.

8/8/2014 12:45 PM

OneNote Online

2 of 3

https://onenote.officeapps.live.com/o/onenoteframe.aspx?Fi=SDE6D29...

Now whether you want to ini alize some peripheral or poll something you will require these three
opera ons.

Bit set opera on


This means making a par cular bit in an register as 1 or se ng it. Now let us see this property of logical
OR.
x OR 1 = 1
So whatever be the value of x if you OR it with 1 the result will be 1. This is what we want right? Ill be
taking one example a er explaining bit reset.

Bit reset opera on


This means that you want to make a par cular bit of a register 0 or reset it. We make use of the
property of logical AND:
x AND 0 = 0
So whatever be the value of x if you AND it with 0 the result is 0. This is what we wanted so bit reset is
done by logical AND opera on.

Masking
O en you are interested only in a par cular bits of a register. This may be the input of some sensor or
something else. Now you want to use masking in this scenario. So masking means you reset all the bits
that are not required and let the bits under considera on as it is. Again we make use of logical AND
proper es.
x AND 0 = 0
x AND 1 = x
So you make the masking pa ern by making the bits you want as 1 and the ones you dont want as 0.
Convert this value to hex and logical AND that mask value with the register.

Bit toggle
Bit toggle is also useful. So for this we make use of the property of ex-or as follows.
x ex-or 1 = complement of x or x-bar
Thus if you ex-or the bit with 1 the bit will be toggled.

Example

8/8/2014 12:45 PM

OneNote Online

3 of 3

https://onenote.officeapps.live.com/o/onenoteframe.aspx?Fi=SDE6D29...

R &= 0x2081 // This is the masking opera on in embedded c


R |= 0x2081 // This will set the bits 0,7,13
R ^= 0x2081 // This will toggle bits 0,7,13
So as you can see this example covers masking. Similarly you need to make the bit pa erns for bit set,
reset and toggle opera on.

8/8/2014 12:45 PM

Das könnte Ihnen auch gefallen