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