In this second part of the MSP430 programming tutorial examples of GPIO register settings will be shown and explained. Additionally register examples for some of the internal peripherals will be demonstrated and explained. The first part of the tutorial can be found here.
Changing the GPIO Registers to your desired configuration
The following examples should help to illustrate how you change the individual bits, as well as multiple bits in the GPIO registers to achieve exactly what you want. There is a reference to BIT3 and BIT7 being defined as hexadecimal values, these values can be found in the msp430g2253.h header file inside Code Composer Studio, or the MSP430 software provided by Texas Instruments.
So by using this statement we only change BIT7 of port 1 to a logic 1 or GPIO P1.7. This is very powerful as it allows individual pins to be configured, without effecting other pins on the same port. But what if you want to adjust multiple pins to outputs, well that can be achieved quite easily, two ways are illustrated below.
So turning a single registry bit or multiple bits to a logic 1 are covered, how about assigning a logic 0 to a single register bit or multiple register bits. The following images will demonstrate how this is achieved.
And for multiple bits.
There is one more operator that is commonly used on GPIO pin register bits, that is the ^ or XOR bitwise operator. This is used in many examples on the internet to toggle the LED’s on the launchpad, the example below demonstrates it’s use.
Although all these examples are only used with the P1OUT register, the same principles can be applied to all the GPIO port registers. The examples shown where multiple registers are written to, using a combined hex value will also optimise any code, saving execution time by removing additional arithmetic in the form of an addition.
Understanding and Changing Peripheral Registers
Understanding how to change bits inside the peripheral registers, is not a great leap in understanding from the GPIO ports. A few examples will be shown which are based on the ADC peripheral. The ADC10 Control Register 1 (ADC10CTL1) will be used as the example register, but all registers will follow the same principle. The image below is extracted from the MSP430 family guide and shows the ADC10CTL1 register.
So what we can see here is the register is a 16 bit register and the bits are split into blocks which correspond to different parameters. I have covered what these individual blocks do in a previous tutorial dedicated to the MSP430 ADC, found here. It can be seen that the blocks correspond to certain bits inside the register, for example INCHx occupies Bits 12-15 of the register and ADC10DIVx occupies Bits 5-7 of the register. To illustrate this we could view the 16 bit register in binary form:
INCHx occupies the bits shown in bold | 0000 0000 0000 0000 |
ADC10DIVx occupies the bits shown in bold | 0000 0000 0000 0000 |
As with the GPIO port pins Texas Instruments have provided defines in the header files, so it is not necessary to remember the binary or hexadecimal values for the register settings. The names used for these register settings are shown above i.e. INCHx and ADC10DIVx. As INCHx occupies four Bits it therefore has 16 possible combinations and ADC10DIVx has 8, hence the small x after each name.
The next two images are again extracted from the MSP430 family guide and show how the bit combinations correspond to different parameters.
So now lets look at a command to set the ADC10CTL1 register using the parameters INCHx and ADC10DIVx.
ADC10CTL1 |= INCH_3 + ADC10DIV_5;
The code snippet above basically adds the two parameters together and assigns them to the register using a compound OR assignment operator. Now lets look at this in binary form, which will help shed some light on the process.
So by using this command the register parameters can be set individually, or multiple parameters can be set in one go. As with the GPIO port settings a hexadecimal value can be used to set the parameters as well.
ADC10CTL1 |= 0x3080;
The code snippet above achieves the same result as well as being more efficient, however the code becomes much more obfuscated.
That covers setting the register bits to 1, how about setting the register bits to 0. This is achieved in the same way as with the GPIO, the image below illustrates the operation.
actually your explanation for the topic is so clear and easy to understand
Thanks always nice to hear when it helps someone.
Hi. I am triying below code for producing pwm on msp4302253 pin P1.2 but I am not able to do it in proteus. What is the reason? I didn’t understand. What’s wrong?
/*
* MSP430 Timer Tutorial Example Code 2
* Anthony Scranney
* http://www.Coder-Tronics.com
* August 2014
*
* PWM example using both TimerA_0 and TimerA_1
*/
#include //msp430g2253
int main(void) {
/*** Watchdog timer and clock Set-Up ***/
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
DCOCTL = 0; // Select lowest DCOx and MODx
BCSCTL1 = CALBC1_8MHZ; // Set range
DCOCTL = CALDCO_8MHZ; // Set DCO step + modulation
/*** GPIO Set-Up ***/
P1DIR |= BIT2; // P1.2 set as output
P1SEL |= BIT2; // P1.2 selected Timer0_A Out1
/*** Timer0_A Set-Up ***/
TA0CCR0 |= 200 – 1; // PWM Period
TA0CCTL1 |= OUTMOD_7; // TA0CCR1 output mode = reset/set
TA0CCR1 |= 100; // TA0CCR1 PWM duty cycle
TA0CTL |= TASSEL_2 + MC_1; // SMCLK, Up Mode (Counts to TA0CCR0)
_BIS_SR(LPM0_bits); // Enter Low power mode 0
}
Hi,
I have not used Proteus and this code was written in Code Composer Studio, so I would try the following. Assuming Proteus has example programs for the MSP430 load one of them and check the code to see if there are any additional header files etc, then try and paste the code in there and tweak it.
Cheers,
Ant
Great tutorial, thanks for taking the time to put these together.
Good help from this