<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE document PUBLIC "-//CNX//DTD CNXML 0.5 plus MathML//EN" "http://cnx.rice.edu/cnxml/0.5/DTD/cnxml_mathml.dtd">
<document xmlns="http://cnx.rice.edu/cnxml" xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" id="id5734376">
  <name>Bit Manipulation in C for TI MSP430 Microcontrollers</name>
  <metadata>
  <md:version>1.2</md:version>
  <md:created>2008/02/06 23:02:49 US/Central</md:created>
  <md:revised>2008/02/11 11:43:00.289 US/Central</md:revised>
  <md:authorlist>
      <md:author id="advplc">
      <md:firstname>Trent</md:firstname>
      
      <md:surname>Kelly</md:surname>
      <md:email>advplc@yahoo.com</md:email>
    </md:author>
  </md:authorlist>

  <md:maintainerlist>
    <md:maintainer id="advplc">
      <md:firstname>Trent</md:firstname>
      
      <md:surname>Kelly</md:surname>
      <md:email>advplc@yahoo.com</md:email>
    </md:maintainer>
  </md:maintainerlist>
  
  <md:keywordlist>
    <md:keyword>ANSI C</md:keyword>
    <md:keyword>bit</md:keyword>
    <md:keyword>bitwise</md:keyword>
    <md:keyword>C</md:keyword>
    <md:keyword>embedded</md:keyword>
    <md:keyword>header</md:keyword>
    <md:keyword>microcontroller</md:keyword>
    <md:keyword>MSP430</md:keyword>
    <md:keyword>truth table</md:keyword>
  </md:keywordlist>

  <md:abstract>Microcontrollers require more bit manipulation than most typical C programs.  The intent of this module is to help programmers new to the MSP430 processor get the results they need from C language bit manipulation instructions.  Much of this content applies to C programming for many microcontrollers as well as for computers; the examples are based on the Texas Instruments MSP430.</md:abstract>
</metadata>
  <content>
    <section id="id-976985685329">  
<para id="id5295663">Hexadecimal numbers will be used in code examples for this tutorial. Each digit of hexadecimal represents four binary bits. If you aren’t familiar with conversions between hexadecimal and binary you should review <cnxn document="m11851">Binary and Hexadecimal Notation</cnxn>. Our goal is to control individual bits, and they will be represented in code as hexadecimal numbers.</para>
<para id="id3867420">The bitwise operators are AND ‘&amp;’, OR ‘|’, XOR (exclusive or) ‘^’, and NOT ‘~’. These operators should not be confused with logical AND ‘&amp;&amp;’, logical OR ‘||’, and logical NOT ‘!’ which are used to evaluate true verses false.</para>
<para id="id6613155">For example:</para>
<code type="block">
int a; int b; int c;

a = 0x0003 | 0x0004; //bitwise OR operation makes integer a equal to 0x0007

while ((a&gt;b) &amp;&amp; (b&gt;c)) //logical AND is true if a&gt;b&gt;c
{ }

</code>
<para id="id5972818">A truth table shows the operation that a bitwise operator performs on each bit position. Each operator produces results depending on the status of inputs A and B. The operations are performed on each bit position (such as bit 0 through bit 15 for integers) in the data, and the truth table defines the result for each possible combination of data bits.</para>
      <table id="id5996495">
        <tgroup cols="5">
          <colspec colnum="1" colname="c1"/>
          <colspec colnum="2" colname="c2"/>
          <colspec colnum="3" colname="c3"/>
          <colspec colnum="4" colname="c4"/>
          <colspec colnum="5" colname="c5"/>
          <tbody>
            <row>
              <entry>A</entry>
              <entry>B</entry>
              <entry>AND A&amp;B</entry>
              <entry>OR A|B</entry>
              <entry>XOR A^B</entry>
            </row>
            <row>
              <entry>0</entry>
              <entry>0</entry>
              <entry>0</entry>
              <entry>0</entry>
              <entry>0</entry>
            </row>
            <row>
              <entry>0</entry>
              <entry>1</entry>
              <entry>0</entry>
              <entry>1</entry>
              <entry>1</entry>
            </row>
            <row>
              <entry>1</entry>
              <entry>0</entry>
              <entry>0</entry>
              <entry>1</entry>
              <entry>1</entry>
            </row>
            <row>
              <entry>1</entry>
              <entry>1</entry>
              <entry>1</entry>
              <entry>1</entry>
              <entry>0</entry>
            </row>
          </tbody>
        </tgroup>
      </table>
<para id="id5733557">Now lets look at a couple of examples with 16 bit integers</para>
<para id="id5681111">0x0003 &amp; 0x0005 Bitwise AND</para>
<code type="block">
0x0003 is 0000000000000011 in binary
0x0005 is 0000000000000101 in binary
Result    0000000000000001 in binary or 0x0001 hex

