You are on page 1of 528

Microcontroller

MSP430

Asha.S.Patil
Department of Electrical & Electronics Engineering
MICROCONTROLLER

MSP430 – Basic concept

Asha.S.Patil
Department of Electrical&Electronics Engineering
MICROCONTROLLER
Basic Concept

What is Microcontroller?

8-bit,16-bit,32-bit

MSP430:

16 bit microcontroller
MICROCONTROLLER
The Family

Broad family of TI’s 16-bit Microcontrollers


 From 1Kbytes ROM,128 bytes RAM
To 60Kbytes ROM,10Kbytes RAM

Three Subfamilies
 MSP430X1XX:basic unit
 MSP430X2XX:Added features
 MSP430X3XX:more features
 MSP430X4XX:built-in LCD driver
MICROCONTROLLER
Introduction to MSP430

Features of MSP430:
 Low supply-voltage Range:1.8 V to 3.6 V
 3 Clock signals-MCLK,SMCLK,ACLK
 16-bit RISC Architecture
51 instructions
16 bit Registers
7 addressing modes
 Two 16-Bit Timer
MICROCONTROLLER
Introduction to MSP430

MSP430G2553

MSP-Mixed Signal Processing


430-MCU Platform
G-Flash Memory
2-2nd Generation(16 MHz )
5-Amount Of Memory(512 RAM and 16K ROM)
5-Low Power Mode(LPM0-LPM5)

3-minor model Variant


MICROCONTROLLER
MSP430G2553 Series

MSP430G2553 Variant

 16 MHz MCU with


 16KB FLASH
 512B SRAM
 10-bit ADC
 Comparator
 UART/SPI/I2C
 Timer
THANK YOU

Asha.S.Patil
ashaspatil@pes.edu
Department of Electrical & Electronics Engineering
Microcontroller
MSP430

Asha.S.Patil
Department of Electrical & Electronics Engineering
MICROCONTROLLER

MSP430 Architecture

Asha.S.Patil
Department of Electrical&Electronics Engineering
MICROCONTROLLER
Anatomy of a typical small MC

Out side world

Program Memory Data memory Input and Output


(flash ROM) Memory (RAM) Ports

Data bus
Address bus

Central Processing
Unit (CPU) Clock
MICROCONTROLLER
Central Processing Unit(MSP430)

1. Arithmetic Logic Unit

2. Registers
15 bits 0
R0/PC Program Counter
R1/SP Stack Pointer
R2/SR/CG1 Status Register
R3/CG2 Constant Generator
R4 General Purpose

R15 General Purpose


MICROCONTROLLER
Registers

1.Program Counter: R0

2.Stack Pointer:R1

PUSH: decrements Sp by 2

POP: increments Sp by 2
MICROCONTROLLER
Registers

3.Status Register(SR):

15 9 8 7 0
Reserved V SCG1 SCG0 OSC CPU GIE N Z C
OFF OFF
MICROCONTROLLER
Registers

4.Constant Generator(CG):

Register As Constant Remarks

R2 00 ---- Register Mode


R2 01 (0) Absolute address mode
R2 10 00004h +4,bit processing
R2 11 00008h +8,bit processing
R3 00 00000h 0,word processing
R3 01 00001h +1
R3 10 00002h +2,bit processing
R3 11 0FFFFh -1,word processing
MICROCONTROLLER
Registers

5.General Purpose Registers:

R4-R15
THANK YOU
Asha.S.Patil
Department of Electrical & Electronics Engineering
ashaspatil@pes.edu
Microcontroller
MSP430
MSP430 Architecture
Asha.S.Patil
Department of Electrical & Electronics Engineering
MICROCONTROLLER

MSP430 –The inside view

Asha.S.Patil
Department of Electrical&Electronics Engineering
MICROCONTROLLER
Registers

Status Register(SR):

15 9 8 7 0
Reserved V SCG1 SCG0 OSC CPU GIE N Z C
OFF OFF
MICROCONTROLLER
Registers

bit C : 0x75 + 0xC7 = 0x13C,

The zero flag Z: is set when the result of an operation is 0.

The negative flag N: is made equal to the msb of the result,


which indicates a negative number if the values are signed.

The signed overflow flag V: is set when the result


of a signed operation has overflowed,
even though a carry may not be generated
MICROCONTROLLER
Memory

Memory:

ROM: Flash ROM,EEPROM

RAM: SRAM,DRAM
MICROCONTROLLER
ORDERING OF BITS, BYTES AND WORDS IN MEMORY
MICROCONTROLLER
Ordering in Memory

1. Little-Endian Ordering

2. Big-Endian Ordering
MICROCONTROLLER
MEMORY MAP OF MSP430G2553
THANK YOU
Asha.S.Patil
Department of Electrical & Electronics Engineering
ashaspatil@pes.edu
Microcontroller
MSP430
Asha.S.Patil
Department of Electrical & Electronics Engineering
MICROCONTROLLER

MSP430 –The Outside View

Asha.S.Patil
Department of Electrical&Electronics Engineering
MICROCONTROLLER
The Outside View—Pin-Out MSP430G2553
MICROCONTROLLER
GENERAL PURPOSE INPUT AND OUTPUT

Controlled by memory mapped registers:


 IN
 OUT
 DIR
 REN
SEL
IE
MICROCONTROLLER
PXDIR (PIN DIRECTION): INPUT OR OUTPUT
MICROCONTROLLER
GPIO OUTPUT
MICROCONTROLLER
GPIO INPUT

Problem with Input Using a Button


• When the button is pressed down (closed),
MSP430 will detect a 0 from the pin.
• When the button is up (open), what will
MSP430 detect?
Ans.: random value

Floating
MICROCONTROLLER
GPIO INPUT

PxREN enables resistors


PxOUT selects pull-up (1) or -down (0)
MICROCONTROLLER
GPIO INPUT

Typical Way of Connecting a Button


• MSP430 offers internal pull-up/down

• PxREN register selects whether this resistor is used


(1 to enable, 0 to disable)
MICROCONTROLLER
GPIO PSEL

PxSEL2 PxSEL Pin Function


0 0 GPIO(Digital I/O)Function
0 1 Primary Peripheral Function
1 0 Reserved
1 1 Secondary Peripheral Function
THANK YOU

Asha.S.Patil
Department of Electrical & Electronics Engineering
ashaspatil@pes.edu
Microcontroller
MSP430

Asha.S.Patil
Department of Electrical & Electronics Engineering
MICROCONTROLLER

MSP430 –Clock System

Asha.S.Patil
Department of Electrical&Electronics Engineering
MICROCONTROLLER
Block Diagram Of Clock Module
MICROCONTROLLER
ClOCK SYSTEM

Master clock: MCLK is used by the CPU and a few peripherals.

Sub-system master clock: SMCLK is distributed to peripherals.

Auxiliary clock: ACLK is also distributed to peripherals.


MICROCONTROLLER
Low Power Mode

Low Power mode of Operation:

i) Active mode: I ≈ 300A.

ii) LOW Power Mode


MICROCONTROLLER
Registers

Low Power Mode:


i)LPM0:Both CPU and MCLK are disabled.SMCLK and ACLK are
active
ii)LPM1:Both CPU and MCLK are disabled.DCO is disabled and
ACLK remains active
iii)LPM2:CPU,MCLK,SMCLK,DCO are disabled. ACLK remain active
iv)LPM3:CPU,MCLK,SMCLK, DCO are disabled DC generator is
enabled and ACLK remain active
v)LPM4:All clocks are inactive.
MICROCONTROLLER
Power Consumption in Various Mode
THANK YOU
Asha.S.Patil
Department of Electrical & Electronics Engineering
ashaspatil@pes.edu
Microcontroller
MSP430

Asha.S.Patil
Department of Electrical & Electronics Engineering
MICROCONTROLLER

MSP430 –IDE

Asha.S.Patil
Department of Electrical&Electronics Engineering
MICROCONTROLLER
Development Environment

 Editor

 Assembler 0r Compiler

 Linker

 Stand –alone simulator


MICROCONTROLLER
Development Environment

 Embedded emulator/debugger

 In circuit emulator

 Flash programmer
MICROCONTROLLER
Development Environment

 A good choice of development systems for the MSP430

is listed on TI’s Tools and Software page.

 Two are available free from TI themselves:

- CCS

-IAR
MICROCONTROLLER
The C Programming language

 Declarations:

const and volatile

while (P1IN == OldP1IN) {

}
MICROCONTROLLER

The C Programming language

Shifts:
Left shift: value <<= 1
Right shift: arithmetic and logical shifts
Rotation
MICROCONTROLLER
The C Programming language

Low level logical Operation:

Boolean logic operations :e.g. if (A && B)

bitwise : e.g. A = 10101010 and B =11001100

A&B=10001000

e.g.2 TestP13 = P1IN & BIT3


MICROCONTROLLER
The C Programming language

 Masks to Modify Individual Bits: Done using OR,

x|0=x and x|1=1

 Clearing a bit (to 0) is done using AND,

x&0=0 and x&1=x

 bits can be toggled (changed from 0 to 1 or 1 to 0) using


the exclusive-or operation.

P1OUT ˆ= BIT3 toggles P1OUT.3.


MICROCONTROLLER
The C Programming language

Unions:

TACTL = MC_2 | ID_3 | TASSEL_2 | TACLR;


MICROCONTROLLER
General Framework of a C program for MSP430

/* Code you write in this class will need this include */

#include “msp430.h” //

#include <stdlib.h> //

#define MAX_ITER 10

int someFunction(char opt, unsigned int scale);

int exitFlag = 0; const float pi = 3.14156;


MICROCONTROLLER
General Framework of a C program for MSP430

void main()
{
char choice;
int test_num,
stuff; // ...
stuff = someFunction('a',test_num);
// ...
}
THANK YOU
Asha.S.Patil
Department of Electrical & Electronics Engineering
ashaspatil@pes.edu
Microcontroller
MSP430

Asha.S.Patil
Department of Electrical & Electronics Engineering
MICROCONTROLLER

MSP430 Assembly Language

Asha.S.Patil
Department of Electrical&Electronics Engineering
MICROCONTROLLER
Assembly Language

Why Assembly language?

High Level Language Vs.Assembly


MICROCONTROLLER
Layout of Assembly Language

 RESET: mov.w #WDTPW| WDTHOLD ,& WDTCTL ; Stop watchdog timer

label: operation operands ; Comment


MICROCONTROLLER
Example instructions of Assembly language

 MOV.w #0x08,R5

 AND.w #0x00,R5

 BIC.W #0x03, R5

 BIS.W #0X03,R4

 CMP.W R5,R6
THANK YOU
Asha.S.Patil
Department of Electrical & Electronics Engineering
ashaspatil@pes.edu
Microcontroller
MSP430

Asha.S.Patil
Department of Electrical & Electronics Engineering
MICROCONTROLLER

MSP430 –Exceptions in MSP430 and Watch dog timer

Asha.S.Patil
Department of Electrical&Electronics Engineering
MICROCONTROLLER
Exceptions

Classes of Exceptions:

Interrupts:
Generated by Hardware

Resets: again generated by hardware


MICROCONTROLLER
Exceptions

Classes of Exceptions and ISR

Interrupts and

Resets
MICROCONTROLLER
Classification of interrupts

The MSP430 generally categorizes interrupts as follows:

 System Reset Interrupts


 Non Maskable Interrupts
 Maskable Interrupts
MICROCONTROLLER
What happens when the regular program is interrupted?

The following steps are executed whenever an interrupt is received:


Currently executing instruction is completed.
The Program Counter/PC (it points to the next instruction) is pushed onto the
stack.
The Status Register (SR) is pushed onto the stack next.
The highest priority interrupt is selected to be serviced.
The interrupt request flag resets automatically on single source flags. Multiple
source flags (like GPIO interrupt which can be sourced from any of the 8 pins of
the port) remain set for servicing by software.
The status register (SR) is cleared. This terminates any low-power mode.
Further interrupts are disabled (**to avoid nested interrupts) by clearing GIE
(global interrupt enable).
The content of interrupt vector is loaded into Program Counter (PC), the
program continues with interrupt service routine (ISR) at that address.
MICROCONTROLLER
Watch Dog Timer(WDT+) of MSP430

WDT Function:

to reset the MC
MICROCONTROLLER
WDT+

 In MSP430x2xxx devices the watch dog timer called


as WDT+ .
 WDT+ is a 16 bit upcounter
MICROCONTROLLER
WDT+

WDTCTL = WDTPW + WDTHOLD


WDTCTL |= WDTHOLD; //Will reset MSP430,WDTIFG flag is set
THANK YOU
Asha.S.Patil
Department of Electrical & Electronics Engineering
ashaspatil@pes.edu
Microcontroller
MSP430

Asha.S.Patil
Department of Electrical & Electronics Engineering
MICROCONTROLLER

Getting started with MSP 430

Asha.S.Patil
Department of Electrical&Electronics Engineering
MICROCONTROLLER
Installation of IDE

CC studio V9: http://www.ti.com/tool/CCSTUDIO-MSP


IAR Workbench:
https://www.iar.com/iar-embedded-workbench/#!?architecture=MSP430

CC studio V9 does not support Simulator and always needs the boards to work with.
IAR Workbench V7.12 does support Simulator and can support
the working of code without the boards.
MICROCONTROLLER
Development Environment

Create CCS C Project, Debug and View


Create a new CCS Project
Project->New CCS Project
Select target -> MSP430G2553
Name of the project
Empty Project (with main)
MICROCONTROLLER
Development Environment
MICROCONTROLLER
Development Environment

You should a window as below:


MICROCONTROLLER

The C Programming language


include<msp430.h>

/**
* main.c
*/
intmain(void)
{
WDTCTL = WDTPW | WDTHOLD; // stop watchdog timer
P1DIR |= 0x01; // configure P1.0 as output

P1OUT = 0x00; // Initial state for LED

Volatile unsigned int i; // volatile to prevent optimization

while(1)
{
P1OUT ^= 0x01; // toggle P1.0
for(i=10000; i>0; i--)
; // delay

}
return 0;
}
MICROCONTROLLER
Development Environment

Click on Project-> Build Project


Click on Run->Load that will enable the hex file to be
downloaded to board but without any debug
Run->Debug will enable the hex file to be
downloaded to board and also debug the project.
After download to board or Simulator explore the
following in debug mode:
View disassembly, registers, memory in debug mode
Explore and understand single stepping,
breakpoints, step over, etc
MICROCONTROLLER
Development Environment

You should a window as below:


MICROCONTROLLER
Assembly language code
THANK YOU
Asha.S.Patil
Department of Electrical & Electronics Engineering
ashaspatil@pes.edu
Microcontroller
MSP430

Asha.S.Patil
Department of Electrical & Electronics Engineering
MICROCONTROLLER

A Simple Tour of the MSP430

Asha.S.Patil
Department of Electrical&Electronics Engineering
MICROCONTROLLER
Development Board
MICROCONTROLLER
Features of MSP430G2553

 Low supply voltage range: 1.8 V to 3.6 V


 Low-power consumption:• 220 A / MHz active mode
• 0.7 A real-time clock mode
• 0.1 A RAM retention
 16-bit RISC Architecture
 Internal frequencies up to 16 MHz with four calibrated frequency
 Serial communication modules
• USART • USI
 Memory
