Module 2 CA&M (Part1)
Module 2 CA&M (Part1)
Microcontroller
Microcontroller is a true computer on a chip. The design incorporates all of
the features found in a microprocessor CPU: ALU, PC, SP, and registers. It also has
added the other features needed to make a complete computer: ROM, RAM, parallel
I/O, serial I/O, counters, and a clock circuit. Like the microprocessor, a micro
controller is a general-purpose device, but one that is meant to read data, performs
limited calculations on that data, and control its environment based on those
calculations. The prime use of a microcontroller is to control the operation of a
machine using a fixed program that is stored in ROM and that does not change over the
lifetime of the system.
5. The micro controller can function as a computer with the addition of no external
digital parts the microprocessor must have many additional parts to be operational.
TYPES OF MICROCONTROLLERS
Figure shows the various types of microcontrollers. The microcontrollers are classified
in terms of internal bus width. Embedded microcontroller, instruction set, memory
architecture, IC chip. Or VLSI (V HDL or Verilog) file and family.
The 8-bit microcontroller: When the internal bus in an MCU is 8-bit and ALU performs
arithmetic and logic operations on a byte at an instruction, the MCU is an 8-bit
microcontroller. Some 8-bit MCUs are Intel 8031/8051 family and Motorola MC68HCl
1 family.
The 16-bit microcontroller: When the internal bus in an N4CU is 16-bit and the ALU
performs arithmetic and logic operations on a word of 16 bits at an instruction, the
MCU is a 16-bit microcontroller. Important 16-bit MCUs are extended 8051XA, Intel
8096 family and Motorola MC68HC12 and MC68332 families. These provide greater
precision and performance compared to the 8-bit MCUs.
The 32-bit microcontroller: When internal bus for the data transfer operations in an
MCU is 32-bit and the ALU performs arithmetic and logic operations on a word of 32
bits at an instruction, the MCU is a 32-bit microcontroller. Important 32-bit NICUs are
Intel/Atmel 251 family, Intel family, MotM01a M683xx family, and ARM 7 or 9 or 11
processor-based systems These computing systems for applications—mobile phones,
MP3 audio systems, MPEG processing, image processing-based products and
aerospace systems are the examples.
External memory microcontroller: When an embedded system has an MCU that has all
the hardware and software units present not as a single unit and has all or part of the
in memory unit externally interfaced using an interfacing circuit which is called the
glue circuit, the MCU is called an external memory microcontroller. For example, 803 1
has the program memory which is interfaced externally to it. The 8051 has both
internal as well as external program memory.
3. CISC and RISC Architecture Microcontrollers
The RISC provides a higher performance in computing than the CISC. This is because
little need of the external fetches, which takes a significant amount of processor time.
High performance is also because of hardwired implementation of instructions
4. Harvard and Princeton Memory Architecture Microcontrollers
(Program and data can be stored on the same memory chip or unit within same address
block.)
SELECTION OF A MICROCONTROLLER
1. Checklist of the Needed Features and Factors Taken into Consideration
• Power dissipation permissible (maximum limit) and the clock Speed need (lowest
• limit)
3. Serial UART
4. Timer l, 2 or 3.
6. Out-compares none or I or 2 or 3 or 4 or 5.
8. PWM none or 1 or 2 or 3 or 4.
11. Digital signal processing (DSP) with DSP instructions processing CPU.
14. Ports with wireless interface and related processing instructions capable CPU.
• Major building blocks of software, their cost and availability and required
Hardware/software tradeoff
• Ease of integration
• Availability of design team expertise
• Availability and cost of development software and hardware tools
• Ease and availability of testing and debugging facilities
• Easy and reliable availability of the MCU, development tools and building blocks for
hardware and software
2. Selection of Processor and Processor Family
1. Biomedical instruments like an ECG LCD display cum recorder, blood cell
recorder cum analyzer, patient monitor system,
2. Communication systems like numeric pagers, cellular phones, cable TV
terminals, FAX and transceivers with or without an accelerator, video game and
so on,
3. Peripheral controllers of a computer such as the keyboard controller, printer
controller, laser printer controller, LAN controller and disk drive controller,
4. Instruments such as an industrial process controller, and electronic smart
weight display system ,
5. target tracker,
6. an automatic signal tracker,
7. Accurate control of the speed and position of a DC motor,
8. Robotics system
9. CNC machine controller
10. Automotive applications like a close loop engine control, a dynamic ride
control, an anti-lock braking system monitor and so on,
11. Electronic data acquisition and supervisory control system, the industrial
moisture recorder cum controller, CRT display controller, digital storage
system and spectrum analyzer. Etc..
Data Memory: Data variables and stacks in an MCU are mostly in a volatile read and
write memory (RAM) or registers.
3. Ports
A port the bits are latched (placed) and sent to the processor or external device or circuit.
When the bits are sent (placed) from an external circuit, the port is called the input
port. When the bits are (placed) for an external circuit by the processor, the port is
called the output port.
There are two types of ports—parallel and serial. A parallel 8-bit port means all the 8
bits can be placed at all its pins simultaneously. A serial port means all the 8 bits of a
byte or character can be placed at its pin in 8 time slots. The time interval for each slot
is the same and depends on a preset bit-rate.
4. On-chip Registers
An MCU has several on-chip registers. Registers are needed in the register addressing
instructions and for the operands in the arithmetic and logic operations. Advantage of
the registers is that a register can be addressed in an instruction by a few bits
5 .Special Function Registers
An MCU can have several registers, which are used for special functions. These registers
are called SFRs (special function registers).
6. UART
The serial-line device is present in most MCUs, because the serial port is needed to
communicate with many external and remote devices. In serial communication, a
stream of 1's and 0's is sent or received at successive time Intervals on a single line
called the serial line. UART: (Universal Asynchronous Receiver and Transmission) is a
very popular mode of serial communication. The transmission and reception in UART
mode is asynchronous. The successive bytes are sent and received at the serial line
such that there may be variable time gap between the bytes. Also the transmitter and
receiver clocks must be of the same rate but they are independent (need not be in
same phase or in synchronization). Such transmission is also called
asynchronous serial
communication. There is a transmit buffer which is present with the byte for serial
transmission. There is a receive buffer, which receives the byte during serial reception.
7. Timers/Counters
The 8051 has two timers/counters. They can be used either as timers. To generate a
time delay or as counters to count events happening outside the microcontroller.
8. PWM
An MCU version may have one or more pulse width modulated outputs (PWMs). PWM
output is one which the width percentage is proportional to the value loaded in a special
function register which loads the modulation parameters.
9. On -Chip A/D Converters (ADC)
Many times, a microcontroller has an on-chip A/D (called ADC also) conversion
feature. This is an important feature in the control applications for converting the
analog signal to digital.
10. Watchdog Timer (WDT)
The watchdog timer is a timing device that resets the system after a pre-defined timeout.
WDT resets and then the further processing is (i ) either from the same address as at
the beginning on the power-up (for example, in 8051) or (ii) as per the bytes at a vector
address in certain MCU timeouts of the WDT (for example, in 68HCl 1). Most
microcontroller variants of an MCU family have on-chip watchdog timer.
8051 MICROCONTROLLER
ARCHITECTURE OF 8051
A and B CPU registers
logical operations, of the 8051 central processing unit (CPU). The A (accumulator)
register is the most versatile of the two CPU registers and is used for many operations,
including addition, subtraction, integer multiplication and division, and Boolean bit
manipulations. The A register is also used for all data transfers between the 8051 and
any external memory. The B register is used with the A register for multiplication and
division operations and pas no other function other than as a location where data may
be stored.
Program Counter and Data Pointer
The 8051 contains two 16-bit registers; the program counter (PC) and
the data pointer (DPTR). Each is used to hold the address of a byte in memory. PC
points the address of the next instruction to be executed. The PC is automatically
incremented after every instruction byte is fetched and may also be altered by certain
instructions. The PC is the only register that does not have an internal address.
The DPTR register is made up of two 8-bit registers, named DPH and
DPL, which are used to furnish memory addresses" for internal and external code
access and external data access:. The DPTR is under the control of program
instructions and can be specified by its 16-bit name, DPTR, or by each individual byte
name, DPH and DPL. DPTR does not have a single internal address; DPH and DPL are
each assigned an address.
PSW (program status word) register
This flag is set whenever there is a carry out from theJl1-bi-t. This flag
bit is affected after an 8-bit addition or subtraction. It can also be set to I or 0 directly
by an instruction such as SETB C" and "CLR C" where. "SETB C" stands for "set bit
carry" and "CLR C. 'for "Clear carry"..
AC, the auxiliary carry flag
In general, the carry flag is used to detect errors in unsigned arithmetic operations. The
over flow flag is only used to detect errors in signed arithmetic operations and
RS0, RS1 - Register bank select bits.
These two bits are used to select one of four register banks of RAM. By
setting and clearing these bits, registers R0-R7 are stored in one of four banks of RAM
This means that RAM location 08 is the first location being used for the stack by the
8051. The storing of a CPU register in the stack is called a PUSH, and loading the
contents of the stack back into a CPU register is called a POP. In other words, a register
is pushed onto the stack to save it and popped off the stack to retrieve it. The job of the
SP is very critical when push and pop actions are performed. To see how the stack
works, let's look at the PUSH and POP instructions
Pushing onto the stack
In the 8051the stack pointer (SP) is pointing to the last used location
of the stack. As we push data onto the stack, the stack pointer (SP) is incremented by
one. Notice that this is different from many microprocessors, 1n x85 processors in
which the SP is decremented when data is pushed onto the stack. When each PUSH is
executed, the contents of the register are saved on the stack and SP is incremented by
1. Notice that for every byte of data saved on the stack, SP is incremented only once.
Popping from the stack
Popping the contents of the stack back into a given register is the
opposite process of pushing. With every pop, the top byte of the stack is copied to the
register specified by the instruction and the stack pointer is decremented once.
The upper limit of the stack
The 8051 microcontroller has a total of 128 bytes of RAM. The 128
bytes of RAM inside the 8051 are assigned addresses 00 to 7FH. They can be accessed
directly as memory locations. These 128 bytes are divided into three different groups as
follows.
1. A total of 32 bytes from locations 00 to IF hex are set aside for register banks and
the stack. These 32 bytes are divided into 4 banks of registers in which each bank
has 8 registers, RO - R7. RAM locations from 0 to 7 are set aside for bank 0 of RO
- R7 where RO is RAM location 0, Rl is RAM location 1, R2 is location 2, and so
on, until memory location 7 which belongs to R7 of bank O. The second bank of
registers RO-R7 starts at RAM location 08 and goes to location OFH. The third
bank of RO- R7 starts at memory location 10H and goes to location 17H; finally,
RAM locations 18H to 1FH are set aside for the fourth bank of RO -R7. The
following shows how the 32 bytes are allocated into 4 banks
2. A total of 16 bytes from locations 20H to 2FH are set aside for bit addressable
read/write memory. A bit-addressable area of 16 bytes occupies RAM byte
addresses 20h to 2Fh, forming a total of 128 addressable bits. An addressable bit
may be specified by its bit address of ooh to 7Fh, or 8 bits may form any byte
address from 20h to 2Fh. Thus, for example, bit address 4Fh is also bit 7 of byte
address 29h. Addressable bits are useful when the program need only remember
a binary event (switch on, light off, etc.).
2. A total of 80 bytes from locations 30H to 7FH are used for read and write storage,
or what is normally called a scratch pad. These 80 locations of RAM are widely
'used for the purpose of storing data and parameters by 8051 programmers.
Internal ROM
The 8051 contain an internal ROM of 4K byte capacity, it occupies
code address space ooooh to OFFFh. The PC is ordinarily used to address program
code bytes from addresses ooooh to FFFFh. Program addresses higher than OFFFh,
which exceed the internal ROM capacity, will cause the 8051 to automatically fetch
code bytes from external program memory. Code bytes can also be fetched
exclusively from an
external memory, addresses ooooh to FFFFh, by connecting the external access pin (EA
pin 31 on the DIP) to ground. The PC does not care where the code is; the circuit designer
decides whether the code is found totally in internal ROM, totally in external ROM, or
in a combination of internal and external ROM.
PIN DESCRIPTION OF THE 8051
Although 8051 family members (e.g., 8751, 89C51, DS5000) come in different
packages, such as DIP (dual in-line package), QFP (quad flat package), and LLC
(leadless chip carrier), they all have 40 pins that are dedicated for various functions
such as I/O, RD, WR, address, data, and interrupts.
From above figure note that of the 40 pins, a total of 32 pins are set aside for the four
ports PO, PI, P2, and P3, where each port takes 8 pins. The rest of the pins are
designated as Vcc, GND, XTALl, XTAL2, RST, EA, PSEN. Or these 8 pins, six of them
(Vcc, GND, XTALl, XTAL2, RST, and EA) are used by all members of the 8051 and 8031
families. In other words, they must be connected in order for the system to work,
regardless of whether the microcontroller is of the 8051 or 8031 family. Vcc,
The 8051 has an on-chip oscillator but requires an external clock to run it. Most often
a quartz crystal oscillator is connected to inputs XTALl (pin 19) and XTAL2 (pin 18).
The quartz crystal oscillator connected to XTAL1 and XTAL2 also needs two capacitors
of 30 pF .One side of
Each capacitor is connected to the ground as shown in Figure
If you decide to use a frequency source other than a crystal oscillator, such as a TTL
oscillator, it will be connected to XTALl; XTAL2 is left unconnected, as shown in Figure
.
RST
Pin 9 is .the RESET pin. It is an input and is active high (normally low). Upon applying
a high pulse to this pin, the microcontroller will reset and terminate an activities. This
is often referred to as a power-on reset. Activating a power-on reset will cause all
values in the registers to be lost. Table provides a partial list of 8051 registers and their
values after power-on reset.
Notice that the value of the PC (program counter) is 0 upon reset, forcing the CPU to
fech the first opcode from ROM memory location 0000. This means that we must place
the first line of source code in ROM location 0 because that is where the CPU. wakes up
and expects to find the first instruction. Figure shows two ways of connecting the RST
pin to the power-on reset circuitry. In order for the RESET input to be effective, it must
'have a minimum duration of 2 machine cycles. In other words, the high pulse must be
high for a minimum of 2 machine cycles before it is allowed to go low.
̅ 𝑬𝐴̅̅̅̅/VPP
The 8051 family member ,such as the 8751, 89C5J, or DS5000, all come with on-chip
ROM to store programs. In such cases, the EA pin is connected to Vcc .For family
members such as the 8031 and 8032 in which there is no on-chip ROM, code is stored
on an external ROM and is
Fetched by the 8031/32. Therefore, for the 8031 the EA pin must be connected to GND
to indicate that the code is stored externally. EA, which stands for "external access," is
pin number 31 in the DIP packages. It is an input pin and must be connected-to either
Vcc or GND. In other words, it cannot be left unconnected
𝑷
̅ 𝑺𝑬𝑁̅̅̅̅̅̅̅̅
This is an output pin. PSEN stands for "program store enable." In an 8051-
based system in which an external ROM holds the program code, this pin is connected
to the OE pin of the ROM.
ALE
ALE (address latch enable) is an output pin and is active high. When
connecting an 8051 'to external memory, port 0 provides both address and data. In other
words, the 8051 multiplexes address and data through port 0 to save pins. The ALE pin
is used for de multiplexing the address and data by connecting to the G pin of the
74LS373 chip.
The four ports PO,PI, P2, and P3 each use 8 pins, making them 8-bit
ports. All the ports upon RESET are configured as output, ready to be used as output
ports. To use any of these ports as an input port, it must be programmed,
Port 0
Port 0 occupies a total of 8 pins (pins 32 - 39). It can be used for input
or output. To use the pins of port 0 as both input and output ports, each pin must be
connected externally to a 10K ohm pull-up resistor. This is due to the fact that PO is an
open drain, unlike PI, P2, and P3. Open drain is a term used for MOS chips in the same
way that open collector is used for TTL chips. In any system using the 8051, we
normally connect P0 to pull-up resistors. In this way we take advantage of port0 for
both input and output. With external pull-up resistors connected upon reset, port 0 is
configured
as an output port. For example, the following code will continuously send out to port 0
the alternating values 55B and AAH.
MOV A, #55H
BACK: MOV PO,A
ACALL DELAY
CPL A
SJMP BACK
Port 0 as input
Port 1
MOV A,
#50H BACK:
MOV Pl, A
ACALL DELAY
CPL A
SJMP BACK
Port 1 as input
MOV A,
#55H BACK:
MOV P2,
A
ACALL DELAY
CPL A
SJMP BACK
Port 2 as input
To make port 2 an input, it must programmed as such by writing I to all its bits. In the
following code, port 2 is configured first as an input port by writing 1s to it. Then data
is received from that port and is sent to PI continuously.
2. Output driver
3. Input buffer
Port 1 structure
Figure shows the structure of PI and its three components. The other
ports, P2 and P3, are basically the same except with extra circuitry to allow their dual
functions. In Figure notice that the Ll load is an Internal load for PI, P2, and P3. When
reading the ports there are two possibilities : (1) reading the input pin, or (2) reading the
latch.
1. As can be seen from Figure bellow, a 1 written to the port bit is written to the latch
and the D latch has "high" on its Q. Therefore, Q = 1 and Q = O.
2. Since Q = 0 and is connected to the transistor Ml gate, the Ml transistor is off.
3. When the Ml transistor is off, it blocks any path to the ground for any signal
connected to the input pin and the input signal is directed to the tri-state TBl.
4. When reading the input port in instructions such as "MOVA, PI" we are really
reading the data present at the pin. In other words, it is bringing into the CPU the
status of the external pin. This instruction activates the read pin of TBI (tristate
buffer 1) and lets data at the pins flow into the CPU's internal bus. Figures 1 and
2 show high and low signals at the input, respectively.
( Figure 3)
Reading latch
Since, in reading the port, some instructions read the port and others
read the latch, we next consider the case of reading the port where it reads the
internal port latch. "ANL PI, A" is an example of instruction that reads the latch instead
of the input pin. Look at the sequence of actions taking place when an instruction such
as "ANL PI, A" is executed.
1. The read latch activates the tri-state buffer of TB2 (Figure C-l7) and brings
the data from the Q latch into the Cpu.
2. This data is ANDed with the contents of register A.
3. The result is rewritten to the latch.
After rewriting the result to the latch, there are two possibilities: (1) If Q
= 0, then Q = 1 and M1 is "on," and the output pin has "0," the same as the status of
the Q latch. (2) If Q = 1, then Q = 0 and the M1 is "off," and the output pin has "I," the
same as the status of the Q latch. From the above discussion, we conclude that the
instruction that reads the latch normally reads a value, performs an operation
(possibly changing the value), and rewrites the value to the latch. This is often called
"readmodify- write,"
Reading a latch
PO structure
open drain
then show how to program the timers to generate time delays. 'Both timer 0 and timer
1 are 16 bits wide. Since the 8051 has an 8-bit architecture, each 16-bit timer is
accessed as two separate registers of low byte and high Byte. Each timer is discussed
separately.
Timer 0 registers
The 16.-bit register of timer 0 is accessed as low byte and high byte. The low byte
register is called TLO (timer 0 low byte) and the high byte register is referred to as
THO (timer 0 high byte). These registers can be accessed like any other register, such
as A, B, RO, Rl, R2,
THO TLO
Timer 1 is also 16 bits, and its 16-bit register is split into two bytes, referred to as TLl
(timer 1 low byte) and THI (timer 1 high byte). These registers are Accessible in the same
way as the registers of timer O.
TH1 TL1
M1 M2 MODE
C/T (clock/timer)
This bit in the TMOD register is used to decide whether the timer is used as a delay
generator or an event counter. If C/T = 0, it is used as a timer for time delay
generation. The clock source for the time delay is the crystal frequency of the 8051.
GATE
The other bit of the TMOD register is the GATE bit. . GATE = 1, meaning that external
hardware is needed to start and stop the timers. In using software to start and stop the
timer where GATE = 0.
SBUF register
SBUF is an 8-bit register used solely for serial communication in the
8051. For a byte of data to be transferred via the TxD line, it must be placed in the
SBUF register. Similarly, SBUF holds the byte of data when it is received by the 805I's
RxD line. SBUF can be accessed like any other register in the 8051
The SCON register is an 8-bit register used to program the start bit,
stop bit, and data bits of data framing, among other things. The following describes
various bits of the SCON register.
• SM0 - Serial port mode bit 0 is used for serial port mode selection.
• SM1 - Serial port mode bit 1.
• SM2 - Serial port mode 2 bit, also known as multiprocessor communication enable
bit. When set, it enables multiprocessor communication in mode 2 and 3, and
eventually mode 1. It should be cleared in mode 0.
• REN - Reception Enable bit enables serial reception when set. When cleared, serial
reception is disabled.
• TB8 - Transmitter bit 8. Since all registers are 8-bit wide, this bit solves the problem
of Transmitting the 9th bit in modes 2 and 3. It is set to transmit logic 1 in the 9th bit.
• RB8 - Receiver bit 8 or the 9th bit received in modes 2 and 3. Cleared by hardware if
9th bit received is a logic 0. Set by hardware if 9th bit received is a logic 1.
• TI - Transmit Interrupt flag is automatically set at the moment the last bit of one byte
is sent. It’s a signal to the processor that the line is available for a new byte transmits.
It must be cleared from within the software.
.RI - Receive Interrupt flag is automatically set upon one byte receive. It signals that
byte is received and should be read quickly prior to being replaced by a new data. This
bit is Cleared from within the software.
PCON (power control) register
D7 DO
SMOD --------
----------- -
PCON register is an 8-bit register. Of the 8 bits, some are unused, and
some bits are used for the power control capability of the 8051( for setting idle and
power down mode ,IDL& PD bits are used). The bit which is used for the serial
communication is D7, the SMOD (serial mode) bit
TCON register
These are the D4 (TCONA) and D6 (TCON.6) bits of the TCON register.
We were introduced to these bits in Chapter 9. They are used to start or stop timers 0
and 1, respectively.
1. Reset. When the reset pin is activated, the 8051 jumps to address location 0000.
2. Two interrupts are set aside for the timers: one for timer 0 and one for timer I.
Memory locations OOOBH and 001BH in the interrupt vector table belong to
timer 0 and timer 1, respectively.
3. Two interrupts are set aside for hardware external hardware interrupts. Pin
numbers 12 (P3.2) and 13 (P3.3) in port 3 are for the external hardware
interrupts INTO and INTI, respectively. These external interrupts are also
referred to as EXI and EX2. Memory locations 0003H and OO13Hin the interrupt
vector table are assigned to INTO and INTI, respectively.
4. Serial communication has a single interrupt that belongs to both receive and
transfer. The interrupt vector table location 0023H belongs to this interrupt.
If EX0= 0 - change of the INT1 pin logic state cannot generate an interrupt.
1 - Enables an external interrupt on the pin INT1 state change.
Steps in enabling an interrupt
1. Bit D7 of the IE register (EA) must be set to high to allow the rest of register to take
effect.
2. If EA = 1, interrupts are enabled and will be responded to if their corresponding bits
in IE are high. If EA = 0, no interrupt will be responded to, even if the associated bit
in the IE register is high.
INTERRUPT PRIORITY IN THE 8051
When the 8051 is powered up, the priorities are assigned according to Table. for
example If external hardware interrupts 0 and 1 activated at the same time, external
interrupt 0 (INTO) is responded to first. Only after INTO has been serviced is INTI
serviced, since INTI has the lower priority. In reality, the priority scheme in the table is
nothing but an internal polling sequence in which the 8051 polls the interrupts in the
sequence listed in Table and responds accordingly.
interrupts
Highest to lowest priority
External hardware interrupt0 (INT0)
Timer 0 interrupt (TF0)
External hardware interrupt (INT1)
Timer interrupt (TF0)
D7 D0
---- ----- PT2 PS PT1 PX1 PT0 PX0
The interrupt priority register is an 8 bit register .it is used change the priority of the
interrupt in 8051.following the bits of IP register
Priority bit = 1 assigns high priority. Priority bit = 0 assigns low priority.
Figure. The clock frequency, f, establishes the smallest interval of time within the
microcontroller, called the pulse, P, time. The smallest interval of time to accomplish
any simple instruction, or part of a complex instruction, however, is the machine cycle.
The machine cycle is itself made up of six states. A state is the basic time interval for
discrete operations of the microcontroller such as fetching an opcode byte, decoding
an opcode, executing an opcode, or writing a data: byte. Two oscillator pulses define
each state. Program instructions may require one, two, or four machine cycles to be
executed, depending on the type of instruction. Instructions are fetched and executed
by the micro controller automatically.
Tins’ = C x 12d
Crystal frequency
the case
for the bit area of RAM. This feature allows the programmer to change only what
'needs to be altered, leaving the remaining bits in that SFR unchanged. Not all of the
addresses from 80h to FFh are used for SFRs, and attempting to use an address that is
not defined or empty, results in unpredictable results.