Uploaded by bkflhdd947

Combined PDF

advertisement
8051 Programming
Programming Language
High Level Programming – Application Oriented such as C, C++, C#,
Java etc
Low Level programming: Assembly Programming
 In the early days of the computer, programmers coded in machine
language, consisting of 0s and 1s
 Tedious, slow and prone to error
 Assembly languages, which provided mnemonics for the machine
code instructions, plus other features, were developed
 An Assembly language program consist of a series of lines of
Assembly language instructions
 Assembly language is referred to as a low level language
 It deals directly with the internal structure of the CPU
© 2019 UPES
Compiler and Assembler
 Compiler: Converts High level language program into machine level
programming
 Assembler: Converts low level language program into machine level
programming
© 2019 UPES
ASSEMBLER DIRECTIVES
The assembling directives give the directions to the CPU.
The 8051 microcontroller consists of various kinds of
assembly directives to give the direction to the control
unit. The most useful directives are 8051 programming,
such as:
1. ORG
2. DB
3. EQU
4. END
ORG(origin): This directive indicates the start of the program.
This is used to set the register address during assembly.
For example; ORG 0000h tells the compiler all subsequent code starting at address 0000h
END:The END directive is used to indicate the end of the
program.
Syntax:
reg equ,09h
—————–
—————–
MOV reg,#2h
END
Assembly Programming Format
 An Assembly language instruction consists of four fields:
© 2019 UPES
Flow to Run Code with Development Tools
© 2019 UPES
 The step of Assembly language are outlines as follows:
Editor: First we use an editor to type a program, many excellent editors or
word processors are available that can be used create and/or edit the
program Notice that the editor must be able to produce an ASCII file
 For many assemblers, the file names follow the usual DOS conventions,
but the source file has the extension “asm“ or “src”, depending on which
assembly you are using
Assembler: The “asm” source file containing the program code created in
step 1 is fed to
 an 8051 assembler :The assembler converts the instructions into machine
code
 The assembler will produce an object file and a list file
 The extension for the object file is “obj” while the extension for the list file
is “lst”
© 2019 UPES
Linker and Loader : Assembler require a third step called linking
 The linker program takes one or more object code files and produce an
absolute object file with the extension “abs”
 This abs file is used by 8051 trainers that have a monitor program
Object to Hex Converter (OH) : Next the “abs” file is fed into a program
called “OH” (object to hex converter) which creates a file with extension
“hex” that is ready to burn into ROM
 This program comes with all 8051 assemblers
 Recent Windows-based assemblers combine step 2 through 4 into one
step
© 2019 UPES
Instructions
Data transfer instructions
Data processing (arithmetic and logic)
Program flow instructions
Data Transfer Instructions
MOV dest, source
Stack instructions
PUSH byte
POP byte
dest  source
;increment stack
;move byte
;move from stack
;decrement
pointer,
on stack
to byte,
stack pointer
Exchange instructions
XCH a, byte
XCHD a, byte
;exchange accumulator and byte
;exchange low nibbles of
;accumulator and byte
Exchange Instructions
two way data transfer
XCH a, 30h
XCH a, R0
XCH a, @R0
XCHD a, R0
;
;
;
;
a  M[30]
a  R0
a  M[R0]
exchange “digit”
a[7..4] a[3..0]
R0[7..4] R0[3..0]
Only 4 bits exchanged
Bit-Oriented Data Transfer
 transfers between individual bits.
 Carry flag (C) (bit 7 in the PSW) is used as a singlebit accumulator
 RAM bits in addresses 20-2F are bit addressable
mov C, P0.0
mov C, 67h
Data Processing Instructions
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
 What is the value of the C, AC,
OV flags after the second
instruction is executed?
mov a, #3Fh
add a, #D3h
0011 1111
1101 0011
0001 0010
C = 1
AC = 1
OV = 0
Subtract
SUBB A, byte
subtract with borrow
Example:
SUBB A, #0x4F
;A  A – 4F – C
Notice that
There is no subtraction WITHOUT borrow.
Therefore, if a subtraction without borrow is desired,
it is necessary to clear the C flag.
Example:
Clr c
SUBB A, #0x4F
;A  A – 4F
Increment and Decrement
INC A
increment A
INC byte
increment byte in memory
INC DPTR
increment data pointer
DEC A
decrement accumulator
DEC byte
decrement byte
 The increment and decrement instructions do NOT affect
the C flag.
 Notice we can only INCREMENT the data pointer, not
decrement.
Example: Increment 16-bit Word
Assume 16-bit word in R3:R2
mov a, r2
add a, #1
mov r2, a
mov a, r3
addc a, #0
mov r3, a
; use add rather than increment to affect C
; add C to most significant byte
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
Note : B gets the High byte
A gets the Low byte

A * B
Division
Integer Division
DIV AB
; divide A by B
A  Quotient(A/B)
B  Remainder(A/B)
OV - used to indicate a divide by zero condition.
C – set to zero
Decimal Adjust
DA a
; decimal adjust a
Used to facilitate BCD addition.
Adds “6” to either high or low nibble after an addition
to create a valid BCD number.
Example:
mov a, #23h
mov b, #29h
add a, b
DA a
; a  23h + 29h = 4Ch (wanted 52)
; a  a + 6 = 52
Logic Instructions
 Bitwise logic operations

