UNIT – 2
- Block diagram of MSP430x5xx series micro-controller ------1
- CPU architecture of MSP430x5xx ------5
- Memory map/ Address space of MSP430x5xx------8
- I/O Ports / GPIO / Digital IO pins ------9
(a) LED interfacing ------
(b) Switches Interfacing and Pull up/down resistor concepts ------10
- Processing of Interrupts ------11
- Clock system of MSP430 ------
- Watchdog Timer ------
- Low power modes of MSP430 ------
4. I/O PORTS (GPIO pins)
MSP430x5xx devices have up to 12 digital I/O ports : P1 to P11 and PJ. Most ports have eight I/O pins, however some ports may contain less.
Each I/O pin is individually configurable for input (or) output direction, and each I/O line can be individually read or written to.
All ports have individually configurable pull-up (or) pull-down resistors, as well as, configurable drive strength.
Ports P1 and P2 always have interrupt capability. Each interrupt for the P1 and P2 I/O lines can be individually enabled and configured to provide an interrupt on a rising edge or falling edge of an input signal.
Individual ports can be accessed as byte wide ports or can be combined into word wide ports and accessed via word formats. Port pairs P1/P2, P3/P4, P5/P6, P7/P8, etc. are associated with the names PA, PB, PC, PD, etc., respectively.
When writing to port PA with word operations, all 16 bits are written to the port.
Writing to the lower byte of the PA port using byte operations, the upper byte remains unchanged. Similarly, writing to the upper byte of the PA port using byte instructions leaves the lower byte unchanged.
Each port is assigned several 8-bit registers that control the function of the pins and provides information on their current status. The following is a list of registers always available for ports:
PxSEL(Port Selection) : selects either digital I/O (0) or an alternate function (1)
PxDIR(Port Direction) : configures the corresponding pin for input mode(0) or output mode(1)
PxIN(Port Input) : Reads the voltage levels on input pins, if they are configured as GPIO.
This is read-only register, and reflects the current status of port pins.
PxOUT (Port Input) : Sends the value to be driven to each pin, if it is configured as GPIO
PxREN ( Port Resistor Enable) : Enables pull-up / pull-down resistors on input pins.
1 Enables pull-up / pull-down resistors
0 Disables pull-up / pull-down resistors
If the pin is configured in input mode and PxREN is enabled then, the corresponding bit
in PxOUT register selects whether the resistors are pull-up (1) or pull-down (0)
Some GPIOs in the MSP430 have the capability to generate an interrupt and inform the CPU when a transition has occurred. MSP430 devices typically have interrupt capability on Ports 1 and 2. The registers controlling these options are as follows:
PxIE (Interrupt Enable) : Each bit enables (1) or disables (0) the interrupt for that particular pin
PxIES ( Interrupt Edge Select) : Selects whether a pin will generate an interrupt on
the rising-edge (0) or the falling-edge (1)
PxIFG (Interrupt Flag) : Set whenever the interrupt is detectd on a particular pin
------
Note : Procedure to set / clear any bit in the Port Registers
7 / 6 / 5 / 4 / 3 / 2 / 1 / 0PDIR / 0 / 0 / 0 / 0 / 1 / 0 / 0 / 0
To set Bit.3 of PDIR
( PDIR.3 = 1 ) / mov.b #0000 1000b, &PDIR
bis.b #0000 1000, &PDIR / Change all bits in PDIR
Only PDIR.3 = 1
PDIR = PDIR |0x08
PDIR = PDIR |BIT3 / Set PDIR.3=1
Set PDIR.3=1
To clear Bit.3 of PDIR
( PDIR.3 = 0 ) / mov.b #0000 0000b, &PDIR
bic.b #0000 1000, &PDIR / Change all bits in PDIR
Only PDIR.3 = 0
PDIR = PDIR & (~0x08)
PDIR = PDIR & (~BIT3) / Clear PDIR.3=0
Clear PDIR.3=0
------
Example (1) : Configure P2.3 as output & send Logic HIGH
#include <msp430.h> // Specific device
void main (void)
{
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
P2SEL = P2SEL & (~BIT3); // Select P2.3 as GPIO pin (P2SEL.3 =0)
P2DIR = P2DIR | BIT3 ; // Set P2.3 as output pin (P2DIR.3=1)
P2OUT = P2OUT | BIT3 ; // Send Logic HIGH at P2.3(P3OUT.3 =1)
}
4(a). LED Interfacing
LEDs can be connected in two standard ways, shown in Figure.
A series resistor (220 Ω to 1 KΩ) limits the current through LED.
In the active high circuit, LED ON if the pin outputs Logic HIGH
In the active low circuit, LED ON if the pin outputs Logic LOW
A microcontroller is typically better at sinking current than sourcing it. Hence, the second method is employed for many applications.
The complete program needs to carry out the following tasks:
- Configure the microcontroller.
- Set the relevant pins to be outputs by setting the appropriate bits of P2DIR.
- Illuminate the LEDs by writing to P2OUT.
- Keep the microcontroller busy in an infinite, empty loop.
Example (2) : Program in C to light LEDs with a constant pattern. Let LED-1 is connected at
P2.3 (active High circuit) & LED-2 at P2.4 (active Low circuit) as shown in Fig.
#include <msp430.h> // Specific device
void main (void)
{
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
P2SEL = P2SEL & ~(BIT3+BIT4); // Select P2.3 & P2.4 as GPIO pins
P2DIR = P2DIR | BIT3+BIT4 ; // Set P2.3 &P2.4 as output pins
P2OUT = P2OUT | BIT3 ;// Send Logic-1 at P2.3 (LED 1ON) active High
P2OUT = P2OUT & ~BIT4 ; // Send Logic-0 at P2.3 (LED 2ON) active Low
for ( ; ; )// Loop forever……
{// doing nothing
}
}
4(b). Switches Interfacing &
Pull up/down Resistors Concepts
Pull-up and pull-down resistors are often used when interfacing a switch or some other inputs with a microcontroller.
Pull-up and Pull-down resistors are used in electronic logic circuits to ensure that inputs to the micro-controller settle at expected logic levels if external devices are disconnected or high-impedance. These resistors used in logic circuits to ensure a well-defined logical level at a pin under all conditions. This is because, when no external devices are connected to an input pin, it doesn't mean that it is a logical ZERO'
Digital logic circuits have three logic states: high, low and floating (or high impedance). The high-impedance state occurs when the pin is not pulled to a high or low logic level, but is left “floating” instead.
A good illustration of this is an unconnected input pin of a microcontroller. It is neither in a HIGH or LOW logic state, and a microcontroller might unpredictably interpret the input value as either a logical HIGH or logical LOW. Pull-up resistors are used to solve the dilemma for the microcontroller by pulling the value to a logical high state, as seen in the Fig.(a). If there weren’t for the pull-up resistor, the MCU’s input would be floating when the switch is open.
Pull-down resistors work in the same manner as pull-up resistors, except that they pull the pin to a logical low value. They are connected between ground and the appropriate pin on a device.
An example of a pull-down resistor in a digital circuit can be seen in the Fig.(b). A pushbutton switch is connected between the supply voltage and a microcontroller pin. In such a circuit, when the switch is closed, the micro-controller input is at a logical high value, but when the switch is open, the pull-down resistor pulls the input voltage down to ground (logical zero value), preventing an undefined state at the input.
Reading Input from a Switch
The standard way of connecting a simple push button is shown in Figure 4.4.
When the switch is open,
The voltage at the pin is basically VCC .
The pull-up resistor Rpull holds the input at logic 1 (voltage VCC)
When the switch is closed
The pin is connected to the Ground (Logic 0).
The input is therefore active low, meaning that it goes low when the button is
pressed. (button down→ input down)
A wasted current flows through the pull-up resistors to ground when the button is pressed. This is reduced by making Rpull large.
Pull-up and pull-down resistors are often used when interfacing a switch or some other input with a microcontroller. Hence, most microcontrollers have internal pull up/down resistors to reduce the no. of external components.
Pull-up or pull-down resistors can be activated by setting bits in the PxREN registers, provided that the pin is configured as an input.
Application of Pull up/down Resistors:
(1) Interfacing of switches / other input devices with microcontrollers
(2) A/D converters to provide a controlled current flow into a resistive sensor.
(3) I2C protocol bus, where pull-up resistors are used to enable a single pin to act as an input (or) output.
Example (3) : Program to light LED when button B1 is pressed
Let LED1 is connected at P2.3 (active Low circuit) and
Push button is connected at P2.1 with internal Pull-up resistor enabled
#include <msp430.h> // Specific device
void main (void)
{
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
P2SEL = P2SEL & ~(BIT1+BIT3); // Select P2.1 & P2.3 as GPIO pins
P2DIR = P2DIR | BIT3 ; // Set P2.3 as output pin (LED1)
P2DIR = P2DIR & ~BIT1 ; // Set P2.1 as input pin (Switch)
P2REN = P2REN & ~BIT1 ; // Pull-up/down Resistor Enable for P2.1
P2OUT = P2OUT & ~BIT1 ; // Select Pull-up Resistors for P2.1
P2OUT = P2OUT | BIT3 ;// Preload LED1 off (active Low )
for ( ; ; )// Loop forever……
{
if ((P2IN & BIT1) == 0) // Is button down? (active low)
{ // Yes: Turn LED1 on (active low!)
P2OUT = P2OUT & ~BIT3 ; // Send Logic-0 at P2.1 (LED1ON)
}
else
{ // No: Turn LED1 off (active low!)
P2OUT = P2OUT | BIT3; // Send Logic-1 at P2.1 (LED1OFF)
}
} // end for loop
}
5. Processing of Interrupts in MSP430 :
Interrupt :
Interrupt is an event that causes the microcontroller to stop the normal program execution. Interrupts are usually generated by hardware and often indicate that an event has occurred that needs an urgent response.
Reset is usually generated by hardware, either when power is applied (or) when some unfortunate things have happened and normal operation cannot continue. This can happen accidentally if the watchdog timer has not been disabled, which is easy to forget. A reset causes the device to (re)start from a well-defined state.
Why Interrupts?
Interrupts are commonly used for a range of applications:
Urgent tasks that must be executed promptly at higher priority than the main code.
Infrequent tasks, such as handling slow input from humans. This saves the overhead of regular polling.
Waking the CPU from sleep. This is particularly important in the MSP430, which typically spends much of its time in a low-power mode and can be awakened only by an interrupt.
Calls to an operating system. These are often processed through a trap or software interrupt instruction but the MSP430 does not have one.
External & Internal interrupts
Interrupts are generated by external devices connected to the microcontroller (or)
Interrupts can be requested by most peripheral modules in the core of the MCU, such as the clock generator, Timer, ADC, Serial UART.. etc.
Maskable & Non-maskable interrupts
Most interrupts are maskable, which means that they are effective only if GIE =1 in the status register (SR). They are ignored if GIE is clear. Therefore both the enable bit in the module and GIE must be set for interrupts to be generated. However, the non-maskable interrupts cannot be suppressed by clearing GIE. There are 3- non maskable interrupts:
- Oscillator fault (OFIFG)
- Access violation to flash memory (ACCVIFG)
- An active edge on the external RST/NMI pin
Interrupt Service Routine :
The code to handle an interrupt is called an interrupt handler or Interrupt Service Routine (ISR). ISR is something like a subroutine called by hardware and Interrupts must be handled in such a way that the code that was interrupted can be resumed without error.
Interrupt Vector :
The CPU must be told where to fetch the next instruction following an interrupt or reset.
The address of ISR is called as Interrupt vector
The MSP430 uses vectored interrupts. Each ISR has its own vector, which is stored at a predefined address in a vector table at the end of the program memory (addresses 0xFFC0–0xFFFF).
When the interrupt is enabled, the present PC value is stored at the stack top and loaded with the address of ISR which is available in Interrupt vector table.
The vector table is at a fixed location, but the ISRs themselves can be located anywhere in memory. The vectors for the maskable interrupts depend on the peripherals in a particular device and are listed in a table of Interrupt Vector Addresses in the data sheet.
Processing of Interrupts:
The request for an interrupt is finally passed to the CPU if the GIE bit is set. Hardware then performs the following steps to launch the ISR:
- The CPU completes the execution of current instruction.
- The PC, which points to the next instruction, is pushed onto the stack.
- The SR is pushed onto the stack.
- The interrupt with the highest priority is selected if multiple interrupts are waiting for service.
- The interrupt request flag is cleared automatically for vectors that have a single source.
- The SR is cleared, which has two effects. First, further maskable interrupts are disabled because the GIE bit is cleared; non-maskable interrupts remain active. Second, it terminates any low-power mode.
- The interrupt vector is loaded into the PC and the CPU starts to execute the interrupt service routine at that address.
This sequence takes six clock cycles in the MSP430 before the ISR commences. The stack at this point is shown in above figure.
The delay between an interrupt being requested and the start of the ISR is called the latency. If the CPU is already running, it takes more than 6- cycles to start the execution of ISR (cycles required to execute the current instruction + 6 cycles)
An interrupt service routine must always finish with the special instruction reti (return from interrupt), which has the following actions. This takes a further five cycles in the MSP430. The stack is restored to its state before the interrupt was accepted.
- The SR pops from the stack. All previous settings of GIE and the mode control bits are now in effect, regardless of the settings used during the interrupt service routine. In particular, this re-enables maskable interrupts and restores the previous low-power mode of operation if there was one.
- The PC pops from the stack and execution resumes at the point where it was interrupted. Alternatively, the CPU stops and the device reverts to its low-power mode before the interrupt.
Example (3) : Program to configure P2.1 as an interrupt pin ( falling edge interrupt )
#include <msp430.h> // Specific device
void main (void)
{
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
P2SEL = P2SEL | BIT1; // Select P2.1 as Interrupt pin
P2DIR = P2DIR & ~BIT1 ; // Set P2.1 as input pin
P2IES = P2IES | BIT1; // Falling edge (1 0)
P2IFG = P2IFG & ~BIT1; // Clear interrupt flag for P2.1
while(1)// Loop forever……
{
if ((P2IFG & BIT1) == 1)
{ // do something }
} // end while
}
VISVODAYA TECHNICAL ACADEMY :: KAVALI