Search Microcontrollers

Sunday, June 10, 2012

MSP430G2 - The GPIO and Pin Multiplexing

One of the most obvious ways to use a microcontroller is to read or write digital signals.

Examples of these activities are detecting if a push button is pressed (read) or lighting up an LED (writing).
The General Purpose Input Output is the easiest peripheral for these purposes.

In the MSP430G2 MCU family, there are up to 8 GPIO ports, each one of which controlling 8 digital I/O pins.
Each one of these digital I/O pins can be configured as Input OR Output via software, this configuration is referred to as DIRECTION of the pin.

Additionally pullup/down resistors can be activated on each pin.
This feature is extremely valuable for Input pins as it can prevent that their state is "floating" between levels due to currents captured from the nearby environment.
MCUs like the MSP430 have software configurable pullup or pullown resistors to force the digital levels to Vcc (pullup) or to GND (pulldown) when no specific signal is provided from outside.

These pull resistors are configured using the PxREN (Resistor ENable) registers.

Let's assume we want to use P1.5 as an output, we will then need to set to 1 it's directions bit in the P1DIR register.

P1DIR |= 0x10; // 10h = 0001 0000b 
               // we turn on the 5th bit in the register.

Then we will control the digital value by setting the 5th bit in the register P1OUT.

P1OUT |= 0x10;  // P1.5 will have Vcc value (1)
P1OUT &= ~0x10; // P1.5 will be driven down to GND (0) 

If we don't have any other pin configured as output in port 1, we can also disregard to save the status of the other pins and use

P1OUT = 0x10; 

Instead if we want to read the digital values from P1.5 :

P1DIR &= ~0x10; // we clear the 5th bit of the dir register to set the gpio pin to input 
myBooleanVariable = (P1IN & 0x10) > 0 ;  // we test the 5th bit, 
    // the result of (P1IN & 0x10) will be either 0 or 0x10

Let's imagine P1.5 is connected to a push button, normally open.
In that case we want a pulldown resistor to secure the level to 0.

P1REN |= 0x10; // setting the 5th bit of the P1REN register 
         // enables the pull (up or down) resistor.
P1OUT &= ~0x10; // When a pin is configured as input and the pull 
  // resistor function is enabled , the P1OUT register 
  // is used to specify the type  of resistor 
  // connection needed : 1 = pullup, 0 = pulldown                          

Digital inputs can be used to trigger interrupts, we will discuss this feature in another lesson.

Multiplexing 

Modern MCUs pack many different functions, but try to keep a reasonable number of pins to reduce the footprint on the PCB (Printed Circuit Board).
To achieve this, a single pin can be used for different purposes and configured via software.
As an example, a pin may be used as GPIO, ADC Input and UART RX.
Before using it , we need to ensure that the correct function is selected, this is done using the PxSEL and PxSEL2 registers.

When both pin specific bits are zero, then the selected function is GPIO.

P1SEL &= ~0x10; 
P1SEL2 &= ~0x10; 

Since a default function is assigned if no specific configuration is performed, it will be common that those instructions are not used.

When bit x of PxSEL is 1 and the same bit in PxSEL2 is 0, the "primary periphearal mode" is selected.
When both two are set to 1 , then the secondary peripheral mode is activated.

To discover which primary and secondary functions the pins have,  you need to refer to the data sheet of the specific MCU you are programming.

Basically 3 different configurations are normally available : GPIO, primary peripheral, secondary peripheral

No comments: