Facilitated By:
Siva Prasad.
UTS
Your Expectations…
Topics that will be covered in this Module
Embedded Systems Overview
Processor Architecture
Internal Architecture of 8051 Microcontroller
Cross Platform Development Model
ALP & Addressing Modes
Instruction Set Architecture
Study of various on chip peripherals
External interfacing Techniques
Timers & Counters
Serial Communication
Interrupt Handling
Embedded Communication Protocols
Objectives of the Module
Goals:-
Deep Level Of Understanding Of 8051
Architecture & Internal Mechanisms.
Understanding Of External Interfacing
Techniques.
Basic Understanding Of Embedded
Systems Development Environment.
Embedded System Definition
• An Embedded system
Employs a combination of hardware & software ( a
“computational engine”) to perform a specific function.
Is a part of larger system that may not be a computer.
Works in a reactive & time constrained environment.
Software is used for providing features & flexibility.
Hardware (processor, ASICs, memory,…) is used for
performance (& sometimes security)
• There are wide range of applications for embedded
systems. Following are few Domains.
– Consumer Products
– Automobile
– Aerospace/Avionics & defense applications
– Process Control & Industrial Automation
Embedded ? System ?
Embedded can mean: System can mean:
– Instructions (code or logic) are – A set of one or more
permanently loaded into the
processor.
microelectronic devices
with some, or all, of the
– Dedicated purpose (not
general purpose).Not capacities of a computer.
designed to be programmed – A processor (such as a
by the end user always runs mainframe computer,
on a fixed application. minicomputer or PC) is
– The components of the usually a general purpose,
system are internal to the
which provides facilities
device that contains them,
and not necessarily visible, for incorporating other
even to experts. features & development
– The design depends on the environments.
environment in which the
system will be mounted.
CPUs
• CPUs can be
– Microprocessors or Micro controllers
memory memory
Input
Input CPU
CPU memory
output output
Embedded Development
Environment
Host – Target Development Environment
Cross Compilers
Typical Microcontroller Firmware
development
cycle
Downloading
Debugging Features
Host - Target Development
Environment
• The distinguishing feature of embedded software development is
host-target development environment
• All the development tools like Editors, compilers and linkers are
available on the host machine
• Typical host machines are Windows 95/98, NT and Unix
workstations where the above development tools are available
• Application programs will be written on the host, get compiled,
linked and get the executable file
• Target systems are the ones where compiled and linked code is
executed
• Target systems being a microprocessor based boards does not offer
any development environment themselves, so an host machine is
required.
Cross Compiler
• Another distinguishing feature of embedded
software development is cross compiler
• Runs on a machine based on one type of CPU and
produces a machine instructions for a different
kind of CPU
• Allows execution at development time
• Possible to target multiple instruction sets
Microcontroller’s Firmware Development Cycle
Downloading
• Downloading is the process of loading the
executable image prepared on the host
system on to a target board
• There are various methods to download
the code to a target machine. They are:
– Serial ports
– EPROM/FLASH
– Floppy disks
– Ethernet
– Across a common Bus
Debug monitor commands
• Following are the typical commands of
debug monitor
– Display memory, Modify memory, Disassemble memory, Fill
memory, Search Memory, Move memory
– Display registers, Modify registers
– Display I/O locations, Modify I/O locations
– Download application program, Pass control to the application
program, Single stepping the assembly instructions
– Set break points, Display break points, Clear break point
Microcontroller
• Microcontroller is a device which integrates number of
components of a microprocessor system onto a single chip. It
typically includes:-
CPU (Central Processing unit)
RAM & ROM
I/O inputs & outputs – Serial & Parallel
Timers
Interrupt Controller
By including the features that are specific to the task
(Control) , Cost is relatively low.Microcontroller are a “one chip
solutions” which drastically reduces parts count and design
costs.
Microcontroller
Microcontroller
General features of a
Microcontroller
• Microprocessor:
> Heart of the Microcontroller
> Can understand a fixed set of commands
> Can generate signals to control external
devices
> Contains limited set of on chip memory
called Registers
On Chip Memory
RAM (Volatile)
ROM(Non Volatile)
1.SRAM
1.PROM
2.DRAM
2.UV-EPROM
3.NV-RAM
3.EEPROM
4.FLASH
I/O Ports
• Used to interface with the peripheral devices and
the controller.
Additional on chip peripherals
on an advanced 8051 controller
• Watchdog Timer & Power Monitoring
• ADC & DAC
• SPI,I2C,CAN
• Built in Temp. Sensor
• Flash Memory & External On Chip RAM
• JTAG Support
• Internal Programmable Oscillator
A/D converter
• Converts Analog to Digital
Cost
Avail Speed
Dev MICRO
Tools CONTROLLER Package
No of Power
Pins Consp
Memory
Major 8 bit Controller Family
• Motorola 6811
• Intel 8051
• Microchip 16Fxx
• Zilog Z80
Companies Producing 8051
• Intel
• Atmel
• Philips
• Seimens
• Dallas
• Maxim
• Sharp and many more…
Block Diagram
External interrupts
4K ROM
Interrupt program 128 bytes Timer0 Counter
Control code
RAM Timer1 Inputs
CPU
Bus Serial
4 I/O Ports Port
OSC Control
TXD RXD
P0 P2 P1 P3
Address/Data
Pin Description of 8051
P1.0 1 40 Vcc
P1.1 2 39 P0.0(AD0)
P1.2 3 8 38 P0.1(AD1)
P1.3 4 37 P0.2(AD2)
P1.4 5 36 P0.3(AD3)
P1.5 6 35 P0.4(AD4)
P1.6
P1.7
7
8
0 34
33
P0.5(AD5)
P0.6(AD6)
RST 9 32 P0.7(AD7)
(RXD)P3.0 10 31 EA/VPP
(TXD)P3.1 11 30 ALE/PROG
(INT0)P3.2 12 5 29 PSEN
(INT1)P3.3 13 28 P2.7(A15)
(T0)P3.4 14 27 P2.6(A14)
(T1)P3.5 15 26 P2.5(A13)
(WR)P3.6
(RD)P3.7
16
17
1 25
24
P2.4(A12)
P2.3(A11)
XTAL2 18 23 P2.2(A10)
XTAL1 19 22 P2.1(A9)
GND 20 21 P2.0(A8)
Pins of 8051
• Vcc ( pin 40) :
– Vcc provides supply voltage to the chip.
– The voltage source is +5V.
C2
XTAL2
30pF
C1
XTAL1
30pF
GND
XTAL Connection to
External Clock Source
NC XTAL2
GND
Architectural concepts of 8051
Microcontroller
• Operates with single Power Supply +5V
• 8-bit CPU optimized for control applications
• 16-bit Program Counter (PC) and 16-bit
Data Pointer (DPTR)
• 8-bit Program Status Word (PSW)
• 8-bit Stack Pointer (SP)
• 4K Bytes of On-Chip Program Memory
(Internal ROM or EPROM or Flash)
• 128 bytes of On-Chip Data Memory (Internal RAM):
Four Register Banks, each containing 8 registers
(R0 to R7)
(Total 32 registers)
16 bytes of bit addressable memory i.e.., 128 bits
80 bytes of general-purpose data memory
(Scratch Pad Area)
• Special Function Registers (SFR’s) to configure/operate
microcontroller
• 32 bit bi-directional I/O Lines (4 ports - P0 to P3)
• Two 16-bit timers/counters (T0 and T1)
•Full duplex UART (Universal Asynchronous
Receiver/Transmitter)
•6-source/5-vector interrupts (2 external and 3
internal) with two priority levels
•On-Chip oscillator and clock circuitry
(Address - 0E0h)
• 8 - Bit Register
• Used for operations such as
• Addition
• Subtraction
• Multiplication
• Division &
• Bit Manipulations
• Used for Data Transfer Between 8051 & External
memory
• Used to store Results obtained from Arithmetic as
well as logical Operations
• Bit Addressable Register
(Address - 0F0h)
• 8 - Bit Register.
• Stack Pointer is Initialized to 07h at RESET
condition.
• Instructions such as PUSH and POP modify SP.
• UP - GROWING stack.
• Can be reinitialized to any desired location by the
user.
(Direct Address - NIL)
• 16 - Bit Register
• Used to hold the Address of external Data
Memory.
• Can be used as two individual 8 - bit registers
such as
• Data Pointer High (DPL) - Address 82h
• Data Pointer Low (DPH) - Address 83h
DPH 83h DPL 82h
DPTR
• 16 - Bit Register
• Used to hold the Address of the next instruction
to be executed
• Not directly accessible to the programmer.
• No internal address.
• Group of registers used for specific purposes
in 8051 microcontroller mainly to
• Configure
• Control, &
• operate.
• Uses address locations from 80h to 0ffh.
• Accumulator - A*
• Register B - B*
• Program Counter - PC
• Data Pointer - DPTR
• Stack Pointer - SP
• Program Status Word - PSW
• Ports - P0* , P1* , P2* , P3* .
• Timer Load Registers - TL0/1 & TH0/1
• Timer Config. & Ctrl Reg. - TCON*, TMOD
• Serial Comm. Control Register - SCON*
• Serial Comm. Buffer Register - SBUF
• Program Control Register - PCON
• Interrupt Enable Register - IE*
• Interrupt Priority Register - IP*
NOTE:
Registers marked with * are both Bit as well as Byte Addressable
• External bus of 8051 is grouped
into 4 ports namely:
• Port - 0
• Port - 1
• Port - 2
• Port - 3
• These ports supports all the three types of
buses namely:
• Address Bus,
• Data Bus, &
• Control Bus.
• All the ports are Bi - directional.
• All the ports are Bit as well as Byte addressable.
• The addresses of the ports are as shown below:
PORTS ADDRESS
PORT 0 080h
PORT 1 090h
PORT 2 0A0h
PORT 3 0B0h
8051 BLOCK DIAGRAM
I/O
Port 0
A0-A7
ALU PSW SFR
D0-D7
Port 1
I/O
A B
Port 2
I/O
A8-A15
DPTR
PC DPH ROM
I/O
Port 3
DPL INT
CNTR
SERIAL
RD/WR
Friday, October 22, 2010
~EA
BYTE / BIT SFR
ALE System Timing
ADDRESSIBLE
PSEN IE
System Interrupt
XTAL1 RB3 IP
Timers
XTAL2 PCON
RB2
RESET Data Buffers SBUF
VCC Memory Controls RB1
SCON
GND
RB0 TCON
TMOD
TL0
TH0
TL1
TH1
SP
81
7 TF1 Timer 1 Overflow flag. Set when timer rolls from all 1s to 0. Cleared when processor
vectors to execute interrupt service routine located at program address 001Bh.
6 TR1 Timer 1 run control bit. Set to 1 by program to enable timer to count; cleared to 0 by program to
halt timer. Does not reset timer.
5 TF0 Timer 0 Over flow flag. Set when timer rolls from all 1s to 0. Cleared when processor
vectors to execute interrupt service routine located at program address 000Bh.
4 TR0 Timer 0 run control bit. Set to 1 by program to enable timer to count; cleared to 0 by program to
halt timer. Does not reset timer.
3 IE1 External interrupt 1 Edge flag. Set to 1 when a high-to-low edge signal is received on port 3 pin
3.3 (INT 1). Cleared when processor vectors to interrupt service routine located at program
address 0013h. Not related to timer operations.
2 IT1 External interrupt 1 signal type control bit. Set to 1 by program to enable external interrupt 1 to
be triggered by a falling edge signal. Set to 0 by program to enable a low-level signal or external
interrupt 1 to generate an interrupt.
1 IE0 External interrupt 0 Edge flag. Set to 1 when a high-to-low edge signal is received on port 3 pin
3.2 (INT 0). Cleared when processor vectors to interrupt service routine located at program
address 0003h. Not related to timer operations.
0 IT0 External interrupt 0 signal type control bit. Set to 1 by program to enable external interrupt 0 to
be triggered by a falling edge signal. Set to 0 by program to enable a low-level signal or external
interrupt 0 to generate an interrupt. Friday, October 22, 2010
THE TIMER MODE CONTROL (TMOD)
7 6 5 4 3 2 1 0
Gate C/T M1 M0 Gate C/T M1 M0
Timer 1 Timer 0
7/3 Gate OR gate enable bit which controls Run/ Stop of timer
6/2 C/T Set to 1 by program to make timer act as counter
5/1 M1 Mode select bit 1
4/0 M0 Mode select bit 0
M1 M0 Mode
0 0 0
0 1 1
1 0 2
1 1 3
Accessible by direct
& indirect addressing
(00-7F)=128 bytes
Lower 128 bytes of internal RAM
0FFFh
4 Kbytes of
Internal
ROM
0000h
EXTERNAL MEMORY ORGANIZATION
8051 Memory
Latch ADD-L
• External Data Memory is accessed using
16 - bit address lines.
RD bar and WR bar are used as strobe signals
• Address range : 0000h to 0FFFFh
- External Memory
00h to 7Fh
- Internal Memory
FFFFh
FFh
SFR’s
Direct
Addressing
64 Kbytes
only
80h of
External
7Fh Direct Memory
& In Direct
Addressing
only
00h
0000h
Program Memory
64K FFFFH
4K External
0FFFH ROM
EA=0
Internal
ROM
EA=1
0000H 0000H
PSEN
Data Memory
64K FFFFH
128 bytes
External
7FH RAM
Internal
RAM
00H 0000H
WR RD
Internal RAM allocation
7FH
0 1 1 08H-0FH
1FH 20H
Register Bank3
18H 17H
Register Bank2
0FH 10H
Register Bank1
08H 07H Stack
Pointer
Register Bank0
00H
Internal RAM and SFR
region
FFH FFH
Upper Accessible
Accessible SFR
128 by Indirect by Direct
addressing addressing
only Ports,
only
Timers,
Control
Registers,
80H 80H
Accumulator
7FH
,Stack
Pointer,
Accessible
Etc,.
by Direct &
Lower Indirect
128 addressing
00H
SFR Memory map
FF
F8
B F7
F0
EF
E8
ACC E7
E0
DF
D8
PSW D7
D0
CF
C8
C7
C0
IP BF
B8
P3 B7
B0
IE AF
A8
P2 A7
A0
SCON SBUF 9F
98
P1 97
90
TCON TMOD TL0 TL1 TH0 TH1 8F
88
P0 SP DPL DPH PCON 87
80
Program status Word
CY AC F0 RS1 RS0 OV -- P
+ 64 Kbytes
OR of
External
0FFFh ROM
4Kbytes of
Internal 0000h
ROM
0000h
8051 Memory Structure
External
External
(64Kb)
indirect direct
(128) (128)
mov R0, a
mov a, R0
Play with the Register Banks
Addressing Modes
Register Indirect – the address of the source or
destination is specified in registers
Uses registers R0 or R1 for 8-bit address:
mov psw, #0 ; use register bank 0
mov r0, #0x3C
mov @r0, #3 ; memory at 3C gets #3
; M[3C] 3
Uses DPTR register for 16-bit addresses:
mov dptr, #0x9000 ; dptr 9000h
mov a, @dptr ; a M[9000]
Note: can only specify RAM or SFRs (direct mode) to push or pop.
Therefore, to push/pop the accumulator, must use acc, not a:
push acc
push a
Stacks
pop
push
stack pointer
stack
Address Modes
Exchange Instructions – two way data
transfer
XCH a, 0x30 ; a M[30]
XCH a, R0 ; a R0
XCH a, @R0 ; a M[R0]
XCHD a, R0 ; exchange
a[7..4] a[3..0] R0[7..4] R0[3..0]
“digit”
Address Modes
• Bit-Oriented Data Transfer – transfers between individual bits.
• SFRs with addresses divisible by 0 or 8 are bit-addressable. (80, 88, 90, 98, etc)
• Carry flag (C) (bit 7 in the PSW) is used as a single-bit accumulator
Examples:
mov C, P0.0 ; C bit 0 of P0
Bit Addressable Memory
2F 7F 78
20h – 2Fh (16 locations X
2E 8-bits = 128 bits)
2D
2C
Bit addressing:
2B
mov C, 1Ah
2A
29
or
28 mov C, 23h.2
27
26
25
24
23 1A
22 10
21 0F 08
20 07 06 05 04 03 02 01 00
SPRs that are Bit Addressable
Address Register
addressable. 0xD8
0xD0
PCA0CN
PSW
SFRs 0xC8 T2CON
0xC0 SMB0CN
Notice that all 4 0xB8 IP
Pink are
parallel I/O ports are implemented in
0xB0 P3
0xA8 IE
bit addressable. enhanced
C8051F020 0xA0 P2
0x98 SCON
0x90 P1
0x88 TCON
0x80 P0
Go Access the Port Bits….
Part II
Arithmetic Instructions
Logic Instructions
Arithmetic Instructions
• Add
• Subtract
• Increment
• Decrement
• Multiply
• Divide
• Decimal adjust
Arithmetic Instructions
Mnemonic Description
ADD A, byte add A to byte, put result in A
ADDC A, byte add with carry
SUBB A, byte subtract with borrow
INC A increment A
INC byte increment byte in memory
INC DPTR increment data pointer
DEC A decrement accumulator
DEC byte decrement byte
MUL AB multiply accumulator by b register
DIV AB divide accumulator by b register
DA A decimal adjust the accumulator
ADD Instructions
add a, byte ; a a + byte
addc a, byte ; a a + byte + C
These instructions affect 3 bits in PSW:
C = 1 if result of add is greater than FF
AC = 1 if there is a carry out of bit 3
OV = 1 if there is a carry out of bit 7, but not from bit 6,
or visa versa.
Instructions that Affect PSW bits
ADD Examples
mov a, #0x3F • What is the value of
add a, #0xD3 the C, AC, OV flags
after the second
0011 1111 instruction is
1101 0011 executed?
0001 0010
C = 1
AC = 1
OV = 0
Signed Addition and Overflow
0111 1111 (positive 127)
2’s complement: 0111 0011 (positive 115)
0000 0000 00 0 1111 0010 (overflow
cannot represent 242 in 8
… bits 2’s complement)
0111 1111 7F 127
1000 1111 (negative 113)
1000 0000 80 -128
1101 0011 (negative 45)
… 0110 0010 (overflow)
1111 1111 FF -1
0011 1111 (positive)
1101 0011 (negative)
0001 0010 (never overflows)
The ADD example…..
Subtract
SUBB A, byte subtract with borrow
Example:
SUBB A, #0x4F ; A A – 4F – C
mov a, r2
add a, #1 ; use add rather than increment to affect C
mov r2, a
mov a, r3
addc a, #0 ; add C to most significant byte
mov r3, a
Multiply
When multiplying two 8-bit numbers, the
size of the maximum product is 16-bits
FF x FF = FE01
(255 x 255 = 65025)
MUL AB ; BA A * B
A Quotient(A/B), B Remainder(A/B)
Example:
mov a, #0x23
mov b, #0x29
add a, b ; a 23 + 29 = 4C (wanted 52)
DA a ; a a + 6 = 52
Logic Instructions
00001111
XRL 10101100
10100011
CPL 10101100
01010011
Address Modes with Logic
ANL – AND a, byte
ORL – OR direct, reg. indirect, reg, immediate
XRL – eXclusive oR
byte, a
direct
byte, #constant
rl a
C
rrc a
mov a, #0A9h ; a A9
add a, #14h ; a BD (10111101), C0
rrc a ; a 01011110, C1
Swap
swap a
mov a, #72h
swap a ; a 27h
Bit Logic Operations
Some logic operations can be used with single bit
operands
ANL C, bit ANL C, /bit
ORL C, bit ORL C, /bit
CLR C
CLR bit
CPL C “bit” can be any of the bit-addressable RAM
locations or SFRs.
CPL bit
SETB C
SETB bit
Rotate and Multiplication/Division
• Note that a shift left is the same as
multiplying by 2, shift right is divide by 2
loop: mov a, P1
jz loop ; if a=0, goto loop,
;else goto next
;instruction
mov b, a
Conditional jumps
Mnemonic Description
JZ <rel addr> Jump if a = 0
JNZ <rel addr> Jump if a != 0
JC <rel addr> Jump if C = 1
JNC <rel addr> Jump if C != 1
JB <bit>, <rel addr> Jump if bit = 1
JNB <bit>,<rel addr> Jump if bit != 1
JBC <bir>, <rel addr> Jump if bit =1, clear bit
CJNE A, direct, <rel Compare A and
addr> memory, jump if not
equal
Conditional Jumps for
Branching
if condition is true condition
goto label false
else true
goto next instruction label
jz led_off
setb C
if a = 0 is true mov P1.6, C
send a 0 to LED sjmp skipover
else led_off: clr C
mov P1.6, C
send a 1 to LED skipover: mov A, P0
More Conditional Jumps
Mnemonic Description
CJNE A, #data <rel addr> Compare A and data, jump if
not equal
CJNE Rn, #data <rel addr> Compare Rn and data, jump
if not equal
CJNE @Rn, #data <rel addr> Compare Rn and memory,
jump if not equal
DJNZ Rn, <rel addr> Decrement Rn and then
jump if not zero
DJNZ direct, <rel addr> Decrement memory and then
jump if not zero
Iterative Loops
For A = 0 to 4 do For A = 4 to 0 do
{…} {…}
ret ; PC stack
Subroutines
call to the subroutine
Main: ...
acall sublabel
...
...
sublabel:...
... the subroutine
ret
Initializing Stack Pointer
• The Stack Pointer (SP) is initialized to 0x07. (Same
address as R7)
• When using subroutines, the stack will be used to store
the PC, so it is very important to initialize the stack
pointer. Location 2F is often used.
ret
END
Subroutine – another example
; Program to compute square root of value on Port 3 (bits 3-0) and
; output on Port 1.
$INCLUDE (C8051F020.inc)
cseg at 0
ljmp Main
reset vector
Main: mov P3MDOUT, #0 ; Set open-drain mode
mov P3, #0xFF ; Port 3 is an input
mov P1MDOUT, #0xFF ; Port 1 is an output
mov XBR2, #40h ; Enable crossbar
loop: mov a, P3 main program
anl a, #0x0F ; Clear bits 7..4 of A
lcall sqrt
mov P1, a
sjmp loop
sqrt: inc a
movc a, @a + PC
subroutine
ret
squares: db 0,1,1,1,2,2,2,2,2,3,3,3,3,3,3,3 data
end
Why Subroutines?
• Subroutines allow us to have "structured"
assembly language programs.
• This is useful for breaking a large design
into manageable parts.
• It saves code space when subroutines can
be called many times in the same
program.
Timeout for Subroutines....
…
Interrupts
mov a, #2
mov b, #16
mul ab
mov R0, a
Program Execution
External ADC 1
Clock
source Serial Timer 4
Valid (UART) 1 ADC 0
External
7 External 6 Timer 3
External Interrupts
• /INT0 (Interrupt 0) and /INT1 (Interrupt 1) are
external input pins.
• Interrupt 6 and Interrupt 7 use Port 3 pins 6 and
7:
INT 6 = P3.6
INT 7 = P3.7
These interrupts can be configured to be
– rising edge-triggered
– falling edge-triggered
External Interrupts