(AND, OR, XOR, NOT)
 Clear
 Rotate
 Swap
Logic instructions do NOT affect the flags in PSW
Bitwise Logic
ANL  AND
ORL  OR
XRL  XOR
CPL  Complement
Examples:
00001111
ANL 10101100
00001100
00001111
ORL 10101100
10101111
00001111
XRL 10101100
10100011
CPL
10101100
01010011
Address Modes with Logic
ANL – AND
ORL – OR
XRL – eXclusive oR
a, byte
direct, reg. indirect, reg,
immediate
byte, a
direct
byte, #constant
CPL – Complement
a
ex:
cpl a
Uses of Logic Instructions
 Force individual bits low, without affecting other bits.
anl PSW, #0xE7
;PSW AND 11100111
 Force individual bits high.
orl PSW, #0x18
;PSW OR 00011000
 Complement individual bits
xrl P1, #0x40
;P1 XRL 01000000
Other Logic Instructions
CLR
RL
RLC
RR
RRC
SWAP
–
–
–
–
–
clear
rotate left
rotate left through Carry
rotate right
rotate right through Carry
swap accumulator nibbles
CLR ( Set all bits to 0)
CLR
CLR
CLR
CLR
A
byte
Ri
@Ri
(direct mode)
(register mode)
(register indirect mode)
Rotate
Rotate instructions operate only on a
RL a
Mov a,#0xF0
RL a
; a 11110000
; a 11100001
RR a
Mov a,#0xF0
RR a
; a 11110000
; a 01111000
Rotate through Carry
RRC a
C
mov a, #0A9h
add a, #14h
; a  A9
; a  BD (10111101), C0
rrc a
; a  01011110, C1
RLC a
C
mov a, #3ch
setb c
; a  3ch(00111100)
; c  1
rlc a
; a  01111001, C1
Rotate and Multiplication/Division
Note that a shift/rotate left is the same as multiplying by
2, shift/rotate right is divide by 2
mov
clr
rlc
rlc
rrc
a, #3
C
a
a
a
;
;
;
;
;
A
C
A
A
A
00000011
0
00000110
00001100
00000110
(3)
(6)
(12)
(6)
Swap
SWAP a
mov a, #72h
swap a
; a  72h
; a  27h
Bit Logic Operations
 Some logic operations can be used with single bit
operands
ANL C, bit
ORL C, bit
CLR C
CLR bit
CPL C
CPL bit
SETB C
SETB bit
 “bit” can be any of the bit-addressable RAM locations
or SFRs.
Program Flow Control
Unconditional jumps (“go to”)
Conditional jumps
Call and return
Unconditional Jumps
SJMP <rel addr>
;
Short jump, relative address is
8-bit 2’s complement number, so jump can be up to 127 locations
forward, or 128 locations back.
LJMP <address 16> ;
AJMP <address 11> ;
Long jump
Absolute jump to anywhere within 2K
block of program memory
JMP @A + DPTR
;
Long indexed jump
Infinite Loops
Start: mov C, p3.7
mov p1.6, C
sjmp Start
Microcontroller application programs are almost always infinite loops!
Conditional Jump
 These instructions cause a jump to occur only if a
condition is true. Otherwise, program execution
continues with the next instruction.
loop: mov a, P1
jz loop
; if a=0, goto loop,
; else goto next instruction
mov b, a
 There is no zero flag (z)
 Content of A checked for zero on time
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,
bit
&clear
CJNE A, direct, <rel addr> Compare A and memory,
jump if not equal
Example: Conditional Jumps
if (a = 0) is true
send a 0 to LED
else
send a 1 to LED
jz led_off
Setb P1.6
sjmp skipover
led_off: clr P1.6
mov A, P0
skipover:
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
{…}
clr a
loop: ...
...
inc a
cjne a, #4, loop
mov R0, #4
loop: ...
...
djnz R0, loop
Iterative Loops(examples)
mov a,#50h
mov b,#00h
cjne a,#50h,next
mov b,#01h
next: nop
end
mov a,#0aah
mov b,#10h
Back1:mov r6,#50
Back2:cpl a
djnz r6,back2
djnz b,back1
end
mov a,#25h
mov r0,#10h
mov r2,#5
Again: mov @ro,a
inc r0
djnz r2,again
end
mov a,#0h
mov r4,#12h
Back: add a,#05
djnz r4,back
mov r5,a
end
Call and Return
Call is similar to a jump, but
Call pushes PC on stack before branching
acall <address ll>
lcall <address 16>
; stack  PC
; PC  address 11 bit
; stack  PC
; PC  address 16 bit
Return
Return is also similar to a jump, but
Return instruction pops PC from stack to get address to jump to
ret
; PC  stack
example of delay
mov a,#0aah
Back1:mov p0,a
lcall delay1
cpl a
sjmp back1
Delay1:mov r0,#0ffh;1cycle
Here: djnz r0,here ;2cycle
ret
;2cycle
end
Delay=1+255*2+2=513 cycle
Delay2:
mov r6,#0ffh
back1: mov r7,#0ffh ;1cycle
Here: djnz r7,here ;2cycle
djnz r6,back1;2cycle
ret
;2cycle
end
Delay=1+(1+255*2+2)*255+2
=130818 machine cycle
Long delay Example
GREEN_LED:
Main:
Again:
Delay:
Loop1:
Loop0:
equ P1.6
org ooh
ljmp Main
reset service
org 100h
clr
GREEN_LED
acall Delay
cpl
GREEN_LED
sjmp Again
main program
mov
mov
mov
djnz
djnz
djnz
ret
END
R7,
R6,
R5,
R5,
R6,
R7,
#02
#00h
#00h
$
Loop0
Loop1
subroutine
Embedded ‘C’ Programming
 Compilers produce hex files that is downloaded to ROM of
