Microcontrollers 8051 MSP430 Notes For IV Sem ECE Students
Microcontrollers 8051 MSP430 Notes For IV Sem ECE Students
4 Sem ECE
Microcontrollers
4 Sem ECE
SYLLABUS
MICROCONTROLLERS
(Common to EC/TC/EE/IT/BM/ML)
Sub Code : 10ES42 IA Marks : 25
Hrs/ Week : 04 Exam Hours : 03
Total Hrs. : 52 Exam Marks : 100
UNIT 1: Microprocessors and microcontroller. Introduction, Microprocessors and
Microcontrollers, RISC & CISC CPU Architectures, Harvard & Von- Neumann CPU
architecture, Computer software. The 8051 Architecture: Introduction, Architecture of
8051, Pin diagram of 8051, Memory organization, External Memory interfacing, Stacks.
6 Hrs
UNIT 2: Addressing Modes: Introduction, Instruction syntax, Data types, Subroutines,
Addressing modes: Immediate addressing , Register addressing, Direct addressing, Indirect
addressing, relative addressing, Absolute addressing, Long addressing, Indexed addressing,
Bit inherent addressing, bit direct
addressing. Instruction set: Instruction timings, 8051 instructions: Data transfer
instructions, Arithmetic instructions, Logical instructions, Branch instructions, Subroutine
instructions, Bit manipulation instruction.
6 Hrs
UNIT 3: 8051 programming: Assembler directives, Assembly language programs and
Time delay calculations.
6 Hrs
UNIT 4: 8051 Interfacing and Applications: Basics of I/O concepts, I/O Port Operation,
Interfacing 8051 to LCD, Keyboard, parallel and serial ADC, DAC, Stepper motor interfacing
and DC motor interfacing and programming
7 Hrs
UNIT 5: 8051 Interrupts and Timers/counters: Basics of interrupts, 8051 interrupt
structure, Timers and Counters, 8051 timers/counters, programming 8051 timers in
assembly and C .
6 Hrs
UNIT 6: 8051 Serial Communication: Data communication, Basics of Serial Data
Communication, 8051 Serial Communication, connections to RS-232, Serial communication
Programming in assembly and C.
8255A Programmable Peripheral Interface:, Architecture of 8255A, I/O addressing,, I/O
devices interfacing with 8051 using 8255A.
6 Hrs
Course Aim The MSP430 microcontroller is ideally suited for development of low-power
embedded systems that must run on batteries for many years. There are also applications
where MSP430 microcontroller must operate on energy harvested from the environment. This
is possible due to the ultra-low power operation of MSP430 and the fact that it provides a
complete system solution including a RISC CPU, flash memory, on-chip data converters and
on-chip peripherals.
Saneesh Cleatus Thundiyil
Microcontrollers
4 Sem ECE
UNIT 7:
Motivation for MSP430microcontrollers Low Power embedded systems, On-chip
peripherals (analog and digital), low-power RF capabilities. Target applications (Singlechip, low cost, low power, high performance system design).
2 Hrs
MSP430 RISC CPU architecture, Compiler-friendly features, Instruction set, Clock system,
Memory subsystem. Key differentiating factors between different MSP430 families. 2 Hrs
Introduction to Code Composer Studio (CCS v4). Understanding how to use CCS for
Assembly, C, Assembly+C projects for MSP430 microcontrollers. Interrupt programming.
3 Hrs
Digital I/O I/O ports programming using C and assembly, Understanding the muxing
scheme of the MSP430 pins.
2 Hrs
UNIT 8:
On-chip peripherals. Watchdog Timer, Comparator, Op-Amp, Basic Timer, Real Time
Clock (RTC), ADC, DAC, SD16, LCD, DMA.
2 Hrs
Using the Low-power features of MSP430. Clock system, low-power modes, Clock
request feature, Low-power programming and Interrupt.
2 Hrs
Interfacing LED, LCD, External memory. Seven segment LED modules interfacing.
Example Real-time clock.
2 Hrs
Case Studies of applications of MSP430 - Data acquisition system, Wired Sensor network,
Wireless sensor network with Chipcon RF interfaces.
3 Hrs
TEXT BOOKS:
1. The 8051 Microcontroller and Embedded Systems using assembly and C -,
Muhammad Ali Mazidi and Janice Gillespie Mazidi and Rollin D. McKinlay; PHI, 2006 /
Pearson, 2006
2. MSP430 Microcontroller Basics, John Davies, Elsevier, 2010 (Indian edition
available)
REFERENCE BOOKS:
1. The 8051 Microcontroller Architecture, Programming & Applications, 2e Kenneth
J. Ayala ;, Penram International, 1996 / Thomson Learning 2005.
2. The 8051 Microcontroller, V.Udayashankar and MalikarjunaSwamy, TMH, 2009
3. MSP430 Teaching CD-ROM, Texas Instruments, 2008 (can be requested
http://www.uniti.in )
4. Microcontrollers: Architecture, Programming, Interfacing and System Design,Raj
Kamal, Pearson Education, 2005
Microcontrollers
4 Sem ECE
UNIT - 1
1.1 MICROPROCESSORS AND MICROCONTROLLERS
Microprocessor
Microcontroller
ALU
Timer/
Counter
Accumulator
Accumulator
Registers
Working Registers
Internal RAM
Program Counter
Stack Pointer
Clock Circuit
Interrupt circuit
Internal
ROM
Stack Pointer
IO Ports
Interrupt
Circuits
Clock
Circuits
Program Counter
Block diagram of microcontroller
Single memory map for data and code Separate memory map for data and code
(program)
(program)
Access time for memory and IO are more
Microprocessor
based
additional hardware
system
Large number of instructions with flexible Limited number of instructions with few
addressing modes
addressing modes
Microcontrollers
4 Sem ECE
CISC
Only load/store instructions are used to access In additions to load and store instructions,
memory
memory access is possible with other
instructions also.
Instructions executed by hardware
Few instructions
Highly pipelined
Less pipelined
Microcontrollers
4 Sem ECE
Harvard architecture
Data
Program
Memory
Data
CPU
CPU
Data
Memory
Address Bus
Data
Memory
Address Bus
Data
Program
Memory
Address Bus
Von-Neumann (Princeton architecture)
It uses single memory
instructions and data.
space
for
Harvard architecture
both It has separate program memory and data
memory
It is not possible to fetch instruction code and Instruction code and data can be fetched
data
simultaneously
Execution of instruction takes more machine Execution of instruction takes less machine
cycle
cycle
Uses CISC architecture
Also known as control flow or control driven Also known as data flow or data driven
computers
computers
Simplifies the chip design because of single Chip design is complex due to separate memory
memory space
space
Eg. 8085, 8086, MC6800
Microcontrollers
4 Sem ECE
Microcontrollers
4 Sem ECE
PSW
Port 0
SFR
General
Purpose
RAM
PC
E
A
ALE
PSEN
XTAL1
XTAL2
System
Timing
System
interrupt
timers
RESET
Data
buffers
Port 1
Memory
control
I/O
A8A15
ROM
Port 3
General
purpose
area
Bit addressible
area
Register Bank 3
Register Bank 2
Register Bank 1
VCC
GND
D0-D7
I/O
Port 2
DPTR
DPH
DPL
I/O
A0-A7
Register Bank 0
I/O
INT
CNTR
SERIAL
RD/WR
IE
IP
PCON
SBUF
SCON
TCON
TMOD
TL0
TH0
TL1
TH1
SFR and
General Purpose RAM
8051 has 4 K Bytes of internal ROM. The address space is from 0000 to 0FFFh. If the
program size is more than 4 K Bytes 8051 will fetch the code automatically from external
memory.
Accumulator is an 8 bit register widely used for all arithmetic and logical operations.
Accumulator is also used to transfer data between external memory. B register is used along
with Accumulator for multiplication and division. A and B registers together is also called
MATH registers.
Microcontrollers
4 Sem ECE
PSW (Program Status Word). This is an 8 bit register which contains the arithmetic status of
ALU and the bank select bits of register banks.
CY AC F0 RS1 RS0 OV - P
CY
carry flag
AC
auxiliary carry flag
F0
available to the user for general purpose
RS1,RS0 register bank select bits
OV
overflow
P
parity
Stack Pointer (SP) it contains the address of the data item on the top of the stack. Stack
may reside anywhere on the internal RAM. On reset, SP is initialized to 07 so that the default
stack will start from address 08 onwards.
Data Pointer (DPTR) DPH (Data pointer higher byte), DPL (Data pointer lower byte). This
is a 16 bit register which is used to furnish address information for internal and external
program memory and for external data memory.
Program Counter (PC) 16 bit PC contains the address of next instruction to be executed.
On reset PC will set to 0000. After fetching every instruction PC will increment by one.
1.5
PIN DIAGRAM
Pinout Description
Pins 1-8
Pin 9
RESET. A logic one on this pin disables the microcontroller and clears the contents of
most registers. In other words, the positive voltage on this pin resets the
microcontroller. By applying logic zero to this pin, the program starts execution from
the beginning.
Pins10-17
PORT 3. Similar to port 1, each of these pins can serve as general input or output.
Besides, all of them have alternative functions
Microcontrollers
4 Sem ECE
Pin 10
Pin 11
Pin 12
Pin 13
Pin 14
Pin 15
Pin 16
Pin 17
Pin 18, 19
XTAL2, XTAL1. Internal oscillator input and output. A quartz crystal which specifies
operating frequency is usually connected to these pins.
Pin 20
GND. Ground.
Pin 21-28
Port 2. If there is no intention to use external memory then these port pins are
configured as general inputs/outputs. In case external memory is used, the higher
address byte, i.e. addresses A8-A15 will appear on this port. Even though memory
with capacity of 64Kb is not used, which means that not all eight port bits are used for
its addressing, the rest of them are not available as inputs/outputs.
Pin 29
PSEN. If external ROM is used for storing program then a logic zero (0) appears on it
every time the microcontroller reads a byte from memory.
Pin 30
ALE. Prior to reading from external memory, the microcontroller puts the lower
address byte (A0-A7) on P0 and activates the ALE output. After receiving signal from
the ALE pin, the external latch latches the state of P0 and uses it as a memory chip
address. Immediately after that, the ALE pin is returned its previous logic state and P0
is now used as a Data Bus.
Pin 31
EA. By applying logic zero to this pin, P2 and P3 are used for data and address
transmission with no regard to whether there is internal memory or not. It means that
even there is a program written to the microcontroller, it will not be executed. Instead,
the program written to external ROM will be executed. By applying logic one to the EA
pin, the microcontroller will use both memories, first internal then external (if exists).
Pin 32-39
PORT 0. Similar to P2, if external memory is not used, these pins can be used as
general inputs/outputs. Otherwise, P0 is configured as address output (A0-A7) when
the ALE pin is driven high (1) or as data output (Data Bus) when the ALE pin is driven
low (0).
Pin 40
communication
output
or
Serial
synchronous
10
Microcontrollers
4 Sem ECE
1F
1E
1D
1C
1B
1A
19
18
17
16
15
14
13
12
11
10
0F
0E
0D
0C
0B
0A
09
08
07
06
05
04
03
02
01
00
B
A
N
K
3
7F
78
77
70
6F
68
67
60
5F
58
57
50
29
4F
48
28
47
40
27
3F
38
26
37
30
28
2F
2E
2D
2C
2B
B
A
N
K
2
2A
25
2F
B
A
N
K
1
24
27
20
23
1F
18
22
17
10
21
0F
08
20
07
00
B
A
N
K
0
7F
7E
.
.
.
.
.
.
.
.
32
31
30
General purpose memory
Working Registers
Register Banks: 00h to 1Fh. The 8051 uses 8 general-purpose registers R0 through R7 (R0, R1,
R2, R3, R4, R5, R6, and R7). There are four such register banks. Selection of register bank can be
done through RS1,RS0 bits of PSW. On reset, the default Register Bank 0 will be selected.
Bit Addressable RAM: 20h to 2Fh . The 8051 supports a special feature which allows access to bit
variables. This is where individual memory bits in Internal RAM can be set or cleared. In all there
are 128 bits numbered 00h to 7Fh. Being bit variables any one variable can have a value 0 or 1. A bit
variable can be set with a command such as SETB and cleared with a command such as CLR.
Example instructions are:
SETB 25h ; sets the bit 25h (becomes 1)
CLR 25h ; clears bit 25h (becomes 0)
Note, bit 25h is actually bit 5 of Internal RAM location 24h.
The Bit Addressable area of the RAM is just 16 bytes of Internal RAM located between 20h and 2Fh.
General Purpose RAM: 30h to 7Fh. Even if 80 bytes of Internal RAM memory are available for
general-purpose data storage, user should take care while using the memory location from 00 -2Fh
Saneesh Cleatus Thundiyil
11
Microcontrollers
4 Sem ECE
since these locations are also the default register space, stack space, and bit addressable space. It is
a good practice to use general purpose memory from 30 7Fh. The general purpose RAM can be
accessed using direct or indirect addressing modes.
PSEN
A14
A13
A12
A9
A8
WR
RD
AL
E
LE
A0-A7
8051
__
EA
GND
LOWER BYTE
ADDRESS
[AD0 AD7]
AD0
AD7
A13
A12
..
A8
WE
OE
A7
..
A1
A0
A14
A13
A12
16 Kbyte
RAM
A0-A7
.
.
.
A3
A2
A1
A0
32 Kbyte
RAM
DAT
A
O/P
DAT
A
O/P
8 BIT
8 BIT
The lower order address and data bus are multiplexed. De-multiplexing is done by the latch.
Initially the address will appear in the bus and this latched at the output of latch using ALE signal.
The output of the latch is directly connected to the lower byte address lines of the memory. Later
data will be available in this bus. Still the latch output is address it self. The higher byte of address
bus is directly connected to the memory. The number of lines connected depends on the memory
size.
The RD and WR (both active low) signals are connected to RAM for reading and writing the data.
PSEN of microcontroller is connected to the output enable of the ROM to read the data from the
memory.
EA (active low) pin is always grounded if we use only external memory. Otherwise, once the
program size exceeds internal memory the microcontroller will automatically switch to external
memory.
Saneesh Cleatus Thundiyil
12
Microcontrollers
4 Sem ECE
1.8 STACK
A stack is a last in first out memory. In 8051 internal RAM space can be used as stack. The address
of the stack is contained in a register called stack pointer. Instructions PUSH and POP are used for
stack operations. When a data is to be placed on the stack, the stack pointer increments before
storing the data on the stack so that the stack grows up as data is stored (pre-increment). As the
data is retrieved from the stack the byte is read from the stack, and then SP decrements to point the
next available byte of stored data (post decrement). The stack pointer is set to 07 when the 8051
resets. So that default stack memory starts from address location 08 onwards (to avoid overwriting
the default register bank ie., bank 0).
Eg; Show the stack and SP for the following.
MOV R6, #25H
MOV R1, #12H
MOV R4, #0F3H
[SP]=07
[R6]=25H
[R1]=12H
[R4]=F3H
PUSH 6
PUSH 1
PUSH 4
[SP]=08
[SP]=09
[SP]=0A
[08]=[06]=25H
[09]=[01]=12H
[0A]=[04]=F3H
POP 6
POP 1
POP 4
[06]=[0A]=F3H
[01]=[09]=12H
[04]=[08]=25H
//CONTENT OF 08 IS 25H
//CONTENT OF 09 IS 12H
//CONTENT OF 0A IS F3H
13
Microcontrollers
4 Sem ECE
UNIT 2
2.1 INSTRUCTION SYNTAX.
General syntax for 8051 assembly language is as follows.
LABEL: OPCODE OPERAND ;COMMENT
LABEL : (THIS IS NOT NECESSARY UNLESS THAT SPECIFIC LINE HAS TO BE ADDRESSED). The label is a symbolic
address for the instruction. When the program is assembled, the label will be given specific address
in which that instruction is stored. Unless that specific line of instruction is needed by a branching
instruction in the program, it is not necessary to label that line.
OPCODE: Opcode is the symbolic representation of the operation. The assembler converts the
opcode to a unique binary code (machine language).
OPERAND: While opcode specifies what operation to perform, operand specifies where to perform
that action. The operand field generally contains the source and destination of the data. In some
cases only source or destination will be available instead of both. The operand will be either
address of the data, or data itself.
COMMENT: Always comment will begin with ; or // symbol. To improve the program quality,
programmer may always use comments in the program.
MOV A,#30H
ADD A, #83
14
Microcontrollers
4 Sem ECE
2. Register addressing.
In this addressing mode the register will hold the data. One of the eight general registers
(R0 to R7) can be used and specified as the operand.
Eg.
MOV A,R0
ADD A,R6
R0 R7 will be selected from the current selection of register bank. The default register bank will be bank 0.
3. Direct addressing
There are two ways to access the internal memory. Using direct address and indirect address. Using
direct addressing mode we can not only address the internal memory but SFRs also. In direct addressing, an 8
bit internal data memory address is specified as part of the instruction and hence, it can specify the address
only in the range of 00H to FFH. In this addressing mode, data is obtained directly from the memory.
Eg.
MOV A,60h
ADD A,30h
4.
Indirect addressing
The indirect addressing mode uses a register to hold the actual address that will be used in data
movement. Registers R0 and R1 and DPTR are the only registers that can be used as data pointers. Indirect
addressing cannot be used to refer to SFR registers. Both R0 and R1 can hold 8 bit address and DPTR can hold
16 bit address.
Eg.
MOV A,@R0
ADD A,@R1
MOVX A,@DPTR
5. Indexed addressing.
In indexed addressing, either the program counter (PC), or the data pointer (DTPR)is
used to hold the base address, and the A is used to hold the offset address. Adding the value of the
base address to the value of the offset address forms the effective address. Indexed addressing is
used with JMP or MOVC instructions. Look up tables are easily implemented with the help of index
addressing.
Eg.
MOVC A, @A+DPTR
// copies the contents of memory location pointed by the sum of the
accumulator A and the DPTR into accumulator A.
MOVC A, @A+PC
// copies the contents of memory location pointed by the sum of the
accumulator A and the program counter into accumulator A.
6. Relative Addressing.
Relative addressing is used only with conditional jump instructions. The relative address,
(offset), is an 8 bit signed number, which is automatically added to the PC to make the address of
the next instruction. The 8 bit signed offset value gives an address range of +127 to 128 locations.
The jump destination is usually specified using a label and the assembler calculates the jump offset
accordingly. The advantage of relative addressing is that the program code is easy to relocate and
the address is relative to position in the memory.
Eg.
7.
SJMP LOOP1
JC BACK
Absolute addressing
Absolute addressing is used only by the AJMP (Absolute Jump) and ACALL (Absolute Call)
instructions. These are 2 bytes instructions. The absolute addressing mode specifies the lowest 11
bit of the memory address as part of the instruction. The upper 5 bit of the destination address are
Saneesh Cleatus Thundiyil
15
Microcontrollers
4 Sem ECE
the upper 5 bit of the current program counter. Hence, absolute addressing allows branching only
within the current 2 Kbyte page of the program memory.
Eg.
AJMP LOOP1
ACALL LOOP2
8. Long Addressing
The long addressing mode is used with the instructions LJMP and LCALL. These are 3 byte
instructions. The address specifies a full 16 bit destination address so that a jump or a call can be
made to a location within a 64 Kbyte code memory space.
Eg.
LJMP FINISH
LCALL DELAY
CLR C ;
CLR 07h
SETB 07H
;
;
16
Microcontrollers
2. SUBB A, #55H
3. MOV DPTR, #2000H
4. MUL AB
4 Sem ECE
2
2
4
2 s
2 s
4 s
2. 8051 Instructions
The instructions of 8051 can be broadly classified under the following headings.
1. Data transfer instructions
2. Arithmetic instructions
3. Logical instructions
4. Branch instructions
5. Subroutine instructions
6. Bit manipulation instructions
Data transfer instructions.
In this group, the instructions perform data transfer operations of the following types.
a. Move the contents of a register Rn to A
i. MOV A,R2
ii. MOV A,R7
d. Move the contents of a memory location to A or A to a memory location using direct and
indirect addressing
i. MOV A, 65H
ii. MOV A, @R0
f.
Move the contents of memory location to another memory location using direct and
indirect addressing
i. MOV 47H, 65H
ii. MOV 45H, @R0
17
Microcontrollers
4 Sem ECE
[SP]=07
[R6]=25H
[R1]=12H
[R4]=F3H
PUSH 6
PUSH 1
PUSH 4
[SP]=08
[SP]=09
[SP]=0A
POP 6
POP 1
POP 4
[06]=[0A]=F3H [SP]=09
[01]=[09]=12H [SP]=08
[04]=[08]=25H [SP]=07
//CONTENT OF 06 IS F3H
//CONTENT OF 01 IS 12H
//CONTENT OF 04 IS 25H
j. Exchange instructions
The content of source ie., register, direct memory or indirect memory will be exchanged
with the contents of destination ie., accumulator.
i. XCH A,R3
ii. XCH A,@R1
iii. XCH A,54h
k. Exchange digit. Exchange the lower order nibble of Accumulator (A0-A3) with lower
order nibble of the internal RAM location which is indirectly addressed by the register.
i. XCHD A,@R1
ii. XCHD A,@R0
18
Microcontrollers
4 Sem ECE
Arithmetic instructions.
The 8051 can perform addition, subtraction. Multiplication and division operations on 8 bit
numbers.
Addition
In this group, we have instructions to
i. Add the contents of A with immediate data with or without carry.
i. ADD A, #45H
ii. ADDC A, #OB4H
ii. Add the contents of A with register Rn with or without carry.
i. ADD A, R5
ii. ADDC A, R2
iii. Add the contents of A with contents of memory with or without carry using direct and
indirect addressing
i. ADD A, 51H
ii. ADDC A, 75H
iii. ADD A, @R1
iv. ADDC A, @R0
CY AC and OV flags will be affected by this operation.
Subtraction
In this group, we have instructions to
i. Subtract the contents of A with immediate data with or without carry.
i. SUBB A, #45H
ii. SUBB A, #OB4H
ii. Subtract the contents of A with register Rn with or without carry.
i. SUBB A, R5
ii. SUBB A, R2
iii. Subtract the contents of A with contents of memory with or without carry using direct and
indirect addressing
i. SUBB A, 51H
ii. SUBB A, 75H
iii. SUBB A, @R1
iv. SUBB A, @R0
CY AC and OV flags will be affected by this operation.
Multiplication
MUL AB. This instruction multiplies two 8 bit unsigned numbers which are stored in A and B
register. After multiplication the lower byte of the result will be stored in accumulator and higher
byte of result will be stored in B register.
Eg.
MOV A,#45H
MOV B,#0F5H
MUL AB
;[A]=45H
;[B]=F5H
;[A] x [B] = 45 x F5 = 4209
;[A]=09H, [B]=42H
Division
19
Microcontrollers
4 Sem ECE
DIV AB. This instruction divides the 8 bit unsigned number which is stored in A by the 8 bit
unsigned number which is stored in B register. After division the result will be stored in
accumulator and remainder will be stored in B register.
Eg.
MOV A,#45H
MOV B,#0F5H
DIV AB
;[A]=0E8H
;[B]=1BH
;[A] / [B] = E8 /1B = 08 H with remainder 10H
Eg 2:
MOV A,#23H
MOV R1,#55H
ADD A,R1
DA A
// [A]=78
// [A]=78
MOV A,#53H
MOV R1,#58H
ADD A,R1
DA A
// [A]=ABh
// [A]=11, C=1 . ANSWER IS 111. Accumulator data is changed after DA A
INC Rn
INC DIRECT
INC increments the value of source by 1. If the initial value of register is FFh, incrementing the value
will cause it to reset to 0. The Carry Flag is not set when the value "rolls over" from 255 to 0.
In the case of "INC DPTR", the value two-byte unsigned integer value of DPTR is incremented. If the
initial value of DPTR is FFFFh, incrementing the value will cause it to reset to 0.
DEC @Ri
DEC decrements the value of source by 1. If the initial value of is 0, decrementing the value will cause
it to reset to FFh. The Carry Flag is not set when the value "rolls over" from 0 to FFh.
Logical Instructions
Logical AND
ANL destination, source:
ANL does a bitwise "AND" operation between source and destination,
leaving the resulting value in destination. The value in source is not affected. "AND" instruction
logically AND the bits of source and destination.
ANL A,#DATA ANL A, Rn
ANL A,DIRECT ANL A,@Ri
ANL DIRECT,A ANL DIRECT, #DATA
Logical OR
ORL destination, source:
20
Microcontrollers
4 Sem ECE
leaving the resulting value in destination. The value in source is not affected. " OR " instruction
logically OR the bits of source and destination.
ORL A,#DATA ORL A, Rn
ORL A,DIRECT ORL A,@Ri
ORL DIRECT,A ORL DIRECT, #DATA
Logical Ex-OR
XRL destination, source:
XRL does a bitwise "EX-OR" operation between source and
destination, leaving the resulting value in destination. The value in source is not affected. " XRL "
instruction logically EX-OR the bits of source and destination.
XRL A,#DATA XRL A,Rn
XRL A,DIRECT XRL A,@Ri
XRL DIRECT,A XRL DIRECT, #DATA
Logical NOT
CPL complements operand, leaving the result in operand. If operand is a single bit then the state of
the bit will be reversed. If operand is the Accumulator then all the bits in the Accumulator will be
reversed.
CPL A, CPL C,
Rotate Instructions
RR A
This instruction is rotate right the accumulator. Its operation is illustrated below. Each bit is shifted one
location to the right, with bit 0 going to bit 7.
RL A
Rotate left the accumulator. Each bit is shifted one location to the left, with bit 7 going to bit 0
RRC A
Rotate right through the carry. Each bit is shifted one location to the right, with bit 0 going into the carry bit in
the PSW, while the carry was at goes into bit 7
RLC A
Rotate left through the carry. Each bit is shifted one location to the left, with bit 7 going into the carry bit in
the PSW, while the carry goes into bit 0.
21
Microcontrollers
4 Sem ECE
The advantages of the relative jump are as follows:1. Only 1 byte of jump address needs to be specified in the 2's complement form, ie. For
jumping ahead, the range is 0 to 127 and for jumping back, the range is -1 to -128.
2. Specifying only one byte reduces the size of the instruction and speeds up program
execution.
3. The program with relative jumps can be relocated without reassembling to generate
absolute jump addresses.
Disadvantages of the absolute jump: 1. Short jump range (-128 to 127 from the instruction following the jump instruction)
Instructions that use Relative Jump
SJMP <relative address>; this is unconditional jump
22
Microcontrollers
4 Sem ECE
In 8051, 64 kbyte of program memory space is divided into 32 pages of 2 kbyte each. The
hexadecimal addresses of the pages are given as follows:Page (Hex)
Address (Hex)
00
01
02
03
.
.
1E
1F
0000 - 07FF
0800 - 0FFF
1000 - 17FF
1800 - 1FFF
F000 - F7FF
F800 - FFFF
It can be seen that the upper 5bits of the program counter (PC) hold the page number and the lower
11bits of the PC hold the address within that page. Thus, an absolute address is formed by taking
page numbers of the instruction (from the program counter) following the jump and attaching the
specified 11bits to it to form the 16-bit address.
Advantage: The instruction length becomes 2 bytes.
Example of short absolute jump: ACALL <address 11>
AJMP <address 11>
The unconditional jump is a jump in which control is transferred unconditionally to the target location.
a. LJMP (long jump). This is a 3-byte instruction. First byte is the op-code and second and third
bytes represent the 16-bit target address which is any memory location from 0000 to FFFFH
eg: LJMP 3000H
b. AJMP: this causes unconditional branch to the indicated address, by loading the 11 bit address to
0 -10 bits of the program counter. The destination must be therefore within the same 2K blocks.
c. SJMP (short jump). This is a 2-byte instruction. First byte is the op-code and second byte is the
relative target address, 00 to FFH (forward +127 and backward -128 bytes from the current PC
value). To calculate the target address of a short jump, the second byte is added to the PC value
which is address of the instruction immediately below the jump.
23
Microcontrollers
2.
4 Sem ECE
24
Microcontrollers
f.
4 Sem ECE
the new address of subroutine is loaded to PC. No flags are
RET instruction
RET instruction pops top two contents from the stack and load it to PC.
g. [PC15-8] = [[SP]]
;content of current top of the stack will be moved to higher byte of PC.
h. [SP]=[SP]-1; (SP decrements)
i. [PC7-0] = [[SP]] ;content of bottom of the stack will be moved to lower byte of PC.
j. [SP]=[SP]-1; (SP decrements again)
Bit manipulation instructions.
8051 has 128 bit addressable memory. Bit addressable SFRs and bit addressable PORT pins. It is possible to
perform following bit wise operations for these bit addressable locations.
1.
LOGICAL AND
a. ANL C,BIT(BIT ADDRESS)
; LOGICALLY AND CARRY AND CONTENT OF BIT ADDRESS, STORE RESULT IN CARRY
b. ANL C, /BIT;
; LOGICALLY AND CARRY AND COMPLEMENT OF CONTENT OF BIT ADDRESS, STORE RESULT IN CARRY
2.
LOGICAL OR
a. ORL C,BIT(BIT ADDRESS)
; LOGICALLY OR CARRY AND CONTENT OF BIT ADDRESS, STORE RESULT IN CARRY
b. ORL C, /BIT;
; LOGICALLY OR CARRY AND COMPLEMENT OF CONTENT OF BIT ADDRESS, STORE RESULT IN CARRY
CLR bit
a. CLR bit
; CONTENT OF BIT ADDRESS SPECIFIED WILL BE CLEARED.
b. CLR C
; CONTENT OF CARRY WILL BE CLEARED.
CPL bit
a. CPL bit
; CONTENT OF BIT ADDRESS SPECIFIED WILL BE COMPLEMENTED.
b.
CPL C
; CONTENT OF CARRY WILL BE COMPLEMENTED.
3.
4.
25
Microcontrollers
4 Sem ECE
UNIT 3
3.1 ASSEMBLER DIRECTIVES.
Assembler directives tell the assembler to do something other than creating the machine code for
an instruction. In assembly language programming, the assembler directives instruct the assembler
to
1. Process subsequent assembly language instructions
2. Define program constants
3. Reserve space for variables
The following are the widely used 8051 assembler directives.
ORG (origin)
The ORG directive is used to indicate the starting address. It can be used only when the
program counter needs to be changed. The number that comes after ORG can be either in
hex or in decimal.
Eg: ORG 0000H
;Set PC to 0000.
DB 40H
DB 01011100B
DB 48
DB
'HELLOW
; hex
; binary
; decimal
; ASCII
END
The END directive signals the end of the assembly module. It indicates the end of the
program to the assembler. Any text in the assembly file that appears after the END directive
is ignored. If the END statement is missing, the assembler will generate an error message.
26
Microcontrollers
4 Sem ECE
27
Microcontrollers
4 Sem ECE
4. Write a program to add two 16 bit numbers stored at locations 51H-52H and 55H-56H and
store the result in locations 40H, 41H and 42H. Assume that the least significant byte of
data and the result is stored in low address and the most significant byte of data or the result
is stored in high address.
ORG 0000H
; Set program counter 0000H
MOV A,51H
; Load the contents of memory location 51H into A
ADD A,55H
; Add the contents of 55H with contents of A
MOV 40H,A
; Save the LS byte of the result in location 40H
MOV A,52H
; Load the contents of 52H into A
ADDC A,56H
; Add the contents of 56H and CY flag with A
MOV 41H,A
; Save the second byte of the result in 41H
MOV A,#00
; Load 00H into A
ADDC A,#00 ; Add the immediate data 00H and CY to A
MOV 42H,A
; Save the MS byte of the result in location 42H
END
5. Write a program to store data FFH into RAM memory locations 50H to 58H using indirect
addressing mode.
ORG 0000H
; Set program counter 0000H
MOV A, #0FFH
; Load FFH into A
MOV RO, #50H
; Load pointer, R0-50H
MOV R5, #08H
; Load counter, R5-08H
Start:MOV @RO, A
; Copy contents of A to RAM pointed by R0
INC RO
; Increment pointer
DJNZ R5, start ; Repeat until R5 is zero
END
6. Write a program to add two Binary Coded Decimal (BCD) numbers stored at locations 60H
and 61H and store the result in BCD at memory locations 52H and 53H. Assume that the
least significant byte of the result is stored in low address.
ORG 0000H
MOV A,60H
ADD A,61H
DA A
MOV 52H, A
MOV A,#00
ADDC A,#00H
MOV 53H,A
END
28
Microcontrollers
8.
9.
10.
11.
4 Sem ECE
INC DPTR
;Increment DPTR
DJNZ R6, again
;Loop until counter R6=0
END
Write a program to compute 1 + 2 + 3 + N (say N=15) and save the sum at70H
OR G 0000H
; Set program counter 0000H
N EQU 15
M OV R 0,#00
; Clear R0
CLR A
; Clear A
a ga in: INC R 0
; Increment R0
ADD A, R0
; Add the contents of R0 with A
CJNE R 0,#N,aga in ; Loop until counter, R0, N
M OV 70H,A
; Save the result in location 70H END
Write a program to multiply two 8 bit numbers stored at locations 70H and 71H and store the
result at memory locations 52H and 53H. Assume that the least significant byte of the result is
stored in low address.
ORG 0000H ; Set program counter 00 OH
MOV A, 70H ; Load the contents of memory location 70h into A
MOV B, 71H ; Load the contents of memory location 71H into B
MUL AB
; Perform multiplication
MOV 52H,A ; Save the least significant byte of the result in location 52H MOV 53H,B ; Save the most
significant byte of the result in location 53
END
Ten 8 bit numbers are stored in internal data memory from location 5oH. Write a
program to increment the data.
Assume that ten 8 bit numbers are stored in internal data memory from location 50H, hence
R0 or R1 must be used as a pointer.
The program is as follows.
OPT 0000H
MOV R0,#50H
MOV R3,#0AH
Loopl: INC @R0
INC RO
DJNZ R3, loopl END
END
Write a program to find the average of five 8 bit numbers. Store the result in H.
(Assume that after adding five 8 bit numbers, the result is 8 bit only).
ORG 0000H
MOV 40H,#05H
MOV 41H,#55H
MOV 42H,#06H
MOV 43H,#1AH
MOV 44H,#09H
MOV R0,#40H
MOV R5,#05H
MOV B,R5
CLR A
Loop: ADD A,@RO
INC RO
29
Microcontrollers
4 Sem ECE
DJNZ R5,Loop
DIV AB
MOV 55H,A
END
12. Write a program to find the cube of an 8 bit number program is as follows
ORG 0000H
MOV R1,#N
MOV A,R1
MOV B,R1
MUL AB
//SQUARE IS COMPUTED
MOV R2, B
MOV B, R1
MUL AB
MOV 50,A
MOV 51,B
MOV A,R2
MOV B, R1
MUL AB
ADD A, 51H
MOV 51H, A
MOV 52H, B
MOV A, # 00H
ADDC A, 52H
MOV 52H, A
//CUBE IS STORED IN 52H,51H,50H
END
13. Write a program to exchange the lower nibble of data present in external memory 6000H and
6001H
ORG 0000H
M OV DPTR , #6000H
MOVX A, @DPTR
MOV R0, #45H
MOV @RO, A
INC DPL
MOVX A, @DPTR
XCHD A, @R0
MOVX @DPTR, A
DEC DPL
MOV A, @R0
MOVX @DPTR, A
END
;
;
;
;
;
;
14. Write a program to count the number of and o's of 8 bit data stored in location 6000H.
ORG 00008
MOV DPTR, #6000h
MOVX A, @DPTR
MOV R0,#08
MOV R2,#00
MOV R3,#00
CLR C
BACK: RLC A
30
Microcontrollers
4 Sem ECE
JC NEXT
; If CF = 1, branch to next
INC R2
; I f C F = 0 , i n c r e m e n t R 2 AJMP NEXT2
NEXT: INC R3
; If CF = 1, increment R3
NEXT2: DJNZ RO,BACK
; Repeat until RO is zero
END
15. Write a program to shift a 24 bit number stored at 57H-55H to the left logically four places.
Assume that the least significant byte of data is stored in lower address.
ORG 0000H
; Set program counter 0000h
MOV R1,#04 ; Set up loop count to 4
again: MOV A,55H
; Place the least significant byte of data in A
CLR C
; Clear tne carry flag
RLC A
; Rotate contents of A (55h) left through carry
MOV 55H,A
MOV A,56H
RLC A
; Rotate contents of A (56H) left through carry
MOV 56H,A
MOV A,57H
RLC A
; Rotate contents of A (57H) left through carry
MOV 57H,A
DJNZ R1,again ; Repeat until R1 is zero
END
16. Two 8 bit numbers are stored in location 1000h and 1001h of external data memory.
Write a program to find the GCD of the numbers and store the result in 2000h.
ALGORITHM
Step 1
Step 2
Step 3
Step 4
Step 5
Step 6
Step 7
Step 8
Step 9
:Initialize external data memory with data and DPTR with address
:Load A and TEMP with the operands
:Are the two operands equal? If yes, go to step 9
:Is (A) greater than (TEMP) ? If yes, go to step 6
:Exchange (A) with (TEMP) such that A contains the bigger number
:Perform division operation (contents of A with contents of TEMP)
:If the remainder is zero, go to step 9
:Move the remainder into A and go to step 4
:Save the contents 'of TEMP in memory and terminate the program
ORG 0000H
; Set program counter 0000H
TEMP EQU 70H
TEMPI EQU 71H
MOV DPTR, #1000H
; Copy address 100011 to DPTR
MOVX A, @DPTR
; Copy First number to A
MOV TEMP, A
; Copy First number to temp INC DPTR
MOVX A, @DPTR
; Copy Second number to A
LOOPS: CJNE A, TEMP, LOOP1 ; (A) /= (TEMP) branch to LOOP1
AJMP LOOP2
; (A) = (TEMP) branch to L00P2
LOOP1:
JNC LOOP3
; (A) > (TEMP) branch to LOOP3
NOV TEMPI, A
; (A) < (TEMP) exchange (A) with (TEMP)
MOV A, TEMP
MOV TEMP, TEMPI
LOOP3:
MOV B, TEMP
DIV AB
; Divide (A) by (TEMP)
MOV A, B
; Move remainder to A
CJNE A,#00, LOOPS
; (A)/=00 branch to LOOPS
LOOP2:
MOV A, TEMP
MOV DPTR, #2000H
MOVX @DPTR, A
; Store the result in 2000H
END
Saneesh Cleatus Thundiyil
31
Microcontrollers
4 Sem ECE
UNIT 5
5.1 BASICS OF INTERRUPTS.
During program execution if peripheral devices needs service from microcontroller, device will
generate interrupt and gets the service from microcontroller. When peripheral device activate the
interrupt signal, the processor branches to a program called interrupt service routine. After
executing the interrupt service routine the processor returns to the main program.
Steps taken by processor while processing an interrupt:
1.
2.
3.
4.
5.
ISR will always ends with RETI instruction. The execution of RETI instruction results in the
following.
1. POP the current stack top to the PC.
2. POP the current stack top to PSW.
Classification of interrupts.
1. External and internal interrupts.
External interrupts are those initiated by peripheral devices through the external pins of
the microcontroller.
Internal interrupts are those activated by the internal peripherals of the microcontroller
like timers, serial controller etc.)
2. Maskable and non-maskable interrupts.
The category of interrupts which can be disabled by the processor using program is called
maskable interrupts.
Non-maskable interrupts are those category by which the programmer cannot disable it
using program.
3. Vectored and non-vectored interrupt.
Starting address of the ISR is called interrupt vector. In vectored interrupts the starting
address is predefined. In non-vectored interrputs, the starting address is provided by the
peripheral as follows.
Microcontroller receives an interrupt request from external device.
Controller sends an acknowledgement (INTA) after completing the execution of
current instruction.
The peripheral device sends the interrupt vector to the microcontroller.
Saneesh Cleatus Thundiyil
32
Microcontrollers
4 Sem ECE
Interrupt source
Type
0003
Timer 0 interrupt
Internal
000B
0013
Timer 1 interrupt
Internal
001B
Serial interrupt
Internal
0023
Highest
Lowest
2. IP Register.
This is an 8 bit register used for setting the priority of the interrupts.
33
Microcontrollers
4 Sem ECE
Timer in 8051 is used as timer, counter and baud rate generator. Timer always counts up
irrespective of whether it is used as timer, counter, or baud rate generator: Timer is always
incremented by the microcontroller. The time taken to count one digit up is based on master clock
frequency.
If Master CLK=12 MHz,
Timer Clock frequency = Master CLK/12 = 1 MHz
Timer Clock Period = 1micro second
This indicates that one increment in count will take 1 micro second.
The two timers in 8051 share two SFRs (TMOD and TCON) which control the timers, and each timer
also has two SFRs dedicated solely to itself (TH0/TL0 and TH1/TL1).
The following are timer related SFRs in 8051.
SFR Name
TH0
TL0
TH1
TL1
TCON
TMOD
Description
Timer 0 High Byte
Timer 0 Low Byte
Timer 1 High Byte
Timer 1 Low Byte
Timer Control
Timer Mode
SFR Address
8Ch
8Ah
8Dh
8Bh
88h
89h
34
Microcontrollers
4 Sem ECE
TMOD Register
TCON Register
35
Microcontrollers
4 Sem ECE
TIMER MODES
Timers can operate in four different modes. They are as follows
Timer Mode-0: In this mode, the timer is used as a 13-bit UP counter as follows.
36
Microcontrollers
4 Sem ECE
timer in mode 2 will count from 50H to FFH. After that 50H is again reloaded. This mode is useful in
applications like fixed time sampling.
37
Microcontrollers
4 Sem ECE
38
Microcontrollers
4 Sem ECE
Keep monitoring the timer flag (TFx) with the JNB TFx,target instruction to see if it
is raised. Get out of the loop when TFx goes high.
Clear the TFx flag.
Go back to step 4, since mode 2 is auto-reload.
BACK: JNB
39
Microcontrollers
4 Sem ECE
UNIT 6
6.1 SERIAL COMMUNICATION.
6.1.1. DATA COMMUNICATION
The 8051 microcontroller is parallel device that transfers eight bits of data simultaneously
over eight data lines to parallel I/O devices. Parallel data transfer over a long is very expensive.
Hence, a serial communication is widely used in long distance communication. In serial data
communication, 8-bit data is converted to serial bits using a parallel in serial out shift register and
then it is transmitted over a single data line. The data byte is always transmitted with least
significant bit first.
6.1.2. BASICS OF SERIAL DATA COMMUNICATION,
Communication Links
1. Simplex communication link: In simplex transmission, the line is dedicated for transmission.
The transmitter sends and the receiver receives the data.
Transmitter
Receiver
2. Half duplex communication link: In half duplex, the communication link can be used for either
transmission or
reception. Data is transmitted in only one direction at a time.
Transmitter
Receiver
Receiver
Transmitter
3. Full duplex communication link: If the data is transmitted in both ways at the same time, it is a
full duplex i.e. transmission and reception can proceed simultaneously. This communication link
requires two wires for data, one for transmission and one for reception.
Transmitter
Receiver
Receiver
Transmitter
Sync
Receiver
40
Microcontrollers
4 Sem ECE
Data
Clock
2. Asynchronous Serial data transmission: In this, different clock sources are used for transmitter
and receiver. In this mode, data is transmitted with start and stop bits. A transmission begins with
start bit, followed by data and then stop bit. For error checking purpose parity bit is included just
prior to stop bit. In Asynchronous serial data communication a single byte is transmitted at a time.
Transmitter
Start D0 D1 D2 D3 D4 D5 D6 D7 D8 Stop
Receiver
Data
Clock 1
Clock2
Baud rate:
The rate at which the data is transmitted is called baud or transfer rate. The baud rate is the
reciprocal of the time to send one bit. In asynchronous transmission, baud rate is not equal to
number of bits per second. This is because; each byte is preceded by a start bit and followed by
parity and stop bit. For example, in synchronous transmission, if data is transmitted with 9600
baud, it means that 9600 bits are transmitted in one second. For bit transmission time = 1 second/
9600 = 0.104 ms.
6.1.3. 8051 SERIAL COMMUNICATION
The 8051 supports a full duplex serial port.
Three special function registers support serial communication.
1. SBUF Register: Serial Buffer (SBUF) register is an 8-bit register. It has separate SBUF
registers for data transmission and for data reception. For a byte of data to be transferred
via the TXD line, it must be placed in SBUF register. Similarly, SBUF holds the 8-bit data
received by the RXD pin and read to accept the received data.
2. SCON register: The contents of the Serial Control (SCON) register are shown below. This
register contains mode selection bits, serial port interrupt bit (TI and RI) and also the ninth
data bit for transmission and reception (TB8 and RB8).
41
Microcontrollers
4 Sem ECE
3. PCON register: The SMOD bit (bit 7) of PCON register controls the baud rate in
asynchronous mode transmission.
42
Microcontrollers
4 Sem ECE
4. Mode 3
This is similar to mode 2 except baud rate is calculated as in mode 1
6.1.5. CONNECTIONS TO RS-232
RS-232 standards:
To allow compatibility among data communication equipment made by various
manufactures, an interfacing standard called RS232 was set by the Electronics Industries
Association (EIA) in 1960. Since the standard was set long before the advent of logic family, its
input and output voltage levels are not TTL compatible.
In RS232, a logic one (1) is represented by -3 to -25V and referred as MARK while logic zero
(0) is represented by +3 to +25V and referred as SPACE. For this reason to connect any RS232 to a
microcontroller system we must use voltage converters such as MAX232 to convert the TTL logic
level to RS232 voltage levels and vice-versa. MAX232 IC chips are commonly referred as line
drivers.
In RS232 standard we use two types of connectors. DB9 connector or DB25 connector.
43
Microcontrollers
4 Sem ECE
44
Microcontrollers
ORG 0030H
START: MOV TMOD, #20H
MOV TH1, #0FDH
MOV SCON, #50H
SETB TR1
LOOP: MOV A, #'E'
ACALL LOAD
MOV A, #'A'
ACALL LOAD
MOV A, #'R'
ACALL LOAD
MOV A, #'T'
ACALL LOAD
MOV A, #'H'
ACALL LOAD
SJMP LOOP
4 Sem ECE
45
Microcontrollers
4 Sem ECE
46
Microcontrollers
4 Sem ECE
Control register is an 8 bit register. The contents of this register called control word. This register
can be accessed to write a control word when A0 and A1 are at logic 1. This control register is not
accessible for a read operation.
Bit D7 of the control register specifies either I/O function or the Bit Set/Reset function. If bit
D7=1, bits D6-D0 determines I/O functions in various modes. If bit D7=0, Port C operates in the Bit
Set/Reset (BSR) mode. The BSR control word does not affect the functions of Port A and Port B.
ADDITIONAL NOTES
THEORY RELATED TO ADC
ADC Devices:
47
Microcontrollers
4 Sem ECE
Analog to digital converters are among the most widely used devices for data acquisitions. Digital
computers use binary (discrete) value but in physical world everything is analog (continuous). A
physical
quantity is converted to electrical signals using device called transducer or also called as sensors.
Sensors and many other natural quantities produce an output that is voltage (or current). Therefore
we need an
analog - to - digital converter to translate the analog signal to digital numbers so that the
microcontroller can read and process them.
An ADC has an n bit resolution where n can be 8, 10, 16, 0r even 24 bits. The higher resolution ADC
provides a smaller step size, where step size is smallest change that can be discerned by an ADC.
This is shown below.
In addition to resolution, conversion time is another major factor in judging an ADC. Conversion
time is defined as the time it takes the ADC to convert the analog input to digital (binary) number.
The ADC chips are either parallel or serial. In parallel ADC, we have 8 or more pins dedicated to
bring out the binary data, but in serial ADC we have only one pin for data out.
ADC 0808
ADC0808, has 8 analog inputs. ADC0808 allows us to monitor up to 8 different analog inputs using
only a single chip. ADC0808 has an 8-bit data output. The 8 analog inputs channels are multiplexed
and selected according to table given below using three address pins, A, B, and C.
48
Microcontrollers
4 Sem ECE
In ADC0808 Vref (+) and Vref (-) set the reference voltage. If Vref (-) = Gnd and Vref (+) = 5V, the
step size is 5V/ 256 = 19.53 mV. Therefore,to get a 10 mV step size we need to set Vref (+) = 2.56V
and Vref(-) = Gnd. ALE is used to latch in the address. SC for start conversion. EOC is for end-ofconversion, and OE is for output enable (READ). Table shows the step size relation to the Vref
Voltage.
49
Microcontrollers
4 Sem ECE
50
Microcontrollers
4 Sem ECE
51
Microcontrollers
4 Sem ECE
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
VCC and VSS are the supply voltage and ground for the whole device (the analog and digital supplies
are separate in the 16-pin package).
P1.0P1.7, P2.6, and P2.7 are for digital input and output, grouped into ports P1 and P2.
TACLK, TA0, and TA1 are associated with Timer_A; TACLK can be used as the clock input to the timer,
while TA0 and TA1 can be either inputs or outputs. These can be used on several pins because of the
importance of the timer.
A0, A0+, and so on, up to A4, are inputs to the analog-to-digital converter. It has four differential
channels, each of which has negative and positive inputs. VREF is the reference voltage for the
converter.
ACLK and SMCLK are outputs for the microcontrollers clock signals. These can be used to supply a
clock to external components or for diagnostic purposes.
SCLK, SDO, and SCL are used for the universal serial interface, which communicates with external
devices using the serial peripheral interface (SPI) or inter-integrated circuit (I2C) bus.
XIN and XOUT are the connections for a crystal, which can be used to provide an accurate, stable
clock frequency.
RST is an active low reset signal. Active low means that it remains high near VCC for normal operation
and is brought low near VSS to reset the chip. Alternative notations to show the active low nature are
_RST and /RST.
NMI is the non-maskable interrupt input, which allows an external signal to interrupt the normal
operation of the program.
TCK, TMS, TCLK, TDI, TDO, and TEST form the full JTAG interface, used to program and debug the
device.
SBWTDIO and SBWTCK provide the Spy-Bi-Wire interface, an alternative to the usual JTAG
connection that saves pins.
Architecture of MSP 430
Block diagram of the MSP430F2003 and F2013, taken from data sheet.
52
Microcontrollers
6.
7.
4 Sem ECE
The brownout protection comes into action if the supply voltage drops to a dangerous level. Most
devices include this but not some of the MSP430x1xx family.
There are ground and power supply connections. Ground is labeled VSS and is taken to define 0V.
The supply connection is VCC which is mostly in the range of 1.83.6V.
53
Microcontrollers
4 Sem ECE
Program counter, PC: This contains the address of the next instruction to be executed
Stack pointer, SP: MSP430 uses the top (high addresses) of the main RAM as stack memory. The stack
pointer holds the address of the most recently added word and is automatically adjusted as the stack grows
downward in memory or shrinks upward.
Status register, SR: This contains a set of flags (single bits), whose functions fall into three categories.
The most commonly used flags are C, Z, N, and V, which give information about the result of the last
arithmetic or logical operation. The Z flag is set if the result was zero and cleared if it was nonzero, for
instance. Setting the GIE bit enables maskable interrupts. The final group of bits is CPUOFF, OSCOFF, SCG0,
and SCG1, which control the mode of operation of the MCU. All systems are active when all bits are clear.
Constant generator: This provides the six most frequently used values so that they need not be fetched from
memory whenever they are needed. It uses both R2 and R3 to provide a range of useful values by exploiting
the CPUs addressing modes.
General purpose registers: The remaining 12 registers, R4R15, are general working registers. They may be
used for either data or addresses because both are 16-bit values, which simplify the operation significantly.
COMPILER FRIENDLY FEATURES
MSP430 stems from its recent introduction is that it is designed with compilers in mind. Most small
microcontrollers are now programmed in C, and it is important that a compiler can produce compact, efficient
code. The MSP430 has 16 registers in its CPU, which enhances efficiency because they can be used for local
variables, parameters passed to subroutines, and either addresses or data. This is a typical feature of a RISC,
but unlike a pure RISC, it can perform arithmetic directly on values in main memory. Microcontrollers
typically spend much of their time on such operations.
MEMORY ADDRESS SPACE
The MSP430 von Neumann architecture has one address space shared with
o special function registers (SFRs),
o peripherals,
o RAM, and
o Flash/ROM memory
Code access are always performed on even addresses.
54
Microcontrollers
4 Sem ECE
55
Microcontrollers
4 Sem ECE
4. Absolute Mode: The constant in this form of indexed addressing is the absolute address of the data.
This is already the complete address required so it should be added to a register that contains 0.
Absolute addressing is shown by the prefix & and should be used for special function and peripheral
registers, whose addresses are fixed in the memory map.
Eg:
mov.b &P1IN ,R6 ;
copies the port 1 input register into register R6
5. Indirect Register Mode:
Eg:
MOV @R10,0(R11)
Operation: Move the contents of the source whose address is in (R10) to the destination
address (R11). Indirect addressing cannot be used for the destination.
6. Indirect Auto increment Mode:
This is available only for the source and is shown by the
symbol @ in front of a register with a + sign after it, such as @R5+. It uses the value in R5 as a pointer
and automatically increments it afterward by 1 if a byte has been fetched or by 2 for a word.
Eg:
MOV @R10+,0(R11)
7. Immediate Mode
Eg:
MOV #45h,TONI: Operation: Move the immediate constant 45h, which is contained in the
word following the instruction, to destination address TONI. When fetching the source, the program
counter points to the word following the instruction and moves the contents to the destination.
CLOCK SYSTEM
Figure below shows a simplified diagram of the Basic Clock Module+ (BCM+) for the MSP430F2xx
family. The clock module provides three outputs:
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.
Most peripherals can choose either SMCLK, which is often the same as MCLK and in the megahertz
range, or ACLK, which is typically much slower and usually 32 KHz. A few peripherals, such as
analog-to-digital converters, can also use MCLK and some, such as timers, have their own clock
inputs. The frequencies of all three clocks can be divided in the BCM+ as shown in figure.
Up to four sources are available for the clock, depending on the family and variant:
Low- or high-frequency crystal oscillator, LFXT1: Available in all devices. It is usually used with a
low-frequency crystal (32 KHz) but can also run with a high-frequency crystal (typically a few MHz)
in most devices. An external clock signal can be used instead of a crystal if it is important to
synchronize the MSP430 with other devices in the system.
Saneesh Cleatus Thundiyil
56
Microcontrollers
4 Sem ECE
High-frequency crystal oscillator, XT2: Similar to LFXT1 except that it is restricted to high
frequencies. It is available in only a few devices and LFXT1 (or VLO) is used instead if XT2 is missing.
Internal very low-power, low-frequency oscillator, VLO: Available in only the more recent
MSP430F2xx devices. It provides an alternative to LFXT1 when the accuracy of a crystal is not
needed.
Digitally controlled oscillator, DCO: Available in all devices and one of the highlights of the
MSP430. It is basically a highly controllable RC oscillator that starts in less than 1s in newer devices.
WATCH DOG TIMERS.
The main purpose of the watchdog timer is to protect the system against failure of the software, such
as the program becoming trapped in an unintended, infinite loop. Watchdog counts up and resets the
MSP430 when it reaches its limit. The code must therefore keep clearing the counter before the limit
is reached to prevent a reset. The operation of the watchdog is controlled by the 16-bit register
WDTCTL
SMCLK
Clock
ACLK
WDT SSEL
WDT CNTCL
(clear)
WDT CNT
(16 bit)
up counter
WDT IFG
PUC
Mode selection
WDT TMSEL
WDT CTL
Control Register
The watchdog counter is a 16-bit register WDTCNT, which is not visible to the user. It is clocked from
either SMCLK (default) or ACLK, according to the WDTSSEL bit. The watchdog is always active after
the MSP430 has been reset. By default the clock is SMCLK, which is in turn derived from the DCO at
about 1 MHz. The default period of the watchdog is the maximum value of 32,768 counts, which is
therefore around 32 ms. We must clear, stop, or reconfigure the watchdog before this time has
elapsed. If the watchdog is left running, the counter must be repeatedly cleared to prevent it counting
up as far as its limit. This is done by setting the WDTCNTCL bit in WDTCTL. The watchdog timer sets
the WDTIFG flag in the special function register IFG1. This is cleared by a power-on reset but its
value is preserved during a PUC. Thus a program can check this bit to find out whether a reset arose
from the watchdog.
BASIC TIMER.
Basic Timer1 is present in all MSP430xF4xx devices. It provides the clock for the LCD module and
generates periodic interrupts. A simplified block diagram of basic timer is shown in figure below.
Newer devices contain a real-time clock driven by a signal at 1Hz from Basic Timer1. The register
BTCTL controls most of the functions of Basic Timer1 but there are also bits in the special function
registers IFG2 and IE2 for interrupts.
Saneesh Cleatus Thundiyil
57
Microcontrollers
4 Sem ECE
The ADC10 module of the MSP430F2274 supports fast 10 bit analogue-to-digital conversions;
The module contains:
10-bit SAR core; The ADC10ON bit enables the core and a flag ADC10BUSY is set while
sampling and conversion is in progress. The result is written to ADC10MEM in a choice
of two formats, selected with the ADC10DF bit.
Clock; This can be taken from MCLK, SMCLK, ACLK, or the modules internal oscillator
ADC10OSC, selected with the ADC10SSELx bits.
Sample-and-Hold Unit: This is shown separately in the block diagram. The time is
chosen with the ADC10SHTx bits, which allow 4, 8, 16, or 64 cycles of ADC10CLK.
Input Selection: A multiplexer selects the input from eight external pins A0A7 (more
in larger MSP430s) and four internal connections.
Conversion Trigger; A conversion can be triggered in two ways provided that the ENC
bit is set. The first is by setting the ADC10SC bit from software (it clears again
automatically).
Saneesh Cleatus Thundiyil
58
Microcontrollers
4 Sem ECE
Port P1 input, P1IN: reading returns the logical values on the inputs if they are
configured for digital input/output. This register is read-only and volatile. It does not
need to be initialized because its contents are determined by the external signals.
Port P1 output, P1OUT: writing sends the value to be driven to each pin if it is
configured as a digital output. If the pin is not currently an output, the value is stored in
a buffer and appears on the pin if it is later switched to be an output. This register is not
initialized and you should therefore write to P1OUT before configuring the pin for
output.
Port P1 direction, P1DIR: clearing a bit to 0 configures a pin as an input, which is the
default in most cases. Writing a 1 switches the pin to become an output. This is for
digital input and output; the register works differently if other functions are selected
using P1SEL.
Port P1 resistor enable, P1REN: setting a bit to 1 activates a pull-up or pull-down
resistor on a pin. Pull-ups are often used to connect a switch to an input as in the section
Read Input from a Switch on page 80. The resistors are inactive by default (0). When
the resistor is enabled (1), the corresponding bit of the P1OUT register selects whether
the resistor pulls the input up to VCC (1) or down to VSS (0).
Port P1 selection, P1SEL: selects either digital input/output (0, default) or an
alternative function (1). Further registers may be needed to choose the particular
function.
Port P1 interrupt enable, P1IE: enables interrupts when the value on an input pin
changes. This feature is activated by setting appropriate bits of P1IE to 1. Interrupts are
off (0) by default. The whole port shares a single interrupt vector although pins can be
enabled individually.
Port P1 interrupt edge select, P1IES: can generate interrupts either on a positive edge
(0), when the input goes from low to high, or on a negative edge from high to low (1). It
is not possible to select interrupts on both edges simultaneously but this is not a
problem because the direction can be reversed after each transition. Care is needed if
the direction is changed while interrupts are enabled because a spurious interrupt may
be generated. This register is not initialized and should therefore be set up before
interrupts are enabled.
Port P1 interrupt flag, P1IFG: a bit is set when the selected transition has been
detected on the input. In addition, an interrupt is requested if it has been enabled. These
bits can also be set by software, which provides a mechanism for generating a software
interrupt (SWI).
59
Microcontrollers
4 Sem ECE
60
Microcontrollers
4 Sem ECE
Additional Questions:
1. Explain the following instructions.
a) DADD: DECIMAL ADD source and carry to the destination.
(Destination) = (carry) + (source) + (destination)
b) BIC: BIC(.b or .w) src, dst: not src and dst to dst.
c) CMP: CMP(.b or .w) src, dst: compare source and destination.
d) SXT dst. Extend bit 7 to bit 8-bit15 (sign extended destination.)
e) CALL (.b or .w) dst: SP-2 > SP, PC+2 > @SP, dst > PC (subroutine call to destination)
Missing 8255 Notes
Eg: interface 8255A with 8051 microcontroller such that the control register is selected for the
address 1003H. find the address of port A,B and C
Solution
The control register is selected for the address 1003H. Address lines A15 to A0 for ports and control register
is as follows.
A15
0
0
0
0
A14
0
0
0
0
A13
0
0
0
0
A12
1
1
1
1
A11
0
0
0
0
A10
0
0
0
0
A9
0
0
0
0
A8
0
0
0
0
A7
0
0
0
0
A6
0
0
0
0
A5
0
0
0
0
A4
0
0
0
0
A3
0
0
0
0
A2
0
0
0
0
A1
0
0
1
1
A0
0
1
0
1
PORT A
PORT B
PORT C
CR
Address of Port A is 1000h, Port B is 1001h, port C is 1002h and control word is 1003h. RD and WR pins of
8051 is connected to RD and WR pins of 8255 as shown in fig. A0 and A1 from 8255 are directly connected to
address lines of 8051. Remaining address lines are connected to the decoder 74LS138 and the output of the
decoder is connected to the CS pin of 8255. Data pins of 8255 is directly connected to the data bus of 8051
microcontroller.
61