• Up to 56 KB Flash • Up to 4 KB SRAM
MICROCONTROLLER
Functional Block Diagram of MSP430G2553

Courtesy:www.ti.com
MICROCONTROLLER
The pin diagram of MSP430G2553
THANK YOU
Asha.S.Patil
Department of Electrical & Electronics Engineering
ashaspatil@pes.edu
Microcontroller
MSP430

Asha.S.Patil
Department of Electrical & Electronics Engineering
MICROCONTROLLER

Beginning Programming for MSP430

Asha.S.Patil
Department of Electrical&Electronics Engineering
MICROCONTROLLER
Basic code example1

#include "msp430x22x4.h"

volatile unsigned int i; // volatile to prevent optimization

void main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
P1DIR |= 0x01; // Set P1.0 to output direction

for (;;)
{
P1OUT ˆ= 0x01; // Toggle P1.0 using exclusive-OR
i = 50000; // Delay
do (i--);
while (i != 0);
}
}
MICROCONTROLLER
Basic code example2

1.LED BLINKING:
Note:for delay :_delay_cycles(2500);
#include <msp430g2553.h>

int main(void)
{

WDTCTL = WDTPW | WDTHOLD; // stop watchdog timer

P1DIR =0X01; //toggle the bit


P1OUT=0X01;
unsigned int i;
while(1)
{
P1OUT ^=0X01; //toggle the bits
for(i=0;i<20000;i++)
{
}
}

return(0);
}
MICROCONTROLLER
Basic code example2
1.LED BLINKING:.To turn on LED using assembly language programming
;;-------------------------------------------------------------------------------
.cdecls C,LIST,"msp430.h" ; Include device header file

;-------------------------------------------------------------------------------
.def RESET ; Export program entry-point to
; make it known to linker.
;-------------------------------------------------------------------------------
.text ; Assemble into program memory.
.retain ; Override ELF conditional linking
; and retain current section.
.retainrefs ; And retain any sections that have
; references to current section.

;-------------------------------------------------------------------------------
RESET mov.w #__STACK_END,SP ; Initialize stackpointer
StopWDT mov.w #WDTPW|WDTHOLD,&WDTCTL ; Stop watchdog timer
;-------------------------------------------------------------------------------
; Main loop here
;------------------------------------------------------------------------------

-------------------------------------------------------------------------------
; Stack Pointer definition
;-------------------------------------------------------------------------------
.global __STACK_END
.sect .stack

;-------------------------------------------------------------------------------
; Interrupt Vectors
;-------------------------------------------------------------------------------
.sect ".reset" ; MSP430 RESET Vector
.short RESET
THANK YOU
Asha.S.Patil
Department of Electrical & Electronics Engineering
ashaspatil@pes.edu
Microcontroller
MSP430

Asha.S.Patil
Department of Electrical & Electronics Engineering
MICROCONTROLLER

MSP430 – Addressing Modes

Asha.S.Patil
Department of Electrical&Electronics Engineering
MICROCONTROLLER
Addressing MODES

 What is Addressing mode?

 The MSP430 has

7 Addressing Modes

4 basic modes for the source

2 for the destination


MICROCONTROLLER
Instruction format

There are three formats of instruction.

 Double operand (Format I)

 Single operand (Format II)

 Jumps
MICROCONTROLLER
Instruction format

Double operand (Format I):

e.g., ADD(.B or .W) src,dst

SUBC(.B or .W) src,dst


MICROCONTROLLER
Instruction format

Single operand (Format II):

e.g., RRA(.B or .W) dst

PUSH(.B or .W) src


MICROCONTROLLER
Instruction format

There are three formats of instruction.

 Double operand (Format I)

 Single operand (Format II)

 Jumps
MICROCONTROLLER
Instruction format

Double operand (Format I):

e.g., ADD(.B or .W) src,dst

SUBC(.B or .W) src,dst


MICROCONTROLLER
Instruction format

Single operand (Format II):

e.g., RRA(.B or .W) dst

PUSH(.B or .W) src


MICROCONTROLLER
Instruction format

Jump:

e.g., JC Label

JNC Label

RET
MICROCONTROLLER
Types of Addressing Mode-MSP430

1.Register Mode: Applicable both for source and

destination

e.g., MOV.W R5,R6

Before After
Execution Execution

R5 1234H R5 1234H
R6 ABCDH R6 1234H
PC 8000H PC 8002H
MICROCONTROLLER
Types of Addressing Mode-MSP430

2.Indexed Addressing Mode: Applicable both for

source and destination e.g., MOV.W 2(R5),6(R6)

Initially If

R5=1002H;2(R5)=1002H+0002H=1004H

R6=A0B2;6(R6)=A0B2H+0006H=A0B8H

Content from 1004H will be moved to A0B8H


MICROCONTROLLER
Types of Addressing Mode-MSP430

Before Execution After Execution

PC Reg Content
PC Reg Content
1004H 1234h 1004H 1234h
1002H xxxxx 1002H xxxxx
FF12H xxxxx FF12H xxxxx

PC Reg Content PC Reg Content

A0BAH xxxx A0BAH xxxx

A0B8H 5555H A0B8H 1234H

A0B6H xxxxx A0B6H xxxxx


THANK YOU
Asha.S.Patil
Department of Electrical & Electronics Engineering
ashaspatil@pes.edu
Microcontroller
MSP430
Asha.S.Patil
Department of Electrical & Electronics Engineering
MICROCONTROLLER

MSP430 – Addressing Modes

Asha.S.Patil
Department of Electrical&Electronics Engineering
MICROCONTROLLER
Types of Addressing Mode-MSP430

3.Symbolic Mode: Applicable both for source and

destination

e.g., mov .w LoopCtr , R6

mov .w X ( PC ) , R6

where X = LoopCtr − PC is the offset that needs to be

added to PC to get the address of LoopCtr


MICROCONTROLLER
Types of Addressing Mode-MSP430

Symbolic Mode: Applicable both for source and

destination

e.g., mov .w LoopCtr , R6

mov .w X ( PC ) , R6

where X = LoopCtr − PC is the offset that needs to be

added to PC to get the address of LoopCtr


MICROCONTROLLER
Types of Addressing Mode-MSP430
MOV.W L1,L2
If L1=5678H,L2=2468H
L1-PC=5678-FF14H=5764(X)+FF14(PC)=5678(L1)
L2-PC=2468H-FF16H=2552H(X)+FF16(PC)=2468H(L1)
Before Execution After Execution
PC Reg Content PC Reg Content

FF16H 2552H(Y) FF16H 2552H

FF14H 5674h(X) FF14H 5764H(Y)

FF12H 4080H(PC) FF12H 4080H

PC Reg Content PC Reg Content


567AH xxxxx 567AH xxxx
5678H 1234h(L1) 5678H 1234H(L1)
5676H xxxxx 5676H xxxxx

PC Reg Content PC Reg Content


246AH xxxx 246AH xxxx
2468H 5555H(L2) 2468H 1234H(L2)
2466H xxxxx 2466H xxxxx
MICROCONTROLLER
Types of Addressing Mode-MSP430

4.Absolute Mode: Applicable both for source and

destination

e.g., Mov .w &L1,&L2 same as

Mov.w 0(L1),0(L2)

Mov .b & P1IN , R6


MICROCONTROLLER
Types of Addressing Mode-MSP430

5.Indirect Register Mode: Applicable only for source

e.g., mov .w @R10, 0(R11)

Before Execution After Execution


R10=5002H,R11=9002H R10=5002H,R11=9002H

PC Reg Content PC Reg Content


50004H xxxx 50004H xxxx
50002H 1234H 50002H 1234H
50000H xxxx 50000H xxxx

PC Reg Content PC Reg Content


90004H xxxx 9004H xxxx
90002H 5555H 9002H 1234H
90000H xxxx 9000H xxxx
MICROCONTROLLER
Types of Addressing Mode-MSP430

6.Indirect Autoincrement Mode: Applicable only for

source

e.g., Mov .w @R10+, 0(R11)

R10 is incremented by 1 if byte is fetched

R10 is incremented by 2 if word is fetched


MICROCONTROLLER
Types of Addressing Mode-MSP430

7.Immediate Addressing Mode: Applicable only for

source

e.g., Mov .w #5000H, R11

Mov #10,R5
MICROCONTROLLER
Types of Addressing Mode-MSP430

8.SP-Relative Mode: Applicable for source and

destination

e.g., Mov.w 2( SP ) , R6
Asha.S.Patil
Department of Electrical & Electronics Engineering

ashaspatil@pes.edu

THANK YOU
Microcontroller
MSP430
Asha.S.Patil
Department of Electrical & Electronics Engineering
MICROCONTROLLER

MSP430 – Instruction Set

Asha.S.Patil
Department of Electrical&Electronics Engineering
MICROCONTROLLER
MSP430 INSTRUCTION SET

27 Core and 24 emulated instruction

27 Core
-Double Operand Instructions(12)
-Single Operand Instructions(7)
-Jump Instructions(8)
MICROCONTROLLER
DOUBLE OPERAND INSTRUCTIONS

The source and destination of an instruction are defined by the


following fields:
ƒsrc :As
S-reg The working register used for the source (src)
ƒ
 dst :Ad
D-reg - The working register used for the destination (dst)

 B/W Byte or word operation:


0: word operation
1: byte operation

ƒ
MICROCONTROLLER
Double operand instruction format

15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0

OPCODE S-REG Ad B/W As D-REG

Mnemonic S-REG Operation V N Z C


D-REG
MOV(.B) Src,dst Src dst - - - -
ADD(.B) Src,dst Src +dst dst * * * *
ADDC(.B) Src,dst Src +dst+c dst * * * *
SUB(.B) Src,dst dst+not.src+1 dst * * * *
SUBC(.B) Src,dst dst+not.src+C dst * * * *
CMP(.B) Src,dst dst-src * * * *
DADD(.B) Src,dst Src +dst+c dst * * * *
(decimally)
MICROCONTROLLER
Double operand instruction format

15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0

OPCODE S-REG Ad B/W As D-REG

Mnemonic S-REG Operation V N Z C


D-REG
BIT(.B) Src,dst Src and dst 0 * * *
BIC(.B) Src,dst Not src and dst dst - - - -
BIS(.B) Src,dst Src or dst dst - - - -
XOR(.B) Src,dst Src xor dst dst * * * *
AND(.B) Src,dst Src and dst dst 0 * * *
MICROCONTROLLER
Example :Double operand instruction format

-Assembly: MOV.W R5,R4


-Instruction Code:0X4504
OPCODE S-REG Ad B/W As D-REG
R5 R4

0100 0101 0 0 00 0100

-Assembly: MOV.W R5,Label


-Instruction Code:0X4580
OPCODE S-REG Ad B/W As D-REG
R5 R4

0100 0101 1 0 00 0100


MICROCONTROLLER
Single OPERAND INSTRUCTIONS

15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0

ƒOPCODE B/W Ad D/S-Reg


Mnemonic S-REG Operation V N Z C
D-REG
RRC(.B) dst C MSB ……..LSB C * * * *
RRA(.B) dst MSB MSB …LSB C 0 - - -
PUSH(.B) Src SP-2 SP, src @SP - - - -
SWPB dst Swap bytes - - - -
CALL dst SP-2 SP, PC+2 @SP - - - -
Dst PC

RETI TOS SR,SP+2 SP * * * *


TOS PC,SP+2 SP
SXT dst Bit7 Bit 8………..bit 15 0 * * *
MICROCONTROLLER
Example:Single OPERAND INSTRUCTIONS

-Assembly: rrc.w r5
-Instruction Code: 0x1005

OPCODE-rrc B/W Ad D/S-Reg


ƒ000100000 0 00 0101
MICROCONTROLLER
Jump Instruction Format

Conditional jumps:
support program branching relative to the PC and do not
affect the status bits.
The possible jump range is from −511 to +512 words
relative to the PC value at the jump instruction.
The 10-bit program-counter offset is treated
as a signed 10-bit value that is doubled and added to the
program counter:
PC new = PC old + 2 + PC offset × 2
Unconditional jumps:
JMP Label
MICROCONTROLLER
Jump Instruction Format

15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0

OPCODE C 10-Bit PC Offset

Mnemonic S Reg,D Reg Operation


JEQ/JZ Label Jump to label if zero bit is set
JNE/JNZ Label Jump to label if zero bit is reset
JC Label Jump to label if carry bit is set
JNC Label Jump to label if carry bit is reset
JN Label Jump to label if negative bit is set
JGE Label Jump to label if (N XOR V)=0
JL Label Jump to label if (N XOR V)=1
JMP Label Jump to label unconditionally
MICROCONTROLLER
Example:Jump Instruction Format

-Assembly: JC main
-Instruction Code: 0X2fe4

OPCODE C 10-Bit PC Offset,


JC Condition 2’s complement(-28)
001 011 1111100100
THANK YOU

Asha.S.Patil
Department of Electrical & Electronics Engineering
ashaspatil@pes.edu
Microcontroller
MSP430
Asha.S.Patil
Department of Electrical & Electronics Engineering
MICROCONTROLLER

MSP430 – Instruction Set

Asha.S.Patil
Department of Electrical&Electronics Engineering
MICROCONTROLLER
Data Movement

MOV instruction

Memory(Or I/O port ) destination

Addressing modes

E.g. MOV #024h,R9


MOV #6511h,R2
MICROCONTROLLER
Arithmetic instruction:*** Affect flags ***

Normal Flag effects for addition and subtraction


 C = 0 if not carry; C= 1 if carry
– For addition: C=1 means a carry is generated
– For subtraction: C=0 means a borrow is needed;
 Z=0 if destination is not zero, Z=1 if result is zero (cleared)
 N = most significant bit of destination
 V = 1 if overflow in addition or subtraction, V=0 if not
overflow
– Addition overflow if two signed integers of same sign yields a
result of different sign
– Subtraction overflow if difference between numbers of
different sign has the sign of subtrahend
ƒ
MICROCONTROLLER
Core Addition and Subtraction Instructions (.w and .b )

add src, dest -- Add source to destination


– dest dest + src

addc src, dest -- Add source and carry to destination


– dest dest + src + Carry Flag

 sub src, dest -- Subtract source from destination


– dest dest + not(src) + 1
MICROCONTROLLER
Core Addition and Subtraction Instructions (.w and .b )

 subc src, dest or sbb src, dest-- Subtract source and


borrow from destination
– dest dest + not(src) + Carry

 cmp src, dest -- Compare dest to src


– dest + not(src )+ 1, no change of operands.

 sxt dest– Sign-extend LSByte to word


– Bit(15)=Bit(14)= … = Bit(8) Bit(7)
• Example R5 = A587 , sxt R5 R5 = FF87
• Example R6 = A577, sxt R6 R6 = 0077
MICROCONTROLLER
Core Addition and Subtraction Instructions (.w and .b )

comp src, dest.

Conditions directly tested in MSP430 are


If A=B then…..;
If A≠ B then ….
If A≥ B then …. *for signed and unsigned+ –
If A< B then ….. *for signed and unsigned+ –
If A less than 0
MICROCONTROLLER
Core Addition and Subtraction Instructions (.w and .b )

dadd src, dest


 Decimal addition – Used for BCD formats