microcontroller
 The size of hex file is the main concern. Microcontrollers have limited onchip ROM Code space for 8051 is limited to 64K bytes
Why Embedded ‘C’ Programming
 C programming is less time consuming, but has larger hex file size. The
reasons for writing programs in C.
 It is easier and less time consuming to write in C than Assembly
 C is easier to modify and update
 You can use code available in function libraries
 C code is portable to other microcontroller with little of no modification
© 2019 UPES
 A good understanding of C data The character data type is the most
types for 8051 can help
natural choice
programmers to create smaller
 8051 is an 8-bit microcontroller.
hex files
Unsigned char is an 8-bit data
 Unsigned char
type in the range of 0 – 255 (00 –
FFH)
 Signed char
 One of the most widely used data
 Unsigned int
types for the 8051
 Signed int
Counter value
 Sbit (single bit)
ASCII characters
 Bit and sfr
 C compilers use the signed char
as the default if we do not put the
keyword
unsigned
© 2019 UPES
© 2019 UPES
Examples
© 2019 UPES
Examples
© 2019 UPES
Example Continued
 The unsigned int is a 16-bit data
type Takes a value in the range
of 0 to 65535 (0000 – FFFFH)
 Define 16-bit variables such as
memory addresses. Set counter
values of more than 256
 Since registers and memory
accesses are in 8-bit chunks, the
misuse of int variables will result
in a larger hex file
 Signed int is a 16-bit data type:
Use the MSB D15 to represent –
or +
 We have 15 bits for the
magnitude of the number from –
32768 to +32767
© 2019 UPES
© 2019 UPES
© 2019 UPES
The 8051
Microcontroller
Difference Between Microprocessor and Microcontroller
General-purpose microprocessors contains (Optional)
 No RAM
 No ROM
 No I/O ports
Microcontroller has on chip
 CPU (microprocessor)
 RAM
 ROM
 I/O ports
 Timer
 ADC and other peripherals
© 2019 UPES
COMPARISON
MICROPROCESSOR
MICROCONTROLLER
No internal memory.
Contains RAM/ROM.
No interfacing circuits or counters or timers.
Contains serial I/O, parallel I/O, and counters.
Has many opcodes for moving data from external
memory to CPU.
Has one or two opcodes.
Has one or two types of bit handling instructions.
They have many.
Concerned with rapid movement of code and data from
external memory to CPU.
Concerned with rapid movement of bits within chip.
Must have many additional parts to be operational.(FD,
K/B, CRT, etc)
Can function as a computer with no addition of external
digital parts.
It is meant to fetch data, perform extensive calculations
on that, and then store those calculations on mass
storage devices or display the results for human use.
It is meant to fetch data, perform limited calculations on
that data, and control its environment based on that
calculation.
VON NEUMAN ARCHITECTURE
HARVARD ARCHITECTURE
An embedded product uses a microprocessor (or microcontroller) to
do one task and one task only
 There is only one application software that is typically burned into ROM
A PC, in contrast with the embedded system, can be used for any
number of applications
 It has RAM memory and an operating system that loads a variety of
applications into RAM and lets the CPU run them
 A PC contains or is connected to various embedded products
 Each one peripheral has a microcontroller inside it that performs only one
task
© 2019 UPES
8051 Microcontrollers Applications as Embedded Devices
Home
 Appliances, intercom, telephones, security systems, garage door
openers, answering machines, fax machines, home computers, TVs,
cable TV tuner, VCR, camcorder, remote controls, video games, cellular
phones, musical instruments, sewing machines, lighting control, paging,
camera, pinball machines, toys, exercise equipment
Office
 Telephones, computers, security systems, fax machines, microwave,
copier, laser printer, color printer, paging
Auto
 Trip computer, engine control, air bag, ABS, instrumentation, security
system, transmission control, entertainment, climate control, cellular
phone, keyless entry
© 2019 UPES
Companies
8-bit microcontrollers
 Motorola’s 6811
 Intel’s 8051
 Zilog’s Z8
 Microchip’s PIC
There are also 16-bit and 32-bit:
microcontrollers made by various
chip makers
The 8051 family has the largest
number of diversified (multiple
source) suppliers
 Intel (original)
 Atmel
 Philips/Signetics
 AMD
 Infineon (formerly Siemens)
 Matra
 Dallas Semiconductor/Maxim
