Unit – II
MSP 430 Microcontroller
Syllabus:
MSP430 Family, Features, Architecture, Register Set, Memory Space, Addressing Modes,
Programming GPIO Interfaces in C language- LED, Switches, Motor
MSP - Mixed Signal Processor
Features:
1 It is a 16 bit RISC CPU following the Von – Neumann Architecture
2 Data bit - 16 bit
3 Address bits – 16 bit
4 Addressable Memory - 64KB
5 Timers - One 16 bit Timer [Timer A] & 1 WDT
6 Ultra Low Power - 3.6 V to 1.8 V
7 Clock Frequency - 16 MHz to 25MHz, Works on 3 Different Clocks
8 Low Power Modes - 6 Modes [Active, LPM 0 – LPM 4]
Family of MSP430 processor:
Mastered by Texas Instruments
Major Versions
MSP430 MSP430X
If Flash is More the 64 KB
MSP430Fxxxx series [ F – Flash] > MSP430Cxxxx Series[ C for Code ROM]
> MSP430Gxxx > MSP430Fxxx
Ex: MSP430G2553 MSP430F5529
MSP430
G[ Gain] , 2[Version] , 553[Code]
Note: Some more versions with inbuilt LCD drivers, increased ADC pins are also
available.
Architecture of Basic MSP430 Processor
Pin Diagram of MSP430G2 series
Memory Space
Register Organization
R0/PC – Program Counter R1/ SP -- Stack Pointer
R2/ Status Register
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Reserved V SCG1 SCG0 OSC OFF CPU OFF GIE N Z GC
R2 and R3 - Constant Generator – Helps in addressing time for few instruction operations
like INC/DEC etc.
Timers
1. Timer A
2. Timer B [ for high end versions]
TIMER A: It typically has three channels and is much more versatile than the simpler timers .
Timer A can handle external inputs and outputs directly to measure frequency, time-stamp
inputs, and drive outputs at precisely specified times, either once or periodically. There
are internal connections to other modules so that it can measure the duration of a signal
from the comparator.
It can also generate interrupts
Watchdog Timer
Protect the system against failure of the software, such as the program becoming
trapped in an un-intended, infinite loop.
The watchdog counts up and resets the MSP430 when it reaches its limit.
The operation of the watchdog is controlled by the 16-bit register WDTCTL.
The watchdog is always active after the MSP430 has been reset.
The default period of the watchdog is around 32 ms.
WDTCTL = WDTPW + WDTHOLD
Clocks and Low Power Modes
MSP430 Clock Options
Typical
Name Description Used-by
Speed
Master
❑ MCLK CPU Fast
Clock
❑ SMCLK Sub-Master Peripherals Fast
[approx 1MHz] Clock [ADC, Timers, LCD, GPIO]
Auxiliary Peripherals
❑ ACLK Slow
Clock [RTC, Timers, LCD, Serial]
•Main Clock (MCLK) : CPU source that may be driven by the internal Digitally Controlled
Oscillator (DCO) up to 25 MHz or with external crystal.
•Sub-Main Clock (SMCLK) : Source for faster individual peripheral modules that may be
driven by the internal DCO up to 25 MHz or with external crystal
•Auxiliary Clock (ACLK) : Source for individual peripheral modules driven by the internal
low-power oscillator or external crystal
LOW POWER MODES
Low Power Modes (LPMs) represents the ability to scale the microcontroller’s power usage by
shutting off parts of the MSP430.
The CPU and several other modules such as clocks are not always needed. Many applications
which wait until a sensor detects a certain event.
It can benefit from turning off unused (but still running) parts of the microcontroller to
save energy, turning them back on quickly when needed.
WDT Timer RTC Serial
Clock
ACLK
SMCLK
MCLK
CPU ADC GPIO LCD
Power modes
•Active Mode : Nothing is turned off (except maybe individual peripheral modules).
No power savings.
•LPM0 :CPU and MCLK are disabled while SMCLK and ACLK remain active.
•LPM1 : CPU and MCLK are disabled, and DCO and DC generator are disabled
if the DCO is not used for SMCLK. ACLK is active.
•LPM2 : CPU, MCLK, SMCLK, DCO are is enabled DC generator remains enabled.
ACLK is active
•LPM3 :CPU, MCLK, SMCLK, DCO are disabled, DC generator is disabled,
ACLK is active
• LPM4:CPU is disabled, ACLK is disabled, MCLK and SMCLK are disabled
DCO’s dc-generator is disabled Crystal oscillator is stopped
Programming Model
Instruction Format
OPCODE Source Operand, Destination Operand
Ex: MOV R10, R11 ;; R10 contents are transferred to R11
OR
MOV.W R10, R11 ;; R10 contents are transferred to R11
Addressing Modes
Mode Example Notes
immediate constant 45h to the
Immediate MOV #45h, R5
destination (register R5).
contents of source (register R4) to
Register MOV R4, R5
destination (register R5).
source address (contents of R4) to the
Indirect Register MOV @(R4), R5
destination (register R5).
source address (contents of R4) to the
Indirect Auto destination (register R5), then
MOV @R4+, R5
increment increment the value in register R4 to
point to the next word.
source address (F000h +R5) to
Indexed MOV F000h(R5), R4
destination (register R4)
source address XPT to the destination
Absolute MOV &XPT, &YPT
address YPT.
source address XPT (x pointer) to the
Symbolic MOV XPT, YPT
destination address YPT (y pointer).
INSTRUCTION SET
Transfer Instructions
Arithmetic Instructions
Logical Instructions
Shift and Rotate Instructions
Status or Bit wise operation Instructions
Flow or Branch Control Instructions
PORT PROGRAMMING USING C
P1.7 P1.6 P1.5 P1.4 P1.3 P1.2 P1.1 P1.0
I/O Port 1 Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0
PxDIR (Pin Direction): Input or Output
P1IN.7
P1OUT.7
P1DIR.7
“1”
7 6 5 4 3 2 1 0
P1IN
P1OUT
P1DIR 1
◆ PxDIR.y: 0 = input◆ Register example:
1 = output P1DIR |= 0x80;
GPIO Output
P1IN.7
“1”
P1OUT.7
“1”
P1DIR.7
“1”
7 6 5 4 3 2 1 0
P1IN x
P1OUT 1
P1DIR 1
◆ PxOUT.y: 0 = low ◆ Register example:
1 = high P1OUT |= 0x80;
Each bit in each PxOUT register is the value to be output on the
corresponding I/O pin when the pin is configured as I/O function, output
direction.
Program an LED
Interface an LED to P1.0 pin of the controller and
Toggle [ ON and OFF] it continuously
P1.0 OPERETES THE LED ,
P1.0 SHOULD BE OUTPUT
1ST STEP, GOTO DIR REG , MAKE P1.0 AS 1
P1DIR = 0x 01 ; 0000 000 1 P1.0
P1OUT = 0x 01 ; 0000 000 1 LED IS ON
P1OUT = 0x 00 ; 0000 000 0 LED IS OFF
Programming a Simple LED
#include "msp430.h" or #include <msp430.h>
void main(void) OR MAIN () OR VOID MAIN ()
{
unsigned int i; // integer i
P1DIR |= 0x01; // Set P1.0 to output direction
}
for (i = 50000; i != 0; i--)
{
P1OUT ˆ= 0x01; // Toggle P1.0 using exclusive-OR
// with Delay
// ^ SYMBOL IS ALTER THE BIT
}
}
Try some more examples:
Use the Port Programming for the pins as P1.0 - LED1 P1.6 - LED2
(i) ON and OFF the LEDs simultaneously (II) Glow the LEDs alternatively
#INCLUDE <MSP430.H>
VOID MAIN()
{
P1DIR = 0x 41 ; // 0 1 00 000 1 = 41 H // MAKE P1.0 AND P1.6 AS OUTPUT PINS
WHILE(1) // AT SOME INSTANT AN INFINTE LOOP STARTS
{
P1OUT = 0x 41 ; // AT A TIME ON AND OFF THE LEDs CONNECTED TO THE TWO PINS
_DELAY_CYCLES(1000) ; GENERATE A DELAY OF 1 SEC = 1000 milliseconds
P1OUT = 0x00 ;
_DELAY_CYCLES(1000) ;
}
}
#INCLUDE <MSP430.H>
VOID MAIN()
{
P1DIR = 0x 41 ; // 0 1 00 000 1 = 41 H // MAKE P1.0 AND P1.6 AS OUTPUT PINS
WHILE(1) // AT SOME INSTANT AN INFINTE LOOP STARTS
{
P1OUT = 0x 41 ; // AT A TIME ON AND OFF THE LEDs CONNECTED TO THE TWO PINS
_DELAY_CYCLES(1000) ; GENERATE A DELAY OF 1 SEC = 1000 milliseconds
P1OUT = 0x00 ;
_DELAY_CYCLES(1000) ;
}
}
#INCLUDE <MSP430.H>
VOID MAIN()
{
WDTCTL = WDTPWD + WDTHOLD; // CLEAR OR STOP WATCH DOG TIMER
P1DIR = 0x 41 ; // 0 1 00 000 1 = 41 H // MAKE P1.0 AND P1.6 AS OUTPUT PINS
WHILE(1) // AT SOME INSTANT AN INFINTE LOOP STARTS
{
P1OUT ^= 0x 41 ; // AT A TIME ON AND OFF THE LEDs CONNECTED TO THE TWO PINS
_DELAY_CYCLES(1000) ; GENERATE A DELAY OF 1 SEC = 1000 milliseconds
}
}
GLOW THE TWO LEDs THIS TIME
ALTERNATIVELY
#INCLUDE <MSP430.H>
VOID MAIN()
{
P1DIR = 0x 41 ; // 0 1 00 000 1 = 41 H // MAKE P1.0 AND P1.6 AS OUTPUT PINS
WHILE(1) // AT SOME INSTANT AN INFINTE LOOP STARTS
{
P1OUT = 0x 01 ; // P1.0 IS ON & P1.6 IS OFF ; 0 0 00 000 1
_DELAY_CYCLES(1000) ; GENERATE A DELAY OF 1 SEC = 1000 milliseconds
P1OUT = 0x40 ; //P1.0 IS OFF & P1.6 IS ON ; 0 1 00 000 0
_DELAY_CYCLES(1000) ;
}
}
GLOWING PATTERN OF LEDs 1. ALL ON, 2. ALTERNATIVELY ON, 3. 4 ON 4 OFF, 4. AAL ARE
OFF
#INCLUDE <MSP430.H>
VOID MAIN ()
{
WDTCTL = WDTPWD + WDTHOLD; // CLEAR OR STOP WATCH DOG TIMER
P1DIR = 0x FF ; // MAKE ALL PINS OF PORT 1 AS OUTPUT
WHILE (1)
{
P1OUT = 0x FF ; // ALL LEDs ARE ON
_DELAY_CYCLES(1000) ; GENERATE A DELAY OF 1 SEC = 1000 milliseconds
P1OUT = 0x AA ; // ALL LEDs ALTERNATE 10101010
_DELAY_CYCLES(1000) ; GENERATE A DELAY OF 1 SEC = 1000 milliseconds
P1OUT = 0x 0F ; // LEDs 00001111
_DELAY_CYCLES(1000) ; GENERATE A DELAY OF 1 SEC = 1000 milliseconds
P1OUT = 0x 00 ; // ALL LEDs ARE OFF
_DELAY_CYCLES(1000) ; GENERATE A DELAY OF 1 SEC = 1000 milliseconds
}
}
#INCLUDE <MSP430.H>
VOID MAIN ()
{
P1DIR = 0x FF ; // MAKE ALL PINS OF PORT 1 AS OUTPUT
P2REN = 0x 01 ; //MAKE P2.0 OF PORT2 AS INPUT PIN 0000 000 1
WDTCTL = WDTPWD + WDTHOLD; // CLEAR OR STOP WATCH DOG TIMER
IF (P2IN && BIT0 == 1)
{
P1OUT = 0x FF ; // ALL LEDs ARE ON
_DELAY_CYCLES(1000) ; GENERATE A DELAY OF 1 SEC = 1000 milliseconds
P1OUT = 0x AA ; // ALL LEDs ALTERNATE 10101010
_DELAY_CYCLES(1000) ; GENERATE A DELAY OF 1 SEC = 1000 milliseconds
P1OUT = 0x 0F ; // LEDs 00001111
_DELAY_CYCLES(1000) ; GENERATE A DELAY OF 1 SEC = 1000 milliseconds
}
ELSE
{
P1OUT = 0x 00 ; // ALL LEDs ARE OFF
_DELAY_CYCLES(1000) ; GENERATE A DELAY OF 1 SEC = 1000 milliseconds
}