– dest BCD addition ( dest + src + Carry)
– C =1 if result is greater than 9999 for words or 99 for
bytes.
– V is undefined
• Example: R5 = 1238 R6 = 7684, C=0
– dadd.w R5, R6 ; R6 = 8922, C=0
– dadd.b R5,R6 ; R6 = 0022, C=1
MICROCONTROLLER
Emulated instruction

adc dest = addc #0,dest (add carry to dest)


dadc dest = dadd #0, dest (decimal addition of carry)
 dec dest = sub #1,dest (decrement destination)
 decd dest = sub #2,dest (decrement dest twice)
 inc dest = add #1,dest (increment destination)
 incd dest = add # 2, dest (increment dest twice)
 sbc dest = subc #0, dest (subtract borrow)
 tst dest = cmp #0, dest ( C=1, V=0 always)
THANK YOU

Asha.S.Patil
Department of Electrical & Electronics Engineering
ashaspatil@pes.edu
Microcontroller
MSP430
Asha.S.Patil
Department of Electrical & Electronics Engineering
MICROCONTROLLER

MSP430 – Instruction Set

Asha.S.Patil
Department of Electrical&Electronics Engineering
MICROCONTROLLER
Logic Bitwise Instructions

Effects on Flags
Flags are affected by logic operations as follows, under
otherwise indicated:
 Z=1 if destination is cleared
 Z=0 if at least one bit in destination is set
 C= Z’ (Flag C is always equal to the inverted value of Z in
these operations)
 N = most significant bit of destination
V = 0
MICROCONTROLLER
Core bitwise Instructions affecting flags

 and src, dest realizes dest src .and. Dest

 xor src, dest realizes dest src .xor. Dest


a) Most common, but not exclusive, use is for inverting
(toggle) selected bits, as indicated by the mask (source).

 bit src, dest realizes src .and. dest but only affects flagsƒ
MICROCONTROLLER
Core bitwise Instructions affecting flags

 Examples starting with


R12= 35AB = 0011 0101 1010 1011
R15= AB96 = 1010 1011 1001 0110
and R12,R15
0010 0001 1000 0010→R15=2182 R12= 35AB
bit R12,R15
Flags for both cases: C=1, Z=0, N=0, V=0
xor R12,R15
1001 1110 0011 1101→R15=9E3Dh C=1, Z=0, N=1, V=0
xor.b R12,R15
0011 1101→R15=003Dh C=1, Z=0, N=0, V=0
MICROCONTROLLER
Core bitwise Instructions – not affecting flags

 bissrc, dest realizes dest src .or. dest,


Sets bits selectd with mask
bic src, dest realizes dest src’ .and. Dest
clear bits selected with masks.
• Examples R12= 35ABh = 0011 0101 1010 1011
R15= AB96h = 1010 1011 1001 0110
bis R12,R15
1011 1111 1011 1111→R15= BFBFh
Flags: unchanged
bic R12,R15
1000 1010 0001 0100→R15= 8A14h
Flags: unchange
MICROCONTROLLER
Emulated Logic Instructions

•Manipulating flags: Inverting a destination:


clc = bic #1, SR ( C 0) inv dest = xor #0FFFFh, dest
clz = bic #2, SR ( Z 0) inv.b dest = xor.b # 0FFh, dest
cln = bic #4, SR ( N 0) Toggles (inverts) all bits in dest
setc = bis #1, SR ( C 1)
setz = bis #2, SR ( Z 1)
setn = bis #4, SR ( N 1)
MICROCONTROLLER
Rolls and Rotates in MSP430

Rolling (Shifting) and Rotating Data bits

 Two core instructions:


– Right rolling arithmetic: rra dest
– Right rotation through Carry: rrc dest

• Two emulated instructions


– Left rolling arithmetic: rla dest = add dest,dest
– Rotate left through carry: rlc dest = addc dest,dest
MICROCONTROLLER
Right Rotation

RRA

Before b15 b14 ……b2 b1 b0 Cfold


After b15 b15 ……b3 b2 b1 bo

RRC

Before b15 b14 ……b2 b1 b0 Cfold


After Cfold b15 …… b2 b1 bo
MICROCONTROLLER
Left Rotation

LRA

Before Cfold b15 b14 ……b2 b1 b0


After b15 b14 …… b3 b2 b1 0

LRC

Before Cfold b15 b14 ……b2 b1 b0


After b15 …… b14 b13 …. bo Cfold
THANK YOU

Asha.S.Patil
Department of Electrical & Electronics Engineering
ashaspatil@pes.edu
Microcontroller
MSP430
Asha.S.Patil
Department of Electrical & Electronics Engineering
MICROCONTROLLER

MSP430 – Programming in Assembly language

Asha.S.Patil
Department of Electrical&Electronics Engineering
MICROCONTROLLER
Programming in assembly language

Data Movement Program:


Example 1:
mov.w #0x200,R5
mov.w #0x230,R6
mov.w #06,R7

L1: mov 0(R5),0(R6)


inc R5
inc R6
dec R7
jnz L1
jmp $
THANK YOU

Asha.S.Patil
Department of Electrical & Electronics Engineering
ashaspatil@pes.edu
Microcontroller
MSP430
Asha.S.Patil
Department of Electrical & Electronics Engineering
MICROCONTROLLER

MSP430 – Hands on with CCS IDE

Asha.S.Patil
Department of Electrical&Electronics Engineering
MICROCONTROLLER
Installation of IDE

Code composer Studio version 9.0.1 –for MSP430

http://www.ti.com/tool/CCSTUDIO-MSP
THANK YOU

Asha.S.Patil
Department of Electrical & Electronics Engineering
ashaspatil@pes.edu
Microcontroller
MSP430
Asha.S.Patil
Department of Electrical & Electronics Engineering
MICROCONTROLLER

MSP430 – Programming in Assembly language

Asha.S.Patil
Department of Electrical&Electronics Engineering
MICROCONTROLLER
Programming in assembly language

Blinking LED
Example 2:
bis.b #1,&P1DIR
outer
xor.b #1,&P1OUT
mov.w #0x2710,r5
mov.w #0x2710,r4
inner
dec.w r4
dec.w r5
tst.w r5
jne inner
jmp outer
THANK YOU

Asha.S.Patil
Department of Electrical & Electronics Engineering
ashaspatil@pes.edu
Microcontroller
MSP430
Asha.S.Patil
Department of Electrical & Electronics Engineering
MICROCONTROLLER

MSP430 – C Programming

Asha.S.Patil
Department of Electrical&Electronics Engineering
MICROCONTROLLER
Functions and Subroutines

Functions :useful for code that is called from more than one
place but should be used much more widely, to encapsulate
every distinct function.
MICROCONTROLLER
What Happens when a Subroutine Is Called?

current value in the PC, to be pushed on to the stack.

address of the subroutine is then loaded into the PC and


execution continues from there.

end of the subroutine the ret instruction pops the return address
off the stack into the PC so that execution resumes with the
instruction following the call of the subroutine.
MICROCONTROLLER
Calling Subroutine

call #DelayTenths
MICROCONTROLLER
Storage for Local Variables

 Most functions need local variables and there are three


ways in which space for these can be allocated:

 CPU registers are simple and fast.

 A second approach is to use a fixed location in RAM;

 The third approach is to allocate variables on the stack and is


generally used when a program has run out of CPU registers.
THANK YOU

Asha.S.Patil
Department of Electrical & Electronics Engineering
ashaspatil@pes.edu
Microcontroller
MSP430
Asha.S.Patil
Department of Electrical & Electronics Engineering
MICROCONTROLLER

MSP430-C Programming

Asha.S.Patil
Department of Electrical&Electronics Engineering
MICROCONTROLLER
Programming Example in C

#include <msp430g2553.h>
/*
* main.c
*/
int main(void)
{
WDTCTL = WDTPW | WDTHOLD; // stop watchdog timer

P1DIR =0X01; //toggle the bit


P1OUT=0X01;
unsigned int i;
while(1)
{
// volatile unsigned long i;
P1OUT ^=0X01; //toggle the bits
for(i=0;i<20000;i++)
{
}
}

return(0);
THANK YOU

Asha.S.Patil
Department of Electrical & Electronics Engineering
ashaspatil@pes.edu
Microcontroller
MSP430
Asha.S.Patil
Department of Electrical & Electronics Engineering
MICROCONTROLLER

MSP430-Some Programming concepts

Asha.S.Patil
Department of Electrical&Electronics Engineering
MICROCONTROLLER
Programming concept

Some basic things:

ALU:used to perform addition,mul,sub and div


logical operations
Monitor Status register :flags registers affected

Storage area:R0-R15
R0-------PC
R1-------SP
R2 &R3—CG

ADDRESSING MODE:
#---------IMMEDIATE AM
@---------INDIRECT ADDRESSING MODE
@+--------AUTOINCREMENT MODE
()-----------INDEXED AM
MICROCONTROLLER
Programming concept

Some basic things:


Its important to realize that 0x000F = 0xF

AND operation is used to set something to 0XFF AND 0X02=0X02


P1OUT |= 0x02;
This results in adding the 1 to position 1, while leaving the register as it was
elsewhere.

It’s important to note that if the position to which 1 is added is already 1,


there is no overflow (because we’re performing an OR). Therefore:
0x02 | 0x02 = 0x02;

We can add together to control multiple bits. For example, we can add a
bit in position 7 and 6 by doing an OR with 0x80 and 0x40:
P1OUT |= 0x80 + 0x40;
MICROCONTROLLER
Programming concept

MSP430 header includes the exact definitions for the bits up


to 16 bits:
#define BIT0 (0x0001)
#define BIT1 (0x0002)

P1OUT |= BIT4; \\ Make P1.4 High(1)


P1OUT |= BIT4 + BIT3; \\ Make P1.4 and P1.3 High(1)
P1OUT &= ˜BIT4; \\ Make P1.4 Low (0)
P1OUT ˆ= BIT4; \\ Toggle P1.4
P1DIR |= 0x01; // Set P1.0 to output direction
THANK YOU

Asha.S.Patil
Department of Electrical & Electronics Engineering
ashaspatil@pes.edu
Microcontroller
MSP430
Asha.S.Patil
Department of Electrical & Electronics Engineering
MICROCONTROLLER

MSP430 – Clock system

Asha.S.Patil
Department of Electrical&Electronics Engineering
MICROCONTROLLER
CLOCK SYSTEM

• All microcontroller contains a clock modules to


drive a CPU and Peripherals.

• The conflicting requirements for clock in high


performance and low power microcontrollers
MICROCONTROLLER
CLOCK SYSTEM
Inside MSP430 (MSP430G2x31)

IO

Timer System
MICROCONTROLLER
CLOCK SYSTEM

Theoretically, One Clock Is Enough


• A clock is a square wave signal whose edges trigger
hardware
• A clock may be generated by various sources of pulses,
e.g. crystal
• But, systems have conflicting requirements
• Low power, fast start/stop, accurate
MICROCONTROLLER
CLOCK SYSTEM

Different Requirements for Clocks


• Devices often in a low-power mode until some event
occurs, then must wake up and handle event rapidly
• Clock must be stabilized quickly
• Devices also need to keep track of real time:
(1) can wake up periodically, or
(2) time-stamp external events
• Therefore, two kinds of clocks often needed:
• A fast clock to drive CPU, which can be started and stopped
rapidly but need not be accurate
• A slow clock that runs continuously to monitor real time
that uses little power and is accurate
MICROCONTROLLER
CLOCK SYSTEM

Different Requirements for Clocks


• Different clock sources also have different characteristics
• Crystal: accurate and stable (w.r.t. temperature or time);
expensive, delicate, drawing large current, external
component, slow to start up or stabilize
• Resistor and capacitor (RC): cheap, quick to start, integrated
within MCU and sleep with CPU; poor accuracy and stability
• Ceramic resonator and MEMS clocks in between
Need multiple clocks
MICROCONTROLLER
Clocks in MSP430

• MSP430 addresses the conflicting demands for high


performance, low power, precise frequency by using
3 internal clocks, which can be derived from up to 4
sources
• Master clock (MCLK): for CPU & some peripherals,
normally driven by digitally controlled oscillator (DCO) in
megahertz range
• Subsystem master clock (SMCLK): distributed to
peripherals, normally driven by DCO
• Auxiliary clock (ACLK): distributed to peripherals, normally
for real-time clocking, driven by a low-frequency crystal
oscillator, typically at 32 KHz
MICROCONTROLLER
Clock Sources in MSP430

• Low- or high-frequency crystal oscillator, LFXT1:


• External; used with a low- or high frequency crystal; an
external clock signal can also be used; connected to MSP430
through XIN and XOUT pins
• High-frequency crystal oscillator, XT2:
• External; similar to LFXT1 but at high frequencies
• Very low-power, low-frequency oscillator, VLO:
• Internal at 12 KHz; alternative to LFXT1 when accuracy of a
crystal is not needed; may not available in all devices
• Digitally controlled oscillator, DCO:
• Internal; a highly controllable RC oscillator that starts fast
MICROCONTROLLER
From Sources to Clocks

• Typical sources of clocks:


• MCLK, SMCLK: DCO (typically at 1.1 MHz)
• ACLK: LFXT 1 (typically at 32 KHz)
MICROCONTROLLER
Control Registers for Clocks

Control Registers for Clock System

• DCOCTL and BCSCTL1 combined define the frequency of


DCO, among other settings
MICROCONTROLLER
Simple Setting of DCO

• Can use Tag-Length-Value (TLV) that are stored in


the flash memory to set DCOCTL and BCSCTL1 for
DCO frequency

BCSCTL1 = CALBC1_1MHZ; // Set range


DCOCTL = CALDCO_1MHZ;
MICROCONTROLLER
CLOCK SYSTEM

Different peripherals may use different clock domains.

Each of the three clock domains above can be sourced


by different
oscillator sources (see section 5.8 of text book or
datasheet).

Each of the three clock domains can also be divided


down by a factor
of 1, 2, 4, or 8.
MICROCONTROLLER
CLOCK SYSTEM

How long will your system run without a reset?


Is precise timing needed?
Are you keeping track of “real time”?
What range of temperatures will your system be
operating in?

Status Register Control of Clock Module

Remember that 4 bits in the SR enable and disable parts


of the clock
module to facilitate low-power modes!
MICROCONTROLLER
CLOCK SYSTEM

Oscillator Faults

Having multiple oscillator candidates for a clock source


allows for some
fault tolerance!

What happens if an external crystal oscillator fails and


your system is part
of a satellite in space?

→ Software can switch over to an alternate, less-


desirable or
backup oscillator as a fail-safe!
MICROCONTROLLER
CLOCK SYSTEM

An RC circuit can be used as a primitive way of


detecting an oscillator fault.
(The MSP430 likely uses a more sophisticated
hardware technique)
Remember:
 = RC
Vout = Vin · e-(t/)
Asha.S.Patil
Department of Electrical & Electronics Engineering

ashaspatil@pes.edu

THANK YOU
Microcontroller
MSP430
Asha.S.Patil
Department of Electrical & Electronics Engineering
MICROCONTROLLER

MSP430 – RESETS

Asha.S.Patil
Department of Electrical&Electronics Engineering
MICROCONTROLLER
MSP430 resets

Power-On Reset (POR)

Caused by hardware.
Response of hardware to brownout (low-power)
condition.

Hard power-on is extreme case of brownout.


Can happen if physical RST pin is asserted.

Some MSP430s have a Supply Voltage Supervisor


(SVS) that can generate a power-on condition if
supply voltage drops below acceptable
levels.
MICROCONTROLLER
MSP430 resets
Power-Up Clear (PUC)

Distinctive from power-on reset.


Caused by software conditions.
Watchdog timer expiration.
Software-generated reset condition (bad Watchdog
password).
Runaway Flash memory programming (bad Flash
controller password).
Access violations (reading IC from peripheral
registers…)
MICROCONTROLLER
MSP430 resets
Conditions after reset

Initial conditions for registers and peripherals vary


(see datasheet for
details).
MICROCONTROLLER
MSP430 resets

General effects:

RST/NMI pin configured as RST.

Most input/output pins configured as inputs:


Software should configure unused pins as
outputs (or otherwise ensure unused inputs
are not floating).
MICROCONTROLLER
MSP430 resets

General effects:

Peripheral registers set the way the datasheet


says:
rw-0 notation means: readable/writeable and
initialized to 0 on PUC.
rw-(0) notation means: readable/writeable
and initialized to 0 only on POR.
MICROCONTROLLER
MSP430 resets
General effects:

Status register is cleared (brought out of low-


power mode if necessary).
Watchdog timer is enabled:
Software should explicitly disable if not
used.

Program counter (R0) is loaded with the reset


vector.
MICROCONTROLLER
MSP430 resets

What caused the reset?

Can be determined through the IFG1 and perhaps


other flag registers.
The following bits will be set to identify the source of
the reset:

WDTIFG: Watchdog caused reset.


OFIFG: Oscillator fault (non-maskable interrupt
actually, not a reset).
RSTIFG: RST pin was asserted.
PORIFG: Set on power-on reset (POR).
MICROCONTROLLER
MSP430 resets

What caused the reset?

NMIIFG: NMI pin was asserted (non-maskable interrupt


actually, not a reset).

On PUC: The WDTIFG bit is not cleared—so the source of


the PUC can be determined.

Other flags for Flash security and the supply voltage


supervisor (if applicable) exist
elsewhere (see datasheets).

Doesn’t appear to be a flag for an illegal opcode!


MICROCONTROLLER
MSP430 resets

A Few Hardware Considerations

Be careful with RST pin! Should never be left floating and de-
coupled from noise as best as possible. Modern uCs/uPs can
have very sensitive RST lines!

Poor quality power supplies can cause problems. A slow


supply power rise on power up (for example) can cause
problems (hardware software race condition).
Asha.S.Patil
Department of Electrical & Electronics Engineering

ashaspatil@pes.edu

THANK YOU
Microcontroller
MSP430
Asha.S.Patil
Department of Electrical & Electronics Engineering
MICROCONTROLLER

MSP430 – Interruppts

Asha.S.Patil
Department of Electrical&Electronics Engineering
MICROCONTROLLER
MSP430 interrupts

A computer has 2 basic ways to react to inputs:


1) polling: The processor regularly looks at the input
and reacts as appropriate.
++ easy to implement and debug easy to implement
and debug
- processor intensive
• if event is rare, you waste a lot of time checking
• processor can’t go into low power (slow or
stopped) modes
MICROCONTROLLER
MSP430 interrupts