© 2019 UPES
Criteria to Choose a Microcontroller
 Meeting the computing needs of the task at hand efficiently and cost
Effectively
 Speed
 Packaging
 Power consumption
 The amount of RAM and ROM on chip
 The number of I/O pins and the timer on chip
 How easy to upgrade to higher performance or lower powerconsumption versions
 Cost per unit
 Availability of software development tools, such as compilers,
assemblers, and debuggers
 Wide availability and reliable sources of the microcontroller
© 2019 UPES
8051 Basic Component
4K bytes internal ROM
128 bytes internal RAM
Four 8-bit I/O ports (P0 - P3).
Two 16-bit timers/counters
One serial interface
CPU
I/O
Port
RAM ROM
Serial
Timer COM
Port
A single chip
Microcontroller
Block Diagram
External Interrupts
Interrupt
Control
Timer 1
Timer 2
4k
ROM
128 bytes
RAM
Bus
Control
4 I/O Ports
CPU
OSC
P0 P2 P1
Addr/Data
P3
Serial
TXD RXD
Other 8051 featurs
 only 1 On chip oscillator (external crystal)
 6 interrupt sources (2 external , 3 internal, Reset)
 64K external code (program) memory(only read)PSEN
 64K external data memory(can be read and write) by
RD,WR
 Code memory is selectable by EA (internal or external)
 We may have External memory as data and code
Overview
The Intel 8051 is a very popular general purpose microcontroller
widely used for small scale embedded systems. Many vendors such
as Atmel, Philips, and Texas Instruments produce MCS-51 family
microcontroller chips.
The 8051 is an 8-bit microcontroller with 8 bit data bus and 16-bit
address bus. The 16 bit address bus can address a 64K( 216) byte
code memory space and a separate 64K byte of data memory space.
The 8051 has 4K on-chip read only code memory and 128 bytes of
internal Random Access Memory (RAM)
Contd.
 Besides internal RAM, the 8051 has various Special
Function Registers (SFR) such as the Accumulator, the
B register, and many other control registers.
 34 8-bit general purpose registers in total.
The ALU performs one 8-bit operation at a time.
 Two 16 bit /Counter timers
 3 internal interrupts (one serial), 2 external interrupts.
 4 8-bit I/O ports (3 of them are dual purposed). One of
them used for serial port,
Some 8051 chips come with UART for serial
communication and ADC for analog to digital
conversion.
8051
Foot Print
P1.0
P1.1
P1.2
P1.3
P1.4
P1.5
P1.6
P1.7
RST
(RXD)P3.0
(TXD)P3.1
(INT0)P3.2
(INT1)P3.3
(T0)P3.4
(T1)P3.5
(WR)P3.6
(RD)P3.7
XTAL2
XTAL1
GND
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
8051
(8031)
(8751)
(8951)
40
39
38
37
36
35
34
33
32
31
30
29
28
27
26
25
24
23
22
21
Vcc
P0.0(AD0)
P0.1(AD1)
P0.2(AD2)
P0.3(AD3)
P0.4(AD4)
P0.5(AD5)
P0.6(AD6)
P0.7(AD7)
EA/VPP
ALE/PROG
PSEN
P2.7(A15)
P2.6(A14)
P2.5(A13)
P2.4(A12)
P2.3(A11)
P2.2(A10)
P2.1(A9)
P2.0(A8)
IMPORTANT PINS (IO Ports)
 One of the most useful features of the 8051 is that it
contains four I/O ports (P0 - P3)
 Port 0 (pins 32-39):P0(P0.0~P0.7)
 8-bit R/W - General Purpose I/O
 Or acts as a multiplexed low byte address and data bus for external
memory design

Port 1 (pins 1-8) :P1(P1.0~P1.7)
 Only 8-bit R/W - General Purpose I/O

Port 2 (pins 21-28):P2(P2.0~P2.7)
 8-bit R/W - General Purpose I/O
 Or high byte of the address bus for external memory design

Port 3 (pins 10-17):P3(P3.0~P3.7)
 General Purpose I/O
 if not using any of the internal peripherals (timers) or external
interrupts.
 Each port can be used as input or output (bi-direction)
Port 3 Alternate Functions
IMPORTANT PINS
PSEN (out):
Program Store Enable, the read
signal for external program memory (active low).
 ALE (out):
Address Latch Enable, to latch
address outputs at Port0 and Port2
 EA (in):
External Access Enable, active low to
access external program memory locations 0 to 4K
 RXD,TXD: UART pins for serial I/O on Port 3
 XTAL1 & XTAL2: Crystal inputs for internal
oscillator.
Pins of 8051
 Vcc(pin 40):
Vcc provides supply voltage to the chip.
The voltage source is +5V.
 GND(pin 20):ground
 XTAL1 and XTAL2(pins 19,18):
