Notes Unit 1
Notes Unit 1
UNIT - 1
8-BIT EMBEDDED PROCESSOR
▪ 8-Bit Microcontroller
▪ Architecture
▪ Programming Parallel Ports
▪ Timers and Serial Port
▪ Interrupt Handling.
1.1. 8051 Microcontroller
8051 microcontroller is an 8-bit microcontroller created in 1981 by Intel Corporation. It has an
8- bit processor that simply means that it operates on 8-bit data at a time. It is among the most
popular and commonly used microcontroller.
As it is an 8-bit microcontroller thus has 8-bit data bus, 16-bit address bus. Along with that, it
holds 4 KB ROM with 128 bytes RAM.
Any programmable device holds a processor, memory, I/O ports and timer within it. But a
microcontroller contains all these components embedded in a single chip. This single-chip
manages the overall operation of the device.
A microprocessor simply contains a CPU that processes the operations with the help of other
peripheral units. Microprocessors are used where huge space is present to inbuilt a large
motherboard like in PCs.
1.3.1. Central processing unit (CPU): 8051 uses the 8-bit processor. This unit carries out the
operation on 8-bit data. A processor is the heart of microcontroller. As the execution of the
program stored in the memory is performed by the processor.
The unit performs arithmetic and logical operations on 8-bit data as it has ALU, with internal
registers and program counters.
Several logical operations are performed by the ALU according to the program stored in the
memory.
The processor of 8051 microcontrollers possesses a special feature by which it can process
single bit or 8-bit data. This simply means that it has the ability to access each single bit data
either to clear, set or move etc. for any logical computation.
Architecture of 8051 Microcontroller
1.3.2. Memory: Basically 8051 microcontroller consists of on-chip program memory i.e., ROM
and on-chip data memory i.e., RAM.
Let us first understand
• ROM
8051 microcontroller has 4 KB ROM with 0000H to 0FFFH as the addressable space. It is
completely a program or code memory that means used by the programmer to store the
programs that are to be executed by the microcontroller.
• The operations that are executed by the device in which the microcontroller is present are
stored in the ROM of the memory at the time of fabrication. Hence cannot be changed or
modified.
• RAM
8051 holds a 128 bytes RAM. Basically, RAM is used to store data or operands for only a
small time duration. It can be altered anytime according to the need of the user. It is also
known as the data memory as it stores the data temporarily.
•
Out of the 128-byte RAM, first, 32 bytes is held by the working registers. Basically, these are
4 banks which separately has 8 registers. These registers are accessed either by its name or
address. It is to be noted here that at a particular time only a single register bank can be used.
• As in 8051, the data and program memory i.e., RAM and ROM hold a definite memory
space. However, for some applications there exist the need for external memory to enhance
the memory space, thus external RAM, ROM/EPROM is used by the 8051 microcontrollers.
1.3.3. Input/ Output port: 8051 consists of 4 parallel ports of 8 bit each thereby providing 32
input-output pins. All the 4 ports function bidirectional i.e., either input or output according to
the software control.
1.3.4. Timer and Control Unit: Timers are used to create a time gap or delay between 2
events. 8051 microcontroller consists of 2 timers of 16 bit each by which the system can produce
two delays simultaneously in order to generate the appropriate delay.
1.3.6. The Data Pointer (DPTR) is the 8051's only user-accessible 16-bit (2-byte) register. The
Accumulator, R0–R7 registers registers.
Basically, microcontrollers use hardware delays in which a physical device is used by the
processor to produce the respective delay. And this physical device is known as a timer.
The timer produces the delay according to the demand of the processor and sends the signal to
the processor once the respective delay gets produced.
1. An 8-bit processor.
2. Data memory or RAM of 128 bytes.
3. Program memory or ROM of 4 KB.
4. 2 timers of 16 bit each.
5. 8-bit data bus.
6. 16-bit address bus.
7. Offers bit addressable format.
8. Special function registers and serial port.
9. 32 input/output lines.
Microcontrollers or processors can understand only binary language in the form of ‘0s or 1s’; An
assembler converts the assembly language to binary language, and then stores it in the
microcontroller memory to perform the specific task.
The 8051 microcontroller is the CISC based Harvard architecture, and it has peripherals like 32
I/O, timers/counters, serial communication and memories. The microcontroller requires a
program to perform the operations that require a memory for saving and to read the functions.
The 8051 microcontroller consists of RAM and ROM memories to store instructions.
A Register is the main part in the processors and microcontrollers which is contained in the
memory that provides a faster way of collecting and storing the data. The 8051 assembly
language programming is based on the memory registers. If we want to manipulate data to a
processor or controller by performing subtraction, addition, etc., we cannot do that directly in the
memory, but it needs registers to process and to store the data. Microcontrollers contain several
types of registers that can be classified according to their instructions or content that operate in
them.
The assembly language is made up of elements which all are used to write the program in
sequential manner. Follow the given rules to write programming in assembly language.
Op-code: The op-code is a single instruction that can be executed by the CPU. Here the op-code
is a MOV instruction.
Operands: The operands are a single piece of data that can be operated by the op-code.
Example, multiplication operation is performed by the operands that are multiplied by the
operand.
• Assembler Directives
• Instruction Set
• Addressing Modes
ORG(origin): This directive indicates the start of the program. This is used to set the register
address during assembly. For example; ORG 0000h tells the compiler all subsequent code
starting at address 0000h.
Syntax: ORG 0000h
(define byte): The define byte is used to allow a string of bytes. For example, print the
“EDGEFX” wherein each character is taken by the address and finally prints the “string” by the
DB directly with double quotes.
Syntax:
ORG 0000h
MOV a, #00h
————-
————-
DB”EDGEFX”
Syntax:
reg equ,09h
—————–
—————–
MOV reg,#2h
The above instruction and its execution. The opcode 74H is saved at 0202 address. The data
6AH is saved at 0203 address in the program memory. After reading the opcode 74H, the data at
the next program memory address is transferred to accumulator A (E0H is the address of
accumulator). Since the instruction is of 2-bytes and is executed in one cycle, the program
counter will be incremented by 2 and will point to 0204 of the program memory.
Note − The '#' symbol before 6AH indicates that the operand is a data (8 bit). In the absence of
'#', the hexadecimal number would be taken as an address.
Direct Addressing Mode
This is another way of addressing an operand. Here, the address of the data (source data) is given
as an operand. Let’s take an example.
MOV A, 04H
The register bank#0 (4th register) has the address 04H. When the MOV instruction is executed,
the data stored in register 04H is moved to the accumulator. As the register 04H holds the data
1FH, 1FH is moved to the accumulator.
Note − We have not used '#' in direct addressing mode, unlike immediate mode. If we had used
'#', the data value 04H would have been transferred to the accumulator instead of 1FH.
Now, take a look at the following illustration. It shows how the instruction gets executed.
As shown in the above illustration, this is a 2-byte instruction which requires 1 cycle to
complete. The PC will be incremented by 2 and will point to 0204. The opcode for the
instruction MOV A, address is E5H. When the instruction at 0202 is executed (E5H), the
accumulator is made active and ready to receive data. Then the PC goes to the next address as
0203 and looks up the address of the location of 04H where the source data (to be transferred to
accumulator) is located. At 04H, the control finds the data 1F and transfers it to the accumulator
and hence the execution is completed.
Register Direct Addressing Mode
In this addressing mode, we use the register name directly (as source operand). Let us try to
understand with the help of an example.
MOV A, R4
At a time, the registers can take values from R0 to R7. There are 32 such registers. In order to
use 32 registers with just 8 variables to address registers, register banks are used. There are 4
register banks named from 0 to 3. Each bank comprises of 8 registers named from R0 to R7.
At a time, a single register bank can be selected. Selection of a register bank is made possible
through a Special Function Register (SFR) named Processor Status Word (PSW). PSW is an
8-bit SFR where each bit can be programmed as required. Bits are designated from PSW.0 to
PSW.7. PSW.3 and PSW.4 are used to select register banks.
Now, take a look at the following illustration to get a clear understanding of how it works.
Opcode EC is used for MOV A, R4. The opcode is stored at the address 0202 and when it is
executed, the control goes directly to R4 of the respected register bank (that is selected in PSW).
If register bank #0 is selected, then the data from R4 of register bank #0 will be moved to the
accumulator. Here 2F is stored at 04H. 04H represents the address of R4 of register bank #0.
Data (2F) movement is highlighted in bold. 2F is getting transferred to the accumulator from
data memory location 0C H and is shown as dotted line. 0CH is the address location of Register
4 (R4) of register bank #1. The instruction above is 1 byte and requires 1 cycle for complete
execution. What it means is, you can save program memory by using register direct addressing
mode.
Register Indirect Addressing Mode
In this addressing mode, the address of the data is stored in the register as
operand. MOV A, @R0
Here the value inside R0 is considered as an address, which holds the data to be transferred to the
accumulator. Example: If R0 has the value 20H, and data 2FH is stored at the address 20H, then
the value 2FH will get transferred to the accumulator after executing this instruction. See the
following illustration.
So the opcode for MOV A, @R0 is E6H. Assuming that the register bank #0 is selected, the R0
of register bank #0 holds the data 20H. Program control moves to 20H where it locates the data
2FH and it transfers 2FH to the accumulator. This is a 1-byte instruction and the program counter
increments by 1 and moves to 0203 of the program memory.
Note − Only R0 and R1 are allowed to form a register indirect addressing instruction. In other
words, the programmer can create an instruction either using @R0 or @R1. All register banks
are allowed.
Indexed Addressing Mode
We will take two examples to understand the concept of indexed addressing mode. Take a look
at the following instructions −
MOVC A, @A+DPTR
and
MOVC A, @A+PC
where DPTR is the data pointer and PC is the program counter (both are 16-bit registers).
Consider the first example.
MOVC A, @A+DPTR
The source operand is @A+DPTR. It contains the source data from this location. Here we are
adding the contents of DPTR with the current content of the accumulator. This addition will give
a new address which is the address of the source data. The data pointed by this address is then
transferred to the accumulator.
The opcode is 93H. DPTR has the value 01FE, where 01 is located in DPH (higher 8 bits) and
FE is located in DPL (lower 8 bits). Accumulator has the value 02H. Then a 16-bit addition is
performed and 01FE H+02H results in 0200 H. Data at the location 0200H will get transferred to
the accumulator. The previous value inside the accumulator (02H) will be replaced with the new
data from 0200H. The new data in the accumulator is highlighted in the illustration.
This is a 1-byte instruction with 2 cycles needed for execution and the execution time required
for this instruction is high compared to previous instructions (which were all 1 cycle each).
The other example MOVC A, @A+PC works the same way as the above example. Instead of
adding DPTR with the accumulator, here the data inside the program counter (PC) is added with
the accumulator to obtain the target address.
1.4.4.3. Instruction Set
it is not zero, it jumps to the target address referred to by the label. Prior to the start of the loop
the register is loaded with the counter for the number of repetitions. Notice that in this
instruction both the register decrement and the decision to jump are combined into a single
instruction.
In the program in Example, the R2 register is used as a counter. The counter is first set to 10. In
each iteration the instruction DJNZ decrements R2 and checks its value. If R2 is not zero, it
jumps to the target address associated with the label “AGAIN”. This looping action continues
until R2 becomes zero. After R2 becomes zero, it falls through the loop and executes the
instruction immediately below it, in this case the “MOV R5 , A” instruction. Notice in the DJNZ
instruction that the registers can be any of RO – R7. The counter can also be a RAM location
Example
Write a program to (a) load the accumulator with the value 55H, and (b) complement the ACC
700 times.
Solution:
Since 700 is larger than 255 (the maximum capacity of any register), we use two registers to hold
the count. The following code shows how to use R2 and R3 for the count.
In this program, R2 is used to keep the inner loop count. In the instruction “DJNZ R2 , AGAIN”,
whenever R2 becomes 0 it falls through and “DJNZ R3 , NEXT” is executed. This instruction
forces the CPU to load R2 with the count 70 and the inner loop starts again. This process will
continue until R3 becomes zero and the outer loop is finished.
JZ (jump if A = 0)
In this instruction the content of register A is checked. If it is zero, it jumps to the target
address. For example, look at the following code.
In this program,. if either RO or Rl is zero, it jumps to the label OVER. Notice that the JZ
instruction can be used only for register A. It can only check to see whether the accumulator is
zero, and it does not apply to any other register. More importantly, you don’t have to perform an
arithmetic instruction such as decrement to use the JNZ instruction. See Example 3-4.
Example
Shifting Operators
The shift operators are used for sending and receiving the data efficiently. The
8051 microcontroller consist four shift operators:
• RR —> Rotate Right
• RRC —>Rotate Right through carry
• RL —> Rotate Left
• RLC —>Rotate Left through carry
Syntax:
MOV A,
#25h RR A
Syntax:
MOV A,
#25h RL A
RRC Rotate Right through Carry:
In this shifting operation, the LSB moves to carry and the carry becomes MSB, and all the bits
are shift towards right side bit by bit position.
Syntax:
MOV A, #27h
RRC A
Syntax:
MOV A, #27h
RLC A
The microcontroller programming differs for each type of operating system. There are many
operating systems such as Linux, Windows, RTOS and so on.
1.4.5. Example Program for LED blinking using with 8051 microcontroller:
• Number Displaying on 7-segment display using 8051 microcontroller
• Timer/Counter calculations and program using 8051 microcontroller
• Serial Communication calculations and program using 8051 microcontroller
There are four ports P0, P1, 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 P0 is an open drain, unlike P1, P2, and P3.Open drain is a term used
for MOS chips in the same way that open collector is used for TTL chips. 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 55H and AAH
MOV A,#55H
BACK: MOV P0,A
ACALL DELAY
CPL A
SJMP BAC
Port 0 as Input : With resistors connected to port 0, in order to make it an input, the port must be programmed
by writing 1 to all the bits. In the following code, port 0 is configured first as an input port by writing 1’s to it,
and then data is received from the port and sent to P1.
Port 1: Port 1 occupies a total of 8 pins (pins 1 through 8). It can be used as input or output. In contrast to
port 0, this port does not need any pull-up resistors since it already has pull-up resistors internally. Upon
reset, Port 1 is configured as an output port. For example, the following code will continuously send out
to port1 the alternating values 55h & AAh
Port 1 as input: To make port1 an input port, it must programmed as such by writing 1 to all its bits. In
the following code port1 is configured first as an input port by writing 1’s to it, then data is received from
the port and saved in R7 ,R6 & R5
Port 2 : Port 2 occupies a total of 8 pins (pins 21- 28). It can be used as input or output. Just like P1, P2
does not need any pull-up resistors since it already has pull-up resistors internally. Upon reset,Port 2 is
configured as an output port. For example, the following code will send out continuously to port 2 the
alternating values 55h and AAH. That is all the bits of port 2 toggle continuously.
Dual role of port 2 : In systems based on the 8751, 8951, and DS5000, P2 is used as simple I/O.
However, in 8031-based systems, port 2 must be used along with P0 to provide the 16-bit address for the
external memory. As shown in pin configuration 8051, port 2 is also designed as A8-A15, indicating the
dual function. Since an 8031 is capable of accessing 64K bytes of external memory, it needs a path for the
16 bits of the address. While P0 provides the lower 8 bits via A0-A7, it is the job of P2 to provide bits
A8-A15 of the address. In other words, when 8031 is connected to external memory, P2 is used for the
upper 8 bits of the 16 bit address, and it cannot be used for I/O.
Port 3 : Port 3 occupies a total of 8 pins, pins 10 through 17. It can be used as input or output. P3 does
not need any pull-up resistors, the same as P1 and P2 did not. Although port 3 is configured as an output
port upon reset. Port 3 has the additional function of providing some extremely important signals such as
interrupts. This information applies both 8051 and 8031 chips.
Read-modify-write feature : The ports in the 8051 can be accessed by the read-modify-write technique.
This feature saves many lines of code by combining in a single instruction all three action of (1) reading
the port, (2) modifying it, and (3) writing to the port. The following code first places 01010101 (binary)
into port 1. Next, the instruction “XLR P1,#0FFH” performs an XOR logic operation on P1 with 1111
1111 (binary), and then writes the result back into P1.
1.5.3. Addition of two 8-bit numbers in 8051 Microcontroller Using Ports Introduction:
To perform addition of two 8-bit numbers using ports in 8051 microcontroller, we need to
connect the two 8-bit numbers to be added to two ports of the microcontroller. We can use any
two ports of the microcontroller, for example, P1 and P2.
The first step is to load the two numbers into two different ports. For example, we can
load the first number into port P1 and the second number into port P2. We can use the MOV
instruction to load the numbers into the ports.
After the addition is complete, we can retrieve the result from the accumulator and store
it in another port or memory location for further processing or display. 8051 microcontroller is a
microcontroller designed by Intel in 1981. It is an 8-bit microcontroller with 40 pins DIP (dual
inline package), 4kb of ROM storage and 128 bytes of RAM storage, 16-bit timers. It consists of
four parallel 8-bit ports, which are programmable as well as addressable as per the requirement.
Problem: To write an assembly language program to add two 8 bit numbers in 8051 microcontroller using
ports.
Example:
Block diagram
Algorithm:
• Initialize Ports P0 and P1 as input ports.
• Initialize Ports P2 and P3 as output ports.
• Initialize the R1 register.
• If carry is present increment R1.
• Move contents in R1 to Port 2.
• Move the sum in step 6 to Port 3.
Program:
ORG 00H // Indicates starting address
Explanation:
• ORG 00H is the starting address of the program.
• Giving the values as #0FFH and #00H initializes the ports as input and output ports
respectively.
• R1 register is initialized to 0 so as to store any carry produced during the sum.
• MOV B, P0 moves the value present in P0 to the B register.
• MOV A, P1 moves the value present in P1 to Accumulator.
• ADD AB adds the values present in Accumulator and B register and stores the result in
Accumulator.
• JNC L2 refers to jump to label L2 if no carry is present by automatically checking whether
the carry bit is set or not.
• If the carry bit is set to increment register R1.
• MOV P2, R1, and MOV P3, A refers to moving the carry bit to P2 and result in Accumulator
to P3.
1.6. 8051 Timers
1.6.1. Introduction to 8051 Timers
8051 microcontrollers have two timers and counters which work on the clock
frequency. Timer/counter can be used for time delay generation, counting external events, etc.
8051 Clock
Every Timer needs a clock to work, and 8051 provides it from an external crystal which is the
main clock source for Timer. The internal circuitry in the 8051 microcontrollers provides a clock
source to the timers which is 1/12th of the frequency of crystal attached to the microcontroller,
also called Machine cycle frequency.
8051 has a Timer Mode Register and Timer Control Register for selecting a mode of operation
and controlling purpose.
Let's see these registers,
1.6.2.1. TMOD register
TMOD is an 8-bit register used to set timer mode of timer0 and timer1.
Its lower 4 bits are used for Timer0 and the upper 4 bits are used for Timer1
Bit 7,3 – GATE:
1 = Enable Timer/Counter only when the INT0/INT1 pin is high and TR0/TR1 is set.
0 = Enable Timer/Counter when TR0/TR1 is set.
Bit 6,2 - C/ (Counter/Timer): Timer or Counter select bit
1 = Use as Counter
0 = Use as Timer
M1 M0 Mode Operation
0 0 0 (13-bit timer mode) 13-bit timer/counter, 8-bit of THx & 5-bit of TLx
0 1 1 (16-bit timer mode) 16-bit timer/counter, THx cascaded with TLx
8-bit timer/counter (auto-reload mode), TLx reload
1 0 2 (8-bit auto-reload mode)
with the value held by THx each time TLx overflow
Split the 16-bit timer into two 8-bit timers i.e. THx
1 1 3 (split timer mode)
and TLx like two 8-bit timer
TCON is an 8-bit control register and contains a timer and interrupt flags.
Bit 7 - TF1: Timer1 Overflow Flag
1 = Timer1 overflow occurred (i.e. Timer1 goes to its max and roll over back to zero).
0 = Timer1 overflow not occurred.
It is cleared through software. In the Timer1 overflow interrupt service routine, this bit will get
cleared automatically while exiting from ISR.
In this example the Timer1is selected, in this case, every 32 (25)event for counter operations or
32 machine cycles for timer operation, the TH1 register will be incremented by 1. When the
TH1overflows from FFH to 00H, then the TF1 of TCON register will be high, and it stops the
timer/counter. So for an example, we can say that if the TH1 is holding F0H, and it is in timer
mode, then TF1will be high after 10H * 32 = 512 machine cycles.
MOVTMOD,
#00H MOVTH1,
#0F0H MOVIE,
#88H SETB TR1
In the above program, the Timer1 is configured as timer mode 0. In this case Gate = 0. Then the
TH1 will be loaded with F0H, then enable the Timer1 interrupt. At last set the TR1 of TCON
register, and start the timer.
Mode1 (16-bit timer mode)
The Mode 1 operation is the 16-bit timer or counter. In the following diagram, we are using
Mode 1 for Timer0.
In this case every event for counter operations or machine cycles for timer operation, the TH0–
TL0 register-pair will be incremented by 1. When the register pair overflows from FFFFH to
0000H, then the TF0 of TCON register will be high, and it stops the timer/counter. So for an
example, we can say that if the TH0 – TL0 register pair is holding FFF0H, and it is in timer
mode, then TF0 will be high after 10H = 16 machine cycles. When the clock frequency is
12MHz, then the following instructions generate an interrupt 16 µs after Timer0 starts running.
MOVTMOD, #01H MOVTL0, #0F0H
MOVTH0, #0FFH MOVIE, #82H SETB TR0
In the above program, the Timer0 is configured as timer mode 1. In this case Gate = 0. Then the
TL0 will be loaded with F0H and TH0 is loaded with FFH, then enable the Timer0 interrupt. At
last set the TR0 of TCON register, and start the timer.
Mode2 (8-bit auto-reload timer mode)
The Mode 2 operation is the 8-bit auto reload timer or counter. In the following diagram, we are
using Mode 2 for Timer1.
In this case every event for counter operations or machine cycles for timer operation, the
TL1register will be incremented by 1. When the register pair overflows from FFH to 00H, then
the TF1 of TCON register will be high, also theTL1 will be reloaded with the content of TH1
and starts the operation again.
So for an example, we can say that if the TH1 and TL1 register both are holding F0H and it is in
timer mode, then TF1 will be high after 10H= 16 machine cycles. When the clock frequency is
12MHz this happens after 16 µs, then the following instructions generate an interrupt once
every 16 µs after Timer1 starts running.
MOVTMOD,
#20H MOVTL1,
#0F0H MOVTH1,
#0F0H
MOVIE, #88H
SETBTR1
In the above program, the Timer1 is configured as timer mode 2. In this case Gate = 0. Then the
TL1 and TH1 are loaded with F0H. then enable the Timer1 interrupt. At last set the TR1 of
TCON register, and start the timer.
Mode 3 of Timer/Counter
Mode 3 is different for Timer0 and Timer1. When the Timer0 is working in mode 3, the TL0
will be used as an 8-bit timer/counter. It will be controlled by the standard Timer0 control bits,
T0 and INT0 inputs. The TH0 is used as an 8-bit timer but not the counter. This is controlled by
Timer1 Control bit TR1. When the TH0 overflows from FFH to 00H, then TF1 is set to 1. In the
following diagram, we can Timer0 in Mode 3.
When the Timer1 is working in Mode 3, it simply holds the count but does not run. When
Timer0 is in mode 3, the Timer1 is configured in one of the mode 0, 1 and 2. In this case, the
Timer1 cannot interrupt the microcontroller. When the TF1 is used by TH0 timer, the Timer1 is
used as Baud Rate Generator.
The meaning of gate bit in Timer0 and Timer1 for mode 3 is as follows
It controls the running of 8-bit timer/counter TL0 as like Mode 0, 1, or 2. The running of TH0 is
controlled by TR1 bit only. So the gate bit in this mode for Timer0 has no specific role.
After every bit is transmitted, it requires delay for next bit transmission. So SBUF needs
triggering which is provided by
• Timer T1 ( here T1 only needs to trigger, T1 does not require its overflow flag , mode 3
in timers ). Here we can vary the delay, so data transmission delay can be varied (
frequency can be varied ). It has a variable baud rate.
• There is an internal clock in 8051 ( fosc / 12 = 1Mhz ) , where delay cannot be varied, this
has fixed trigger delay. So frequency cannot be varied.It has a fixed baud rate.
Whenever SBUF transferred 8bit of data , Ti flag becomes 1. Whenever processors go to ISR( in
other interrupts the flag is auto cleared whenever processor goes to ISR ) , in this the Ti flag is
not auto cleared.
RxD:
This pin is basically for data reception . It received data bit by bit ( as the transmitter sends LSB
first, it received LSB first ). There is also a register SBUF which stores 8 received bits. Once the
8 bits are received, instead of sending an interrupt it firstly checks for errors ( errors caused due
to transmission). Once there is no error in the received information Ri flag is set and an interrupt
is sent to the processor. Processor goes to ISR ( here also Ri is not cleared automatically ).
How are SBUF in TxD and RxD different from each other ?
In SBUF of TxD, data is sent from processor to SBUF
In SBUF of RxD, data is sent from SBUF to the processor.
In this way both registers are differentiated by the processor.
Receiver assumes blue data ( when there was no communication ) also as data.
Here green is the start bit which is zero. And then the yellow colored is stop bit which is 1.
Whenever the first 0 bit comes, the receiver discards the start bit and accepts the next 8 bits and
stores in SBUF. Then the 9th bit is 1 , this bit is stored in RB8 ( will be discussed later ). Then
after this whenever the next zero bit comes ( that zero bit is discarded and accepts the next 8 bits
and so on ).
Stop bit is also used for error checking. Whenever SM2=1, It checks for error, If the RB8 = 1 (
which means stop = 1 received, so the data is received correctly ) and if RB8=0 ( transmitter
generated stop as 1, but received as 0 ) so there is an error. If there is an error in received data, no
interrupt is sent to the processor.
The 9th bit is a programmable bit and it is given through TB8. Here 9th bit is 1 and it is used for
error checking and stop bit for triggering the data high ( so start bit gets 0 and so on ).
Why the 9th bit , when the already stop bit exists?
Standard value of 9th bit is 1 and can be made 0.
Whenever SM2 = 1( receiver accepts only errorless data ) and if 9th bit is 1, then only errorless
data is accepted or else discarded. Discarding data is a purpose.
Mode 0 :
Totally there were four modes in serial port of 8051, but for better understanding mode 0 is
explained after three modes. In this mode data is transferred and received only through the RxD
channel. TxD is used for clocks. This is synchronous mode of communication.
Such a system is also known as half duplex mode. It has fixed baud rate.
SCON register:
Interrupts are events detected by the MCU which cause normal program flow to be pre-empted.
Interrupts pause the current program and transfer control to a specified user-written firmware
routine called the Interrupt Service Routine (ISR). The ISR processes the interrupt event, then
resumes normal program flow.
Interrupt occurs
When an interrupt request flag is set and the interrupt is properly enabled, the interrupt process
begins:
1. Global Interrupts are disabled by clearing GIE to 0.
2. The current program context is saved to the shadow registers.
3. The value of the Program Counter is stored on the return stack.
4. Program control is transferred to the interrupt vector at address 04h.
ISR runs
The ISR is a function written by the user and placed at address 04h. The ISR does the following:
1. Checks the interrupt-enabled peripherals for the source of the interrupt request.
2. Performs the necessary peripheral tasks.
3. Clears the appropriate interrupt request flag.
4. Executes the Return From Interrupt instruction (RETFIE) as the final ISR instruction.
TMR1GIE -
ADIE
-
RCIE
-
Timer1 Gate Interrupt Enable
Analog-to-Digital Converter (ADC) Interrupt Enable
TXIE -
SSPIE -
CCP1IE
-
Universal Synchronous Asynchronous Receiver Tranmsitter (USART) Receive
Interrupt Enable
USART Transmit Interrupt Enable
Synchronous Serial Port (MSSP) Interrupt Enable
CCP1 Interrupt Enable
Timer2 Interrupt Enable
Timer1 Interrupt Enable
OSFIE -
C2IE - Co
C1IE - Co
EEIE - EE
BCLIE -
LCDIE -
--- - Unim
CCP2IE -
PIE2 register
Oscillator Fail Interrupt Enable
parator C2 Interrupt Enable
parator C1 Interrupt Enable
PROM Write Completion Interrupt Enable
MSSP Bus Collision Interrupt Enable
LCD Module Interrupt Enable
plemented, read as 0
CCP2 Interrupt Enable
PIE3 register
---
-
CCP5IE -
CCP4IE -
CCP3IE -
TMR6IE -
--- Unimp
-
TMR4IE -
---
-
PIE
1
Unimplemented read as 0
CCP5 Interrupt Enable
CCP4 Interrupt Enable CCP3 Interrupt Enable
Timer6 Interrupt Enable
emented, read as 0
Timer4 Interrupt Enable
emented, read as 0
, PIE2, and contain the individual interrupt enable flags for the MCU's peripherals.
Interrupt Request Registers
PIR1 register
TMR1GIF
-
ADIF ADC
- USA
RCIF USA
- - MS
TXIF -
SSPIF
CCP1IF -
TMR2IF
-
TMR1IF
-
RT Receive Interrupt Flag
RT Transmit Interrupt Flag
SP Interrupt Flag
CCP1 Interrupt Flag
Timer2 Interrupt Flag
Timer1 Interrupt Flag
OSFIF -
C2IF - Co
C1IF - Co
EEIF - EE
BCLIF -
LCDIF -
--- - Unim
CCP2IF -
PIR2 register
Oscillator Fail Interrupt Flag
parator C2 Interrupt Flag
parator C1 Interrupt Flag
PROM Write Completion Interrupt Flag
MSSP Bus Collision Interrupt Flag
LCD Module Interrupt Flag
plemented, read as 0
CCP2 Interrupt Flag
PIR3 register
---
-
CCP5IF -
CCP4IF -
CCP3IF -
TMR6IF -
--- Unimp
-
TMR4IF -
--- Unimp
-
PIR
1
Unimplemented, read as 0
CCP5 Interrupt Flag
CCP4 Interrupt Flag
CCP3 Interrupt Flag
Timer6 Interrupt Flag
emented, read as 0
Timer4 Interrupt Flag
emented, read as 0
, PIR2, and OPTION_REG OPTION_REG contain the individual interrupt request flags for
the MCU's peripherals.
The INTEDG flag in OPTION_REG is used to set a rising or falling edge on the INT pin as the
trigger for an INTE interrupt.
Enabling Interrupts
Core Interrupts
Three interrupt sources (Timer0, External Interrupt, and Interrupt on Change) have interrupt
enable bits located in INTCON. These interrupts are referred to as core interrupts.