Unit 1
Unit 1
Unit I
8-BIT EMBEDDED PROCESSOR
Figure1.3 shows a high level flow through the development process and
identifies the major elements of the development life cycle.
Microprocessor Microcontroller
⮚ The CPU is the brain of the microcontrollers expected task reading user's
programs and executing the as per instructions stored there in.
⮚ Its primary elements are an Accumulator (AC), Stack Pointer (SP) Program
Counter (PC), Program Status Word (PSW), Data Pointer (DTPR) and few
more 8-bit register.
ARITHMETIC LOGIC UNIT (ALU)
⮚ The temporary register is used to hold data during an arithmetic / logic operation.
⮚ The result is stored in the accumulator register.
ACCUMULATOR
These Register Banks are selected with the help of PSW (Program Status Word)Register bits
i.e. RS0, RS1.
There are many ways to categorize these 21 Special Function Registers but I find thefollowing
way as an appropriate one. The 21 Special Function Registers of 8051 Microcontroller are
categorized in to seven groups. They are:
Pointer Registers: DPTR (Data Pointer – DPL, DPH) and SP (Stack Pointer)
I/O Port Latches: P0 (Port 0), P1 (Port 1), P2 (Port 2) and P3 (Port 3)
All the 21 8051 Microcontroller Special Function Registers (SFRs) along with their
functions and Internal RAM Address is given in the following table.
CPU or Math Registers A
or Accumulator (ACC)
The Accumulator or Register A is the most important and most used 8051 Microcontroller
SFRs. The Register A is located at the address E0H in the SFR memory space. The
Accumulator is used to hold the data for almost all the ALU Operations.
Some of the operations where the Accumulator is used are:
• Arithmetic Operations like Addition, Subtraction, Multiplication etc.
• Logical Operations like AND, OR, NOT etc.
• Data Transfer Operations (between 8051 and External Memory)
The name “Accumulator” came from the fact this register is used to accumulate (or store) the
result of all Arithmetic and most of the Logical Operations.
B (Register B)
The B Register is used along with the ACC in Multiplication and Division operations. These
two operations are performed on data that are stored only in Registers A and B. During
Multiplication Operation, one of the operand (multiplier or multiplicand) is stores in B
Register and also the higher byte of the result.
In case of Division Operation, the B Register holds the divisor and also the remainder of the
result. It can also be used as a General Purpose Register for normal operations and is often used
as an Auxiliary Register by Programmers to store temporary results.
Register B is located at the address F0H of the SFR Address Space.
SP or Stack Pointer points out to the top of the Stack and it indicates the next data to be
accessed. Stack Pointer can be accesses using PUSH, POP, CALL and RET Instructions. The
Stack Pointer is an 8-bit register and upon reset, the Stack Pointer is initialized with 07H.
When writing a new data byte into the stack, the SP (Stack Pointer) is automatically
incremented by 1 and the new data is written at an address SP+1. When reading data from
stack, the data is retrieved from the Address in SP and after that the SP is decremented by 1
(SP-1).
I/O Port Registers (P0, P1, P2 and P3)
The 8051 Microcontroller four Ports which can be used as Input and/or Output. These four
ports are P0, P1, P2 and P3. Each Port has a corresponding register with same names (the
Port Registers are also P0, P1, P2 and P3). The addresses of the Port Registers are as follows:
P0 – 80H, P1 – 90H, P2 – A0H and P2 – B0H.
Each bit in these SFRs corresponds to one physical Pin in the 8051 Microcontroller. All these
Port Registers are both Bit Addressable and Byte Addressable. Writing 1 or 0 on a Port Register
Bit will reflect as an appropriate voltage (5V and 0V) on the corresponding Pin.
If a Port Bit is SET (declared as 1), the corresponding Port Pin will be configured as Input and
similarly if a Port Bit is CLEARED (declared as 0), the corresponding Port Pin is configured
as Output. Upon reset, all the Port Bits are SET (1) and hence, all the Port Pins are configured
as Inputs.
The following table describes the function of each bit in the PCON Register.
Bit Symbol Description Additional Info
7 SMOD Serial Comm. Baud Rate Modify Bit If 1, doubles the baud
rate using Timer 1. If 0,
normal timer 1 baud
rate.
6–4 —- —
3 GF1 General Purpose User Flag (Bit 1)
2 GF0 General Purpose User Flag (Bit 0)
1 PD Power Down Bit To enter Power Down
Mode, set to 1
0 IDL Idle Mode Bit To enter Idle Down
Mode, set to 1
The following table describes the function of each bit in the SCON Register.
The following table gives the description of each bit in the TCON SFR.
Bit Symbol Description Additional Info
7 TF1 Timer 1 Overflow Flag Set when Timer 1 overflows (all 1s to 0).
Cleared when processor executes ISR at 001BH.
6 TR1 Timer 1 Run Control Bit To enable Timer/Counter, set to 1. Clear to halt
the timer.
5 TF0 Timer 0 Overflow Flag Set when Timer 0 overflows (all 1s to 0).
Cleared when processor executes ISR at 000BH.
4 TR0 Timer 0 Run Control Bit To enable Timer/Counter, set to 1. Clear to halt
the timer.
3 IE1 Ext. Interrupt 1 Edge Flag Set when HIGH to LOW is received on INT1
(P3.3). Cleared when processor executes ISR at
0013H.
2 IT1 Ext. Interrupt 1 Type If 1, Interrupt 1 occurs on falling edge. If 0,
Control Bit Interrupt 1 occurs on low level.
1 IE0 Ext. Interrupt 0 Edge Flag Set to 1 when HIGH to LOW is received on
INT0 (P3.2). Cleared when processor executes
ISR at 0003H.
0 IT0 Ext. Interrupt 0 Type If 1, Interrupt 0 occurs on falling edge. If 0,
Control Bit Interrupt 0 occurs on low level.
The Gatex bit is used to operate the Timerx with respect to the INTx pin or regardless of the
INTx pin.
• GATE1 = 1 ==> Timer1 is operated only if TR1 (in TCON) is SET and Signal on
INT1 is HIGH.
• GATE1 = 0 ==> Timer1 is operated irrespective of Signal on INT1 pin but TR1 (in
TCON) must be SET.
• GATE0 = 1 ==> Timer0 is operated only if TR0 (in TCON) is SET and Signal on
INT0 is HIGH.
•
GATE0 = 0 ==> Timer0 is operated irrespective of Signal on INT0 pin but TR0 (in
TCON) must be SET.
The C/Tx bit is used selects the source of pulses for the Timer to count.
• C/T1 = 1 ==> Timer1 counts pulses from Pin T1 (P3.5) (Counter Mode)
• C/T1 = 0 ==> Timer1 counts pulses from internal oscillator (Timer Mode)
• C/T0 = 1 ==> Timer0 counts pulses from Pin T0 (P3.4) (Counter Mode)
• C/T0 = 0 ==> Timer0 counts pulses from internal oscillator (Timer Mode)
The following table describes the functions of each bit in the IE Register.
Bit Symbol Description Additional Info
7 EA Global Interrupt Enable Bit. If set to 1, individual interrupts can
be enabled. If set to 0, all interrupts
are disabled.
6 — —
5 ET2 Reserved
4 ES Serial Port Interrupt Enable If set to 1, Serial Port interrupt is
Bit enabled. If set to 0, Serial Port
interrupt is disabled.
3 ET1 Timer 1 Overflow Interrupt If set to 1, Timer 1 Overflow
Enable Bit interrupt is enabled. If set to 0,
Timer 1 Overflow interrupt is
disabled.
2 EX1 Ext. Interrupt 1 Enable Bit If set to 1, Ext. Interrupt 1 is
enabled. If set to 0, Ext. Interrupt 1
is disabled.
1 ET0 Timer 0 Overflow Interrupt If set to 1, Timer 0 Overflow
Enable Bit interrupt is enabled. If set to 0,
Timer 0 Overflow interrupt is
disabled.
0 EX0 Ext. Interrupt 0 Enable Bit If set to 1, Ext. Interrupt 0 is
enabled. If set to 0, Ext. Interrupt 0
is disabled.
NOTE: The Interrupt Enable (IE) SFR is bit addressable.
IP (Interrupt Priority)
The IP or Interrupt Priority Register is used to set the priority of the interrupt as High or Low.
If a bit is CLEARED, the corresponding interrupt is assigned low priority and if the bit is SET,
the interrupt is assigned high priority.
The following table describes the functions of each bit in the IP Register.
Bit Symbol Description
7 — —
6 — —
5 PT2 Reserved
4 PS Priority of Serial Port Interrupt
3 PT1 Priority of Timer 1 Overflow Interrupt
2 PX1 Priority of Ext. Interrupt 1
1 PT0 Priority of Timer 0 Overflow Interrupt
0 PX0 Priority of Ext. Interrupt 0
NOTE: The Interrupt Priority (IP) SFR is bit addressable.
In this tutorial, we have seen the 8051 Microcontroller Special Function Registers (SFRs), their
addresses, structures, reset values etc.
Immediate Addressing
In Immediate Addressing mode, the operand, which follows the Opcode, is a constant data of
either 8 or 16 bits. The name Immediate Addressing came from the fact that the constant data
to be stored in the memory immediately follows the Opcode.
The constant value to be stored is specified in the instruction itself rather than taking from a
register. The destination register to which the constant data must be copied should be the
same size as the operand mentioned in the instruction.
Example: MOV A, #030H
Here, the Accumulator is loaded with 30 (hexadecimal). The # in the operand indicates that it
is a data and not the address of a Register.
Immediate Addressing is very fast as the data to be loaded is given in the instruction itself.
Register Addressing
In the 8051 Microcontroller Memory Organization Tutorial, we have seen the organization of
RAM and four banks of Working Registers with eight Registers in each bank.
In Register Addressing mode, one of the eight registers (R0 – R7) is specified as Operand in
the Instruction.
It is important to select the appropriate Bank with the help of PSW Register. Let us see a
example of Register Addressing assuming that Bank0 is selected.
Example: MOV A, R5
Here, the 8-bit content of the Register R5 of Bank0 is moved to the Accumulator.
Direct Addressing
In Direct Addressing Mode, the address of the data is specified as the Operand in the
instruction. Using Direct Addressing Mode, we can access any register or on-chip variable.
This includes general purpose RAM, SFRs, I/O Ports, Control registers.
Example: MOV A, 47H
Here, the data in the RAM location 47H is moved to the Accumulator.
Mnemonic
INC Increment by 1
DEC Decrement by 1
MUL Multiply
DIV Divide
The arithmetic instructions have no knowledge about the data format i.e., signed, unsigned,
ASCII, BCD, etc. Also, the operations performed by the arithmetic instructions affect flags
like carry, overflow, zero, etc. in the PSW Register.
All the possible Mnemonics associated with Arithmetic Instructions are mentioned in the
following table.
Logical Instructions
The next group of instructions are the Logical Instructions, which perform logical operations
like AND, OR, XOR, NOT, Rotate, Clear and Swap. Logical Instruction are performed on
Bytes of data on a bit-by-bit basis.
Mnemonics associated with Logical Instructions are as follows:
•
Mnemonic Description
ORL Logical OR
XRL Ex-OR
The following table shows all the possible Mnemonics of the Logical Instructions.
Boolean or Bit Manipulation Instructions
As the name suggests, Boolean or Bit Manipulation Instructions deal with bit variables. We
know that there is a special bit-addressable area in the RAM and some of the Special
Function Registers (SFRs) are also bit addressable.
The Mnemonics corresponding to the Boolean or Bit Manipulation instructions are:
Mnemonic Description
JBC Jump if specified Bit is Set and also clear the Bit
ORL Bitwise OR
These instructions can perform set, clear, and, or, complement etc. at bit level. All the
possible mnemonics of the Boolean Instructions are specified in the following table.
Program Branching Instructions
The last group of instructions in the 8051 Microcontroller Instruction Set are the Program
Branching Instructions. These instructions control the flow of program logic. The mnemonics
of the Program Branching Instructions are as follows.
Mnemonic Description
LJMP Long Jump (Unconditional)
AJMP Absolute Jump (Unconditional)
SJMP Short Jump (Unconditional)
JZ Jump if A is equal to 0
JNZ Jump if A is not equal to 0
CJNE Compare and Jump if Not Equal
DJNZ Decrement and Jump if Not Zero
NOP No Operation
LCALL Long Call to Subroutine
ACALL Absolute Call to Subroutine (Unconditional)
RET Return from Subroutine
RETI Return from Interrupt
JMP Jump to an Address (Unconditional)
All these instructions, except the NOP (No Operation) affect the Program Counter (PC) in
one way or other. Some of these instructions has decision making capability before
transferring control to other part of the program.
The following table shows all the mnemonics with respect to the program branching
instructions.
Basic Programming in 8051
Solution: – here one is internal memory location and other is memory external location. so first
the content of ext memory location FF00h is loaded in acc. then the content of int memory
location FFh is saved first and then content of acc is transferred to FFh. now saved content of
RET
Solution: –first we shall get the upper nibble of r7 in r6. Then we swap nibbles of r7 and
make OR operation with r6 so the upper and lower nibbles are duplicated
Mov a, r7 ; get the content in acc
Orl a, r6 ; OR operation
RET
Statement 3: – treat r6-r7 and r4-r5 as two 16 bit registers. Perform subtraction between
them. Store the result in 20h (lower byte) and 21h (higher byte).
Solution: – first we shall clear the carry. Then subtract the lower bytes afterward then
RET
Statement 4: – divide the content of r0 by r1. Store the result in r2 (answer) and r3
Solution:-after getting answer to restore original content we have to multiply answer with
Div ab ; divide A by B
RET
Statement 5: – transfer the block of data from 20h to 30h to external location 1020h to
1030h.
Solution: – here we have to transfer 10 data bytes from internal to external RAM. So first,
we need one counter. Then we need two pointers one for source second for destination.
Mov r7, #0Ah ; initialize counter by 10d
Djnz r7, nxt ; decrease r7. if zero then over otherwise move next
RET
Statement 6: – find out how many equal bytes between two memory blocks 10h to 20h and
20h to 30h.
Solution: – here we shall compare each byte one by one from both blocks. Increase the count
Inc r1
djnz r7, nxt ; decrease r7. if zero then over otherwise move next
RET
Statement 7: – given block of 100h to 200h. Find out how many bytes from
this block are greater then the number in r2 and less then number in r3. Store
the count in r4.
Solution: – in this program, we shall take each byte one by one from given
block. Now here two limits are given higher limit in r3 and lower limit in r2.
So we check first higher limit andthen lower limit if the byte is in between
1. I/O Programming
In 8051, I/O operations are done using four ports and 40 pins. The following pin diagram
shows the details of the 40 pins. I/O operation port reserves 32 pins where each port has 8 pins.
The other 8 pins are designated as Vcc, GND, XTAL1, XTAL2, RST, EA (bar), ALE/PROG (bar),
and PSEN (bar).It is a 40 Pin PDIP (Plastic Dual Inline Package)
The four ports P0, P1, P2, and P3, each use 8 pins, making them 8-bit ports. Upon RESET,
all the ports are configured as inputs, ready to be used as input ports. When the first 0 is written to
a port, it becomes an output. To reconfigure it as an input, a 1 must be sent to a port.
Port 0 (Pin No 32 – Pin No 39)
It has 8 pins (32 to 39). It can be used for input or output. Unlike P1, P2, and P3 ports, we
normally connect P0 to 10K-ohm pull-up resistors to use it as an input or output port being an open
drain.
It is also designated as AD0-AD7, allowing it to be used as both address and data. In case
of 8031 (i.e. ROMless Chip), when we need to access the external ROM, then P0 will be used for
both Address and Data Bus. ALE (Pin no 31) indicates if P0 has address or data. When ALE = 0,
it provides data D0-D7, but when ALE = 1, it has address A0-A7. In case no external memory
connection is available, P0 must be connected externally to a 10K-ohm pull-up resistor.
Port 0 is also designated as AD0–AD7, as it can be used for both data and address handling.
While connecting an 8051 to external memory, Port 0 can provide both address and data. The 8051
microcontroller then multiplexes the input as address or data in order to save pins.
Port 1 (Pin 1 through 8)
It is an 8-bit port (pin 1 through 8) and can be used either as input or output. It doesn't
require pull-up resistors because they are already connected internally. Upon reset, Port 1 is
configured as an input port. The following code can be used to send alternating values of 55H and
AAH to Port 1.
Toggle all bits of continuously
MOV A,#55
BACK: MOV P2,A
ACALL DELAY
CPL A //complement(invert) reg. A
SJMP BACK
If Port 1 is configured to be used as an output port, then to use it as an input port again, program it
by writing 1 to all of its bits as in the following code.
MOV A ,#0FFH //A = FF hex
MOV P1,A //Make P1 an input port
MOV A,P1 //get data from P1
MOV R7,A //save it in Reg R7
ACALL DELAY //wait
Port 2 occupies a total of 8 pins (pins 21 through 28) and can be used for both input and
output operations. Just as P1 (Port 1), P2 also doesn't require external Pull-up resistors because
they are already connected internally. It must be used along with P0 to provide the 16-bit address
for the external memory. So it is also designated as (A0–A7), as shown in the pin diagram. When
the 8051 is connected to an external memory, it provides path for upper 8-bits of 16-bits address,
and it cannot be used as I/O. Upon reset, Port 2 is configured as an input port. The following code
can be used to send alternating values of 55H and AAH to port 2.
//Toggle all bits of continuously
MOV A,#55
BACK: MOV P2,A
ACALL DELAY
CPL A // complement(invert) reg. A
SJMP BACK
If Port 2 is configured to be used as an output port, then to use it as an input port again, program it
by writing 1 to all of its bits as in the following code.
//Get a byte from P2 and send it to P1
MOV A,#0FFH //A = FF hex
MOV P2,A //make P2 an input port
BACK: MOV A,P2 //get data from P2
MOV P1,A //send it to Port 1
SJMP BACK //keep doing that
Dual role of Port 2
Besides working as I/O, Port P2 is also used to provide 16-bit address bus for external
memory along with Port 0. Port P2 is also designated as (A8– A15), while Port 0 provides the
lower 8-bits via A0–A7. In other words, we can say that when an 8051 is connected to an external
memory (ROM) which can be maximum up to 64KB and this is possible by 16 bit address bus
because we know 216 = 64KB. Port2 is used for the upper 8-bit of the 16 bits address, and it cannot
be used for I/O and this is the way any Program code of external ROM is addressed.
Port 3 (Pins 10 through 17)
It is also of 8 bits and can be used as Input/Output. This port provides some extremely
important signals. P3.0 and P3.1 are RxD (Receiver) and TxD (Transmitter) respectively and are
collectively used for Serial Communication. P3.2 and P3.3 pins are used for external interrupts.
P3.4 and P3.5 are used for timers T0 and T1 respectively. P3.6 and P3.7 are Write (WR) and Read
(RD) pins. These are active low pins, means they will be active when 0 is given to them and these
are used to provide Read and Write operations to External ROM in 8031 based systems.
Fig(1)
In data transmission if the data can be transmitted and received, it is a duplex transmission.
This is in contrast to simplex transmission such as with printers, in which the computer only sends
data. Duplex transmissions can be half or full duplex, depending on whether or not the data transfer
can be simultaneous. If data is transmitted one way at a time, it is referred to as half duplex. If the
data can go both ways at the same time, it is full duplex. Full duplex requires two wire conductors
for the data lines, one for transmission and one for reception, in order to transfer and receive data
simultaneously.
The data coming in at the receiving end of the data line in a serial data transfer is all 0s and
1s, it is difficult to make sense of the data unless the sender and receiver agree on a set of rules, a
protocol, on how the data is packed, how many bits constitute a character, and when the data begins
and ends.
Asynchronous serial data communication is widely used for character oriented
transmissions. In this method, each character is placed between start and stop bits. This is called
framing. In data framing for asynchronous communications, the data such as ASCII characters,
are packed between a start bit and stop bit. The start bit is always one bit, but the stop bit can be
one or two bits. The start bit is always a 0 (low) and the stop bit is 1(high). Look at below figure
in which the ASCII character “A” ( 8-bit binary 0100 0001) is framed between the start bit and a
stop bit. Notice that the LSB is sent out first.
Figure(2)
Data Transfer Rate
The rate of data transfer in serial data communication is stated in bps (bits per
second). Another widely used terminology for bps is baud rate. The baud rate is the modem
terminology and s defined as the number of signal changes per second.
The 8051 has two timers; timer 0, timer 1. They can be used either as timers or as event
counters. 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.
TIMER 0 registers
The 16 bit register of timer 0 is accessed as low byte and high byte. The low byte register is
called TL0 (timer 0 low byte) and the high byte register is referred to as TH0 ( timer 0 high byte).
These registers can be accessed like any other register, such as A, B, R0, R1, R2 etc. For example,
the instruction “ MOV TL0,#25H” loads the value 25H into TL0.
TIMER 1 registers
Timer 1 is also 16 bits, and its 16 bit register is split into two bytes, referred to as TL1(timer
1 low byte) and TH1 (timer 1 high byte). These registers are accessible in the same way as the registers
of timer 0.
TMOD (Timer Mode) Register
Both timers 0 and 1 use the same register, called TMOD, to set th various timer operation
modes. TMOD is an 8-bit register in which the lower 4 bits are set aside for timer 0 and the
upper4 bits are set aside for timer 1. In each case, the lower 2 bits are used to set the timer mode and
the upper 2 bits to specify the operation. TMOD register is shown in fig(3).
GATE: The T MOD register of Fig(3) that both timers 0 and 1 have the GATE bit. Every
timerhas means of starting and stopping. Some timers do this by software, some by hardware, and
some both software and hardware controls. The timers in the 8051 have both. The start and stop of the
timer are controlled by way of software by the TR (timer start) bits TR0 and TR1. This is achieved
by the instructions “SETB TR1” and “CLR T R1” for timer 1 and “SETB TR0” and “CLR TR0”
for time 0. The SETB instruction starts it, and it is stopped by the CLR instruction. These instructions
start and stop the timers as long as GATE=0 in the TMOD register.
M1, M0: M0 and M1 select the timer mode. As show in the below Table, there are three modes;
0, 1, and 2. Mode 0 is a 13 bit timer, mode 1 is a 16 bit timer and mode 2 is an 8-bit timer.
M1 M2 MODE
0 0 0
0 1 1
1 0 2
1 1 3
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.
Timer Programming
Mode 1 Programming
1. It is a 16-bit timer, therefore it allows values of 0000 to FFFFH to be loaded into the
timer’s registers TL and TH.
2. After TH and TL are loaded with a 16-bit initial value, the timer must be started. This is
done by “SETB TR0” for Timer 0 and “SETB TR1” for Timer 1.
3. After the timer is started, it starts to count up. It counts up until it reaches its limit of
FFFFH. When it rolls over from FFFFH to 0000, it sets high flag bit called TF (timer flag).
This timer flag can be monitered. When this timer flag is raised, one option would be to
stop the timer with the instructions “CLR TR0” for Timer 0 and “CLR TR1” for Timer 1.
Notice that each timer has its own timer flag: TF0 for Timer 0 and TF1 for Timer 1.
4. After the timer reaches its limit and rolls over, in order to repeat the process the registers
TH and TL must be reloaded with the original value, and TF must be reset to ‘0’.
1. Load the TMOD value register indicating which timer (Timer 0 or Timer 1) is to be used
and which timer mode ( 0 or 1 ) is selected.
2. Load registers TL and TH with initial count values
3. Start the timer.
4. Keep monitoring the timer flag (TF). Get out of the loop when TF becomes high
5. Stop the timer.
6. Clear the TF flag for the next round.
7. Go back to Step 2 to load TH and TL again.
Example
1. It is an 8-bit timer, therefore, it allows only values of 00 to FFH to be loaded into the
timer’s register TH.
2. After TH is loaded with the 8-bit value, the 8051 gives a copy of it to TL. Then the timer must
be started. This is done by the instruction “SETB TR0” for Timer 0 and “SETB TR1” for
Timer 1.
3. After the timer is started, it starts to count up by incrementing the TL register. It counts up
until it reaches its limit of FFH. When it rolls over from FFH to 00, it sets high the TF (Timer
Flag). If we are using Timer 0, TF0 goes high. If we are using Timer 1, TF1 is raised.
4. When the TL register rolls from FFH to 00 and TF is set to 1, TL is reloaded automatically
with the original value kept by the TH register. To repeat the process, we must simply clear
TF and let it go without any need by the programmer to reload the original value. This makes
mode 2 an auto-reload, in contrast with mode 1 in which the programmer has to reload TH
and TL
It must be emphasized that mode 2 is an 8-bit timer. However, it has an auto- reloading
capability. In auto-reload, TH is loaded with the initial count and a copy of it is given to TL.
This reloading leaves TH unchanged, still holding a copy of the original value. This mode has
many applications, including setting the baud rate in serial communication.
To generate the time delay using the timer’s mode 2, take the following steps.
1. Load the TMOD value register indicating which timer (Timer 0 or Timer 1) is to be used,
and select the timer mode (mode 2)
2. Load the TH registers with the initial count value
3. Start the timer.
4. Keep monitoring the timer flag (TF) with the “JNB TF0, Target” or “JNB TF1, Target”
instruction to see whether it is raised. Get out of the loop when TF goes high.
5. Clear the TF flag.
6. Go back to step 4, since mode 2 is auto-reload.
Program: Assembly Language Program to generate the square on pin P1.0, assuming
XTAL = 11.0592 MHz.
MOV TMOD, #20H ; T1/ mode 2/ 8-bit/ auto-reload
MOV TH1, #05H ; TH1 = 05
SETB TR1 ; Start Timer 1
BACK: JNB TF1, BACK ; stay until timer rolls over
CPL P1.0 ; complement P1.0 to get high, low
CLR TF1 ; clear Timer 1 flag
SJMP BACK ; mode 2 is auto-reload
COUNTER PROGRAMMING
The timer / counter of the 8051 is used to generate time delays. These timers can also be used
as counters counting events happening outside the 8051. As far as the use of a timer as an
event counter is concerned, everything that in programming the timer applies to programming
it as a counter, except tha source of the frequency. When the timer/counter is used as a timer,
the 8051’s crystal is used as the source of the frequency. When it is used as a counter, it is a
pulse outside the 8051 that increments the TH, TL register.
(MSB) (LSB)
GATE C/T M1 M2 GATE C/T M1 M2
Timer 1 Timer 0
Fig:TMOD register
TCON REGISTER
TCON register is an 8-bit register. As shown in above figure the upper four bits are used to store
the TF and TR bits of both Timer 0 and Timer 1. The lower four bits are set aside for controlling
the interrupt bits. TCON is a bit addressable register.
4. INTERRUPTS
An interrupt is an internal or external event that interrupts the microcontroller to inform it
that a device needs its service. Whenever any device needs its service, the device notifies
the microcontroller by sending it as interrupt signal. Upon receiving an interrupt signal, the
microcontroller interrupts whatever it is doing and serves the device. The program which
is associated with the interrupt is called interrupt Service Routine (ISR). The
microcontroller can serve many devices based on the priority assigned to it.
Execution of an Interrupt
In order to use any interrupt, the following steps must be taken.
1. It finishes the instruction it is executing and saves the address of the next instruction
(PC) on the stack.
2. It also saves the current status of all the interrupts internally.
3. It jumps to a fixed location in memory called the interrupt vector or table that holds the
address of the Interrupt Service Routine (ISR).
4. The microcontroller gets the address of the ISR from the interrupt vector table and jumps
to it. It starts to execute the interrupt service subroutine until it reaches the last instruction
of the subroutine which is RET 1.
5. Upon executing RET 1 instruction, the microcontroller returns to the place where it was
interrupted. First it gets the program counter (PC) address from the stack by popping the
top two byes of the stack into the PC. Thenit startsto execute from that address.
Figure 8: IE Register