interrupts: The processor is “interrupted” by an


event.
+ very efficient time-wise: no time wasted looking
for an event that hasn’t occurred.
+ very efficient energy-wise: processor can be
asleep most of the time.
- can be hard to debug
MICROCONTROLLER
What happens on interrupt?

interrupt Acceptance

The interrupt latency is 6 cycles (CPU), from the


acceptance of an interrupt request to the start of
execution of the interrupt‐service routine.
MICROCONTROLLER
What happens on interrupt?

The interrupt logic executes the following:


1.Any currently executing instruction is completed.
2. The PC, which points to the next instruction, is
pushed onto the stack.
3. The SR is pushed onto the stack.
4. The interrupt with the highest priority is selected
if multiple interrupts occurred during the last
instruction and are pending for service
5.The interrupt request flag resets automatically on
single‐source flags. Multiple source flags remain set
for servicing by software.
MICROCONTROLLER
What happens on interrupt?

Any currently executing instruction is completed. 2.


The PC, which points to the next instruction, is
pushed onto the stack.
3. The SR is pushed onto the stack.
4. The interrupt with the highest priority is selected
if multiple interrupts occurred during the last
instruction and are pending for service
The interrupt request flag resets automatically on
single‐source flags. Multiple source flags remain set
for servicing by software.
MICROCONTROLLER
What happens on interrupt?
6. The SR is cleared. This terminates any
low‐power mode. Because the GIE bit is cleared,
further interrupts are disabled.
7. The content of the interrupt vector is loaded
into the PC: the program continues with the
interrupt service routine at that address.
MICROCONTROLLER
What happens on interrupt?
MICROCONTROLLER
What happens on interrupt?
Return From Interrupt
The interrupt handling routine terminates with
instruction:
RETI(return from ISR)
The return from the interrupt takes 5 cycles
(CPU) or 3 cycles (CPUx) to execute the
following actions.
MICROCONTROLLER
What happens on interrupt?

1. The SR with all previous settings pops from the


stack. All previous settings of GIE, CPUOFF, etc. are
now in effect, regardless of the settings used
during the interrupt service routine.
2. The PC pops from the stack and begins execution at
the point where it was interrupted.
MICROCONTROLLER
MSP430 interrupts
Asha.S.Patil
Department of Electrical & Electronics Engineering

ashaspatil@pes.edu

THANK YOU
Microcontroller
MSP430
Asha.S.Patil
Department of Electrical & Electronics Engineering
MICROCONTROLLER

MSP430 – Interruppts

Asha.S.Patil
Department of Electrical&Electronics Engineering
MICROCONTROLLER
MSP430 interrupts

A computer has 2 basic ways to react to inputs:


1) polling: The processor regularly looks at the input
and reacts as appropriate.
++ easy to implement and debug easy to implement
and debug
- processor intensive
• if event is rare, you waste a lot of time checking
• processor can’t go into low power (slow or
stopped) modes
MICROCONTROLLER
MSP430 interrupts

interrupts: The processor is “interrupted” by an


event.
+ very efficient time-wise: no time wasted looking
for an event that hasn’t occurred.
+ very efficient energy-wise: processor can be
asleep most of the time.
- can be hard to debug
MICROCONTROLLER
What happens on interrupt?

interrupt Acceptance

The interrupt latency is 6 cycles (CPU), from the


acceptance of an interrupt request to the start of
execution of the interrupt‐service routine.
MICROCONTROLLER
What happens on interrupt?

The interrupt logic executes the following:


1.Any currently executing instruction is completed.
2. The PC, which points to the next instruction, is
pushed onto the stack.
3. The SR is pushed onto the stack.
4. The interrupt with the highest priority is selected
if multiple interrupts occurred during the last
instruction and are pending for service
5.The interrupt request flag resets automatically on
single‐source flags. Multiple source flags remain set
for servicing by software.
MICROCONTROLLER
What happens on interrupt?

Any currently executing instruction is completed. 2.


The PC, which points to the next instruction, is
pushed onto the stack.
3. The SR is pushed onto the stack.
4. The interrupt with the highest priority is selected
if multiple interrupts occurred during the last
instruction and are pending for service
The interrupt request flag resets automatically on
single‐source flags. Multiple source flags remain set
for servicing by software.
MICROCONTROLLER
What happens on interrupt?
6. The SR is cleared. This terminates any
low‐power mode. Because the GIE bit is cleared,
further interrupts are disabled.
7. The content of the interrupt vector is loaded
into the PC: the program continues with the
interrupt service routine at that address.
MICROCONTROLLER
What happens on interrupt?
MICROCONTROLLER
What happens on interrupt?
Return From Interrupt
The interrupt handling routine terminates with
instruction:
RETI(return from ISR)
The return from the interrupt takes 5 cycles
(CPU) or 3 cycles (CPUx) to execute the
following actions.
MICROCONTROLLER
What happens on interrupt?

1. The SR with all previous settings pops from the


stack. All previous settings of GIE, CPUOFF, etc. are
now in effect, regardless of the settings used
during the interrupt service routine.
2. The PC pops from the stack and begins execution at
the point where it was interrupted.
MICROCONTROLLER
MSP430 interrupts
Asha.S.Patil
Department of Electrical & Electronics Engineering

ashaspatil@pes.edu

THANK YOU
Microcontroller
MSP430
Asha.S.Patil
Department of Electrical & Electronics Engineering
MICROCONTROLLER

MSP430 – Interruppts

Asha.S.Patil
Department of Electrical&Electronics Engineering
MICROCONTROLLER
MSP430 interrupts

A computer has 2 basic ways to react to inputs:


1) polling: The processor regularly looks at the input
and reacts as appropriate.
++ easy to implement and debug easy to implement
and debug
- processor intensive
• if event is rare, you waste a lot of time checking
• processor can’t go into low power (slow or
stopped) modes
MICROCONTROLLER
MSP430 interrupts

interrupts: The processor is “interrupted” by an


event.
+ very efficient time-wise: no time wasted looking
for an event that hasn’t occurred.
+ very efficient energy-wise: processor can be
asleep most of the time.
- can be hard to debug
MICROCONTROLLER
What happens on interrupt?

interrupt Acceptance

The interrupt latency is 6 cycles (CPU), from the


acceptance of an interrupt request to the start of
execution of the interrupt‐service routine.
MICROCONTROLLER
What happens on interrupt?

The interrupt logic executes the following:


1.Any currently executing instruction is completed.
2. The PC, which points to the next instruction, is
pushed onto the stack.
3. The SR is pushed onto the stack.
4. The interrupt with the highest priority is selected
if multiple interrupts occurred during the last
instruction and are pending for service
5.The interrupt request flag resets automatically on
single‐source flags. Multiple source flags remain set
for servicing by software.
MICROCONTROLLER
What happens on interrupt?

Any currently executing instruction is completed. 2.


The PC, which points to the next instruction, is
pushed onto the stack.
3. The SR is pushed onto the stack.
4. The interrupt with the highest priority is selected
if multiple interrupts occurred during the last
instruction and are pending for service
The interrupt request flag resets automatically on
single‐source flags. Multiple source flags remain set
for servicing by software.
MICROCONTROLLER
What happens on interrupt?
6. The SR is cleared. This terminates any
low‐power mode. Because the GIE bit is cleared,
further interrupts are disabled.
7. The content of the interrupt vector is loaded
into the PC: the program continues with the
interrupt service routine at that address.
MICROCONTROLLER
What happens on interrupt?
MICROCONTROLLER
What happens on interrupt?
Return From Interrupt
The interrupt handling routine terminates with
instruction:
RETI(return from ISR)
The return from the interrupt takes 5 cycles
(CPU) or 3 cycles (CPUx) to execute the
following actions.
MICROCONTROLLER
What happens on interrupt?

1. The SR with all previous settings pops from the


stack. All previous settings of GIE, CPUOFF, etc. are
now in effect, regardless of the settings used
during the interrupt service routine.
2. The PC pops from the stack and begins execution at
the point where it was interrupted.
MICROCONTROLLER
MSP430 interrupts
Asha.S.Patil
Department of Electrical & Electronics Engineering

ashaspatil@pes.edu

THANK YOU
Microcontroller
MSP430
Asha.S.Patil
Department of Electrical & Electronics Engineering
MICROCONTROLLER

MSP430 – TIMER

Asha.S.Patil
Department of Electrical&Electronics Engineering
MICROCONTROLLER
Inside MSP430 (MSP430G2x31)
IO
k
m

Timer System
MICROCONTROLLER
Inside MSP430
MICROCONTROLLER
Inside MSP430
.Timer_A Control Register: TACTL
Typical Operations of Timer_A

Continuously
count up/down

Is time up Yes
yet?
TAIFG has to be explicitly
cleared by the CPU
TACCRx If TAIE=1, setting of TAIFG
causes an interrupt to the CPU
TACTL

TACTL = TASSEL_2 + MC_1; // src from SMCLK, up mode


MICROCONTROLLER
Timer Mode

•MCx=00: Stop mode


• The timer is halted
•MCx=01: Up mode
• The timer repeatedly counts from 0 to TACCR0
•MCx=10: Continuous mode
• The timer repeatedly counts from 0 to 0FFFFh
•MCx=11: Up/down mode
• The timer repeatedly counts from 0 to TACCR0 and back down to 0
MICROCONTROLLER
Up Mode
The up mode is used if the timer period must be different
from 0FFFFh counts.
1. Timer period 100 → store 99 to TACCR0
2. When TACCR0 == 99, set TACCR0 CCIFG interrupt flag
3. Reset timer to 0 and set TAIFG interrupt flag
TAIFG is set, and Timer_A
interrupts CPU
MICROCONTROLLER
Continuous Mode

• In the continuous mode, the timer repeatedly counts up to 0FFFFh


and restarts from zero
• The TAIFG interrupt flag is set when the timer resets from 0FFFFh to
zero
MICROCONTROLLER
Up/down Mode

• The up/down mode is used if the timer period must be different from
0FFFFh counts, and if a symmetrical pulse generation is needed.
→ The period is twice the value in TACCR0.

Timer interrupts!
(TAIFG is set)
Asha.S.Patil
Department of Electrical & Electronics Engineering

ashaspatil@pes.edu

THANK YOU
Microcontroller
MSP430
Asha.S.Patil
Department of Electrical & Electronics Engineering
MICROCONTROLLER

MSP430 – TIMER

Asha.S.Patil
Department of Electrical&Electronics Engineering
MICROCONTROLLER
Inside MSP430 (MSP430G2x31)
IO
k
m

Timer System
MICROCONTROLLER
Inside MSP430
MICROCONTROLLER
Inside MSP430
.Timer_A Control Register: TACTL
Typical Operations of Timer_A

Continuously
count up/down

Is time up Yes
yet?
TAIFG has to be explicitly
cleared by the CPU
TACCRx If TAIE=1, setting of TAIFG
causes an interrupt to the CPU
TACTL

TACTL = TASSEL_2 + MC_1; // src from SMCLK, up mode


MICROCONTROLLER
Timer Mode

•MCx=00: Stop mode


• The timer is halted
•MCx=01: Up mode
• The timer repeatedly counts from 0 to TACCR0
•MCx=10: Continuous mode
• The timer repeatedly counts from 0 to 0FFFFh
•MCx=11: Up/down mode
• The timer repeatedly counts from 0 to TACCR0 and back down to 0
MICROCONTROLLER
Up Mode
The up mode is used if the timer period must be different
from 0FFFFh counts.
1. Timer period 100 → store 99 to TACCR0
2. When TACCR0 == 99, set TACCR0 CCIFG interrupt flag
3. Reset timer to 0 and set TAIFG interrupt flag
TAIFG is set, and Timer_A
interrupts CPU
MICROCONTROLLER
Continuous Mode

• In the continuous mode, the timer repeatedly counts up to 0FFFFh


and restarts from zero
• The TAIFG interrupt flag is set when the timer resets from 0FFFFh to
zero
MICROCONTROLLER
Up/down Mode

• The up/down mode is used if the timer period must be different from
0FFFFh counts, and if a symmetrical pulse generation is needed.
→ The period is twice the value in TACCR0.

Timer interrupts!
(TAIFG is set)
Asha.S.Patil
Department of Electrical & Electronics Engineering

