Pages

Bit Manipulation in C for TI MSP430 Microcontrollers

Summary: 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.

 
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 Binary and Hexadecimal Notation. Our goal is to control individual bits, and they will be represented in code as hexadecimal numbers.
The bitwise operators are AND ‘&’, OR ‘|’, XOR (exclusive or) ‘^’, and NOT ‘~’. These operators should not be confused with logical AND ‘&&’, logical OR ‘||’, and logical NOT ‘!’ which are used to evaluate true verses false.
For example:

int a; int b; int c;

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

while ((a>b) && (b>c)) //logical AND is true if a>b>c
{ }
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.
TABLE 1
ABAND A&BOR A|BXOR A^B
00000
01011
10011
11110
Now lets look at a couple of examples with 16 bit integers
0x0003 & 0x0005 Bitwise AND

0x0003 is 0000000000000011 in binary
0x0005 is 0000000000000101 in binary
Result    0000000000000001 in binary or 0x0001 hex
0x00AA ^ 0x00FE Bitwise exclusive or

0x00AA is 0000000010101010 in binary
0x00FE is 0000000011111110 in binary
Result    0000000001010100 in binary or 0x0054 hex
The NOT operator ‘~’, also called ones complement, is used to reverse the state of each bit.

~0x0F results in 0xF0
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:

#include <msp430x42x0.h>
Inside this header file we can see a number of statements such as:

/* 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 */
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).
These two statements accomplish the same thing:

SD16CTL = SD16DIV0 | SD16DIV1; //0x0040 OR 0x0080 = 0x00C0
SD16CTL = SD16DIV_3; //0x00C0
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.

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 & 0xF0) == 0xF0) // &0xF0 isolates the four inputs
  { // execute this code if all switches are off (1111)
  }
  if ((P6IN & 0xF0) == 0xe0)
  { // execute this code if the switch on 6.4 is on and the others are off (1110)
  }
  if ((P6IN & 0x80) == 0) // &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()
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.

P6OUT ^= 0x02; // if the LED was off it lights, and if it was lit it turns off
What if we wanted the LED output to be on no matter what its former state was?

P6OUT |= 0x02; // the LED output is on, and the other bits are undisturbed.
What if we wanted the LED output to be off no matter what its former state was?

P6OUT &= 0xFD; // the LED output is off, and the other bits are undisturbed.
What if we wanted to set SD16CTL to ‘clock divide by 4’ and it is currently set to ‘clock divide by 8’?

SD16CTL &= ~( SD16DIV0 | SD16DIV1); // clear clock divider bits
SD16CTL |= SD16DIV_2; // clock divider select /4

0 comments:

Post a Comment

Share your knowledge

Related Posts Plugin for WordPress, Blogger...

Popular Projects

program for Dual DAC 8051 Microcontroller Based DC Motor Control A Microcontroller Based Turbidity Meter A m -Controller Based Thermostat ASCII to BCD conversion in 8051 AT90LS8515 Digital Message Machine Audio Frequency Response Analyzer Audio Homing Robot Automated Juice Mixer Automated Pet Feeder Autonomous Car Autonomous Parallel Parking RC Car Autonomous Search Robot Autonomous Tank Autonomous Vehicle Contrast Following Rover Autonomous navigating robot BCD number to ASCII in 8051 Balance Bot Blind Bot Blood Pressure Monitor Bloodshed Dev-C++ 5 Compiler/IDE Breath Alcohol Tester Converters on TI MSP430 CrossStudio MSP430 IDE Design of a Real-Time Digital Guitar Tuner Digital Oscilloscope Digital Stethoscope Digital clock project using PIC16C54A microcontroller Digital thermometer ECG monitoring system GPS Data Logger with Wireless Trigger Handwriting Recognition Systm Home Security System Home energy managment IAR Embedded Workbench IDE INFRARED TRACKING SYSTEM IntelliBOT Laser Communications System Line following van MSP-EXP430FG4618 Development Tool and the eZ430 kits MSP430FG4618 device implement a Buzzer tone generator MSP430FG4618 device implement a Real Time Clock MSP430FG4618 device implement a voltage ramp generator MSP430FG4618 device present a message on the LCD Basic Microcontroller(8051) Lab Mivo- RFID based mobile payment system Multi-Zone Fire Alarm System PC based temperature control PIC 16f877 RPM Meter PIC16C54 dual dice electronic project circuit PIC16F84A digital thermometer microcontroller project PIC16F886 horn driver PWM motor contoller with MSP430 Program Block data transfer in 8051 Program to add two BCD numbers in 8051 Program to check whether a 4th bit of a byte is 1 Program to convert ASCII to hex in 8051 Program to count from 0-9 in 8051 Program to count number of 1's in a given data byte in 8051 Program to divide an 8 bit no by another 8 bit number in 8051 Program to find largest of n numbers in 8051 Program to find the LCM of two numbers in 8051 Program to find the square of an 8 bit number in 8051 Program to generate 50msec delay in 8051 Program to implement BCD counter to count from 0-99 in 8051 Program to implement BCD counter to count from 99-0 in 8051 Program to interchange two blocks of data in 8051 Program to multiply 16 bit number by 8 bit number in 8051 Program to search an element in an array in 8051 Program to sort an array of 10 elements in 8051 Programming the ez430 Proximity Security System RAMP wave in 8051 RC Car Controller RObo Dog Radio-controlled Truck Retina color tracker Robotic Arm Controller with GUI Robotic Car Traction Control Safety-sensor vehicle Security Entrance System Self-Powered Solar Data Logger Snake Arm Ultrasonic Positioning Control System Store FFh if 1 Super Train Controller TI MSP430 Microcontrollers Timers on the MSP430 TouchPad Drawing Board Ultra-Sonic Parking Assistant Ultrasonic Parking Controller Ultrasonic Range finder Voice Activated Alarm Clock Voice Recognition Robotic Car Voting Machine Weather Station Web-Monitored Thermostat Wireless Drawing Device Wireless Telemetry Wireless message Communicator Write a C program to display the code of the key pressed in 8051 Zigbee Wireless Relay Control and Power Monitoring System add two multibyte numbers in 8051 convert a decimal number to hex number in 8051 convert an 8bit Hex number to decimal number in 8051 convert hex number to ASCII number in 8051 eZ430-F2013 Development Tool use SD16_A ADC eZ430-RF2500 Development Tool use ADC10 else store 00 in the same location in 8051 find the GCF of two numbers in 8051 find the average of 10 numbers in 8051 generate Fibonacci series in 8051 metal detector project microcontroller using IAR Embedded Workbench program for Elevator Interface in 8051 program for Stepper motor interface in 8051 spectrum analyser square wave in 8051 triangle wave in 8051 voice recognition security system

Sites U missed

Hint

Open Pictures in new page by right click on it, if it is not shown full image.