</code>
<para id="id5721532">0x00AA ^ 0x00FE Bitwise exclusive or</para>
<code type="block">
0x00AA is 0000000010101010 in binary
0x00FE is 0000000011111110 in binary
Result    0000000001010100 in binary or 0x0054 hex

</code>
<para id="id6482955">The NOT operator ‘~’, also called ones complement, is used to reverse the state of each bit.</para>
<code type="block">
~0x0F results in 0xF0

</code>
<para id="id5719530">Lets put these operators to work for a MSP430. At the beginning of a MSP430 C program there is usually an #include directive that tells the compiler to use a header file for the MSP430 device that will run the program. For example: </para>
<code type="block">
#include &lt;msp430x42x0.h&gt;

</code>
<para id="id5969252">Inside this header file we can see a number of statements such as:</para>
<code type="block">
/* SD16CTL */
#define SD16DIV0 (0x0040) /* SD16 Clock Divider Select 0 */
#define SD16DIV1 (0x0080) /* SD16 Clock Divider Select 1 */
#define SD16DIV_0 (0x0000) /* SD16 Clock Divider Select /1 */
#define SD16DIV_1 (SD16DIV0) /* SD16 Clock Divider Select /2 */
#define SD16DIV_2 (SD16DIV1) /* SD16 Clock Divider Select /4 */
#define SD16DIV_3 (SD16DIV0+SD16DIV1) /* SD16 Clock Divider Select /8 */

</code>
<para id="id6165944">These statements assign a name to content that the compiler will substitute into the code where the name is used. When the compiler sees SD16DIV0, it substitutes in the hexadecimal number 0x0040. These particular define statements apply to use of the SD16 analog to digital converter peripheral. The /*SD16CTL */ remark indicates that the following defined names are for use in configuring the SD16CTL register. Notice that names are defined to set individual bits (SD16DIV0, SD16DIV1) or choose the function controlled by those bits (SD16DIV_0 – SD16DIV_3).</para>
<para id="id5972450">These two statements accomplish the same thing:</para>
<code type="block">
SD16CTL = SD16DIV0 | SD16DIV1; //0x0040 OR 0x0080 = 0x00C0
SD16CTL = SD16DIV_3; //0x00C0

</code>
<para id="id6220014">A switch can be attached to the MSP430 as follows: Connect a 100K ohm resister from the digital power rail to an input pin. Then attach a single pole single throw switch to the same input pin with the other terminal connected to the digital common rail. When the switch is on (closed) the input pin sees a logic 0. When the switch is off (open) the input pin sees a logic 1 because the resistor is carrying minimal current. Assume pins 6.4, 6.5, 6.6, and 6.7 are connected to switches in this way.</para>
<code type="block">
void main(void)
{
 P6DIR=0xF0; // set 6.4-6.7 to inputs and 6.0 – 6.3 to outputs

 while (1) // Begin while forever
 {
  if ((P6IN &amp; 0xF0) == 0xF0) // &amp;0xF0 isolates the four inputs
  { // execute this code if all switches are off (1111)
  }
  if ((P6IN &amp; 0xF0) == 0xe0)
  { // execute this code if the switch on 6.4 is on and the others are off (1110)
  }
  if ((P6IN &amp; 0x80) == 0) // &amp;0x80 isolates only bit 7 for input 6.7
  { // execute this code if the switch on 6.7 is on no matter how the other switches are set
  }
 } // end while forever

} // end main()

</code>
<para id="id5937012">If output pin 6.1 is connected to a LED, we can toggle it to the opposite state with an exclusive or operator. A ^= B is the same thing as writing A = A ^ B.</para>
<code type="block">
P6OUT ^= 0x02; // if the LED was off it lights, and if it was lit it turns off

</code>
<para id="id6630360">What if we wanted the LED output to be on no matter what its former state was?</para>
<code type="block">
P6OUT |= 0x02; // the LED output is on, and the other bits are undisturbed.

</code>
<para id="id5703307">What if we wanted the LED output to be off no matter what its former state was?</para>
<code type="block">
P6OUT &amp;= 0xFD; // the LED output is off, and the other bits are undisturbed.

</code>
<para id="id6633022">What if we wanted to set SD16CTL to ‘clock divide by 4’ and it is currently set to ‘clock divide by 8’?</para>
<code type="block">
SD16CTL &amp;= ~( SD16DIV0 | SD16DIV1); // clear clock divider bits
SD16CTL |= SD16DIV_2; // clock divider select /4

</code>
    </section>
  </content>
</document>
