0% found this document useful (0 votes)
20 views22 pages

Mod 2

The document discusses digital input, output, and displays for embedded systems using the MSP430 microcontroller. It covers topics like parallel ports, digital inputs, digital outputs, timers, communication peripherals, serial peripheral interface (SPI), inter-integrated circuit (I2C) communication, and asynchronous serial communication.

Uploaded by

Hitesh Mohanty
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views22 pages

Mod 2

The document discusses digital input, output, and displays for embedded systems using the MSP430 microcontroller. It covers topics like parallel ports, digital inputs, digital outputs, timers, communication peripherals, serial peripheral interface (SPI), inter-integrated circuit (I2C) communication, and asynchronous serial communication.

Uploaded by

Hitesh Mohanty
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Module – 2 Subject: ES (Embedded Systems)

MSP430 I/O and


Communication.

By
Venkata Sridhar .T M.Tech, PhD, MIETE

Assistant Professor of ETC.


IIIT-Bhubaneswar.
Syllabus : Module-2

Digital Input, Output, and Displays : Parallel Ports, Digital Inputs, Switch Debounce, Digital
Outputs, Interface between Systems, Driving Heavier Loads, Liquid Crystal Displays, Simple
Applications of the LCD. 4 Hrs

Timers: Watchdog Timer, Timer_A, Timer_A Modes, Timer_B,Timer_B Modes, Setting the
Real-Time Clock, State Machines. 3 Hrs

MSP430 Communication:
Communication Peripherals in the MSP430, Serial Peripheral Interface, SPI with the USI, SPI
with the USCI, A Thermometer Using SPI Modes, Inter-integrated Circuit Bus(I²C) and its
operations, State Machines for I²C Communication, A Thermometer Using I²C, Asynchronous
Serial Communication, Asynchronous Communication with the USCI_A, A Software UART Using
Timer_A, Other Types of Communication.5 Hrs

2 By T.VenkataSridhar , ETC
Digital Input, Output, and Displays
A microcontroller interacts in many ways with the system in which it is
embedded.

It may receive inputs from a human user through switches, for example. These
are digital in the sense that they are either on or off, high or low.

Similar signals arise from some sensors, such as detectors for the water level or
door lock in a washing machine.

Going in the opposite direction, the microcontroller turns external devices on or


off. These might be indicators, such as simple light-emitting diodes (LEDs) or
more complicated seven-segment displays.

The MSP430 can supply these directly if they work from the same voltage and
draw a sufficiently small current.

Heavier loads require transistors or integrated circuits to drive them.

3 By T.VenkataSridhar , ETC
Digital Input and Output: Parallel Ports
1. The most straightforward form of input and output is through the digital
input/output ports using binary values (low or high, corresponding to 0 or 1).

2. There are 10–80 input/output pins on different devices in the current


portfolio of MSP430s; the F20xx has one complete 8-pin port and 2 pins on a
second port, while the largest devices have ten full ports.

3. Almost all pins can be used either for digital input/output or for other
functions and their operation must be configured when the device starts up.

4. Each pin can be configured and controlled individually; thus some pins can
be digital inputs, some outputs, some used for analog functions, and so on.

Port P1 input, P1IN: reading returns the logical values on the inputs if they are
configured for digital input/output.
This register is read-only and volatile.
It does not need to be initialized because its contents are determined by the
external signals.
4 By T.VenkataSridhar , ETC
Port P1 output, P1OUT: writing sends the value to be driven to each pin if it is
configured as a digital output. If the pin is not currently an output, the value is
stored in a buffer and appears on the pin if it is later switched to be an output. This
register is not initialized and one should therefore write to P1OUT before
configuring the pin for output.

Port P1 direction, P1DIR: clearing a bit to 0 configures a pin as an input, which


is the default in most cases. Writing a 1 switches the pin to become an output.
This is for digital input and output; the register works differently if other functions
are selected using P1SEL.

Port P1 resistor enable, P1REN: setting a bit to 1 activates a pull-up or


pull-down resistor on a pin. The resistors are inactive by default (0). When the
resistor is enabled (1), the corresponding bit of the P1OUT register selects
whether the resistor pulls the input up to VCC (1) or down to VSS (0).

Port P1 selection, P1SEL: selects either digital input/output (0, default) or an


alternative function (1). Further registers may be needed to choose the particular
function.

5 By T.VenkataSridhar , ETC
Port P1 interrupt enable, P1IE: enables interrupts when the value on an input
pin changes. This feature is activated by setting appropriate bits of P1IE to 1.
Interrupts are off (0) by default. The whole port shares a single interrupt vector
although pins can be enabled individually.

Port P1 interrupt edge select, P1IES: can generate interrupts either on a positive
edge (0), when the input goes from low to high, or on a negative edge from high
to low (1).