These 2 pins provide external clock.
Way 1:using a quartz crystal oscillator
Way 2:using a TTL oscillator
Example 4-1 shows the relationship between XTAL and the
machine cycle.
Pins of 8051
 /EA(pin 31):external access
 There is no on-chip ROM in 8031 and 8032 .
 The /EA pin is connected to GND to indicate the code is stored externally.
 /PSEN & ALE are used for external ROM.
 For 8051, /EA pin is connected to Vcc.
 “/” means active low.
 /PSEN(pin 29):program store enable
 This is an output pin and is connected to the OE pin of the ROM.
 See Chapter 14.
Pins of 8051
 ALE(pin 30):address latch enable
It is an output pin and is active high.
8051 port 0 provides both address and data.
The ALE pin is used for de-multiplexing the address and data by
connecting to the G pin of the 74LS373 latch.
PC & DATA POINTER
 16 bit registers

PC is Used to hold the address of a byte in memory
 Program instruction bytes are fetched from locations in memory that are addressed by PC.
 Program ROM may be on the chip at addresses 0000 H to 0FFF H, external to the chip for addresses that exceed
0FFF H or totally external for all addresses from 0000 H to FFFF H.
 The PC is automatically incremented after every instruction byte is fetched and may also be altered by certain
instructions.
 PC is the only register that does not have an internal address.
 DPTR is made of 2 8-bit registers DPH and DPL.
 They are used to furnish memory addresses for internal and external code access and external data access.
 DPTR is under the control of program instructions.
 DPTR does not have single internal address (DPL & DPH are each assigned an address).
A & B CPU REGISTERS
 34 GPR
 2 of these are A & B- mathematical core registers
 Other 32 arranged as part of internal RAM as four banks of registers, B0 to B3, of 8 registers each, named R0 to
R7.
 A- addition, subtraction, integer multiplication, division, Boolean bit manipulations, data transfer between 8051 &
external memory
 B- used with A register, or to store data
FLAGS & PROGRAM STATUS WORD (PSW)
 FLAGS are 1 bit registers provided to store the result of certain program instructions.
 Other instructions can test condition of flags and make decisions based upon the flag states.
 Flags are grouped inside PSW & PCON (power control) registers.
 8051 has 4(math flags) + 3(general purpose user) = 7 flags.
 Math flags respond automatically to the output of math operations ( carry C, auxiliary carry AC, overflow OV and
parity P).
 G.P.F. can be set to 1 or cleared to 0by the programmer as desired (F0, GF0 & GF1).
 All flags can be set or cleared by programmer at will.(math flags?)
CONTINUED…
 PSW contains
a. Math flags
b. User program flags F0
c. Register select bits ( selects which of the 4 GPR banks are in use by program)
*GF0 & GF1 are stored in PCON.
PSW REGISTER
Truth table
for register
select bits
INTERNAL MEMORY
 Separate memory for program and code.
 Harvard architecture(same address in different memories).
INTERNAL RAM
 128 byte internal RAM.(starting and ending address ? )
 32 B from address 00H to 1F H that make 32 working registers organized as four register banks (0 to 3).
 Each bank has 8 registers R0 to R7.
 Each register can be addressed by name (how) or by RAM address (address of R0 of bank 3 ? )
 Register banks which are not selected can be used as general purpose RAM.
 RESET selects bank 0.
 A bit addressable area of 16 bytes occupies RAM byte addresses 20 H to 2F H (forming 128
addressable bits).
 A general purpose RAM area above bit area addresses from 30 H to 7F H.
CONTINUED…
Internal RAM
organization
Separate read instructions for external data and code memory.
Internal code
Memory
ROM or EPROM
4k or up
External data memory
RAM
64k
0xFF
SFR(direct access)
128 bytes
0x80
0x7F
General purpose RAM
(variable data)
80 bytes
Bit addressible RAM
16x8 bits
16 bytes
0x30
0x2F
0x20
0x1F
Register bank 0(R0-R7)
Register bank 1(R0-R7)
Register bank 2(R0-R7)
0x00
Register bank 3(R0-R7)
Internal data memory
RAM
4 x 8 =
32 bytes
External code memory
ROM or EPROMext
64k
STACK & STACK POINTER
 Stack refers to an area internal to RAM that is used in conjunction with certain opcodes to store and retrieve
data quickly.
 The 8 bit SP register holds an internal RAM address that is called the “ top of the stack”.
 This is that address location in RAM where the last byte of data was stored by a stack operation. (last in at top)

STACK OPERATION
SPECIAL FUNCTION REGISTERS
 SFR is used for operations where internal 128 B RAM addresses ( 00 H to 7F H) are not used.
 These are addressed from 80 H to FF H*.
 Some SFRs are bit addressable.
SFR WITH ADDRESSES
INTERNAL ROM
 Used for program code.
 It occupies code address space form 0000 H to 0FFF H. (4K ROM)
 PC addresses program code bytes form 0000 H to FFFF H. ( ? )
 EA’ (pin 31)
I/O PINS, PORTS & CIRCUITS
 40 pin DIP.
 24 pins are dual function pins.
 Thus, effective pin configuration becomes 64.
 The function a pin performs at an instant depends on what is physically connected to it and what software commands