ashaspatil@pes.edu

THANK YOU
Microcontroller
MSP430
Asha.S.Patil
Department of Electrical & Electronics Engineering
MICROCONTROLLER

MSP430 – TIMER

Asha.S.Patil
Department of Electrical&Electronics Engineering
MICROCONTROLLER
Inside MSP430 (MSP430G2x31)
IO
k
m

Timer System
MICROCONTROLLER
Inside MSP430
MICROCONTROLLER
Inside MSP430
.Timer_A Control Register: TACTL
Typical Operations of Timer_A

Continuously
count up/down

Is time up Yes
yet?
TAIFG has to be explicitly
cleared by the CPU
TACCRx If TAIE=1, setting of TAIFG
causes an interrupt to the CPU
TACTL

TACTL = TASSEL_2 + MC_1; // src from SMCLK, up mode


MICROCONTROLLER
Timer Mode

•MCx=00: Stop mode


• The timer is halted
•MCx=01: Up mode
• The timer repeatedly counts from 0 to TACCR0
•MCx=10: Continuous mode
• The timer repeatedly counts from 0 to 0FFFFh
•MCx=11: Up/down mode
• The timer repeatedly counts from 0 to TACCR0 and back down to 0
MICROCONTROLLER
Up Mode
The up mode is used if the timer period must be different
from 0FFFFh counts.
1. Timer period 100 → store 99 to TACCR0
2. When TACCR0 == 99, set TACCR0 CCIFG interrupt flag
3. Reset timer to 0 and set TAIFG interrupt flag
TAIFG is set, and Timer_A
interrupts CPU
MICROCONTROLLER
Continuous Mode

• In the continuous mode, the timer repeatedly counts up to 0FFFFh


and restarts from zero
• The TAIFG interrupt flag is set when the timer resets from 0FFFFh to
zero
MICROCONTROLLER
Up/down Mode

• The up/down mode is used if the timer period must be different from
0FFFFh counts, and if a symmetrical pulse generation is needed.
→ The period is twice the value in TACCR0.

Timer interrupts!
(TAIFG is set)
Asha.S.Patil
Department of Electrical & Electronics Engineering

ashaspatil@pes.edu

THANK YOU
Microcontroller
MSP430
Asha.S.Patil
Department of Electrical & Electronics Engineering
MICROCONTROLLER

MSP430 – TIMER

Asha.S.Patil
Department of Electrical&Electronics Engineering
MICROCONTROLLER
Timer_A Capture/Compare Block
Timer Block

• May contain several


Capture/Compare Blocks
Capture/Compare Block • Each Capture/Compare Block is
controlled by a control register,
TACCTLx
• Inside each Capture/Compare Block,
the Capture/Compare Register,
TACCRx, holds the count to configure
the timer
MICROCONTROLLER
Modes of Capture/Compare Block

• Compare mode:
• Compare the value of TAR with the value stored in TACCRn and update an
output when they match
• Capture mode: used to record time events
• Records the “time” (value in TAR) at which the input changes in TACCRx
• The input, usually CCIxA and CCIxB, can be either external or internal from
another peripheral or software, depending on board connections

TACCR0 = 24000; // represent 2 sec with 12kHz clk src


MICROCONTROLLER
Modes of Capture/Compare Block
TACCTL cont’d
Asha.S.Patil
Department of Electrical & Electronics Engineering

ashaspatil@pes.edu

THANK YOU
Microcontroller
MSP430
Asha.S.Patil
Department of Electrical & Electronics Engineering
MICROCONTROLLER

MSP430 –Watchdog TIMER

Asha.S.Patil
Department of Electrical&Electronics Engineering
MICROCONTROLLER
Watchdog Timer

The primary function of the watchdog-timer module (WDT) is


➢to perform a controlled-system restart after a software problem occurs.
If the selected time interval expires, a system reset is generated.
➢If the watchdog function is not needed in an application,
the module can work as an interval timer, to generate an interrupt after the selected time
interval.
MICROCONTROLLER
Watchdog Timer

Features of the Watchdog Timer include:


➢Eight software-selectable time intervals
➢Two operating modes: as watchdog or interval timer
➢Expiration of the time interval in watchdog mode,
which generates a system reset; or in timer mode,
which generates an interrupt request
➢Safeguards which ensure that writing to the
WDT control register is only possible using a password
➢Support of ultralow-power using the hold mode

Watchdog/Timer two functions:


➢SW Watchdog Mode
➢Interval Timer Mode
MICROCONTROLLER
Watchdog Timer-Diagram
MICROCONTROLLER
Watchdog Timer Counter

The watchdog-timer counter (WDTCNT) is a 16-bit up-counter


➢ that is not directly accessible by software.
➢ is controlled through the watchdog-timer control register (WDTCTL),
➢ WDTCTL is a 16-bit read/write register
➢ WDTCTL is located at the low byte of word address 0120h.
➢ Any read or write access must be done using word instructions with no suffix or .w
suffix.
➢ In both operating modes (watchdog or timer), it is only possible to write to WDTCTL
using the correct password.
MICROCONTROLLER
Watchdog Timer Control Register

WDTCTL MDB, HighByte


0120h
R/W MDB, LowByte

7 0

Password Compare
HOLD NMIES NMI TMSEL CNTCL SSEL IS1 ISO
EQU

Read: HighByte is 069h Write: HighByte is 05Ah, otherwise


WDT 16-bit Control Register with Write Protection

security key is violated


MICROCONTROLLER
WDTCTL
Bits 0, 1: Bits IS0 and IS1 select one of four taps from the WDTCNT, as described in
following table. Assuming f crystal = 32,768 Hz and f System = 1 MHz, the following
intervals are possible:
SSEL IS1 IS0 Interval [ms]
0 1 1 0.064 tSMCLK × 2 6
0 1 0 0.5 tSMCLK × 2 9
1 1 1 1.9 tSMCLK × 2 6
0 0 1 8 tSMCLK × 2 13
1 1 0 16.0 tACLK × 2 9
0 0 0 32 tSMCLK × 2 15 <– Value after PUC (reset)
1 0 1 250 tACLK × 2 13
1 0 0 1000 tACLK × 2 15
MICROCONTROLLER
WDTCTL
Bit 2: The SSEL bit selects the clock source for WDTCNT.
SSEL = 0: WDTCNT is clocked by SMCLK .
SSEL = 1: WDTCNT is clocked by ACLK.
Bit 3: Counter clear bit. In both operating modes, writing a 1 to this bit
restarts the WDTCNT at 00000h. The value read is not defined.
Bit 4: The TMSEL bit selects the operating mode: watchdog or timer.
TMSEL = 0: Watchdog mode
TMSEL = 1: Interval-timer mode
Bit 5: The NMI bit selects the function of the RST/NMI input pin. It is cleared by the PUC
signal.
NMI = 0: The RST/NMI input works as reset input.
As long as the RST/NMI pin is held low, the internal signal is active (level sensitive).
NMI = 1: The RST/NMI input works as an edge-sensitive non-maskable interrupt input
MICROCONTROLLER
WDTCTL
Bit 6: If the NMI function is selected, this bit selects the activating edge of the RST/NMI
input. It is cleared by the PUC signal.
NMIES = 0: A rising edge triggers an NMI interrupt.
NMIES = 1: A falling edge triggers an NMI interrupt.
CAUTION: Changing the NMIES bit with software can generate an NMI interrupt.
Bit 7: This bit stops the operation of the watchdog counter. The clock multiplexer is
disabled and the counter stops incrementing. It holds the last value until the hold bit is
reset and the operation continues. It is cleared by the PUC signal.
HOLD = 0: The WDT is fully active.
HOLD = 1: The clock multiplexer and counter are stopped
Asha.S.Patil
Department of Electrical & Electronics Engineering

ashaspatil@pes.edu

THANK YOU
Microcontroller
MSP430
Asha.S.Patil
Department of Electrical & Electronics Engineering
MICROCONTROLLER

MSP430 – TIMER

Asha.S.Patil
Department of Electrical&Electronics Engineering
MICROCONTROLLER
Timer_A PROGRAMMING in C

Flash red LED at 1 Hz if SMCLK at 800 KHz


MICROCONTROLLER
#include <msp430g2553.h>
#define LED1 BIT0
void main (void) {
WDTCTL = WDTPW|WDTHOLD; // Stop watchdog timer
P1OUT = ~LED1;
P1DIR = LED1;
TACCR0 = 49999;
TACTL = MC_1|ID_3|TASSEL_2|TACLR; //Setup Timer_A
//up mode, divide clk by 8, use SMCLK, clr timer
for (;;) { // Loop forever
while (!(TACTL&TAIFG)) { // Wait time up
} // doing nothing
TACTL &= ~TAIFG; // Clear overflow flag
P1OUT ^= LED1; // Toggle LEDs
} // Back around infinite loop
Asha.S.Patil
Department of Electrical & Electronics Engineering

ashaspatil@pes.edu

THANK YOU
Microcontroller
MSP430
Asha.S.Patil
Department of Electrical & Electronics Engineering
MICROCONTROLLER

MSP430 – TIMER

Asha.S.Patil
Department of Electrical&Electronics Engineering
MICROCONTROLLER
Timer_A PROGRAMMING

mov.w #(WDTPW|WDTHOLD), &WDTCTL


mov.b #1, &P1DIR
loop1:

call #delay ;delay depends on the value moved to r15

delay:
mov.w #32767, &TACCR0 ;seting the timer compare value

mov.w #(TASSEL_1|MC_1|TACLR), &TACTL ;setting the timer with ACLK clock


xor.b #1, &P1OUT

ret
jmp $
MICROCONTROLLER
Timer_A PROGRAMMING

mov.w #(WDTPW|WDTHOLD), &WDTCTL


mov.b #1, &P1DIR
loop1:

call #delay ;delay depends on the value moved to r15

delay:
mov.w #32767, &TACCR0 ;seting the timer compare value
mov.w #CCIE, &TACCTL0 ;compare interrupt enable
mov.w #(TASSEL_1|MC_1|TACLR), &TACTL ;setting the timer with ACLK clock
xor.b #1, &P1OUT
BIC.W #TAIFG,&TA0CTL
ret
jmp $
MICROCONTROLLER
Timer_A PROGRAMMING
MICROCONTROLLER

#include “MSP430.h”
VOID main(void)
{
WDTCTL=WDTPW|WDTHOLD;
TA0CTL=TASSWL_1+MC_1+ID_3;
TA0CCR0=4000;
P1DIR|=0X01;
P1OUT|=0X00;
P1OUT^=0X01;
}
MICROCONTROLLER
MICROCONTROLLER
MICROCONTROLLER
Asha.S.Patil
Department of Electrical & Electronics Engineering

ashaspatil@pes.edu

THANK YOU
Microcontroller
MSP430

Asha.S.Patil
Department of Electrical & Electronics Engineering
MICROCONTROLLER

Beginning Programming for MSP430

Asha.S.Patil
Department of Electrical&Electronics Engineering
MICROCONTROLLER
Basic code example1

#include "msp430x22x4.h"

volatile unsigned int i; // volatile to prevent optimization

void main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
P1DIR |= 0x01; // Set P1.0 to output direction

for (;;)
{
P1OUT ˆ= 0x01; // Toggle P1.0 using exclusive-OR
i = 50000; // Delay
do (i--);
while (i != 0);
}
}
MICROCONTROLLER
Basic code example2

1.LED BLINKING:
Note:for delay :_delay_cycles(2500);
#include <msp430g2553.h>

int main(void)
{

WDTCTL = WDTPW | WDTHOLD; // stop watchdog timer

P1DIR =0X01; //toggle the bit


P1OUT=0X01;
unsigned int i;
while(1)
{
P1OUT ^=0X01; //toggle the bits
for(i=0;i<20000;i++)
{
}
}

return(0);
}
MICROCONTROLLER
Basic code example2
1.LED BLINKING:.To turn on LED using assembly language programming
;;-------------------------------------------------------------------------------
.cdecls C,LIST,"msp430.h" ; Include device header file

;-------------------------------------------------------------------------------
.def RESET ; Export program entry-point to
; make it known to linker.
;-------------------------------------------------------------------------------
.text ; Assemble into program memory.
.retain ; Override ELF conditional linking
; and retain current section.
.retainrefs ; And retain any sections that have
; references to current section.

;-------------------------------------------------------------------------------
RESET mov.w #__STACK_END,SP ; Initialize stackpointer
StopWDT mov.w #WDTPW|WDTHOLD,&WDTCTL ; Stop watchdog timer
;-------------------------------------------------------------------------------
; Main loop here
;------------------------------------------------------------------------------

-------------------------------------------------------------------------------
; Stack Pointer definition
;-------------------------------------------------------------------------------
.global __STACK_END
.sect .stack

;-------------------------------------------------------------------------------
; Interrupt Vectors
;-------------------------------------------------------------------------------
.sect ".reset" ; MSP430 RESET Vector
.short RESET
THANK YOU
Asha.S.Patil
Department of Electrical & Electronics Engineering
ashaspatil@pes.edu
Microcontroller
MSP430

Asha.S.Patil
Department of Electrical & Electronics Engineering
MICROCONTROLLER

MSP430 –Clock System

Asha.S.Patil
Department of Electrical&Electronics Engineering
MICROCONTROLLER
Block Diagram Of Clock Module
MICROCONTROLLER
ClOCK SYSTEM

▪Master clock: MCLK is used by the CPU and a few peripherals.

▪Sub-system master clock: SMCLK is distributed to peripherals.

▪Auxiliary clock: ACLK is also distributed to peripherals.


MICROCONTROLLER
Low Power Mode

Low Power mode of Operation:

i) Active mode: I ≈ 300A.

ii) LOW Power Mode


MICROCONTROLLER
Registers

Low Power Mode:


i)LPM0:Both CPU and MCLK are disabled.SMCLK and ACLK are
active
ii)LPM1:Both CPU and MCLK are disabled.DCO is disabled and
ACLK remains active
iii)LPM2:CPU,MCLK,SMCLK,DCO are disabled. ACLK remain active
iv)LPM3:CPU,MCLK,SMCLK, DCO are disabled DC generator is
enabled and ACLK remain active
v)LPM4:All clocks are inactive.
MICROCONTROLLER
Power Consumption in Various Mode
THANK YOU
Asha.S.Patil
Department of Electrical & Electronics Engineering
ashaspatil@pes.edu
Inside MSP430 (MSP430G2x31)
IO
Clock
System

Timer System
Inside Timer_A

• Timer_A Control Register: TACTL


Typical Operations of Timer_A

Continuously
count up/down

Is time Yes
up yet?
TAIFG has to be
explicitly cleared by
the CPU
TACCRx If TAIE=1, setting of
TAIFG causes an
interrupt to the CPU
TACTL

TACTL = TASSEL_2 + MC_1; // src from SMCLK, up mode


Timer Mode
•MCx=00: Stop mode
– The timer is halted
•MCx=01: Up mode
– The timer repeatedly counts from 0 to TACCR0
•MCx=10: Continuous mode
– The timer repeatedly counts from 0 to 0FFFFh
•MCx=11: Up/down mode
– The timer repeatedly counts from 0 to TACCR0
and back down to 0
Up Mode
The up mode is used if the timer period must be
different from 0FFFFh counts.
1. Timer period 100 → store 99 to TACCR0
2. When TACCR0 == 99, set TACCR0 CCIFG interrupt flag
3. Reset timer to 0 and set TAIFG interrupt flag

