Laboratory3 ECI
Laboratory3 ECI
1. Investigate in further detail the operation of the GPIO ports on the MSP430F5438
2. Understand the function and use of interrupt based software.
3. Discover the low power options available with the MSP430F5438.
4. Use further control logic to manipulate output with interrupt based software.
2 Introduction
Polling is to constantly check or “poll” something. This can be applied in software for example to
any user interface where the microcontroller can poll the buttons to verify if there are any user
inputs, as we have seen previously. On the other hand, Interrupts use hardware to implement a
more efficient way of letting the firmware know of any occurring events.
Interrupts are like a telephone. Consider these two situations. You are at your house and you are
expecting a call. You could periodically grab the telephone every 5 seconds and check if there is
anyone in the line calling you. Yet, that would mean that you would waste a lot of time polling the
telephone line without any success. On the other hand, you can continue whatever you are doing
and when the telephone rings you receive an interrupt. Once it rings, you leave whatever you’re
doing and take care of the telephone (you service the telephone interrupt).
Now extending on this idea, imagine you are at home and your house phone rings at the same time
as your mobile phone. Your now required to make a choice as to which to answer, the same could
occur with interrupts within a MCU, therefore in the same way that you would use a priority based
scheme to decide which call to answer, interrupts service routines are given priorties also.
page 1
The MSP430F5438 provides various interrupt sources which are detailed in page 13 of the datasheet.
Lab Question 1. From the functional block diagram within the datasheet for the
MSP430F5438, which two Input/Output ports have Interrupt Capability?
Lab Question 2. From Table 2 within the datasheet, which interrupt within the
MSP430F5438 has the highest priority?
Lab Question 3. By performing an appropriate internet search, detail what is the difference
between Maskable and Non Maskable interrupts?
To help new developers get up to speed with the rich set of functionality that is available with the
MSP430 there exists extensive documentation, relating to both the MCU and the Experimenter
board. The following wiki is populated by TI and is a good place to begin to find documentation and
software examples.
http://processors.wiki.ti.com/index.php/MSP-EXP430F5438_Experimenter_Board
The following documents (pdfs) will be required for this and any subsequent laboratories, they are
available at the following website and copies are available on GCU Learn.
http://www.ti.com/tool/msp-exp430f5438
MSPF5438 Experimenter User Guide (Rev G.)
http://www.ti.com/product/msp430f5438a
MSP430F543xA, MSP430F541xA Mixed Signal Microcontroller (Rev. B)
page 2
We then setup the pullup resistors, by enabling pull ups then setting the pull up to the up
configuration.
As this is something we may wish to do commonly, it makes sense to place this software into a
peripheral initialization function, that we can call at the start of our programs. This promotes
software reuse, and will save time and effort later. The following is an example function that will
setup S1 to operate as you used in the previous laboratory.
void periphInit(void)
{
Lab Question 4. Update the function above to setup s2, and LEDs 1 and 2 appropriately.
3.1 Interrupts
Interrupts are asynchronous breaks in normal program flow that occur as a result of events
outside the running program. They are usually hardware related, stemming from events
such as a button press, the completion of a data transfer or the expiration of a timer. We
can see from these examples that interrupt conditions are independent of particular
instructions; they can happen at any time, stopping normal program exectution only when
necessary. Interrupts trigger execution of instructions that perform work on behalf of the
system, but not necessarily the current program.
• Urgent tasks that must be executed promptly at higher priority than the main code.
However, it is even faster to execute a task directly by hardware if this is possible.
• 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.
page 3
In the metaphorical example above, you required to pick up the phone only once you had been
alerted to it. Once we have been alerted to the presence of an interrupt the MCU will require to
take some action. The action to be performed in response to an interrupt is written by a
programmer just as normal software is created. It is peformed within what is referred to as an
interrupt handler or an interrupt service routine (ISR). An ISR is like a function but with the critical
distinction that they are requested by hardware at unpredictable times rather than called by
software in an orderly manner determined by the programmer.
When an interrupt occurs the processor will peform the following sequence.
As we have seen in the previous laboratories to setup the operation of I/O ports, we set
values within registers. So far the registers we have used are
PxDIR
PxOUT
PxSEL
PxREN
PxIN
Lab Question 5. Write a sentence detailing the function of each of the above registers.
(PxOUT has two functions)
Each pin of ports P1 and P2 is able to generate an interrupt request (pin is interruptible)
and is configured using the PxIFG, PxIE, and PxIES registers. The port makes use of all the
same configuration registers as non-interruptible ports (as described above), but with
three additional registers:
PxIE configuration:
page 4
Bit = 1: The interrupt is enabled;
Each PxIE bit enables the interrupt request associated with the
corresponding PxIFG interrupt flag;
PxIES configuration:
PxIFG configuration:
Following the same procedure that was utilized in the first two laboratories.
page 5
//***************************************************************************************
// Description: Software to demonstrate interrupt based digital input on
// push buttons on P2.6
// Author:
// Date:
//****************************************************************************
#include "msp430x54x.h"
void main(void)
{
long int i=0;
// Stop WDT
WDTCTL = WDTPW + WDTHOLD;
// Enable interrupts
_EINT();
page 6
// Port 2 interrupt service routine
#pragma vector=PORT2_VECTOR
__interrupt void Port_2(void)
{
// P1.0 = toggle LED 1
P1OUT ^= BIT0;
// Enable interrupts
_EINT();
The first line is setting up the transition type that will cause an interrupt, in this case a high
to low transition.
Lab Question 6. What other registers have we set in this lab and in the previous lab, to
ensure that pressing the switch will cause a high to low transition?
The second line clears the interrupt flag within the register for that pin. When an interrupt
occurs, a flag (bit within a register) is set high. The programmer can utilize this flag to see
which bit was set within an interrupt handler and take action accordingly.
The third line enables the interrupt for the specific bit on the port.
// P2.6 Interrupt Enable
page 7
P2IE |= BIT6;
The fourth line is the global interrupt enable. The interrupts are activated using eint(); At
this point you might be asking yourself: Wait, I activated the interrupt when I called the line
with P2IE. Why do I have to enable it again? The answer lies in the name of these interrupts.
These interrupts are maskable, which means that although they are controlled individually, a
second layer exists which can enable or disable them together. Therefore, we must disable
the masking to allow our interrupts to run. The major benefit of the interrupt solution is that
code continues to be executed after the interrupt is enabled. Rather, the for loop is
executed. Whenever the user presses on the button, the interrupt handler is executed and
then the CPU returns to its state of execution before the interrupt occurred.
Lab Question 7. Change the above code so that LED 1 toggles when switch S2 is pressed
using an interrupt
page 8
– DCO's dc generator is disabled
– ACLK remains active
• Low-power mode 4 (LPM4)
– CPU is disabled
– ACLK is disabled
– MCLK, FLL loop control, and DCOCLK are disabled
– DCO's dc generator is disabled
– Crystal oscillator is stopped
– Complete data retention
We will discuss the specifics of these modes in the lectures. For the moment, here is the
code to place the MCU into Low power mode 0, and wake the CPU only when the push
button is pressed, LED1 is then toggled within the interrupt.
//******************************************************************************
// Description: Software to demonstrate interrupt based digital input, with
// Author:
// Date:
//******************************************************************************
#include "msp430x54x.h"
void main(void)
{
// Stop WDT
WDTCTL = WDTPW + WDTHOLD;
page 9
{
// P1.0 = toggle LED 1
P1OUT ^= BIT0;
is used to place the CPU into a low power mode, and it enables interrupts.
Lab Question 8. Modify the above code to toggle LED 1 if swtich s1 is pressed, and to
toggle LED 2 if switch 2 is pressed. This should be performed using interrupts. The
processor should be placed into low power mode 0. (Hint: This will require checking the
flag within the interrupt handler to see which push button was pressed.)
Lab Question 9. Repeat the above, placing all of setup code for the LED and push button
switchs into an initialization function.
In the next laboratory we will investigate how to perform serial communcations between
two MSP430F5438
Lab Question 10 Discuss the parameters that need to be agreed between two devices to
allow serial communications using the RS232 protocol?
page 10
Although most users of Personal Computers would not be aware of this, clocks are at the
heart of any synchronous digital system. CPUs require clocks to run the CPU since
asynchronous operation is not possible in computer proccesing (it presents many difficulties
such as when the data to be processed from comes from different places at different
speeds). In PCs, the selection of clock speeds is determined by various factors. Unless you
are overclocking, you will never deal with them directly. Microcontrollers, on the other
hand, are usually very flexible with clocks and require that the designer specify what clocks
will be used and at what speeds. Usually this means that both hardware and software
aspects of clocks be considered during the design stage.
Lab Question 11 From the data sheet for the MSP430F5438 and associated documentation,
what are the names of the three clock signals available within the MSP430f5438?
4 Lab Reflection
In general, your lab write-up should indicate that you have acquired a better understanding of the
topics treated by the laboratory assignment. You should write half a page of text that explains the
following aspects in the box below. Please create a cohesive piece of text and do not just provide
unconnected sentences in response to the following aspects of your learning experience.
Three new facts/concepts that you learnt while undertaking the lab.
The most useful thing that you learnt.
What understanding you already had of the material being explored.
How this laboratory experience relates to any other learning that you are also undertaking at the
moment, or have undertaken in the past.
A statement of anything additional that you would like to explore in this area of work. Has the
investigation given you any ideas about possible applications of this technology?
page 11
page 12