are used to program the pin. (programmer & circuit designer)
 Each port has a D-type output latch for each pin.
 The SFR at that port is made up of these eight latches.
 It can be addressed at SFR address at that port.
 Eight latches for port 0 are addressed at 80 H.
 Port 0 Pin 3 is the bit 2 of the Port 0 SFR.
 The port latches should not be confused with port pins.
PORT 0
 Serves as inputs, outputs or when used together as bi-directional low order address and data bus for external
memory.
PORT 1
 No dual functions.
PORT 2
 Maybe used as I/O port.
 Alternatively used to supply higher order address byte in conjunction with Port 0 lower order byte to address
external memory.
PORT 3
 I/O port similar to port 1.
 Under the control of P3 latches or of SFRs the I/O functions can be programmed for alternate use.
EXTERNAL MEMORY
 Memory not limited by 128B RAM or 4K ROM.
 2 separate external memory spaces can be made available by PC & DPTR.
 External control pins can be used to enable external RAM & ROM.
 Capacity of each 64KB.
COUNTERS & TIMERS
 For counting of external events
 For this purpose 2 16-bit up counters named, T0 & T1 are provided (processor relieved)
 Each can count internal clock pulses (timer) and also programmed to count external clock pulses(counter).
 These are divided into two 8-bit registers called called timer low (TL0, TL1) and timer high (TH0, TH1).
 All counter actions are controlled by bit states in the timer mode control register TMOD, the timer/counter
control register TCON and certain program instructions.
 TMOD is solely dedicated to two timers and can be considered to be two duplicate 4-bit registers each of which
controls the action of one of the timers.
 TCON has control bits or flags for the timers in the upper nibble and control bits and flags for the external
interrupts in the lower nibble.
CONTINUED…
SERIAL DATA INPUT/OUTPUT
 Serial data communication--- cost effective way
 8051 uses SBUF register to hold data
 SCON controls data communication
 PCON controls data rate
 RXD (P3.0) & TXD (P3.1) connect to the serial data network.
SBUF
 Physically two registers, one is write only to hold data to be transmitted out via TXD.
 Other is read only and holds received data from external sources via RXD.
 Both use address 99H.
SCON
 SCON consists of SMX bits.
 SMX bits are used to select four programmable modes for serial data communication.
INTERRUPTS
 These are hardware signals, that force the program to call a subroutine.
 8051- 5 interrupts
 Timer flag 0, timer flag 1 & serial port interrupt – generated automatically by internal operations.
 INT0’ & INT1’ are triggered by external signals provided by circuitry connected to their pins.
 The programmer can alter the control bits in IE register, IP register and timer control register TCON
EMBEDDED SYSTEM
What is an Embedded system?
• An embedded system is one that has computer hardware with software
embedded in it as one of its components.
• Embedded system is the system designed for particular or specific task using
dedicated controllers.
• We can say that it is “A combination of computer hardware and software, and
perhaps additional mechanical or other parts, designed to perform a dedicated
function. In some cases, embedded systems are part of a larger system or
product, as is the case of an antilock braking system in a car. ”.
An embedded system is a special-purpose computer
system designed to perform certain dedicated
functions. It is usually embedded as part of a complete
device including hardware and mechanical parts.
An embedded product uses a microprocessor (or microcontroller)
to do one task and one task only
 There is only one application software that is typically burned into
ROM
A PC, in contrast with the embedded system, can be used for any
number of applications
 It has RAM memory and an operating system that loads a variety of
applications into RAM and lets the CPU run them
 A PC contains or is connected to various embedded products
 Each one peripheral has a microcontroller inside it that performs only