TAIFG is set, and


Timer_A interrupts CPU
Continuous Mode
• In the continuous mode, the timer repeatedly
counts up to 0FFFFh and restarts from zero
• The TAIFG interrupt flag is set when the timer
resets from 0FFFFh to zero
Up/down Mode
• The up/down mode is used if the timer period
must be different from 0FFFFh counts, and if a
symmetrical pulse generation is needed.
→ The period is twice the value in TACCR0.

Timer interrupts!
(TAIFG is set)
Timer_A Capture/Compare Block
Timer Block

• May contain several


Capture/Compare Blocks
• Each Capture/Compare
Capture/Compare Block Block is controlled by a
control register, TACCTLx
• Inside each
Capture/Compare Block,
the Capture/Compare
Register, TACCRx, holds the
count to configure the
timer
Modes of Capture/Compare Block
• Compare mode:
– Compare the value of TAR with the value stored in
TACCRn and update an output when they match
• Capture mode: used to record time events
– Records the “time” (value in TAR) at which the
input changes in TACCRx
– The input, usually CCIxA and CCIxB, can be either
external or internal from another peripheral or
software, depending on board connections
TACCR0 = 24000; // represent 2 sec with 12kHz clk src
TACCTL
TACCTL cont’d
Inside MSP430 (MSP430G2x31)
IO
Clock
System

Timer System
Theoretically, One Clock Is Enough
• A clock is a square wave signal whose edges
trigger hardware
• A clock may be generated by various sources
of pulses, e.g. crystal
• But, systems have conflicting requirements
– Low power, fast start/stop, accurate
Different Requirements for Clocks
• Devices often in a low-power mode until some
event occurs, then must wake up and handle
event rapidly
– Clock must be stabilized quickly
• Devices also need to keep track of real time: (1)
can wake up periodically, or (2) time-stamp
external events
• Therefore, two kinds of clocks often needed:
– A fast clock to drive CPU, which can be started and
stopped rapidly but need not be accurate
– A slow clock that runs continuously to monitor
real time that uses little power and is accurate
Different Requirements for Clocks
• Different clock sources also have different
characteristics
– Crystal: accurate and stable (w.r.t. temperature or
time); expensive, delicate, drawing large current,
external component, slow to start up or stabilize
– Resistor and capacitor (RC): cheap, quick to start,
integrated within MCU and sleep with CPU; poor
accuracy and stability
– Ceramic resonator and MEMS clocks in between
Need multiple clocks
Clocks in MSP430
• MSP430 addresses the conflicting demands
for high performance, low power, precise
frequency by using 3 internal clocks, which
can be derived from up to 4 sources
– Master clock (MCLK): for CPU & some peripherals,
normally driven by digitally controlled oscillator
(DCO) in megahertz range
– Subsystem master clock (SMCLK): distributed to
peripherals, normally driven by DCO
– Auxiliary clock (ACLK): distributed to peripherals,
normally for real-time clocking, driven by a low-
frequency crystal oscillator, typically at 32 KHz
Clock Sources in MSP430
• Low- or high-frequency crystal oscillator, LFXT1:
– External; used with a low- or high frequency crystal; an
external clock signal can also be used; connected to
MSP430 through XIN and XOUT pins
• High-frequency crystal oscillator, XT2:
– External; similar to LFXT1 but at high frequencies
• Very low-power, low-frequency oscillator, VLO:
– Internal at 12 KHz; alternative to LFXT1 when accuracy of a
crystal is not needed; may not available in all devices
• Digitally controlled oscillator, DCO:
– Internal; a highly controllable RC oscillator that starts fast
From Sources to Clocks
• Typical sources of clocks:
– MCLK, SMCLK: DCO (typically at 1.1 MHz)
– ACLK: LFXT 1 (typically at 32 KHz)
Control Registers for Clocks
Control Registers for Clock System

• DCOCTL and BCSCTL1 combined define the


frequency of DCO, among other settings
Simple Setting of DCO
• Can use Tag-Length-Value (TLV) that are
stored in the flash memory to set DCOCTL and
BCSCTL1 for DCO frequency

BCSCTL1 = CALBC1_1MHZ; // Set range


DCOCTL = CALDCO_1MHZ;
BCSCTL2
MCLK SMCLK

BCSCTL2 |= SELM_3 + DIVM_3; // MCLK = VLO/8


BCSCTL3
In MSP430G2231

BCSCTL3 |= LFXT1S_2; // Enable VLO as MCLK/ACLK src


Recall Sample Code for Timer_A
• Flash red LED at 1 Hz if SMCLK at 800 KHz
#include <msp430g2553.h>
#define LED1 BIT0
void main (void) {
WDTCTL = WDTPW|WDTHOLD; // Stop watchdog timer
P1OUT = ~LED1;
P1DIR = LED1;
TACCR0 = 49999;
TACTL = MC_1|ID_3|TASSEL_2|TACLR; //Setup Timer_A
//up mode, divide clk by 8, use SMCLK, clr timer
for (;;) { // Loop forever
while (!(TACTL&TAIFG)) { // Wait time up
} // doing nothing
TACTL &= ~TAIFG; // Clear overflow flag
P1OUT ^= LED1; // Toggle LEDs
} // Back around infinite loop
}
Sample Code for Setting Clocks
• Set DCO to 1MHz, enable crystal
#include <msp430g2231.h> (#include <msp430g2553.h> )
void main(void) {
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
if (CALBC1_1MHZ ==0xFF || CALDCO_1MHZ == 0xFF)
while(1); // If TLV erased, TRAP!
BCSCTL1 = CALBC1_1MHZ; // Set range
DCOCTL = CALDCO_1MHZ;
P1DIR = 0x41; // P1.0 & 6 outputs (red/green LEDs)
P1OUT = 0x01; // red LED on
BCSCTL3 |= LFXT1S_0; // Enable 32768 crystal
IFG1 &= ~OFIFG;// Clear OSCFault flag
P1OUT = 0; // red LED off
BCSCTL2 |= SELS_0 + DIVS_3; // SMCLK = DCO/8
// infinite loop to flash LEDs
}
Basic Lab
• Flash green LED at 1 Hz by polling Timer_A,
which is driven by SMCLK sourced by DCO
running at 1 MHz.
– Hint: Since TAR register is 16-bit (0~65535) long,
you should be careful of its overflow by using
clock source “Divider”.
• While keeping the green LED flashing at 1 Hz,
pushing the button turns on the red LED and
releasing the button turns off the red LED.
– Hint: Your CPU has to poll two things
simultaneously, Timer_A and button
Bonus
• Whenever the button is pushed, turn off the
green LED and then turn on the red LED for 2
sec. Afterwards, return to normal flashing of
the green LED at 1 Hz.
• Use ACLK to drive Timer_A, sourced from VLO
(running at 12 KHz). The green LED still needs
to flash at 1 Hz.
Microcontroller
MSP430
Asha.S.Patil
Department of Electrical & Electronics Engineering
MICROCONTROLLER

MSP430 – LCD interfacing

Asha.S.Patil
Department of Electrical&Electronics Engineering
MICROCONTROLLER
LCD codes

#include <msp430g2553.h>

#define DR P1OUT = P1OUT | BIT4 // define RS high


#define CWR P1OUT = P1OUT & (~BIT4) // define RS low
#define READ P1OUT = P1OUT | BIT5
// define Read signal R/W = 1 for reading
#define WRITE P1OUT = P1OUT & (~BIT5)
// define Write signal R/W = 0 for writing
#define ENABLE_HIGH P1OUT = P1OUT | BIT6
// define Enable high signal
#define ENABLE_LOW P1OUT = P1OUT & (~BIT6)
// define Enable Low signal
MICROCONTROLLER
LCD codes

void delay(unsigned int k)


{
for(j=0;j<=k;j++)
{
for(i=0;i<10;i++);
}
}

void data_write(void)
{
ENABLE_HIGH;
delay(2);
ENABLE_LOW;
}
MICROCONTROLLER
LCD codes

void data_read(void)
{
ENABLE_LOW;
delay(2);
ENABLE_HIGH;
}

void data_write(void)
{
ENABLE_HIGH;
delay(2);
ENABLE_LOW;
}
MICROCONTROLLER
LCD codes

void check_busy(void)
{
P1DIR &= ~(BIT3); // make P1.3 as input
while((P1IN&BIT3)==1)
{
data_read();
}
P1DIR |= BIT3; // make P1.3 as output
}
MICROCONTROLLER
LCD codes

void send_command(unsigned char cmd)


{
check_busy();
WRITE;
CWR;
P1OUT = (P1OUT & 0xF0)|((cmd>>4) & 0x0F); //
send higher nibble
data_write(); // give enable trigger
P1OUT = (P1OUT & 0xF0)|(cmd & 0x0F); // send
lower nibble
data_write(); // give enable trigger

}
MICROCONTROLLER
LCD codes

void send_data(unsigned char data)


{
check_busy();
WRITE;
DR;
P1OUT = (P1OUT & 0xF0)|((data>>4) & 0x0F); // send
higher nibble
data_write(); // give enable trigger
P1OUT = (P1OUT & 0xF0)|(data & 0x0F); // send lower
nibble
data_write(); // give enable trigger
}
MICROCONTROLLER
LCD codes

void send_string(char *s)


{
while(*s)
{
send_data(*s);
s++;
}
}
MICROCONTROLLER
LCD codes

void lcd_init(void)
{
P1DIR |= 0xFF;
P1OUT &= 0x00;
send_command(0x33);
send_command(0x32);
send_command(0x28); // 4 bit mode
send_command(0x0E); // clear the screen
send_command(0x01); // display on cursor on
send_command(0x06); // increment cursor
send_command(0x80); // row 1 column 1
}
MICROCONTROLLER
LCD codes

Main Code:

#include "lcd.h"

void main(void)
{
WDTCTL = WDTPW + WDTHOLD; // stop watchdog
timer
lcd_init();
send_string("PESU EEE");
send_command(0xC0);
send_string("MICROCONTROLLER");
while(1){}
}
Asha.S.Patil
Department of Electrical & Electronics Engineering

ashaspatil@pes.edu

THANK YOU
Microcontroller
MSP430
Asha.S.Patil
Department of Electrical & Electronics Engineering
MICROCONTROLLER

MSP430 – LCD interfacing

Asha.S.Patil
Department of Electrical&Electronics Engineering
MICROCONTROLLER
MSP430
MICROCONTROLLER
MSP430
MICROCONTROLLER
LCD pin description
MICROCONTROLLER
LCD interfacing diagram
MICROCONTROLLER
LCD connections

Connections:
P1.0 - D4 Pin11
P1.1 - D5 Pin12
P1.2 - D6 Pin13
P1.3 - D7 Pin14
P1.4 - RS Pin4
P1.5 - R/W Pin5
P1.6 - E Pin6
Gnd-Pin 1
5V-Pin 2
3.3V-Anode pin 15
Gnd-CATHODE pin 16
MICROCONTROLLER
LCD 4 bit mode

▪ 4 bit mode working In 4 bit mode we send the data


nibble by nibble, first upper nibble and then lower nibble.

▪ so the lower four bits (D0-D3) of a byte form the lower


nibble while the upper four bits (D4-D7) of a byte form
the higher nibble.

▪ This enables us to send 8 bit data be it the ASCII code


or the command code by using 4 pins instead of 8.

▪ The connections remain identical. The only change is


that the lower nibble pins of LCD are unused.
Asha.S.Patil
Department of Electrical & Electronics Engineering

ashaspatil@pes.edu

THANK YOU
Microcontroller
MSP430
Asha.S.Patil
Department of Electrical & Electronics Engineering
MICROCONTROLLER

MSP430 – motor Control coding example

