Académique Documents
Professionnel Documents
Culture Documents
Chapter No. 1 2
')a
No. of Hours 4 8
2 6 4 16 12 12 64
3
5
">64
"1<7 8
\.
"'
"
CONTENTS
CHAPTER -1 INTRODUCTION TO MICROCONTROLLERS Block diagram of microcomputer, microprocessor and microcontroller Microcontroller types - embedded, external memory, Harvard and Princeton RISC and CISC architectures. Microcontroller memory types Evolution of microcontrollers - 4-bit, 8-bit, 16-bitand 32-bit
1-15
CHAPTER - 2 ARCHITECTURE
OF MCS 8051
16 34
Introduction to MCS 8051 family Pin diagram of 8051 with functions Block diagram of 8051 with details of GPR, PC, data pointer, flags, PSW, SP, SFR, I/O ports, counter /timer, serial I/O, data memory, program memory, register banks and stack
35 44
-f-
CHAPTER
-4 INTRODUCTION
TO PIC CONTROLLERS
45-53
Introduction to 16F74
-5 PROGRAM
DEVELOPMENT TOOLS
54 62
--.
..... ~
Definitions of instructions, program and software Machine instruction format, addressing modes and types Tools - Assembler, linker, loader and compiler Flowchart and algorithm Assembly instruction format 8051 data typ~s and directives
iiii8iic~ -
CHAPTER
-6 INSTRUCTION
.
SET OF 8051
63 82
,
Instruction classification
groups
CHAPTER
-7 HARDWARE
83 -110
I/O port programming I/O bit manipulation Bit addressable RAM Single bit operation with carry Counter/timer programming in the 8051 Interrupts programming Programming the serial communication interrupt Interfacing external memory
CHAPTER
-8 APPLICATIONS
OF 8051
111-126
Programmable peripheral interface (8255A) 8051 interfacing Switch / LED / relay / buzzer / optocoupler interface Analog to digital converter (ADC) interface Stepper motor interface Digital-to-analog converter (DAC) interface LCD (liquid crystal display) interface Hex keypad interface APPENDIX A SAMPLE PROGRAMS MULTIPE CHOICE QUESTIONS MODEL QUESTION PAPERS
'"
INTRODUCTION TO MICROCONTROLLERS
,
Control bus
MAR
[ [ (
PC
1Ro
R1
Control
I
IR
. .
.....J
Flag Register
SP
I
Rn
ALU
Interrupt circuits
Introduction to Microcontrollers
stack only. This end is usually called the top of the stack. The stack pointer is a register used as a memory pointer. It contains the address of the current top element of the stack.
Memory Address Register (MAR) The MAR is used to hold the address of the locationto or from which data are to be transferred. Interrupt Circuits Normal execution of the program may sometimes be disturbed if some device requires urgent servicing.To achieve this, the device can raise an interruptsignal. An inter' u t from the input/out utdevicefor se,ryJce ...!b~J2r"~;tc;~ssQr. b Interrupt circuits han e sue requ~sts. The proces$or
provides the requested service by executing an aE!E!iate interrupt service routine (ISR).
~,
-~
.
";:;:::'
The MDR contains the data to be written into or read out of the addressed location.
... .
~ -~.
--- -
Bus Structure
The universal feature in all microprocessors is bus structure. The microprocessor has an internal bus. All the hardware functional units inside a microprocessor are connected to the internal bus . consisting of (a) Address bus (b) Data bus (c) Control bus Addrcss Bus The address bus consists of 20, 24 or 32 parallel signal lines. On these lines the CPU sends
,\".
u.oidirec1l9n;;T-.
Data Bus
the address of the-.!!I~mory 109atioo that is to be written or to be read from. Address bus!s
..
- -"
,.
-~
The data bus consists of 1J.IJ6.!..~~ or ?411.rall1 lines.The data bus lines are bidirectional. signal This means that th.e.~~ r~pjs_!~tlJ'!.9Q~!~f?S line~JLoT merno~ or from a p~rt as ~Ssends" data out on these lines to a memory location or to a port. "" ,~ -- - -ControlBus
.
-- --
TioWrite. - - - -~' -
The CPU sends out.signals_Qnthe cont(ol busto enabL~.~h~ <2utput~ otaddressd memory devicesor port devices. Typical control b~aJ 9r~ Memory Read, Memory Write, I/O Read and
"
-.
---
--
4
Data bus I
"-"
Microcontrollers
and Applications
Mem 0 ry unit
CPU 1- GeneralPurpose
MicroI
L v
---
- - -I 1- - l---I
I
- - -- l-cu '- Q)
J:::cu
.;:: Q)
Q)e CL.Q) (J
I I
I
II I
I I
I
r
.
Input / Output
I
I I
RAM
ROM
I I
I/O
Port
0.. 1::
Serial COM
I
I I
processor I I
Port
L_-
- - - - r--_J
L-
--
- - - -=- - -I - - . -
Address bus
Microprocessor is also called central processing unit (CPU) because it can be designed to do the functions as the central processing unit in a large computer. The Central processing unit (CPU) does arithmetic operations like addition, subtraction, multiplication, division, comparison and logic operations such as AND, OR and XOR. It also regulates the operation of the complete machine. It fetches and interprets and it causes certain parts of the circuitry to respond according to those instructions. The CPU can be considered as the nerve element of the computer's brain. M'rJ1ory Unit The memory unit stores programs, data, calculations and results. Two types of memory are included in the computer: (a) Temporary memory called random-access memory (RAM) (b) Permanent memory called read-only memory (ROM). RAM is sometimes called "main memory". Information stored in RAM exists only as long as power is applied to the computer. When power is removed, the programs and data stored in the RAM are lost unless copied to external permanent memory. The ROM chips are permanently programmed with computer instructions and special data. They are referred to as "firmware" because software is embedded into the hardware. External memory can be attached to the computer by connecting floppy disk drives or hard disk drives.
1"1. I I
I 'I
Introduction to Microcontrollers
Some devices are used for both input and output. These include the mass storage devices (the floppy disk drives, the hard disk drives, and archival storage tape systems) and MOdulator DEModulators (MODEMs) which enable computer to computer communication over long distances.
.
Power
,.
Power
distribution
Control store
Reset
Reset
control
...
C/) Q,)
0 C/)
()
t 0 0..
C/)
/' .
'.......
./
0 --
Clocking
......
RAM
Microcontrollers
and Applications
MICROCONTRO LLERTYPES
EMBEDDED MICROCONTROLLERS
When all the hardwar~ required to run the appHc,C!!iQD~ provided on the mlcrocontroller chip, it is referred to as an ~~rT1beddedmlcrocOntroller. Fig. 1.3 shows the block diagram of an embedded microcontroller. Power, reset and clock are required to operate the device. I/O pins are provided to allow the interfacing with external devices. .-
i
I J
I
I
I I
I
I
I
-.
Merits
.:. Embedded microcontrollers are cheaper .:. They are much more precise and easier to control
II. I
"-"I
::31
Introduction to Microcontrollers
Addressbus
... ,
.
Micro co ntro lier
..., ...
Control bus
... ,...
RAM/ROM
Data bus
,...
Fig. 1.4: Eternal memory microcontro/lers External memory microcontrollers allow the connection of external memory as shown in Fig. 1.4. The design emphasis is on high-speed computation features r~ther than on-chip features, such as RAM and ROM., The difference between microcomputers and microcontrollers becomes very fine here. The e~ternal memory microcontrol~e.rqjffers trom a miGroproce~~9r.in ~hearea of builtin periphei"al features such as timers, interrupt contl'ollers, QMA and I/O devices~The _E!~ernalmemory DUS -can be used to ~_d9r~s meJ!1.o!YQ1appeg 110 devices ~xe~nd thELcapil"Qilities of the microcontroller significantly. When external memory is used with tti~ micJ.Q~ntr()!ler, all the built-in nanrware Tnterfates can also beacceSSed. This featuremakes the microcontroller flexibletor different
applications.
MICROCONTROLLER
PRINCETON (VON
-ARCHITECTURES
ARCHITECTURE
NEUMANN)
Fig. 1.5 shows the block diagram of Princeton architecture. It has COlDDlO..!l.l1lernory storing for instructi?n as well as data. ThE!memory interfac.e unit is responsible for deciding wt}~ther.The..!!l~rnory 'access -isfor instruction fetch or data transfer. The processing speed can beTncreased by using the cQ!Jfept of prefetching:"Wlffi1hiscO'ncept, the time required to execute an Tnstruction can be utilized ., -" ~-.to fetch the next instruction. - -
--- - -
!'9 ro -0
"0 >-
CPU
~ if
8 Merits
Microcontrollers
and Applications
.:. Princeton architecture simplifies the microcontroller chip design because only one memory is
accessed.
.:. Random access memory (RAM) can be used for both instruction and data storage.
.:. Availability of program counter stack contents allows greater flexibil~ty in developing software.
HARDVARD ARCHITECTURE
Fig. 1.6 shows the block diagram of Harvard architecture.J1.Y.J~.~_~eparate emory banks ,for m storing instruction and data. Harvard architecture executes instructions in fewer irlstruction cycles than the Princeton architecture because of instruction parallelism. Instruction parallelism can be defined as a technique of simultaneous issue and processing of multiple instructions within a single processor(CPU).
;-_.
""
...
>o
<oE Ow E
CPU
>-
Merits .:. Harvard architecture executes instructions in fewer instruction cycles than the Princeton architecture because of instruction parallelism .:. Harvardarchitecture is suitable for real-timeapplications
~
CISC stands for Complex Instruction Set Computer. CISC has large instruction sets, multiple addressing modes and multiple instruction formats and sizes. Their control is microprogrammed and different instructions take different number of cycles to execute. The control units are complex. RISC stands for Reduced Instruction Set Computer. RISC has relatively less instructions, addressing modes and instruction formats. As a result, a relatively small and simple decoding and executing hardware subsystem of the CPU is required. The chip area dedicated to the control unit is less. There is more area available for other features. In RISC systems, stroamlined operation is accomplished by standard, fixed size of the instruction (equal to the word length and to the width of the data bus) and single-cycle execution of all instructions.
-I.
I ....... "',
Introduction
to Microcontrollers
!
I
CISC CISC stands for Complex Instruction Set ~puter RISC has relatively more instructions, addressing modes and instruction formats. As a result, a relatively large and complex decoding and executing hardware subsystem of the CPU is required. The chip area,dedicated to the control unit is more. There is less area available for other features. ---features. lmpler ang sm'aUer~controlunit results in More complex and larger control unit results re9\;!~d number of design errors and higher in increased number of design errors and reliability. reduced reliability. RISC design approach is ..2.ujt,iJ?I~ for CISC design approach is not suitable for efficient handling of pipelines efficient handling~elines Throughput is less. Througb.putis more. It takes a shorter time to complete the It takes a longer time to complete the design design of a RiSe control unit. Due to shortE2.r of a CISC control unit. Due to longer design design time, the chances of the end product time, the chances of the end product becoming obsolete are 1El.S.becoming obsolete are more. Overall design cost of RISC control unit is Overall design cost of CISC control unit is more less~ .. Because of the simplicity, I~s number of Because of the complexity, more number of instruction formats and standard instruction instruction formats and different instruction lengths, design of -virtual memory lengths, design of virtual memory management subsystem is difficult. management subsystem is easier. Since the total number of instructions in a Since the total number of instructions in a R~SC system is !pa,lI, con;:!2ilerdesign is CISC system is larg~, Gompilerdesign is more sents an simple.r. RISC instruction - set presents a complex. CISC instruction set pre r~ burden on the compiler increased burden on the compiler. The availability of a relatively lQr:.9.e number CISC system d,gesnqt .provide as m any CPU of CPU registers in a RISC permits a more registers as in RISC. Therefore, it does not efficient code optimization stage in a permit efficient code optimization stage in a compiler. . compiler. RISC compiler is :?imRIEtr.CISC compiler is complex
Table. 1.2: RiSe versus CISC
RISC RISC stands for Reduced Instruction Set ~uter RISC has relatively less instructions, addressing modes and inStruction formats. As a result, a relatively small cmd~simPle decoding and execu~ hardware subsystem of the CPU is required. The chip area dedicated to the control unit"is less. There is more area available for other
TYPES OF CONTROL
STORAGE
The following different types of control store may be available in a microcontroller: Mask ROM (MROM) The mask ROM is the type in which data are permanently stored in the memory during the manufacturing process. Programmable ROM (PROM) The PROM is the type in which the data are electrically stored by the user with the aid of specialized equipment. Erasable PROM (EPROM) The EPROM is a MOS (metal oxide semiconductor) device. The EPROM is electrically programmable by the user, but the stored gcya9an be e(ased either by expo~ure to UV (Ultraviolet) light or by electrical means. The latter type is called an EEPROM (Electrically Erasable PROM) or EAPROM (Electrically Alterable PROM). Flash Memory
.
Flash memory is a QQ!J:Y..oJatilememorv.haLq,an be electricallyerased and reprogrammed.It is a specific type of ~EPROM.that is erased and programmedin large blocks. Flash memory costs far
less than byte-programmable VARIABLE EEPROM. DATA STORAGE
Yolatile memory rEttCiins storeq_i~formation C!.s~loQ~s electrical power supply is on. If the
electrical power is removed, all information stored in the memory wiWb~ lost. Many semiconductor
memories are volatile. Volatile memory is used for temporary storage. Volatile memory is referred to .. -
as random access memory (RAM) and can be read from and written to by the microcontroller(s processor.
I - -. -- -- - -- ..
- -
--
.-
ns
Introduction to Microcontrollers
11
(1) ~its: Single flip-flops ~re called bits. Bit manipulation is efficient and faster. (2) Registers: a-bit (byte) locations accessed by the processor are called registers. The.~!equire .. -
special instructions or addressing modes. (3) Variable RAM: Temporary storage space used for saving and accessing data in a random fashion. It is also known as scratchpad RAM. "-.
<
---
.
,
j f
(4) ~rogram counter stack: A stack, also knowJJ...aslasJin first out (LIFO), can be thought of as a
s.etof registers, one on top of another. Each register can store one value. A stack is a data .-structurewith two principal operations, ~USH and'pOP.A PUSH operation pla~e7anew val~e on top of the stack. The prior top level becomes the seconq level. A.ppP operatiQndoes the -opposite.It readsthe value from the top level, and then removes it so that the prior second level oecomes the new top level. Stack is used to store addresses and data while a subprogram . -...
--
EfXecut~s.
All variable storage is implemented as static random access memory (SRAM). The storage elements used in the static memory are latches, so data can be stored for an indefinite period of time as long as the power is on. Memory refreshing is not necessary.
MICROCONTROLLER VO SPACE
Input-output (I/O) devices are the means through which the microcontroller communicates with the outside world. Because more than one device is usually connected to a computer, some means have to be provided by which a particular device can be selected to participate in a given operation. There are two schemes for addressing I/O devices: (1) Memory mapped I/O scheme (2) 1/0 mapped I/O (Isolated I/O) scheme
:f
12
Microcontrollers and Applications
EVOLUTION OF MICROCONTROLLERS
Microcontroller models vary in data size from 4 to 32 bits. Four-bit units are produced in huge volumes for very simple applications and a-bit units are the most versatile. Sixteen- and 32-bit units are used in high-speed control and signal processing applications. Many .models feature programmable pins that allow external memory to be added with the loss of I/O capability.
4-BIT MICROCONTROLLERS
In four-bit microcontrollers, the word-bit count is 4. Pin count and package size are reduced to minimize the cost. It still allows the implementation of useful intelligence. 4-bit microcontrollers are the most popular today. 4-bit microcontrollers are intended for use in large volumes as true 1-chip computers. Applications .) Appliances .:. Toys
a-BIT MICROCONTROLLERS
Eight-bit microcontrollers are suitable for ~<l!Lcomputjng tasks and control and monitoring _9P..EI!9~i9ns.ASCII data is stored in byte sizes. Therefore, a-bit word size is the natural choice for data communications. Most integrated circuit memories and many logic functions are arranged in an a-bit configuration that interfaces easily to data buses of a bits. Applications of eight-bit microcontrollers can range from simple appliance control to high-speed machine control and data collection. Different microcontroller manufacturers offer different amounts of internal ROM-and RAM. Memory can be expanded to include off-chip ROM and RAM. In some cases the ROM is an Electrically Reprogrammable Read Only Memory (EPROM). The EPROM . vers~ons can be us~d by the designer in the research and development stage of a product. After the design is finalized, ROM version can be ordered in large quantities. EPROM design is suitable in case the configuration is changed frequently or production volumes are less. Some microcontrollers use fewer external pins to reduce the package size and the cost. Special features' such as analogto-digital (AID) and digital-to-analog (D/A) converters may be included on the chip.The pulse width modulation (PWM) output is useful for controlling motor speed; it can be done using software in the a-bit units.
Applications .:. Simple appliance control .:. High-speed machine control .:. Data collection
16-BIT MICRO CONTROLLERS """"i. ... .... Sixteen-bit microcontrollers are suitable for high-speed control such as control of servomechanisms or for digital sign~1processing (DSP) applications. The 16-bit controllers have beendesignedto take advantageof high-levelprogramminglanguagesin sophisticatedapplications.
- - - --
. _..
"
Introduction to Microcontrollers
13
.:. Digital signal processing (DSP) 32-BIT MICROCONTROLLERS Thirty two-bit microcontrollers are suitable for environments in which application programs run under an operating system. The design emphasis is on high-speed computation features rather than on-chip features, such as RAM, ROM, timers and serial ports. The line between microcomputers and microcontrollers becomes very fine here. The 32-bit controllers have been designed to take advantage of high-level programming languages in sophisticated applications. These microcontrollers are called as "embedded controllers" by some manufacturers. Functions needed for I/O, data communications and timing and counting are done by adding supporting chips. Applications .:. Robotics .:. Highly intelligent instrumentation .:. Avionics .:. .:. .:. Image processing Telecommunications Automobiles
t
14
Microcontrollers
and Applications
IMPORTANT POINTS
(... The microprocessor is a general-purpose single-chip central processing unit (CPU) of a digital computer. (... The microcontroller is a true computer on a chip. It has features such as ROM, RAM, parallel 1/ 0, serial I/O, counters and a clock circuit in addition to those found in a microprocessor. (... When all the hardware required to run the application is provided on the microcontroller chip, it
I'" Princeton (Von Neumann) architecture has common memory for storing instruction as well as
i
(... Harvardarchitecture uses separate memory banks for storing instructionand data. It executes /
r... CISC stands for Complex Instruction Set Computer. CISC has large instruction sets, multiple
I'" RISC stands for Reduced Instruction Set Computer. RISC has relatively less instructions, addressin,g modes and instruction formats. (... Control storage is also known as program memory and firmware. It is non-volatile. r... Volatile memory is referred to as random access memory (RAM) and it is used for variable storage. (.- In memory mapped I/O scheme there is only one address space. I/O devices are identified by assigning them unique addresses within the memory address space of the computer. (... In I/O mapped I/O (isolated I/O) scheme, there are two address spaces: (a) memory address space and (b) I/O address space. In this scheme the addresses assigned to memory locations can also be assigned to I/O devices.
REVIEW QUESTIONS
Objective Type 1. The is a true computer on a chip. 2. architecture has common memory for storing instruction as well as data. 3. architecture uses separate memory banks for storing instruction and data. 4. microcontroller consists of all the hardware required to run the application. 5. In . I/O scheme, there is only one address space. 6. In I/O scheme, the addresses assigned to memory locations can also be assigned
to I/O devices.
.....
Introduction
to Microcontrollers
15
Answers 1. microcontroller 4. Embedded Descriptive Type 1. Explain why EPROM versions of microcontrollers exist. 2. 3. 4. 5. 6. (2)
2. Princeton (Von Neumann) 5. memory mapped
List four major differences between a micropr?cessor and a microcontroller. (4) List four major differences between RISC and CISCo (4) List some applications of microcontrollers. (4) Write a short note on microcontroller memory types. (5) With the help of block diagrams distinguish between Princeton (Von Neumann) and Harvard architecture. (6)
,,'
r-
..,
=:1
17
respectively,
of 8051
Internal Memory
The internal memory stores programs, data, calculations and results. The 8051 has two typesof internal memory: (a) Random access memory (RAM) or data memory and (b) Read only memory(ROM) or program memory.The 8051 has,. separate address spaces for data and program memory. RandomAccess Memory(RAM) The intemal RAM is of 128 bytes rangingfrom OOH 7FH. It is also refenedto as data memory. to ReadOnlyMemory(ROM) The intemal ROM is of 4KB ranging from OOOOH OFFFH.It is also referred to as program to memory. ,.
,.
~
r' .J
I
I
I
I
External Interrupts
...... ())
L.. Q,)(J)
..... c ~ ~c. 0 c
Interrupt Control
Four 110Ports
Osc
Bus control
Serial Port
f TXD RXD
I.
I-
PO P2 ~ Address I Data
P1
P3
Architecture
of MCS 8051
19
Vs
VSS1
-G
-:" I
1 I I I I
I
I I i I I I I I I 1 I I
..
BUFFER
SFRs TIMERS
PC
NCREMENTE
L-
TIMING AND
CONTROL
::::I <5
!;; ~
t!;;
~
:z go::
DATA:S
MULTIPLE
osc
XTAL2
1 XTAL1
------
---------P1.0-P1.7
----------P3.0 - P3.7
I I I I I I I I I I I I I I I I I I I I I I 61 I I I I I I I I I I I I I I I
1
Special Function Registers
The 8051
has several special function registers (not shown in the block diagram) designated as
A, 8, DPTR, PSW, IP, IE, TCON, SCON, PCON, Po' P1, P2, P3' S~UF etc. All these are..!?~. address~le and some of them.are bitaddressablealso.The SFRscan be accessed by their names Q'rbytheir addresses ranging from 801i to FFH. These are useful in accessing I/O ports, timers/ counters., erial port, power,controlling etc.~Some the addressesfrom 80H to FFH are notdefined s of 0' ,., for SFRs. Attempting to use such addresses may cause unpredictable results. . . --- -;- ~ ~ 1
;; W" r
'
I..,
f
20
Microcontrollers and Applications
instructions. ~-'
e::-
- -..
-- -
t""
- -~
Serial Port
The 8051 communicates ~ith other devicE?s by sending anq receiving da_ta 121ts seri2-lIy using ac ~e:~aljata goQll'JJunicatio"- ~~~u~. R..=gister SBU Fis ~<? tlQld dat~. Register ~ON controls data comr!J.Y,!1LGatipn. Register PCON controls data rates. Pins RXD and TXD connect to the serial data
network.
~-'
Interrupt
-.
,l
. -
...
~~
-- -
--.....
Control
Interrupt is a hardware signal activated by an I/O device to alert the CPU. Interrupts may be generatedcaus~~~~~ Request ~y s e
.
oe.eraj!Q.n~Q.rprovid~d by the main programThe start of an Int~rrupt PU to suspend the execution ofelitern~1 source~.;.. andarrivalthe e,xecutlonof
the Interrup.t Service Routine (ISRtpresent in program Q1emoJY. After responding to the interrupt, the CPU-returns back to the mmn program which it was executing before the interrupt occurred. Program resumption is done with the help of stac~. The PC (prograr:n counter) address is sayg&tQn ~..$tack ~fore chanQing it to !h_einterrupt ~ddre~.s in RQM. The PC address wm b. r~stored from the stack ~Lan~1 il1struction is executed ~t theD9 of the IS~. Most applications of micr~contro.Uers involve real-time interrupts which require quicker respon~e.\\ -, . Interrupts can be e~le~_or..gisabled unger program control by altering control bits in the inteJIupt enable register (IE). interrupt Briority re.,glster(IP) and timer control register (TCON). Five interrupts are provided in the 8051. The interrupts are of two types: (1) Internal interrupts and (2) External interrupts. ,
~
\ 1
\ I I I
, I
I~ 1\
I
I
Internal Interrupts The followingthree interrupts are generated by internaloperations: (a) Timerflag 0 (TFO) (b) Timerflag 1 (TF1) (c) Serialport interrupt (RI orTI) When a timer/counteroverflo~s, the correspondingtimerflag, TFOorTFl, is sett01.Theflag is clearedto 0 wnen Theresu1tinginterrupfgeneratesa progra'm'camofneappropriate timer subroutine in"memory.If a data byte isreceiv-ea, interrupt bit RI is set to 1 i,nthe qCON register.When a data oyte has been transmitted. interrupt bifTI is set to 1 in SCaN. These are ORed together to provide a single interrupt to the process'or:the serial port interrupt. ,..
- -
.--
ii
21
External Interrupts The following two interrupts are triggered by external signals: (a) INTO (Port.pin P3.2) (b) INT1 (Port pin P3.3) Reset Whenevera high level isappJiedtothe RSTpin, the 8051 enters a resetcondition. ~e!.c"an
~ec~ns12~~~d_!9 _~e,a ~on:mas!<~b~einterru.pt:. A reset is an ~~sol!:ltEZJ;OJJl.lrt~nq t9jur1]~~99Lam adttress O_OO.Q~~nd commence execuung from there. Program counter (PC) is not stored fo"f later programresumption. -"
110 Ports
special circuits like latch, buffer ana driver are Included in these ports. --
Ports represent physical c5>nnecti?n CPLl ~t~_ou!sLd~e_wQIlci of Microcontroller u_s~s them in order to m_Ql)itor control other compon~l'1tsor devices...Physically, port fsa register inside a .or mrer6COiitrolier ~igh js~onngcJ.ea by-~iresto the pins of a 'microcon~ue to tu.n'ctionaTity,' P somepins have multiple roles.There are fourllO gort)(PO"- 3) of 8 lines each. Some of these portscan ba,configurecr.asirf~ or output. In-"" -., _. ~ can performcertain specific functions. addition, po1T -~_.~
--
..
Port 3: P3.0 - P3.7 (pins 10 -17) Port 3 is a bidirectional I/O port consisting of 8 lines. In addition, port 3 provides special functions as listed below:
(I >
'
",J ""
,
22
Microcontrollers-and Applicati<:>ns \J
Port 1 Bit 0 I 1 Port 1 Bit 1 I 2 Port 1 Bit 2 Port 1 Bit 3 Port 1 Bit 4 Port 1 Bit 5 Port 1 Bit 6 Port 1 Bit 7 Reset Input Port 3 Bit 0 (Receive Data) Port 3 Bit 1
(XMIT Data)
Vee 40
+5V
(ADO)PO.O 39
(ADl)PO.l
I Port0 Bit 0
I3
I
4
38
(Address/Data 0)
(AD2)PO.2 37 (AD3)PO.3 36
I5
I6
I
7
(AD5)PO.5 34
(Address/Data 5)
Port0 Bit5
I8
(AD6)PO.633 (AD7)PO.732
(Vpp)/EA 31 (PROG)AlE 30 PSEN 29
I Port0 Bit 6
I
(Address/Data 6)
I9
Port0 Bit7
(Address/Data 7) External Enable (EPROMProgramming Voltage) Address latch Enable (EPROMProgramPulse) ProgramStore Enable
281 27
15 P3.5(Tl)
(A13)P2.5 26
(A 12)P2.4
Port 2 Bit 5 (Address 13) Port2 Bit 4 (Address 12) Port2 Bit 3 (Address 11)
16 P3.6(WR) 17 P3.7(RO)
18 XTAL2
25
(A11)P2.3 ,24
(A10)P2.2 23
19 XTAL1
(A9)P2.1 22
Port2Bit 1
(Address 9)
I
,<-... I "
I
Ground
I 20
Vss
(A1P2.0
Note: Alternate functions are shown be'low the port name (in parentheses). Pin numbers and pin names are shown inside the DIP package. Fig. 2.3: Pin diagram of 8051
..
..
23
P3.0 (RXD): Serial input port P3.1 (TXD): Serial output port P3.2 (INTO )': External interrupt with vector 0003H P3.3 (INT1 ): External interrupt with vector 0013H P3.4 (TO): Timer 0 external input P3.5 (T1): Timer 1 external input P3.6 (WR): External data memory write strobe P3.? (RD ): External data memory read strobe
Ground:
- 28)
Port 2 is a bidirectional I/O port consisting of 8 lines. In addition, port 2 provides high order addressbyte (A8 - A15) through which the 8051 can access 64KB of external memory.
This selection is made by strapping the EA pin to either VCC or VSS. If the EA pin is strapped to V'cC,the 8051can access4KB of internalROM (OOOOH- OFFFH) and externalROMof 60KB (1GOGH FFFFH). If the EA pin is strapped to VSS, then all program fetches are directed to external ROM (OOOOH FFFFH).This pin also receivesthe 12.75Vprogrammingsupply voltage (VPP)during EPROM programming.
'l1l
24
Microcontrollers
and Applications
Port 0 I Address Data Bus: P 0.7 - P 0.0 I AD7 - ADO (pins 32 - 39)
Port 0 is a bidirectional I/O port consisting of 8 lines. Port 0' is also the mujtiplexedJow-order
~dress and data bus AD7 - ADO dlJring ~sses.. t9 extemalprogram and data rl}e!!1<?'l"ALE signal Indicates whether address or data is present on these lines. When ALE = 1, address A7 - AO is present. Later when ALE =0, data 07 - DO is transferred on these lines.
8051MEMORYORGANIZATION
The 8051 requires memory for program and data. ROM is used for program code and RAM is used for data storage. The 8051 can access 64KB each of ROM and RAM. The 8051 has separate address spaces for program and data memory. The total address space of 64KBis arranged as two parts: (1) Internal (on-chip) ROM/RAM and (2) External ROM/RAM. PROGRAM MEMORY
The Program memory can be up to 64KB long. The lower 4KB can reside on-chip. If the EA pin
is strapped to VCC, the 8051 can access 4KB of internal ROM (OOOOH-OFFFH) and external ROM
of 60KB (1 GOGH - FFFFH) as shown in Fig. 2.4 (a). If the EA pin is strapped to VSS, then all program
(OOOOH
- FFFFH)
external ROM, PSEN , is used for all external program fetches. PSEN is not activated for internal program fetches. FFFF FFFF
-- --
--
25 FFFF
INTERNAL
FFI
I I
SFRs DIRECT ADDRESSING ONLY AND DIRECT AND INDIRECT ADDRESSING 0000
80 7F
00
--;;
reg'ister bank1 .
-. -'
--
- ----.
...,-.
--
--
...
'"
-'
:I
26
Microcontrollers and Applications A
7f
30 2F 20 IF 18 11 10 Of
08 07 06 05 04 03 02 01 00 I Byte Addresses
7F 00
Register Bank 0
RO
fntff'nal RAM
I~
"""
.--
SCRATCH PAD AREA 30Hthrough 7FH are availableto the use.L.a~..9atC!J18M. However,if the s~ck pointer has been initializedto this area, enough bytes- should be left aside to prevent SP data destruction. ~ -~~. c- - - .-~ ..._= , REGISTER STRUCTURE OF THE 8051
Reg1sters are used to store opcodes, operands and addresses of memory or I/O devices temporarily. The register structure of the 8051 can be treated as a coJJectionof 8- and 16-bit registers and 8-bit memory locations. These registers and memory locations can be made to operate using the software instructions that are incorporated as part of the design. The registers in the 8051 can.be
.' .
classifiedas follows:
(1) Byte addressable registers (2) Byte and bit addressable registers (3) General purpose registers (4) Special function registers (5) 8-bit registers (6) 16-bit registers (7) RAM independent registers (8) RAM dependent registers (part of RAM) GENERAL PURPOSE REGISTERS
As discussed earlier, the 8051 has an internal RAM of 128 bytes, out of which 32 bytes are kept
aside as general purpose registers (GPRs) ranging from 0 to 1FH (32 bytes). GPRs are organized as
four banks of 8 registers (RO - R7) each. That is, each register bank contains ~ight 1-byte registers ROthrough R7. GPRs are addressed as RO.. ..R7 orOOH 1FH.ln order to access them as RO R7, a particular bank should be chosen in software using 04 and 03 bits of Program Status Word (PSW). The device defaults to register bank 0 after reset.
8051 SPECIAL
FUNCTION
REGISTERS
The 8051 has several special function registers (SFRs) designated as ACC, B, OPTR, PSW, IP, IE, TCON, SCON, PCON, Po' Pl' P2, P3, SBUF etc. These are useful in accessing I/O ports, timers/ counters, serial port, power controlling etc. SFRs can only be accessed by direct addressing. Sixteen addresses in SFR space are both byte- and bit-addressable. The bit-addressable SFRs are those whose address ends in OH or 8H. The SFRs can be accessed by their names or by their addresses ranging from 80H to FFH. Some of the addresses from 80H to FFH are not defined. Attempting to use such addresses may cause unpredictable results. Fig. 2.7 gives a brief look at the special function register (SFR) space. Special function registers can be further grouped as follows: (1) . Math registers (A and B) (2) Interrupt registers (IP and IE) (3) Timer/counter registers (TLO, TL 1, THO and TH1) (4) Timer control registers (TMOO and TCON) (5) Serial data registers (SCON, SBUF and PCON) (6) Program status word (PSW)
...
,.J
"'...
28
Microcontrollers
and Applications
(7) Stack pointer (SP) (8) Program counter (PC) (9) Data pointer (DPH and DPL) (10) Ports (PO, P1, P2 and P3) 8 BYTES
FB
!."
.
8
I
FF F7 EF
FO
E8 EO 08 DO C8 CO 88 80 AS AD 98 90 88 80
A A PSI/V
E7 OF 07 CF C7
IP P3
IE P2
8F
.
SBUF
87 AF A7 9F 97
SCaN
P1 TCON PO
TMOD
SP
TLO DPL
TU OPH
THO
TH1 PCON
8F 87
'"
81T ADDRESSABLE
The B register is used for multiplication and division along with the accumulator. as a scratch pad where data may be stored.
jI
Interrupt Registers (IP and IE) Interruptscan be e~ableQor Qisa~d uodJ~!J?r2gr~m controlby a~ering controlbits in the interrupt nableregist~r\fE) andinterrupt riority e p registet (IP). - --f ---
...........
,.....-
29
Timer/Counter Registers (TLO, TL 1, THO and TH1) The 8051has two 16-bit upcounters, namedTOand T1. The counters aredivided intotwo 8-bit registerscalledthe timer low (TLO,TL1) and high (THO,TH1) bytes. Timer Control Registers (TMOD and TCON) All counter pction is controJJ.~dy bit states in TMOD (timer mode control register), TCON b (timer/counter ontrol register) and certainprogrammslructR5ns. c ... ~
'1
The 8051 communicates with other devices by sending and receiving data bits serially using
a
..
~~!ia' data communication circuit. Register SBUF (sefia:raata b,uffer}'is ~~ed fo~h<?ldd~ta.,~l)F is Ehy-sicPll-1WQJ~gJsters. On!.is ~rife only~and is ~~~d _tohold data to_Detransmitted out of the 8051. viaTXD(transmitdata).The.2!her1~..pd onlyand bo.lds receiveddat'!from external source~'yJ.9RXD, {receive data). B.th rT1lJtuallyexclusive regtster,s use addresS99F-J.Regi~r.qON (serial control) controls data communication. Register PCON (power control) controls data ~at~. .J
" -0, -,-"
'
PF (Parity FlagJ PF is set to 1 if the accumulator contains an odd number of 1s. Otherwise it is cleared. CY (Carry Flag)
An additioncauses.CYflag to'be set if there is a carry out of the !X1$1?and a subtraction causes itto be set if a borrow is needed. Other instructionsalso affect this flag. ~~ "_.,' .-
AC(AuxiliaryCarry Flag)
AF is WJ1.1b.~ a carry 9ut of bi.L3during_an addition or ~ borrow bi.bit 3 du~~ 1his flag is used -- - exclusively for ---...-M_'-MBCD arithmetic. OV (Overflow Flag)
s~btraction.
OF is set if an ov.2!1lpY'l ga!ts, i.e., a resu.l! i~ 91}t 01r:ange. Mor_especifically, for additiQn this Q
.
siThfraction, it is set when the MSB needs a bo!!QY'I. nd t~ere is no borrow-1rom the MSB, or ~ige a versa. The overflow flag will be set if signe-d arithmetic is too large to fit in the destination register or
"-
'.
'"
U) 0
Cy
AC
Fa
RSj
RSa
Ov
PS'WO
Parity of accumi.llator set by hardw'are to 1 if It cootains an odd number of 1s; other.vise it is reset m O.
PSW.1 User-definal~eflag PS'W.2 Ovemowflag set by arithmeiicoperations PSW~ Registerbank select bit 0 Fig. 2.8: 8051 program status word
~ n' "'" 0 (') 0,. :J -+"'" 0 (!) "'" (II Q :J 0... -0 -0 (') Q ~ 0 :J V>
L I
,..
.. .""rco' .. -
," ,
"""-',,,..
-.-
31
BankSelection Two bits RS1 and RSOof PSW are used for RAM bank selection as shown in Table. 2.1.
RS1 (PSW.4) 0 0 1 1 RSO lPSW.3) 0 1 0 1 Bank 0 1 2 3
A stack is a section of memory arranged in last-in-first-out (LIFO) manner. It~~et as..ide to storead and data while a sub ro J.!!Jj$e~tin9: The mem-9IY~ b'(te w~~ mostrecentl store IScalle stack.,Tile stack pointer (SP) an 8-bt re9iste~ that points.-
To the top of stacR. Reset initializes the stack pointer to location 07H. Itis incremented once to start . ~ -from location r-~ which is register ROQ!..r~mistE2~ 08~{ p~r}k1. =f - r --When data is stored on !be staQk the SP vallJe is iocremented by one. When data is retrieve
!Lo v'
-bit re ist~ namely reJ-l ;:)ng DPL. of MOVC and MOVX instructions
~ addre~<!iQQ.tt-
t5>
Program Counter
(PC)
The program counter (PC) ~ps track of the e<sec~tion f ~. o It is a 16-blt registe~ th.?.t containsJhe . ~ _. -, 'the -~. memory(ROM)address ofthe~instructioncurrentlybeing~xecut~. Duril1g execu1io[1 - re' .~ r . ,9fthecurrent:ipstructlonl contentsof. e add~ess ! the a ~g.areUQd~~ to c~porJ.D ~tothe. addressof the next instruction to be.executed. It is customary to say that the PC points to'the/ .--: -,..-' instructionthat is to be f~ch~d from the memq!)'.When 8051 js powered up, the Pc. is loaded with OOOOH. Therefore the first instruction byte of a program must reside in ROM location OOOOH. , . ~ .
Ports (PO, P1, P2 and P3) Ports PO... P2 and P3 are 8-bit registers. These are byte addressable as well as bit P1, addressable.
,& ..
32
Microcontrollers
and Applications
Interrupt
Timer/ Counter
Timer control
All counter action is controlled by bit states in TMOD (timer mode control register), TCON (timer/counter control register) and certain program instructions. Register SCON controls data communication.
--
Serial data
Register SBUF is used to hold data. Register PCON controls data rates. PSW consists of PF, CY, AC and OV flags. Two bits RS1 and RSO of PSW are used for RAM bank selection The stack pointer register points to the top of stack. Data pointer is a 16-bit register. It is operated as two 8-bit registers, namely DPH and DPL. DPTR is used for addressing the off-chip ROM and RAMwith MOVC and MOVXinstructions respectively. Since DPTR is a 16-bit register, 64KB of ROM/RAMcan be addressed (OOOOH FFFFH). to Ports PO, P1, P2 and P3 are 8-bit registers. These are byte addressable as wellas bit addressable.
~ -I..
, .,
"
Port
-.:.
... --=-~
.-
:II
33
IMPORTANT POINTS
..
Important features of 8051 microcontroller (1) 8-bit CPU (2) 8-bit registers: A, B, RO - R7 (3) 16-bit program counter (PC) and data pointer (OPTR) (4) 8-bit program status word (PSW), stack pointer (SP) (5) Internal ROM of 4KB (6) Internal RAM of 128 bytes (7) Four flags: PF, CY, AC and OV (8) 32 I/O pins arranged as four 8-bit ports: PO- P3 (9) Two 16-bit timer/counters: TO and T1
(10) Two external and three internal interrupt sources and reset (6 interrupts) (11) 1 serial port The flag register in the 8051 is called Program Status Word (PSW). The size of the flag register in the 8051 is 8 bits. 01 (PSW.1) and 05 (PSW.5) are user-definable. RAM bank selection is done as
follows:
RAM Address 00 - 1FH 20 - 2FH 30 - 7FH Description Reqister Banks 0-3 Bit addressable RAM Scratch Pad RAM
I'- The program counter (PC) keeps track of the execution of a program. When 8051 is powered
up, the PC is loaded with OODOH.
4. The flag register in the 8051 is called 5. The size of the flag register in the 8051 is 6. On power-up, the 8051 uses RAM location 7. On power-up, the 8051. uses bank .
8. On power-up, the 8051 uses RAM locations
..
.J
~ """"
l
~
34
Microcontrollers and Applications
Answers
1. 8 4. PSW 7. 0
2.2 5.8
8. 0
to
3. 32 6. 08
7
Descriptive Type 1. Listthe features of 8051 microcontroller. 2. Write a brief note on 8051 memory organization. (5) (5)
i
(5)
4. Explain the program status word (PSW) of the 8051 (5) 5. Explainthe role of program counter (PC), stack pointer (SP) and data pointer (DPTR) in 8051. (6) (9) 6. Draw the block diagram of 8051 microcontroller and explain.
t
II
I-\..
1(.
I ., ,.,
IS
suchas hard disk drives, modems, printers, pattern recognition and motor control. FEA TU R ES
.:. .:.
--
.:. .:.
.
.
HighSpeedInput/Output ~
Motor Control ~
BEN E FITS
.:.
.:.
Lowcost
More compact code than accumulator-based
of 232 registers can be directly
architecture
at any time
.:. Minimum
.:.
addressed
.l
36
Up to 32K internalOTPROM
.:. Dynamicallyconfigurable8- or 16-bitbuswidth .:. 16-bit counter .:. Full duplex serial port
.:. HOLD/HLDA bus protocol .:. Up to 3 dedicated PWM generators .:. 16-Bit watchdog timer
.:.
.:. 8-Channel high speed I/O (HSIO) subsystem .:. 16-bit timer'
8XC196KB
The 8xC196KB is the first memberof the CHMOS MCS96 controllerfamily. It is available in 8K byte ROM and 8K byte OTPROM versions. All versions feature' 232 bytes of register RAM. The 8xC196KB uses the High-Speed Input/Output (HSIO) structurefor event control. The HSIO has up to 4 input and 6 output,lines and uses either of two 16-bittimer/counters as a time base. Additional features include a hardware-generated pulse width modulator (PWM), a full-duplex serial I/O (SIO) port, a watchdog timer and an 8-channel 10-bit resolution analog to digital (AID) converter. The 8xC196KB has 48 input/output (I/O) lines which are shared with the peripherals. 8XC196KC The 8xC196KC is the next step up in the CHMOS 196 family. It is available in 16K byte ROM and 16K byte OTPROM versions. All versions feature 488 bytes of register RAM. The 8xC196KC is offered in a 20 MHz version, allowing 25% increase in performance. The 8xC196KC has all the peripherals as the 8xC196KB with the following enhancements: (1) Three hardware PWM generators (2) AID converter has both 8- and 10-bit conversion modes with programmable sample and conversion times. (3) Peripheral transaction server (PTS) acts as a microcoded interrupt handler which greatly reduces CPU overhead during interrupt servicing. 8xC196KD
The 8xC196KD has all the features of the 8xC196KC, but has extended The 8xC196KD is available in 32K byte ROM and 32K byte OTPROM versions. 1000 bytes of Register RAM. With the availability of 32K of memory, program level languages becomes much more practical. The 8xC196KD is also offered the on chip memory. Both versions feature development in high in a 20 MHz version.
\
L
The most recent products form the EPA Family.This family of devices has the advanced peripheralswhich include a flexible input/outputsystemand EPA (event processor array).
:I
15
Introduction to Advanced
Microcontrollers
37
Key Features .:. Up to 50 MHz operation .:. Fast register to register architecture
.:.
Up to 1000 bytes of register RAM Dynamicallyconfigurable 8- or 16-bitbus width Dynamicallymultiplexed/demultiplexed (NP, bus
bus protocol
.:.
.:. HOlD/HlDA
Two 16-bittimer/counterswith prescalarand quadrature counting mode Fullduplexserial port with independentbaud rate generat~r Fullduplex synchronous serial port 16-bitwatchdogtimer. IDLEand POWERDOWN modes 3 PWMoutputs (NP,NU) . .:. Up to 8 channel 8- or 1O-bitAID converter .:. PeripheralTransactionServer
8XC196NT
The 8xC196NT has 1M byte external addressability. Four of the AID inputs are replaced with the extended address port (EPORT). The four EPORT pins can be used as additional address lines (A 16 - A 19) or standard low speed lIas or.a combination of both. The 8xC196NT has 20 MHz operation.
8XC196NP
The 8xC196NP offers a dynamically selectable multiplexed/demultiplexed bus. Other key features the 8xC196NPare the chip select unit, 1M byte addressing, 3 PWM outputs and 25 MHz of operationat 5 volts.
8XL196NP
The 8xCl 196NP is functionally identical with the 8xC196NP, but offers low power operation (3 volts at 1"3MHz).
80C196NU
The 80C196NU doubles the performance of the 8xC196NP, operating at 50 MHz (5V). The 80C196NU alsofeatures a 32-bit accumulatorimplantedin hardware to increase the performanceof multiply/accumulate instructions.The 80C196NUis pin compatiblewith the 8xC196NP.This feature, alongwith its clock doubling circuitry, enables the 80C196NU to provide a high performance insocketupgrade to 8xC196NP designs. Other key features include 1M byte addressing, 3 PWM outputs,chip select unit and both 40 and 50 MHz (5V) versions.
...
"!~ ..
38
Microcontrollers
and Applications I
MOTORCONTROLFAMILY
The motor control family is c.omprisedof devices that support motor control applications.This family also uses the EPA system for I/O control. . Key Features
.:. Register RAM file .:. On-chip ROM/OTPROM .:. Fast, flexible interrupts .:. Event processor array (EPA)
.:.
.:. High performance CPU .:. Sophisticated three-phase PWM waveform generator .:. Peripheral transaction server (PTS) .:. Two 16-bit timer/counter
16-bitwatchdogtimer
.:. Frequency generator module (MD only) .:. Serial I/O (MH only)
8XC196MCIMH
'-
II
The 8xC196MC/MH are members of the MCS 96 motor control family. This device has a peripheral set which is optimized for 3-phase AC induction and DC brushless motor control as well as power
inverterapplications. Some of the specific features are listed below: (1) Waveform generator (WFG): WFG is used to generate 3-phase PWM. The WFG generates three complementary non-overlapping PWM pulses. The WFG features programmable frequency,duty cycle and dead times. (2) PWM generators: There are two PWM generators. These have a common programmable frequency and separately programmable duty cycles and 8-bit resolution. (3) Advanced event processor array (EPA) structure: Advanced EPA structure is used for event monitoring and control. (4) PeripheralTransaction Server (PTS): PTS supports microcoded interrupt processing requiring less CPU intervention. 8xC196MC 8xC196MH (5) (6) (7) (8) (9) (10) (11) (12) (13)
I-~ ""
Register RAM Factory programmed ROM ROM OTPROM AID converter I/O lines Input only lines Capture/comparemodules Compareonly modules
488 bytes 16K bytes 16K bytes 16K bytes 13 channels 40 13 4 4 Not available
744 bytes 32K bytes 32K bytes 32K bytes 8 channels 44 8 2 4 Available
(14) TwochannelUART
"'.,.,
~ ,~ -~ :::~
_1
39
8XC196MD
is
The 8xC196MD has all of the 8xC196MC features with the following enhancements: .:. A frequency generator allows generating a programmable frequency square wave, which finds use in infrared remote control communications. )r .:. Twoadditional capture I compare and two compare only modules are added to the event processor array, giving additional event capture and generation capabilities. .:. Eightadditionailia pins, two input only and one analog/digital input pin are added. The 8xC196MD maintains pin-for-pin compatibility with the 8xC196MC device, allowing easy upgrades of existing
designs.
MCS-251MICROCONTROLLER FAMILY
OVERVIEW
MCS 251 architecture is the next generation of Intel MCS 51 architecture. It increases system performance by a factor of five using existing MCS 51 microcontroller code. By rewriting code using the MCS 251 architecture instructions, designers can increase performance up to 15 times. The following list gives an overview of MCS-251 microcontroller family:
.:.
.:. 5 to 15 times increase in performance compared to MCS 51 microcontroller at the same clock speed .:. 3-stage pipeline CPU architecture
.:.
2 clocks (1 state)~perinstruction
8-BitALU
.:. 64KB stack space .:. New instructions and addressing modes . .
.:.
8, 16 and 32-bit data transfer, arithmetic and logical instructions Supports register, immediate, direct, indirect, displacement, relative and bit addressing