one task
© 2019 UPES
Why not use PCs for all embedded computing?
First, real-time performance requirements often drive us to different architectures.
real-time performance is often best achieved by multiprocessors.
Second, low power and low cost also drive us away from PC architectures and
toward multiprocessors.
Challenges in Embedded Computing System Design
1. How much hardware do we need?
(too little hardware and the system fails to meet its deadlines, too much hardware
and it becomes too expensive)
2. How do we meet deadlines?
(speed up the hardware or increasing the CPU clock rate may not make enough
difference to execution time, since the program’s speed may be limited by the
memory system.)
3. How do we minimize power consumption?
(In battery-powered applications, power consumption is extremely important, Even
in non battery applications, excessive power consumption can increase heat
dissipation.)
4. How do we design for upgradability?
5. Does it really work?
© 2014 UPES
Significanceof ES
• Due to their compact size, low cost and simple design aspects
made embedded systems very popular and encroached into
human lives and have become indispensable. They are found
everywhere from kitchen ware to space craft. To emphasize this
idea here are some illustrations.
Embedded systems everywhere?
Embedded systems span all aspects of modern life and there are many examples of their use.
a)
Biomedical Instrumentation – ECG Recorder, Blood cell recorder, patient monitor system
b)
Communication systems – pagers, cellular phones, cable TV terminals, fax and transreceivers,
video games and so on.
c)
Peripheral controllers of a computer – Keyboard controller, DRAM controller, DMA controller,
Printer controller, LAN controller, disk drive controller.
d) Industrial Instrumentation – Process controller, DC motor controller,
robotic systems, CNC machine controller, close loop engine controller,
industrial moisture recorder and controller.
e) Scientific – digital storage system, CRT display controller, spectrum
analyzer.
Were the embedded systems existing earlier ?
• Yes, We have been enjoying the grace of embedded system quite a long time. But they were not so
popular because in those days most of the embedded systems were designed around a microprocessor
unlike today’s systems which were built around a microcontroller.
• As we know a microprocessor by itself do not possess any memory, ports etc. So everything must be
connected externally by using peripherals like 8255, 8257, 8259 etc. So the embedded system designed
using microprocessor was not only complicated in design but also large in size. At the same time the
speed of microprocessor is also a limitation for high end applications.
What is inside an embedded system ?
• Every embedded system consists of custom-built hardware built around a Central Processing Unit
(CPU). This hardware also contains memory chips onto which the software is loaded. The software
residing on the memory chip is also called the ‘firmware’.
• The operating system runs above the hardware, and the application software runs above the
operating system. The same architecture is applicable to any computer including a desktop
computer. However, there are significant differences. It is not compulsory to have an
operating system in every embedded system.
• For small appliances such as remote control units, air- conditioners, toys etc., there is
no need for an operating system and we can write only the software specific to that
application. For applications involving complex processing, it is advisable to have an
operating system.
• In such a case, you need to integrate the application software with the operating system
and then transfer the entire software on to the memory chip. Once the software is
transferred to the memory chip, the software will continue to run for a long time and
you don’t need to reload new software
.
Layered architecture of an Embedded System
Now let us see the details of the various building blocks of the hardware of an embedded
system.
•
•
•
•
•
•
Central Processing Unit (CPU)
Memory (Read only memory and Random access memory)
Input Devices
Output Devices
Communication interfaces
Application specific circuitry
Hardware architecture of an embedded system
Features of an embedded system
Embedded systems do a very specific task, they cannot be programmed to do different things.
• Embedded systems have very limited resources, particularly the memory. Generally, they do not have
secondary storage devices such as the CDROM or the floppy disk.
• Embedded systems have to work against some deadlines. A specific job has to be completed within a
specific time. In some embedded systems, called real-time systems, the deadlines are stringent.
Missing a dead line may cause a catastrophe – loss of life or damage to property.
• Embedded systems are constrained for power, As many embedded systems operate through a battery,
the power consumption has to be very low.
• Embedded systems need to be highly reliable. Once in a while, pressing ALT-CTRL-DEL is OK on your
desktop, but you cannot afford to reset your embedded system.
• Some embedded systems have to operate in extreme environmental conditions such as very high
temperatures and humidity.
• Embedded systems that address the consumer market (for example electronic toys) are very costeffective. Even a reduction of Rs.10 is lot of cost saving, because thousands or millions systems may
be sold.
• Unlike desktop computers in which the hardware platform is dominated by Intel and the operating
system is dominated by Microsoft, there is a wide variety of processors and operating systems for
the embedded systems. So, choosing the right platform is the most complex task .
Classification of Embedded Systems
Based on functionality and performance requirements,
embedded systems are classified as :
•
•
•
•
Stand-alone Embedded Systems
Real-time Embedded Systems
Networked Information Appliances
Mobile Devices
Stand-alone Embedded Systems
As the name implies, stand-alone systems work in stand-alone mode. They
take inputs, process them and produce the desired output. The input can
be electrical signals from transducers or commands from a human being
such as the pressing of a button. The output can be electrical signals to
drive another system, an LED display or LCD display for displaying of
information to the users. Embedded systems used in process control,
automobiles, consumer electronic items etc. fall into this category.
Real-time Systems
• Embedded systems in which some specific work has to be done in a specific time period are
called real-time systems. For example, consider a system that has to open a valve within 30
milliseconds when the humidity crosses a particular threshold. If the valve is not opened within
30 milliseconds, a catastrophe may occur. Such systems with strict deadlines are called hard realtime systems.
• In some embedded systems, deadlines are imposed, but not adhering to them once in a while may
not lead to a catastrophe. For example, consider a DVD player. Suppose, you give a command to
the DVD player from a remote control, and there is a delay of a few milliseconds in executing that
command. But, this delay won’t lead to a serious implication. Such systems are called soft realtime systems .
Hard Real-Time Embedded System
Networked Information Appliances
•Embedded systems that are provided with network interfaces and
accessed by networks such as Local Area Network or the Internet are
called networked information appliances. Such embedded systems are
connected to a network, typically a network running TCP/IP (Transmission
Control Protocol/Internet Protocol) protocol suite, such as the Internet or
a company’s Intranet.
•These systems have emerged in recent years. These systems run the
protocol TCP/IP stack and get connected through PPP or Ethernet to a
network and communicate with other nodes in the network.
Here are some examples of such systems
• A networked process control system consists of a number of embedded systems connected
as a local area network. Each embedded system can send real-time data to a central
location from where the entire process control system can be monitored. The monitoring
can be done using a web browser such as the Internet Explorer.
• A web camera can be connected to the Internet. The web camera can send pictures in
real-time to any computer connected to the Internet. In such a case, the web camera has
to run the HTTP server software in addition to the TCP/IP protocol stack.
• The door lock of your home can be a small embedded system with TCP/IP and HTTP server
software running on it. When your children stand in front of the door lock after they return
from school, the web camera in the door-lock will send an alert to your desktop over the
Internet and then you can open the door-lock through a click of the mouse.
This image shows a weather monitoring system connected to the Internet. TCP/IP protocol suite and HTTP
web server software will be running on this system. Any computer connected to the Internet can access
this system to obtain real-time weather information.
The networked information appliances need to run the complete TCP/IP protocol stack including the
application layer protocols. If the appliance has to provide information over the Internet, HTTP web
server software also needs to run on the system.
Mobile Devices
Mobile devices such as mobile phones, Personal Digital Assistants (PDAs), smart phones etc. are a
special category of embedded systems. Though the PDAs do many general purpose tasks, they need to
be designed just like the ‘conventional’ embedded systems.
The limitations of the mobile devices – memory constraints, small size, lack of good user interfaces such
as full fledged keyboard and display etc. are same as those found in the embedded systems discussed
above. Hence, mobile devices are considered as embedded systems.
However, the PDAs are now capable of supporting general purpose application software such as
word processors, games, etc.
Communication Interfaces
For embedded systems to interact with the external world, a number of communication interfaces are
available. They are
•
Serial Communication Interfaces (SCI): RS-232, RS-422, RS-485 etc
•
Synchronous Serial Communication Interface: I2C, JTAG, SPI, SSC and ESSI
• Universal Serial Bus (USB)
• Networks:
Ethernet, Controller Area Network, LonWorks, etc
• Timers:
PLL(s), Capture/Compare and Time Processing Units
• Discrete IO:
General Purpose Input/Output (GPIO)
• Analog to Digital/Digital to Analog (ADC/DAC)
DESIGN METRICS OF EMBEDDED SYSTEMS
 A Design Metric is a measurable feature of the system’s performance, cost, time for implementation