Asha.S.Patil
Department of Electrical&Electronics Engineering
MICROCONTROLLER
MSP430
MICROCONTROLLER
MSP430
#include "msp430g2553.h"
void main()
{
WDTCTL = WDTPW + WDTHOLD;

void Delay(int j);

P2DIR |= BIT1 + BIT2 +BIT3 + BIT4 ; //


P2.3,P2.4,P2.6 all output
P2OUT &= ~ BIT1 + BIT2+ BIT3 + BIT4; // Clear
P2.3,P2.4,P2.6

// Rotate the Motor clockwise


P2OUT |= BIT3; // P2.3 = 1,P2.4 = 0
P2OUT |=BIT1;

Delay(10); // Rotate motor for sometime


MICROCONTROLLER
MSP430 PWM
// Stop the motor
P2OUT &= ~BIT3 + BIT4;
P2OUT &= ~BIT1 + BIT2;// P2.3 = 0,P2.4 = 0
Delay(5); // Stop the motor for sometime

// Rotate the Motor Counter clockwise


P2OUT &= ~BIT3; // P2.3 = 0
P2OUT |= BIT4; // P2.4 = 1

Delay(10); // Rotate motor for sometime

}
void Delay(int j)
{
int i;
for(j=0;j<10;j++)
for(i=0;i<0xFFFF;i++);
Asha.S.Patil
Department of Electrical & Electronics Engineering

ashaspatil@pes.edu

THANK YOU
Microcontroller
MSP430
Asha.S.Patil
Department of Electrical & Electronics Engineering
MICROCONTROLLER

MSP430 – motor Control

Asha.S.Patil
Department of Electrical&Electronics Engineering
MICROCONTROLLER
MSP430
control two DC brushed motors using MSP430 Launchpad
(MSP430G2553) and L293D Motor Driver IC .
L293D is a quadruple high-current half-H driver IC which can be
used for controlling DC brushed motors,solenoids,relays etc.
MICROCONTROLLER
MSP430
MICROCONTROLLER
MSP430 PWM
The IC contains 4 half H drivers which can be combined together
to create two full H bridge circuits for controlling two DC brushed
motors.
The L293D is designed to provide bidirectional drive currents of
up to 600-mA at voltages from 4.5 V to 36 V, Here we will be
running the motors at 12V.
MICROCONTROLLER
MSP430
Asha.S.Patil
Department of Electrical & Electronics Engineering

ashaspatil@pes.edu

THANK YOU
Microcontroller
MSP430
Asha.S.Patil
Department of Electrical & Electronics Engineering
MICROCONTROLLER

MSP430 – PWM

Asha.S.Patil
Department of Electrical&Electronics Engineering
MICROCONTROLLER
MSP430 PWM
Program to generate PWM signal at P1.2
#include “MSP430.h”
Void main(void)
{
WDTCTL=WDTPW|WDTHOLD;
P1DIR|=0X04; DIRECTION OF PORT PIN p1.2 AS OUTPUT
P1SEL|=0X04;INITIALIZE PORT FOR OUTPUT SIGNAL
OUT1(PWM FUNCTION)
TACCRO=0XFF; COUNT FOR PERIOD(ON+OFF)
TACCR1=0X20; COUNT FOR ON TIME
TACCTL1=OUTMOD_7; RESET/SET OUTPUT MODE
TACTL=TASSEL_1+ID_1+MC_1;ACLK/2,UPMODE,CLEAR ,NO
INTERRUPT
LOW_POWER_MODE_3();
WHILE(1);
}
MICROCONTROLLER
MSP430 PWM
Timer Frequency=32KHz/2=16KHz

Time for one clock cycle=1/16KHz=0.0625msec

Total period T=count in TACCR0*Time for 1 clock cycle


=256 *0.0625msec
=1.25msec
Asha.S.Patil
Department of Electrical & Electronics Engineering

ashaspatil@pes.edu

THANK YOU
Microcontroller
MSP430
Asha.S.Patil
Department of Electrical & Electronics Engineering
MICROCONTROLLER

MSP430 – PWM

Asha.S.Patil
Department of Electrical&Electronics Engineering
MICROCONTROLLER
MSP430 PWM

PWM is used to create digital signals in the form logical


high and logical low signals.

PWM is used to control voltage magnitude across load.

PWM is also used to control speed of dc motor by


applying variable voltage across dc motor.
MICROCONTROLLER
MSP430 PWM
What is pulse width modulation?
Pulse width modulation which is also known as PWM is a
special type of digital signal which remain high for some
time and remain low for some.

For example, if timer period of PWM signal is 20ms which


is 50 hertz in frequency, if the logical high time is 10ms
and logical low time of signal is 10ms.

So, we will say the on time of PWM is 10ms and off time
is 10ms.
MICROCONTROLLER
MSP430 PWM

Two method are used to generate pulse width


modulation:

Using analog components like operational amplifier


Digital devices like microcontrollers
MICROCONTROLLER
MSP430 PWM
Timer period is a total time of PWM which is equal to on
time plus off time.

Time period = on time + off timer


Duty cycle is a most important parameter used for pulse
width modulation. It defines the percentage of time the
signal is high.

Duty Cycle = on time / (on time + off time)


Duty cycle = on time / time period
MICROCONTROLLER
MSP430 PWM
MICROCONTROLLER
MSP430 PWM

There are three to five capture/compare blocks inside Timer_A of MSP430 controller.

Any one of the blocks may be used to capture timer data or generate intervals.

The compare mode is used to generate PWM output signals.


MICROCONTROLLER
MSP430 PWM

Timer_A is used to generate PWM signals

In Timer_A ,the TACCR0 is stored for total period value of PWM and
TACCR1 is used to store on period of PWM duty cycle.

The Timer_A TAR register is in upmode and gets incremented for every clock given by selected clock source.
MICROCONTROLLER
MSP430 PWM

Setting PWM mode through the CCTLx register


MICROCONTROLLER
MSP430 PWM

To use the PWM to achieve a duty cycle of 20%, we would use the following code:

CCTL1 = OUTMOD_7;
CCR1 = 200 - 1;
Asha.S.Patil
Department of Electrical & Electronics Engineering

ashaspatil@pes.edu

THANK YOU
Microcontroller
MSP430
Asha.S.Patil
Department of Electrical & Electronics Engineering
MICROCONTROLLER

MSP430 – Basics of Analog to Digital Converter

Asha.S.Patil
Department of Electrical&Electronics Engineering
MICROCONTROLLER
Basics of ADC

There are 2 types of ADC, ADC10 and ADC12

The ADC10 is a 10bit analog to digital conversion and the ADC12 is a


12bit analog to digital conversion.

The MSP430G2253 only incorporates the ADC10, apart from having


greater resolution the ADC12 does not differ that much from the
ADC10.
MICROCONTROLLER
Basics of ADC

what 10bit resolution is and how this maybe relevant to the accuracy of
a project.
10bit means 2 to the power of 10, which equals 1024, the 1024 is the
number of sample steps taken over a given range (in the case of
microcontrollers it’s actually 0 to 1023).
The range is defined as what voltage range the ADC accepts, to find
this we need to see the datasheet for the MSP430G2253.
MICROCONTROLLER
Basics of ADC

if 1.5V was fed into the ADC input.


MICROCONTROLLER
ADC

ADC10AE0 |= 0x02; (0x02 is selecting the ADC A1 or port P1.1).


MICROCONTROLLER
ADC
MICROCONTROLLER
ADC
Below is a table describing the portions of these control registers that are essential
for basic use of the ADC.
MICROCONTROLLER
ADC
Configuring the ADC The ADC can be configured with only a few lines of code.
Below is an example of a simple configuration function, written in the C
programming language.

void ADC_capture(void)
{
/* Configure ADC Channel */
ADC10CTL0 = ADC10SHT_3 + ADC10ON + ADC10IE; //64 clk ticks, ADC on, enable interrupt

ADC10CTL1 = ADC10SSEL_0 + INCH_5; //SMCLK, channel 5

ADC10CTL0 |= ENC + ADC10SC; //Enable and start conversion

while ((ADC10CTL1 & ADC10BUSY) == 0x01); //Wait for conversion to end


Asha.S.Patil
Department of Electrical & Electronics Engineering

ashaspatil@pes.edu

THANK YOU
Microcontroller
MSP430
Asha.S.Patil
Department of Electrical & Electronics Engineering
MICROCONTROLLER

MSP430 – Stepper motor

Asha.S.Patil
Department of Electrical&Electronics Engineering
MICROCONTROLLER
Stepper Motor

There are actually many ways you can interface a stepper motor to
your controller, out of them the most used interfaces are:

Interface using L293D - H-Bridge Motor Driver


Interface using ULN2003/2004 - Darlington Arrays
MICROCONTROLLER
Stepper Motor
MICROCONTROLLER
Stepper Motor
MICROCONTROLLER
Stepper Motor

For n=200
360/200=1.8
1.8x4=7.2(for full step)
If we want to rotate for 180 degree
180/7.2=25

So loop count =25


MICROCONTROLLER
Stepper Motor

#include <MSP430g2553.H>
#define stepper P1

void delay()
{
unsigned char i,j,k;
for(i=0;i<6;i++) {
for(j=0;j<255;j++)
for(k=0;k<255;k++);
}
}
MICROCONTROLLER
Stepper Motor

void main()
{
while (1) {
stepper = 0x0C;
delay();
stepper = 0x06;
delay();
stepper = 0x03;
delay();
stepper = 0x09;
delay();
}
}
MICROCONTROLLER
Stepper Motor

The bipolar stepper motor

usually has four wires coming out of it. Unlike unipolar steppers, bipolar
steppers have no common center connection.

They have two independent sets of coils instead


MICROCONTROLLER
Stepper Motor

Bipolar stepper motor


MICROCONTROLLER
Stepper Motor

Working

When we energize a coil of stepper motor, The shaft of stepper


motor (which is actually a permanent magnet) align itself
according to poles of energized coil.

So when motor coils are energized in a particular sequence,


motor shaft tend to align itself according to pole of coils and
hence rotates.
MICROCONTROLLER
Stepper Motor

Stepper motors can be driven in two different patterns or


sequences.
namely,
Full Step Sequence

Half Step Sequence


MICROCONTROLLER
Stepper Motor

wave Step Sequence

step A B C D

1 1 0 0 0

2 0 1 0 0

3 0 0 1 0

4 0 0 0 1
MICROCONTROLLER
Stepper Motor

Full Step Sequence

step A B C D

1 1 1 0 0

2 0 1 1 0

3 0 0 1 1

4 1 0 0 1
MICROCONTROLLER
Stepper Motor
Half Step Sequence

step A B C D
1 1 0 0 0
2 1 1 0 0
3 0 1 0 0
4 0 1 1 0
5 0 0 1 0
6 0 0 1 1
7 0 0 0 1
8 1 0 0 1
Asha.S.Patil
Department of Electrical & Electronics Engineering

ashaspatil@pes.edu

THANK YOU
Microcontroller
MSP430
Asha.S.Patil
Department of Electrical & Electronics Engineering
MICROCONTROLLER

MSP430 – Stepper motor

Asha.S.Patil
Department of Electrical&Electronics Engineering
MICROCONTROLLER
Stepper Motor

Stepper motors can be used in various areas of your microcontroller


projects such as making robots, robotic arm, automatic door lock
system etc.

Types of stepper motors (unipolar and bipolar stepper motors ),

different controlling types (Half step and Full step),

Interfacing Techniques (using L293D or ULN2003) and programming


your microcontroller in C and assembly to control stepper motor.
MICROCONTROLLER
Stepper Motor

Unipolar stepper motor

The unipolar stepper motor has five or six wires and four coils (actually
two coils divided by center connections on each coil). The center
connections of the coils are tied together and used as the power
connection. They are called unipolar steppers because power always
comes in on this one pole.
MICROCONTROLLER
Stepper Motor

Unipolar stepper motor

The unipolar stepper motor has five or six wires and four coils (actually
two coils divided by center connections on each coil).
The center connections of the coils are tied together and used as the
power connection.
They are called unipolar steppers because power always comes in on
this one pole.
MICROCONTROLLER
Stepper Motor

The bipolar stepper motor

usually has four wires coming out of it. Unlike unipolar steppers, bipolar
steppers have no common center connection.

They have two independent sets of coils instead


MICROCONTROLLER
Stepper Motor

Bipolar stepper motor


MICROCONTROLLER
Stepper Motor

Working

When we energize a coil of stepper motor, The shaft of stepper


motor (which is actually a permanent magnet) align itself
according to poles of energized coil.

So when motor coils are energized in a particular sequence,


motor shaft tend to align itself according to pole of coils and
hence rotates.
MICROCONTROLLER
Stepper Motor

Stepper motors can be driven in two different patterns or


sequences.
namely,
Full Step Sequence

Half Step Sequence


MICROCONTROLLER
Stepper Motor

wave Step Sequence

step A B C D

1 1 0 0 0

2 0 1 0 0

3 0 0 1 0

4 0 0 0 1
MICROCONTROLLER
Stepper Motor

Full Step Sequence

step A B C D

1 1 1 0 0

2 0 1 1 0

3 0 0 1 1

4 1 0 0 1
MICROCONTROLLER
Stepper Motor
Half Step Sequence

step A B C D
1 1 0 0 0
2 1 1 0 0
3 0 1 0 0
4 0 1 1 0
5 0 0 1 0
6 0 0 1 1
7 0 0 0 1
8 1 0 0 1
Asha.S.Patil
Department of Electrical & Electronics Engineering

ashaspatil@pes.edu

THANK YOU
Microcontroller
MSP430
Asha.S.Patil
Department of Electrical & Electronics Engineering
MICROCONTROLLER

MSP430 – Serial Communication

Asha.S.Patil
Department of Electrical&Electronics Engineering
MICROCONTROLLER
SERIAL COMMUNICATION

• A line driver such as the MAX232 chip is required to convert


RS232 voltage levels to TTL levels, and vice versa
• 8051 has two pins that are used specifically for transferring and
receiving data serially
• These two pins are called TxD and RxD
• Part of the port 3 group (P3.0 and P3.1)
• These pins are TTL compatible
• They require a line driver to make them RS232 compatible
MICROCONTROLLER
SERIAL COMMUNICATION
MICROCONTROLLER
SERIAL COMMUNICATION
MAX233
• To save board space, some designers use MAX233 chip from
Maxim
• MAX233 performs the same job as MAX232
• But eliminates the need for capacitors
• MAX233 and MAX232 are not pin compatible
Asha.S.Patil
Department of Electrical & Electronics Engineering

ashaspatil@pes.edu

THANK YOU
Microcontroller
MSP430
Asha.S.Patil
Department of Electrical & Electronics Engineering
MICROCONTROLLER

MSP430 – Serial Communication

Asha.S.Patil
Department of Electrical&Electronics Engineering
MICROCONTROLLER
i2C

Invented by Philips as chip-chip interconnect for TV sets


2 signal lines, Vcc and Gnd
SDA (serial data) and SCL (serial clock)
Both bi-directional (actively pulled down)
7-bit slave address
Data in 8-bit bytes – 100 kbps – 2.4 mbps
MICROCONTROLLER
i2C
MICROCONTROLLER
i2C
MICROCONTROLLER
i2C

• Start/Stop closely tied to physical structure


• SDA falling/rising when SCK is high
• Data must remain stable while sck is high, may change while low
• Slow slave and stretch clock
• Multi-master arbitration by detection of failure to drive
Asha.S.Patil
Department of Electrical & Electronics Engineering

ashaspatil@pes.edu

THANK YOU
Microcontroller
MSP430
Asha.S.Patil
Department of Electrical & Electronics Engineering
MICROCONTROLLER

MSP430 – Serial Communication

Asha.S.Patil
Department of Electrical&Electronics Engineering
MICROCONTROLLER
Serial Peripheral Interface

• Serial Peripheral Interface – SPI


• It is a synchronous serial data link standard named by Motorola that operates in full duplex
mode
• Devices communicate in master/slave mode where the master device initiates the data frame.
Multiple slave devices are allowed with individual slave select (chip select) lines.
• The SPI bus specifies four logic signals.
• SCLK — Serial Clock (output from master)
• MOSI/SIMO — Master Output, Slave Input (output from master)
• MISO/SOMI — Master Input, Slave Output (output from slave)
• SS — Slave Select (active low; output from master)
SPI Mode: Signal Definition
• SIMO Slave in, master out
• Master mode: SIMO is the data output line.
• Slave mode: SIMO is the data input line.
• SOMI Slave out, master in
• Master mode: SOMI is the data input line.
• Slave mode: SOMI is the data output line.
• UCLK USART SPI clock
• Master mode: UCLK is an output.
• Slave mode: UCLK is an input.
• STE Slave transmit enable. Used in 4-pin mode to allow multiple masters on a single bus. Not used in 3-pin
mode.
• 4-Pin master mode:
• When STE is high, SIMO and UCLK operate normally.
• When STE is low, SIMO and UCLK are set to the input direction.
• 4-pin slave mode:
• When STE is high, RX/TX operation of the slave is disabled and SOMI is forced to the input direction.
• When STE is low, RX/TX operation of the slave is enabled and SOMI operates normally.
USART: SPI Mode
SPI Mode: Initialization Sequence
• 1) Set SWRST (BIS.B #SWRST,&UxCTL)
• 2) Initialize all USART registers with SWRST=1 (including UxCTL)
• 3) Enable USART module via the MEx SFRs (USPIEx)
• 4) Clear SWRST via software (BIC.B #SWRST,&UxCTL)
• 5) Enable interrupts (optional) via the IEx SFRs (URXIEx and/or UTXIEx)
• Note: Failure to follow this process may result in unpredictable USART behavior.
SPI Master Mode
• The USART initiates data transfer when data is moved to the UxTXBUF. The UxTXBUF data is moved to the TX shift register when the TX
shift register is empty, initiating data transfer on SIMO starting with the MSB. Data on SOMI is shifted into the receive shift register on the
opposite clock edge, starting with the most-significant bit.
• When the character is received, the receive data is moved from the RX shift register to the UxRXBUF and the receive interrupt flag,
URXIFGx, is set, indicating the RX/TX operation is complete.
• A set transmit interrupt flag, UTXIFGx, indicates that data has moved from UxTXBUF to the TX shift register and UxTXBUF is ready for new
data. It does not indicate RX/TX completion.
• To receive data into the USART in master mode, data must be written to UxTXBUF because receive and transmit operations operate
concurrently.
• In 4-pin master mode, STE is used to prevent conflicts with another master. The master operates normally when STE is high. When STE is
low:
• SIMO and UCLK are set to inputs and no longer drive the bus
• The error bit FE is set indicating a communication integrity violation to be handled by the user
SPI Slave Mode
• UCLK is used as the input for the SPI clock and must be supplied by the external master. The data-transfer rate is determined by
this clock and not by the internal baud rate generator.
• Data written to UxTXBUF and moved to the TX shift register before the start of UCLK is transmitted on SOMI. Data on SIMO is
shifted into the receive shift register on the opposite edge of UCLK and moved to UxRXBUF when the set number of bits are
received
• When data is moved from the RX shift register to UxRXBUF, the URXIFGx interrupt flag is set, indicating that data has been
received. The overrun error bit, OE, is set when the previously received data is not read from UxRXBUF before new data is
moved to UxRXBUF.
• In 4-pin slave mode, STE is used by the slave to enable the transmit and receive operations and is provided by the SPI master.
When STE is low, the slave operates normally. When STE is high:
• Any receive operation in progress on SIMO is halted
• SOMI is set to the input direction
C Examples, SPI Full-Duplex
//******************************************************************************
#include <msp430x14x.h>
// MSP-FET430P140 Demo - USART0, SPI Full-Duplex 3-Wire Slave P1.x Exchange