It is not possible to select interrupts on both edges simultaneously but this is not a
problem because the direction can be reversed after each transition. Care is needed
if the direction is changed while interrupts are enabled because a spurious
interrupt may be generated. This register is not initialized and should therefore be
set up before interrupts are enabled.

Port P1 interrupt flag, P1IFG: a bit is set when the selected transition has been
detected on the input. In addition, an interrupt is requested if it has been enabled.
These bits can also be set by software, which provides a mechanism for
generating a software interrupt (SWI).

6 By T.VenkataSridhar , ETC
Circuit of an Input/Output Pin
It is a lot easier to understand the peculiarities of input and output if you have a
rough idea of the circuit. Figure below shows a basic CMOS inverter. This is much
simpler than a real input/output pin but contains enough to explain the main
features. The inverter itself requires only a complementary pair of
metal–oxide–silicon field-effect transistors (MOSFETs). Almost all very
large-scale integrated (VLSI) circuits are based on MOSFETs. They come in two
“polarities,” n-channel and p-channel, both of which are used in complementary
metal–oxide–silicon (CMOS) technology.

Figure: (a) Circuit of a simple CMOS inverter including the input protection diodes.
(b) Schematic cross-section of an n-channel MOSFET.
7 By T.VenkataSridhar , ETC
If the input is a logical 1, the output should be a logical 0, which needs the switch to
VSS closed and that to VCC open, as in Figure (a). Everything is reversed when the
input is a logical 0, near VSS. This can be achieved by using an n-channel MOSFET
(n-MOSFET for short) for the lower switch and a p-MOSFET for the upper switch,
as shown in Figure (b).

Figure : Operation of a CMOS inverter. (a) Model inverter using switches with a logical
input of 1 and output of 0. (b) Corresponding operation of MOSFETs. (c) Operation when
the input lies near the middle of the supply voltages VSS and VCC rather than close to either
extreme. Both MOSFETs conduct and a large current flows from VCC to VSS.

8 By T.VenkataSridhar , ETC
Configuration of Unused Pins

Not all of the input/output pins are used in most applications.

Unused pins must never be left unconnected in their default state as inputs.

This follows a general rule that inputs to CMOS must never be left unconnected
or “floating.” A surprising number of problems can be caused by floating inputs.

The most trivial is that the input circuit draws an excessive current from the
power supply.

This is because the input is likely to float to the midpoint of VSS and VCC, turning
on both MOSFETs and leading to the situation shown in Figure (c).

The shoot-through current may exceed 40µA, a huge waste by the standards of the
MSP430.

9 By T.VenkataSridhar , ETC
Digital Inputs
Digital inputs to the MSP430 are typically connected to digital outputs from other
circuits or to components such as switches.

Interrupts on Digital Inputs

Ports P1 and P2 can request an interrupt when the value on an input changes.
This is one of the few interrupts that remains active in LPM4 and is therefore
useful to wake the CPU in portable equipment that lies idle for a long time.

Interrupts for port P1 are controlled by the registers P1IE (Interrupt Enable) and
P1IES (Interrupt Enable Select), mentioned previously, and similarly for port P2.

There is a single vector for each port, so the user must check P1IFG (Interrupt
Flag) to determine the bit that caused the interrupt.

This bit must be cleared explicitly; it does not happen automatically as with
interrupts that have a single source.
The direction of the transition that causes the interrupt can be changed in P1IES
at any time by the program.
10 By T.VenkataSridhar , ETC
The use of interrupts is illustrated in Listing below, which is perhaps the ultimate
development of the programs to light an LED when a button is pressed. The device
spends most of its time in LPM4, waiting for an interrupt on pin P2.1. Both the
LED and the direction of the transition for an interrupt are toggled in the ISR. Any
pending requests for an interrupt are cleared by a loop before returning to LPM4.

Listing : Program butled4.c in C to light LED1 when button B1 is pressed using


11
interrupts and low-power mode 4.
By T.VenkataSridhar , ETC
Multiplexed Inputs: Scanning a Matrix Keypad
Many products require numerical input and provide a keypad for the user. These
often have 12 keys, like a telephone, or more.

An individual connection for each switch would use an exorbitant number of pins
so they are usually arranged as a matrix instead. Only seven pins are needed for a
12-key pad, as shown in Figure below, or eight pins for 16 keys.

12 By T.VenkataSridhar , ETC
Another way of reading the keypad uses two steps. It is assumed that only a single
key is pressed.

13 By T.VenkataSridhar , ETC
Switch Debounce
We have just seen that there is no escape from analog electronics when dealing
with signals outside the microcontroller. Unfortunately a switch also involves
mechanical engineering and we cannot avoid a problem called switch bounce.
14 By T.VenkataSridhar , ETC
What Does Switch Bounce Look Like?
In Earlier studies of digital electronics you might have learnt that bounce is worse
when a switch is closed than when it is opened and may last for around 50 ms
(general).