and safety etc.
 Most of these are conflicting requirements i.e. optimizing one shall not optimize the other.
 A cheaper processor may have a lousy performance as far as speed and throughput is concerned.
1. NRE cost (nonrecurring engineering cost)
It is one-time cost of designing the system. Once the system is designed, any number of units can
be manufactured without incurring any additional design cost; hence the term nonrecurring.
2. Unit cost
The monetary cost of manufacturing each copy of the system, excluding NRE cost.
3. Size
The physical space required by the system, often measured in bytes for software, and gates or
transistors for hardware.
27
© 2014 UPES
CONTD..
4. Performance
The execution time of the system
5. Power Consumption
It is the amount of power consumed by the system, which may determine the lifetime of a
battery, or the cooling requirements of the IC, since more power means more heat.
6. Flexibility
The ability to change the functionality of the system without incurring heavy NRE cost. Software
is typically considered very flexible.
7. Time-to-prototype
The time needed to build a working version of the system, which may be bigger or more
expensive than the final system implementation, but it can be used to verify the system’s
usefulness and correctness and to refine the system’s functionality.
28
© 2014 UPES
CONTD..
 8. Time-to-market
The time required to develop a system to the point that it can be released and sold to
customers. The main contributors are design time, manufacturing time, and testing time. This
metric has become especially demanding in recent years. Introducing an embedded system to
the marketplace early can make a big difference in the system’s profitability.
9. Maintainability
It is the ability to modify the system after its initial release, especially by designers who did not
originally design the system.
10. Correctness
This is the measure of the confidence that we have implemented the system’s functionality
correctly. We can check the functionality throughout the process of designing the system, and
we can insert test circuitry to check that manufacturing was correct.
29
© 2014 UPES
ES LIFE CYCLE:
1.Need/opportunity
2.Concept development
3.Manufacturing process design
4.Production
5.Deployment
6.Support/maintenance
7.Upgrades
8.Retirement/Disposals
© 2014 UPES
ES DESIGN PROCESS
© 2014 UPES
Software Design Cycle
32
© 2014 UPES
Hardware/Software Codesign
A definition:
Meeting System level objectives by exploiting
the synergism of hardware and software
through their concurrent design
33
Why codesign?
• Reduce time to market
• Achieve better design
• Explore alternative designs
• Good design can be found by balancing the HW/SW
• To meet strict design constraint
• power, size, timing, and performance trade-offs
• safety and reliability
• system on chip
34
Concurrent design
Traditional design flow
Concurrent (codesign) flow
start
start
HW
SW
HW
Designed by independent
groups of experts
SW
Designed by Same group of
experts with cooperation
35
Typical codesign process
System
Description
Modeling
HW/SW
Partitioning
Software
synthesis
Interface
synthesis
System
integration
Unified representation
Hardware
synthesis
Instruction set level
HW/SW evaluation
36
HW/SW Co-design: Main Advantages
• To explore different design alternatives.
• To evaluate cost-performance trade-offs
• To reduce system design time
⇒ Reduction of product time-to-market and cost
• To improve product quality through design process optimization
• To support system-level specifications
⇒ To facilitate the reuse of hardware and software parts.
• To provide an integrated framework for the synthesis and
validation of hardware and software components.
Download