//
void main(void)
// Description: SPI Master communicates at fast as possible, full-duplex with
{
// SPI Slave using 3-wire mode. The level on P1.4/5 is TX'ed and RX'ed to P1.0

// and P1.1. Master will pulse slave Reset on init to insure synch start.

// Slave normal mode is LPM4. WDTCTL = WDTPW + WDTHOLD; // Stop watchdog

// ACLK = n/a, MCLK = SMCLK = DCO ~ 800kHz, ULCK = external P1OUT = 0x00; // P1.0 setup for LED output
// P1DIR |= 0x03;
// fet140_slav0 fet140_mstr0
P3SEL |= 0x0E; // P3.1,2,3 SPI option select
// MSP430F169 Slave MSP430F169 Master
U0CTL = CHAR + SYNC + SWRST; // 8-bit, SPI
// ----------------- -----------------
U0TCTL = CKPL + STC; // Polarity, 3-wire
// | XIN|- /|\| XIN|-
U0BR0 = 0x02; // SPICLK = SMCLK/2
// | | | | |
U0BR1 = 0x00;
// | XOUT|- --|RST XOUT|-

// | | /|\ | | U0MCTL = 0x00;

// | RST|--+<----|P3.0 | ME1 |= USPIE0; // Module enable


// LED <-|P1.0 | | P1.4|<- U0CTL &= ~SWRST; // SPI enable
// LED <-|P1.1 | | P1.5|<- IE1 |= URXIE0 + UTXIE0; // RX and TX int. enable
// ->|P1.4 | | P1.0|-> LED
_BIS_SR(LPM4_bits + GIE); // Enter LPM4 w/ int.
// ->|P1.5 | | P1.1|-> LED
}
// | SIMO0/P3.1|<-------|P3.1 |
#pragma vector=USART0RX_VECTOR
// | SOMI0/P3.2|------->|P3.2 |
__interrupt void SPI0_rx (void) {
// | UCLK/P3.3|<-------|P3.3 |
Asha.S.Patil
Department of Electrical & Electronics Engineering

ashaspatil@pes.edu

THANK YOU
Microcontroller
MSP430
Asha.S.Patil
Department of Electrical & Electronics Engineering
MICROCONTROLLER

MSP430 – Serial Communication

Asha.S.Patil
Department of Electrical&Electronics Engineering
MICROCONTROLLER
SERIAL COMMUNICATION
MICROCONTROLLER
SERIAL COMMUNICATION

In MSP430, serial communication is handled by an on chip


peripheral called USCI (Universal Serial
Communications Interface​).

The peripheral is designed in such a way that it can handle multiple


serial communication formats ,synchronous as well as
asynchronous like SPI,I2C,IrDA,UART
MICROCONTROLLER
SERIAL COMMUNICATION

MSP430G2553
has two USCI modules names
as USCI_A0 and USCI_B0 for handling multiple
communication formats.
MICROCONTROLLER
SERIAL COMMUNICATION

USCI_A0 can be configured to handle LIN,IrDA,SPI and


asynchronous serial communication (UART) while USCI_B0 can
handle SPI and I2C.
MICROCONTROLLER
SERIAL COMMUNICATION

we will configure USCI_A0 in MSP430G2553 to handle


asynchronous serial communication or commonly known as UART
mode.
The UART mode uses two pins to transmit(UCA0TXD​) and receive
data(UCA0RXD​) .
Asynchronous serial communication is widely used for
communicating with external devices like PC/Laptops,GSM
modems,GPS modules etc.
It also forms the basis for many standard protocols
like RS232,RS422,RS485 etc.
MICROCONTROLLER
SERIAL COMMUNICATION
Simplified block diagram of USCI_A0 in UART mode below.
Asha.S.Patil
Department of Electrical & Electronics Engineering

ashaspatil@pes.edu

THANK YOU
Microcontroller
MSP430
Asha.S.Patil
Department of Electrical & Electronics Engineering
MICROCONTROLLER

MSP430 – Serial Communication

Asha.S.Patil
Department of Electrical&Electronics Engineering
MICROCONTROLLER
SERIAL COMMUNICATION

Configuring the Baud rate


MSP430 data sheet has an in depth description about configuring the
baud rate for various modes.
Easiest way to configure the baud rate of a MSP430 device is to use
the Baud rate selection table listed in the usermanual.
Just find out the UCBRx values corresponding to your BRCLK frequency
and the baud rate you wish to use .
After finding out values just put them in the UCA0BR0 and UCA0BR1
register to configure the baud rate.
MICROCONTROLLER
SERIAL COMMUNICATION

Find the below table in the MSP430x2xxx usermanual.


MICROCONTROLLER
SERIAL COMMUNICATION

In case we are using SMCLK @ 1MHz as BRCLK and 9600bps


as the baud rate .Now check the table for the corresponding
UCBRx value (here 104) and put them in UCA0BR0 and
UCA0BR1 registers to configure the baud rate.
MICROCONTROLLER
SERIAL COMMUNICATION

Another register you should consider while configuring the baud


rate is the UCAxMCTL(UCA0MCTL) which is used to select the
modulation stage.Put the UCBRSx value (here 1) corresponding
to the selected baud rate in the UCA0MCTL register to configure
the modulation stage
MICROCONTROLLER
SERIAL COMMUNICATION

UCA0CTL1 |= UCSSEL_2 + UCSWRST; // USCI Clock = SMCLK,USCI_A0


disabled
UCA0BR0 = 104; // 104 From datasheet table-
UCA0BR1 = 0; // -selects baudrate =9600,clk = SMCLK
UCA0MCTL = UCBRS_1; // Modulation value = 1 from datasheet
UCA0STAT |= UCLISTEN; // loop back mode enabled
UCA0CTL1 &= ~UCSWRST; // Clear UCSWRST to enable USCI_A0

UCA0TXBUF = 'A';
Asha.S.Patil
Department of Electrical & Electronics Engineering

ashaspatil@pes.edu

THANK YOU
Microcontroller
MSP430
Asha.S.Patil
Department of Electrical & Electronics Engineering
MICROCONTROLLER

MSP430 – Serial Communication

Asha.S.Patil
Department of Electrical&Electronics Engineering
MICROCONTROLLER
SERIAL COMMUNICATION

Now when you are interfacing with a PC RS232 serial port ,you require
atleast 3 lines RXD ,TXD andGround to communicate with the
Launchpad board.
The RS232 signals coming from the PC serial serial port is in the range
of +15 to -15V so you can't connect it directly to the microcontroller
pins.You have to build a RS232 level converter(for eg MAX232) to
convert between RS232 and TTL signal levels.
The below figure shows the connection diagram between an MSP430
Launch pad and PC DB9 serial port.
We can see that UCA0RXD (Blue Wire) the receive pin of
microcontroller is connected to the Transmit pin of PC serial port.
MICROCONTROLLER
SERIAL COMMUNICATION
MICROCONTROLLER
SERIAL COMMUNICATION

In case we are using SMCLK @ 1MHz as BRCLK and 9600bps


as the baud rate .Now check the table for the corresponding
UCBRx value (here 104) and put them in UCA0BR0 and
UCA0BR1 registers to configure the baud rate.
MICROCONTROLLER
SERIAL COMMUNICATION

Another register you should consider while configuring the baud


rate is the UCAxMCTL(UCA0MCTL) which is used to select the
modulation stage.Put the UCBRSx value (here 1) corresponding
to the selected baud rate in the UCA0MCTL register to configure
the modulation stage
MICROCONTROLLER
SERIAL COMMUNICATION

UCA0CTL1 |= UCSSEL_2 + UCSWRST; // USCI Clock = SMCLK,USCI_A0


disabled
UCA0BR0 = 104; // 104 From datasheet table-
UCA0BR1 = 0; // -selects baudrate =9600,clk = SMCLK
UCA0MCTL = UCBRS_1; // Modulation value = 1 from datasheet
UCA0STAT |= UCLISTEN; // loop back mode enabled
UCA0CTL1 &= ~UCSWRST; // Clear UCSWRST to enable USCI_A0

UCA0TXBUF = 'A';
Asha.S.Patil
Department of Electrical & Electronics Engineering

ashaspatil@pes.edu

THANK YOU
Microcontroller
MSP430
Asha.S.Patil
Department of Electrical & Electronics Engineering
MICROCONTROLLER

MSP430 – Serial Communication

Asha.S.Patil
Department of Electrical&Electronics Engineering
MICROCONTROLLER
SERIAL COMMUNICATION

Start and Stop Bits


A protocol is a set of rules agreed by both the sender and receiver on:
How the data is packed
How many bits constitute a character
When the data begins and ends
Asynchronous serial data communication is widely used for character-oriented transmissions
Each character is placed in between start and stop bits, this is called framing
MICROCONTROLLER
SERIAL COMMUNICATION
MICROCONTROLLER
SERIAL COMMUNICATION

Start and Stop Bits (cont.)


Due to the extended ASCII characters, 8-bit ASCII data is common
In older systems, ASCII characters were 7-bit
In modern PCs the use of one stop bit is standard
In older systems, two stop bits were used
Due to the slowness of the receiving mechanical device
To give the device sufficient time to organize itself before transmission of the next byte
MICROCONTROLLER
SERIAL COMMUNICATION

Start and Stop Bits (cont.)


Assuming that we are transferring a text file of ASCII characters using 1 stop bit
We have a total of 10 bits for each character
This gives 20% overhead
In some systems, to maintain data integrity, the parity bit of the character byte is included in the data frame
UART chips allow programming of the parity bit for odd-, even-, and no-parity options
MICROCONTROLLER
SERIAL COMMUNICATION

Data Transfer
The rate of data transfer in serial data communication is stated in bps (bits per second)
Another widely used terminology for bps is baud rate
It is modem terminology and is defined as the number of signal changes per second
In modems, there are occasions when a single change of signal transfers several bits of data
MICROCONTROLLER
SERIAL COMMUNICATION

Data Transfer
As far as the conductor wire is concerned, the baud rate and bps are the same
The data transfer rate of given computer system depends on communication ports
incorporated into that system
IBM PC/XT could transfer data at the rate of 100 to 9600 bps
Pentium-based PCs transfer data at rates as high as 56K bps
In asynchronous serial data communication, the baud rate is limited to 100K bps
MICROCONTROLLER
SERIAL COMMUNICATION

RS232 Standards
An interfacing standard RS232 was set by the Electronics Industries Association (EIA) in 1960
The standard was set long before the advent of the TTL logic family, its input and output voltage levels are n
A 1 is represented by -3 ~ -25 V
A 0 bit is +3 ~ +25 V
Making -3 to +3 undefined
MICROCONTROLLER
SERIAL COMMUNICATION
MICROCONTROLLER
SERIAL COMMUNICATION

RS232 Standards (cont.)


Since not all pins are used in PC cables, IBM introduced the DB-9 version of the serial I/O standard
MICROCONTROLLER
SERIAL COMMUNICATION

Data Communication Classification


Current terminology classifies data communication equipment as:
DTE (data terminal equipment)
Terminal and computers that send and receive data
DCE (data communication equipment)
Communication equipment, such as modems
The simplest connection between a PC and microcontroller at least requires:
Three pins, TxD, RxD, and ground
MICROCONTROLLER
SERIAL COMMUNICATION

DTR (data terminal ready)


When terminal is turned on,
it sends out signal DTR to indicate that it is ready for communication
DSR (data set ready)
When DCE is turned on and has gone through the self-test,
it assert DSR to indicate that it is ready to communicate
MICROCONTROLLER
SERIAL COMMUNICATION

RTS (request to send)


When the DTE device has byte to transmit,
it asserts RTS to signal the modem that it has a byte of data to transmit
CTS (clear to send)
When the modem has room for storing the data it is to receive,
it sends out signal CTS to DTE to indicate that it can receive the data now
MICROCONTROLLER
SERIAL COMMUNICATION

DCD (data carrier detect)


The modem asserts signal DCD to inform the
DTE that a valid carrier has been detected and that contact between it and
the other modem is established
RI (ring indicator)
An output from the modem and an input to a PC indicates that the telephone is ringing
It goes on and off in synchronous with the ringing sound
Asha.S.Patil
Department of Electrical & Electronics Engineering

ashaspatil@pes.edu

THANK YOU
Microcontroller
MSP430
Asha.S.Patil
Department of Electrical & Electronics Engineering
MICROCONTROLLER

MSP430 – Serial Communication

Asha.S.Patil
Department of Electrical&Electronics Engineering
MICROCONTROLLER
SERIAL COMMUNICATION

Computers transfer data in two ways:


Parallel
Often 8 or more lines (wire conductors) are used to transfer data to a device that is
only a few feet away
Serial
To transfer to a device located many meters away, the serial method is used
The data is sent one bit at a time
MICROCONTROLLER
SERIAL COMMUNICATION
At the transmitting end, the byte of data must be converted to serial bits
Using a parallel-in-serial-out shift register
At the receiving end, the serial data is received and packed into byte
Using a serial-in-parallel-out shift register
MICROCONTROLLER
SERIAL COMMUNICATION
When the distance is short, the digital signal can be transferred as it is on a simple wire
and requires no modulation
If data is to be transferred on the telephone line, it must be converted from 0s and 1s to
audio tones
This conversion is performed by a device called a modem, “Modulator/demodulator”
MICROCONTROLLER
SERIAL COMMUNICATION
Serial data communication uses two methods:
Synchronous method transfers a block of data at a time
Asynchronous method transfers a single byte at a time
It is possible to write software to use either of these methods
The programs can be tedious and long
MICROCONTROLLER
SERIAL COMMUNICATION
Half- and Full-Duplex Transmission
If data can be transmitted and received, it is a duplex transmission
If data transmitted one way a time, it is referred to as half duplex
If data can go both ways at a time, it is full duplex
This is contrast to simplex transmission
MICROCONTROLLER
SERIAL COMMUNICATION
Half- and Full-Duplex Transmission
If data can be transmitted and received, it is a duplex transmission
If data transmitted one way a time, it is referred to as half duplex
If data can go both ways at a time, it is full duplex
This is contrast to simplex transmission
MICROCONTROLLER
SERIAL COMMUNICATION
Asha.S.Patil
Department of Electrical & Electronics Engineering

ashaspatil@pes.edu

THANK YOU

You might also like