In this MSP430 programming tutorial part 1 some of basic C operators used for programming the MSP430 will be looked at. The GPIO port registers will then be looked at in greater detail. In part 2 example code for the GPIO registers will be shown and explained, as well as examples for the the ADC peripheral register. The MSP430G2253 Launchpad will be used as the reference microcontroller, the primary IDE used is Code Composer Studio (CCS). The MSP430G series family guide, as well as other useful information can be downloaded directly from Texas Instruments webpage here.
C Language Bitwise Operators
If you are familiar with Bitwise operators, skip this section and and start with the MSP430 GPIO ports section further down this page.
Some basic C language bitwise operators will be looked at first, then how these apply to GPIO ports will be demonstrated. The C language has 8 types of operators and bitwise operators are 1 of these. The bitwise operators are fairly easy to understand and if you have ever looked at logic gates and truth tables, then some of these will be immediately recognisable. For the following examples two variables will be used a and b, they will also be assigned values; Decimal: a = 48 and b = 24, Binary: a = 0011 0000 and b = 0001 1000.
Bitwise Operator &
The binary AND operator copies a bit or logic 1 to the result, only if a logic 1 exits in both operands. So the result of a&b = 0001 0000 or 16 in decimal.
Bitwise Operator |
The binary OR operator copies a bit or logic 1 to the result, if it exists in either operand. So the result of a|b = 0011 1000 or 56 in decimal.
Bitwise Operator ^
The binary XOR operator copies a bit or logic 1 to the result, if it exists in one operand as a logic 1, but not both. So the result of a^b = 0010 1000 or 40 in decimal.
Bitwise Operator ~
The binary NOT operator effectively flips the bits, so 1’s become 0s and vice versa. So the result of ~a = 1100 1111 or -48 in decimal (signed variable) or 207 in decimal (unsigned variable).
Bitwise Operator <<
|a = a<<2||1100 0000|
The binary LEFT SHIFT operator moves the operands bits left, by the number of bits specified, which is this case is 2. So the result of a = a<<2 = 1100 0000 or 192 in decimal. This also effectively multiplies the value of a by a factor of 4.
Bitwise Operator >>
|a = a>>2||0000 1100|
The binary RIGHT SHIFT operator moves the operands bits right, by the number of bits specified, which is this case is 2. So the result of a = a>>2 = 0000 1100 or 12 in decimal. This also effectively divides the value of a by a factor of 4.
MSP430 GPIO Ports
Looking at the 20 pin MSP430G2253 supplied with the Launchpad, it has two GPIO ports. Both ports have 8 GPIO pins numbered as follows, port 1 pins P1.0 to P1.7 and port 2 pins P2.0 to P2.7. The image below is extracted from the MSP430G2253 datasheet.
All the individual GPIO pins can be configured to connect to internal peripherals, for example, providing a connection for the ADC to an external source, or providing the output from Timer module in the shape of a PWM signal. The GPIO’s as the acronym tells also provide General Purpose Input and Output operations. Not all of the GPIO pins can be configured to be used by all the internal peripherals, a detailed list of how the pins can configured can be found in the datasheet for that particular microcontroller. The image below again shows an extract from the MSP430G2253 datasheet, illustrating the GPIO pins P1.0 and P1.1 and their individual associated peripheral functions.
Defining how each of the eight GPIO pins are configured for each port, is achieved by individual registers.
PxIN Input Register
The PxIN register reflects the value of the signal being input into the GPIO pin, when configured as an I/O function. So by reading this value you can determine if there is a logic 0 or a logic 1 on the input. Bit = 0: The input is low, Bit = 1: The input is high. A statement using this function for GPIO port 2 and pin P2.4, could look like this if ((P2IN & Bit4) == BIT4);.
PxOUT Output Register
The PxOUT register determines the value output to the GPIO pin, when the pin is configured as an I/O function. The PxOUT register works in conjunction with the PxREN as follows:
Pullup/pulldown resistor disabled: Bit = 0: The output is low, Bit = 1: The output is high.
Pullup/pulldown resistor enabled: Bit = 0: The pin is pulled down, Bit = 1: The pin is pulled up.
A statement using this function for GPIO port 1 and pin P1.4, could look like this P1OUT &= ~BIT4.
PxREN Pullup/Pulldown Resistor Register
The PxREN register enables or disables the internal pullup/pulldown resistor, which corresponds to the individual I/O pin. Bit = 0: Pullup/pulldown resistor disabled, Bit = 1: Pullup/pulldown resistor enabled. A statement using this function for GPIO port 1 and pin P1.5, could look like this P1REN |= BIT5.
PxDIR Direction Register
The PxDIR register selects the direction of the I/O pin, whether it will be an input or an output. This is regardless of the selected function of the pin. Bit = 0: The port pin is switched to input direction, Bit = 1: The port pin is switched to output direction. A statement using this function for GPIO port 1 and pin P1.3, could look like this P1DIR |= BIT3.
PxSEL and PxSEL2 Function Select Registers
The PxSEL and PxSEL2 registers allow the individual GPIO pins to be associated with the internal peripheral module functions, or simply left as standard I/O ports. The image below was extracted from the MSP430 family guide.
A statement using this function for GPIO port 2 and pin P2.1, could look like this P2SEL |= BIT1;. When using these registers, it is important to consult the datasheet and pin schematics, for the specific device.
P1IFG, P2IFG Interrupt Flag Registers
Only GPIO ports 1 and 2 have interrupt functionality. The P1IFG and P2IFG registers hold the interrupt flag for the corresponding I/O pin, the interrupt flag is set when the selected input signal edge occurs at the pin. Bit = 0: No interrupt is pending, Bit = 1: An interrupt is pending. A statement using this function for GPIO port 1 and pin P1.1, could look like this P1IFG &= ~BIT1;.
P1IES, P2IES Interrupt Edge Select Registers
The P1IES and P2IES registers allow the interrupt edge type to be selected for each I/O pin. Bit = 0: The PxIFGx flag is set with a low to high transition, Bit = 1: The PxIFGx flag is set with a high to low transition. A statement using this function for GPIO port 1 and pin P1.1, could look like this P1IES &= ~BIT1;.
P1IE, P2IE Interrupt Enable Registers
The P1IE and P2IE register bit enables the associated PxIFG interrupt flag. Bit = 0: The interrupt flag is disabled, Bit = 1: The interrupt flag is enabled. A statement using this function for GPIO port 1 and pin P1.1, could look like this P1IE |= BIT1;.
Texas Instruments also recommends configuring unused pins as I/O function, output direction, and left unconnected to prevent a floating input and reduce power consumption.
So how to change the GPIO Registers to what you want, well part 2 of this tutorial will make the GPIO settings clear and hopefully easy. Additionally an ADC register will be explained and demonstrated.
I take great care when writing all the tutorials and articles, ensuring all the code is fully tested to avoid issues for my readers. All this takes time and a great deal of work, so please support the site by using the Adfly links etc. If you have found this useful or have any problems implementing, please feel free to leave a comment and I will do my best to help.