This may be true for toggle switches, the sort with an arm that sticks out, which
one click up and down. These are expensive and now rarely used.

Most switches are push buttons, mounted directly on a PCB. In fact the PCB
forms part of the switch itself in products such as remote controls.

Experimental measurements of the behaviour of a push button on a simple,


demonstration board for the MSP430F20xx.

Figure below shows oscilloscope traces from a few experiments.

The input was the usual active low circuit, a push button between the pin and
ground, using the internal pull-up.

The output was an LED driven active high and the program was a simple loop
that copied the input port to the output port.
15 By T.VenkataSridhar , ETC
Each loop took roughly 0.4s and the latency, the time between reading from the
input and writing to the output, varied from 0.2 to 0.6s depending on when the
input changed in relation to the sequence of instructions in the loop.

Figure : Examples of switch


bounce measured on a
demonstration board for an
MSP430.
The program is a simple loop
that copies the input from an
active low push button to an
active high LED.
Traces (a) and (b) are for
pressing the button while
(c)–(f ) are for releasing it.
All times are in
microseconds but note the
different scales for the time
base. The origin of time is at
the first transition on the
output.

16 By T.VenkataSridhar , ETC
Releasing the button was quite different. Figure (c) shows a clean transition. This
is slow compared with the response when the button is pressed and looks like a
classic, exponential RC charging curve. The reason is that the input pin is not
connected directly to VCC but only through the weak internal pull-up.
Debouncing in Hardware
Debouncing was traditionally done using hardware. The most effective solution is
to use a two-way toggle switch (single pole, double throw, or SPDT) and a
set–reset (SR) flip-flop in the circuit of below Figure (a). Special ICs were
formerly made for this application but are now far more expensive than an
MSP430.

Figure: Two classic circuits for debouncing a switch in hardware: (a) digital approach
using a SR flip-flop and a SPDT switch; (b) analog approach using a RC filter. This
must be followed by a Schmitt trigger as shown, not just a simple CMOS input.
17 By T.VenkataSridhar , ETC
It is good to use an MSP430 with a pair of push buttons for “on” and “off” to
emulate a two-way toggle switch and flip-flop.

The circuit in above Figure (a) uses a standard SR flip-flop, in which case the
rocker of the switch must be connected to VCC and the inputs need pull-down
resistors.

Most switches are connected to ground instead with pull-up resistors.

The flip-flop cannot be used to debounce a simple push button (single pole, single
throw, or SPST switch) so the classic solution comes from analog electronics: an
RC filter.

The circuit is shown in above Figure (b). In a nutshell, the resistors and capacitor
slow down the signal seen by the microcontroller and smooth out any rapid changes
caused by bouncing.

An unavoidable side effect is that the output is slowly varying and must be
connected to a Schmitt trigger, not a standard logic input. Otherwise, the slow
passage of the input signal through the undefined range of voltages gives more
trouble than the original bounce.
18 By T.VenkataSridhar , ETC
Figure : Charge and discharge curves for a simple RC circuit.

The above Figure (a) provides a reminder of the behaviour of an RC circuit, a


capacitor C in series with a resistor R. This has a time-constant τ = RC. Suppose
that the capacitor is initially uncharged and the circuit is connected at t = 0 across a
battery of voltage VCC. The equation for the voltage VC(t) on the capacitor as it
charges is

Similarly, if the capacitor is first charged to VCC, then the resistor and capacitor
are connected in a short circuit at t = 0, the capacitor discharges and the voltage is
given by

19 By T.VenkataSridhar , ETC
Debouncing in Software
In practice debouncing is usually carried out inside the microcontroller. A wide
variety of algorithms is in use, which range from trivial approaches to
sophisticated digital equivalents of the RC filter described in the previous slides.

The simplest method is to detect a transition from the switch, wait for a fixed
delay, and test the input again.

If the input remains the same, it is accepted as a valid new value, otherwise it is
assumed to be an error and ignored. The delay should be longer than the expected
duration of any bounce and 10 ms is a common choice.

20 By T.VenkataSridhar , ETC
Digital Outputs
The standard circuits for connecting an LED to a pin of a microcontroller are
shown in Figure below, which includes the transistors inside the MSP430.
Always include current-limiting resistors in series with the LEDs. Remember also
that LED stands for light-emitting diode and that a diode passes current in only
one direction, shown by the arrow in the symbol. This refers to conventional
current, which flows from positive to negative. No light is produced if the LED is
connected backward.

Figure: Standard connection of an LED to pin P1.0 in (a) active high and (b) active low
configurations. (c) Connection of a bidirectional pair of LEDs between a pair of pins,
21 P1.1 and P1.2. By T.VenkataSridhar , ETC
Further Refer
MSP430 Microcontroller Basics (Elsevier Ltd Publications, Copyright 2008)

By
John H. Davies

www.ti.com/msp430

22
UP
By T.VenkataSridhar , ETC

You might also like