Unit I
Unit I
Unit I
Microcontrollers for an Embedded System – 8051 – Architecture – Addressing Modes – Instruction Set –
Program and Data Memory – Stacks – Interrupts – Timers/Counters – Serial Ports – Programming.
1
ET3491 EMBEDDED SYSTEM AND IOT DESIGN Unit-1
MICROCONTROLLER :
6 The Instruction set of The instruction sets are simple with less
microprocessor is complex with number of instructions.
large number of instructions.
*****************************************************************
2
ET3491 EMBEDDED SYSTEM AND IOT DESIGN Unit-1
Draw the architectural block diagram of 8051 microcontrolle r and explain. (NOV 2011, MAY 2010, NOV
2009, NOV2008, May 2008, MAY 2007, MAY 2006, NOV 2016, May 2016)
It has hardware architecture with RISC (Reduced Instruction Set Computer) concept.
The block diagram of 8051 microcontroller is shown in Fig 3.
8051 has 8-bit ALU.
ALU can perform all the 8-bit arithmetic and logical operations in one machine cycle.
The ALU is associated with two registers A & B
A and B Registers:
The A and B registers are special function registers.
A & B registers hold the results of many arithmetic and logical operations of 8051.
The A register is also called the Accumulator.
A register is used as a general register to accumulate the results of a large number of instructions.
By default, it is used for all mathematical operations and data transfer operations between CPU and
external memory.
The B register is mainly used for multiplication and division operations along with A register.
Ex: MUL A B : DIV AB.
It has no other function other than as a store data.
R registers:
"R" registers are a set of eight registers that are named R0, R1, etc. up to R7.
These registers are used as auxiliary registers in many operations.
The "R" registers are also used to temporarily store values.
3
ET3491 EMBEDDED SYSTEM AND IOT DESIGN Unit-1
4
ET3491 EMBEDDED SYSTEM AND IOT DESIGN Unit-1
5
ET3491 EMBEDDED SYSTEM AND IOT DESIGN Unit-1
The bits PSW3 and PSW4 are denoted as RS0 and RS1.
These bits are used to select the bank registers of the RAM location.
The selection of the register Banks and their addresses are given below.
0 0 0 00H-07H
0 1 1 08H-0FH
1 0 2 10H-17H
1 1 3 18H-1FH
6
ET3491 EMBEDDED SYSTEM AND IOT DESIGN Unit-1
The 8051 microcontroller has 128 bytes of Internal RAM and 4K B of on chip ROM.
The RAM is also known as Data memory and the ROM is known as program (Code) memory.
Code memory holds program that is to be executed.
Program Address Register holds address of the ROM/ Flash memory.
Data Address Register holds address of the RAM.
I/O ports:
The 8051 microcontroller has 4 parallel I/O ports, each of 8-bits.
So, it provides 32 I/O lines for connecting the microcontroller to the peripherals.
The four ports are P0 (Port 0), P1 (Port1), P2 (Port 2) and P3 (Port3).
Explain diffe rent types addressing modes of 8051 microcontroller. (NOV 2008, NOV 2015, April 2017)
The way in which the data operands are specified is known as the addressing modes. There are various
methods of denoting the data operands in the instruction.
The 8051 microcontroller supports 5 addressing modes. They are
1. Immediate addressing mode
2. Direct Addressing mode
3. Register addressing mode
4. Register indirect addressing mode
5. Indexed addressing mode
Immediate addressing mode:
The addressing mode in which the data operand is a constant and it is a part of the instruction itself is
known as Immediate addressing mode.
Normally the data must be preceded by a # sign.
This addressing mode can be used to transfer the data into any of the registers including DPTR.
Examples:
MOV A, # 27 H : The data (constant) 27 is moved to the accumulator register
ADD R1, #45 H : Add the constant 45 to the contents of the accumulator
MOV DPTR, # 8245H : Move the data 8245 into the data pointer register.
7
ET3491 EMBEDDED SYSTEM AND IOT DESIGN Unit-1
Arithmetic instructions:
8
ET3491 EMBEDDED SYSTEM AND IOT DESIGN Unit-1
ADD
• 8-bit addition between the accumulator (A) and a second operand.
• The result is always in the accumulator.
• The CY flag is set/reset appropriately.
ADDC
• 8-bit addition between the accumulator, a second operand and the previous value of the
CY flag.
• Useful for 16-bit addition in two steps.
• The CY flag is set/reset appropriately.
DAA
• Decimal adjust the accumulator.
• Format the accumulator into a proper 2 digit packed BCD number.
• Operates only on the accumulator.
• Works only after the ADD instruction.
SUBB
• Subtract with Borrow.
• Subtract an operand and the previous value of a borrow (carry) flag from the
accumulator.
• A A - <operand> - CY.
• The result is always saved in the accumulator.
• The CY flag is set/reset appropriately.
INC
• Increment the operand by one.
• The operand can be a register, a direct address, an indirect address, the data pointer.
DEC
• Decrement the operand by one.
• The operand can be a register, a direct address, an indirect address.
MUL AB / DIV AB
• Multiply A by B and place result in A and B registers.
• Divide A by B and place quotient in A register & remainder in B register.
•
9
ET3491 EMBEDDED SYSTEM AND IOT DESIGN Unit-1
10
ET3491 EMBEDDED SYSTEM AND IOT DESIGN Unit-1
11
ET3491 EMBEDDED SYSTEM AND IOT DESIGN Unit-1
MOVX
Data transfer between the accumulator and a byte from external data memory.
• MOVX A, @Ri
• MOVX A, @DPTR
• MOVX @Ri, A
• MOVX @DPTR, A
PUSH / POP
Push and Pop a data byte onto the stack.
The data byte is identified by a direct address from the internal RAM locations.
• PUSH DPL
• POP 40H
XCH
Exchange accumulator and a byte operand
• XCH A, Rn
• XCH A, direct
• XCH A, @Ri
XCHD
Exchange lower digit of accumulator with the lower digit of the memory location specified.
• XCHD A, @Ri
• The lower 4-bits of the accumulator are exchanged with the lower 4-bits of the internal
memory location identified indirectly by the index register.
• The upper 4-bits of each are not modified.
12
ET3491 EMBEDDED SYSTEM AND IOT DESIGN Unit-1
Explain the working of program control transfer instructions of 8051. (May 2012)
13
ET3491 EMBEDDED SYSTEM AND IOT DESIGN Unit-1
• 3 byte instruction capable of referencing any location in the entire 64K of program
memory.
Absolute Jump – AJMP
• Uses an 11-bit address.
• 2 byte instruction
• The 11-bit address is substituted for the lower 11-bits of the PC to calculate the 16-bit
address of the target.
• The location referenced must be within the 2K Byte memory.
Indirect Jump – JMP
• JMP @A + DPTR
The 8051 provides 2 forms for the CALL instruction:
Absolute Call – ACALL
• Uses an 11-bit address similar to AJMP
• The subroutine must be within the same 2K page.
Long Call – LCALL
• Uses a 16-bit address similar to LJMP
• The subroutine can be anywhere.
Both forms push the 16-bit address of the next instruction on the stack and update the stack
pointer.
The 8051 provides 2 forms for the return instruction:
Return from subroutine – RET
• Pop the return address from the stack and continue execution there.
Return from Interrupt Service Routine – RETI
• Pop the return address from the stack.
• Continue execution at the address retrieved from the stack.
• The PSW is not automatically restored.
The 8051 supports 5 different conditional jump instructions.
ALL conditional jump instructions use an 8-bit signed offset.
Jump on Zero – JZ / JNZ
• Jump if the A == 0 / A != 0
• The check is done at the time of the instruction execution.
14
ET3491 EMBEDDED SYSTEM AND IOT DESIGN Unit-1
Explain in detail the inte rnal memory organization of 8051 microcontroller (NOV 2014, May 2012, NOV
2011, NOV 2010, May 2010, MAY 2009, NOV 2008, NOV 2007)
The 8051 microcontroller has 128 bytes of Internal RAM and 4kB of on chip ROM.
The RAM is also known as Data memory and the ROM is known as program (Code) memory.
Code memory holds the actual 8051 program to be executed.
In 8051, memory is limited to 64KB.
Code memory may be found on-chip, as ROM or EPROM.
It may also be stored completely off- chip in an external ROM / EPROM.
The 8051 has only 128 bytes of Internal RAM but it supports 64KB of external RAM.
15
ET3491 EMBEDDED SYSTEM AND IOT DESIGN Unit-1
Since the memory is off-chip, it is not as flexible for accessing and is also slower.
Internal RAM is found on-chip on the 8051. So it is the fastest RAM available.
It is flexible in terms of reading, writing and modifying its contents.
Internal RAM is volatile.
When the 8051 is reset, internal RAM is cleared.
The 128 bytes of internal RAM is organized as below.
Four register banks (Bank0, Bank1, Bank2 and Bank3) each of 8-bits (total 32 bytes).
The default bank register is Bank0.
The remaining Banks are selected with the help of RS0 and RS1 bits of PSW Register.
16 bytes of bit addressable area and
80 bytes of general purpose area (Scratch pad memory) of internal RAM as shown in the diagram below.
This area is utilized by the microcontroller as a storage area for the operating stack.
The 32 bytes of RAM from address 00 H to 1FH are used as working registers organized as four banks
of eight registers each.
The registers are named as R0-R7.
Each register can be addressed by its name or by its RAM address.
For example: MOV A, R7 or MOV R7,#05H
16
ET3491 EMBEDDED SYSTEM AND IOT DESIGN Unit-1
Structure of Internal ROM (On –chip ROM / Program Memory / Code Memory):
The 8051 microcontroller has 4KB of on chip ROM, but it can be extended up to 64KB.
This ROM is also called program memory or code memory.
The CODE segment is accessed using the program counter (PC) for opcode fetches and by DPTR for
data.
The external ROM is accessed when the EA pin is connected to ground or the contents of program
counter exceeds 0FFFH.
When the Internal ROM address is exceeded the 8051 automatically fetches the code bytes from the
external program memory.
Write the available special function registers in 8051. Explain each register with its format and functions.
(April 2017, NOV 2015)
In 8051 microcontroller, there are registers which uses the RAM addresses from 80h to FFh.
They are used for certain specific operations. These registers are called Special Function Registers
(SFRs).
Most of SFRs are bit addressable and other few registers are byte addressable.
In these SFRs, some of them are related to I/O ports (P0, P1, P2 and P3) and some of them are for
control operations (TCON, SCON & PCON).
Remaining are the auxiliary SFRs, that they don't directly configure the 8051.
The list of SFRs and their functional names are given below.
2 B* B-Register 0F0
6 P0* Port 0 80
P1* Port 1 90
8 P2* Port 2 0A
9 P3* Port 3 0B
18
ET3491 EMBEDDED SYSTEM AND IOT DESIGN Unit-1
The register used to access the stack is called the Stack pointer (SP).
SP is an 8-bit register. So, it can take values of 00 to FF H.
When the 8051 is powered up, the SP register contains the value 07.i.e the RAM location value 08 is the
first location being used for the stack by the 8051 controller.
There are two important instructions to handle stack. One is the PUSH and the other is the POP.
The loading of data from CPU registers to the stack is done by PUSH.
The loading of the contents of the stack back into a CPU register is done by POP.
Interrupts:
Interrupt Structure :
An interrupt is an external or internal event that disturbs the microcontroller to inform it that a device
needs its service.
The program which is associated with the interrupt is called the interrupt service routine (ISR) or
interrupt handle r.
Upon receiving the interrupt signal, the microcontroller finishes current operation and saves the PC on
stack.
Jumps to a fixed location in memory depending on type of interrupt.
Starts to execute the interrupt service routine until RETI.
Upon executing the RETI the microcontroller returns to the place where it was interrupted. Get pop PC
from stack.
The 8051 microcontroller has FIVE interrupts in addition to Reset. They are
Each interrupt has a specific place in code memory where program execution begins.
19
ET3491 EMBEDDED SYSTEM AND IOT DESIGN Unit-1
EA : Global enable/disable. To enable the interrupts, this bit must be set high.
Upon reset, the interrupts have the following priority from top to down. The interrupt with the highest
PRIORITY gets serviced first.
20
ET3491 EMBEDDED SYSTEM AND IOT DESIGN Unit-1
IP.7: reserved
IP.6: reserved
1. Explain the different modes of operation of time rs in 8051 in detail with its associated registers.
Describe different modes of operation of time rs /counte rs in 8051 with its associated registers.
(NOV 2009, MAY 2009. May 2007, May 2016)
Draw and explain the functions of TCON and TMOD registers of 8051. (Dec 2008)
Explain the on-chip timer modes of an 8051 Microcontroller. (April 2010, NOV 2016)
Time r Registers.
The 8051 has two timers/counters, they can be used either as timers (used to generate a time delay)
or as event counters.
TIMER 0:
Timer 0 is a 16-bit register and can be treated as two 8-bit registers (TL0 & TH0).
These registers can be accessed similar to any other registers like A, B or R1 etc
Ex : The instruction MOV TL0,#07 moves the value 07 into lower byte of Timer0.
Similarly MOV R1, TH0 saves the contents of TH0 in the R1 register.
21
ET3491 EMBEDDED SYSTEM AND IOT DESIGN Unit-1
TIMER 1:
Timer 1 is also a 16-bit register and can be treated as two 8-bit registers (TL1 & TH1).
These registers can be accessed similar to any other registers like A, B or R1etc
Ex : The instruction MOV TL1,#05 moves the value 05 into lower byte of Timer1.
Similarly MOV R0,TH1 saves the contents of TH1 in the R0 register.
22
ET3491 EMBEDDED SYSTEM AND IOT DESIGN Unit-1
GATE:
This bit is used to start or stop the timers by hardware.
When GATE= 1, the timers can be started / stopped by the external sources.
When GATE= 0, the timers can be started or stopped by software instructions like SETB TRX
or CLR TRX.
C/T (Counter/Timer):
This bit decides whether the timer is used as delay generator or event counter.
When C / T = 0, timer is used as delay generator.
When C / T =1, timer is used as an event counter.
The clock source for the time delay is the crystal frequency of 8051.
The clock source for the event counter is the external clock source.
M1, M0 (Mode):
These two bits are the timer mode bits.
The timers of the 8051 can be configured in four modes Mode0, Mode1, Mode2 & Mode 3.
The selection and operation of the modes is shown below.
23
ET3491 EMBEDDED SYSTEM AND IOT DESIGN Unit-1
24
ET3491 EMBEDDED SYSTEM AND IOT DESIGN Unit-1
26
ET3491 EMBEDDED SYSTEM AND IOT DESIGN Unit-1
Notice that to repeat the process, we must reload the TL and TH registers, and start the process is repeated.
Example 2:
27
ET3491 EMBEDDED SYSTEM AND IOT DESIGN Unit-1
In Example 1, calculate the amount of time delay in the DELAY subroutine generated by the timer.
Assume XTAL = 11.0592 MHz.
Solution:
The timer works with a clock frequency of 1/12 of the XTAL frequency, we have 11.0592 MHz / 12 =
921.6 kHz as the timer frequency.
As a result, each clock has a period of T =1/921.6kHz,T=1.085µs.
In other words, Timer 0 counts up each 1.085µs resulting in delay = number of counts × 1.085 µs.
The number of counts for the roll over is FFFFH – FFF2H = 0DH (13 decimal).
Add one to 13 because of the extra clock needed when it rolls over from FFFF to 0 and raise the TF flag.
This gives 14 × 1.085µs = 15.19µs for half the pulse. For the entire period it is T = 2 × 15.19µs =
30.38µs as the time delay generated by the timer.
(a) In hexadecimal
(FFFF – YYXX + 1) ×1.085 µs, where YYXX are TH, TL initial values respectively. Notice that value YYXX
are in hex.
(b) In decimal
Convert YYXX values of the TH, TL register to decimal to get a NNNN decimal, then (65536 - NNNN) ×
1.085 µs
Example 3:
In Example 1, calculate the frequency of the square wave generated on pin P1.5.
Solution:
In the timer delay calculation of Example 1, we did not include the overhead due to instruction in the
loop.
To get a more accurate timing, we need to add clock cycles due to these instructions in the loop.
To do that, we use the machine cycle as shown below.
Cycles
HERE: MOV TL0,#0F2H 2
MOV TH0,#0FFH 2
CPL P1.5 1
ACALL DELAY 2
SJMP HERE 2
28
ET3491 EMBEDDED SYSTEM AND IOT DESIGN Unit-1
Example 4:
Find the delay generated by timer 0 in the following code, using both of the Methods. Do not include
the overhead due to instruction.
CLR P2.3 ;Clear P2.3
MOV TMOD,#01 ;Timer 0, 16-bitmode
HERE: MOV TL0,#3EH ;TL0=3Eh, the low byte
MOV TH0,#0B8H ;TH0=B8H, the high byte
SETB P2.3 ;SET high timer 0
SETB TR0 ;Start the timer 0
AGAIN: JNB TF0,AGAIN ;Monitor timer flag 0
CLR TR0 ;Stop the timer 0
CLR TF0 ;Clear TF0 for next round
CLR P2.3
Solution:
(FFFFH – B83E + 1) = 47C2H = 18370 in decimal and 18370 × 1.085 µs = 19.93145 ms
29
ET3491 EMBEDDED SYSTEM AND IOT DESIGN Unit-1
TH and TL
30
ET3491 EMBEDDED SYSTEM AND IOT DESIGN Unit-1
When it is used as a counter, it is a pulse outside of the 8051 that increments the TH, TL register.
TMOD and TH, TL registers are the same as in timer concept, except the source of the frequency.
The C/T bit in the TMOD register decides the source of the clock for the timer
When C/T = 1, the timer is used as a counter and gets its pulses from outside the 8051.
The counter counts up as pulses are fed from pins 14 and 15.
these pins are called T0 (timer 0 input) and T1 (timer 1 input)
If GATE = 1, the start and stop of the timer are done externally through pins P3.2 and P3.3 for timers 0
and 1, respectively
This hardware allows starting or stopping the timer externally at any time via a simple switch
31
ET3491 EMBEDDED SYSTEM AND IOT DESIGN Unit-1
The frequency for the timer is always 1/12th the frequency of the crystal attached to the 8051.
Example 6:
Assuming that clock pulses are fed into pin T1, write a program for counter 1 in mode 2 to count the pulses and
display the state of the TL1 count on P2, which connects to 8 LEDs.
Solution:
MOV TM0D,#01100000B ;counter 1, mode 2, C/T=1 external pulses
MOV TH1,#0 ;clear TH1
SETB P3.5 ;make T1 input
AGAIN: SETB TR1 ;start the counter
BACK: MOV A,TL1 ;get copy of TL
MOV P2,A ;display it on port 2
JNB TF1,Back ;keep doing, if TF = 0
CLR TR1 ;stop the counter 1
CLR TF1 ;make TF=0
SJMP AGAIN ;keep doing it
Notice in the above program the role of the instruction SETB P3.5.
Since ports are set up for output when the 8051 is powered up.
So, we make P3.5 an input port by making it high.
32
ET3491 EMBEDDED SYSTEM AND IOT DESIGN Unit-1
In other words, we must configure (set high) the T1 pin (pin P3.5) to allow pulses to be fed into it.
RS232
It is an interfacing standard RS232.
It was set by the Electronics Industries Association (EIA) in 1960.
The standard was set long before the advent of the TTL logic family.
Its input and output voltage levels are not TTL compatible.
In RS232, a 0 is represented by -3 to -25 V, while a 1 bit is +3 to +25 V.
IBM introduced the DB-9 version of the serial I/O standard.
33
ET3491 EMBEDDED SYSTEM AND IOT DESIGN Unit-1
It asserts RTS to signal the modem that it has a byte of data to transmit.
CTS (clear to send)
It is to receive, it sends out signal CTS,
MAX232
A line driver ( MAX232) is required to convert RS232 voltage levels to TTL levels, and vice versa.
8051 has two pins that are used specifically for transferring and receiving data serially.
These two pins are called TxD and RxD and are part of the port 3 (P3.0 and P3.1).
These pins are TTL compatible.
They require a line driver to make them RS232 compatible.
Baud rate:
The baud rates in 8051 are programmable.
8051 divides the crystal frequency by 12 to get machine cycle frequency.
8051 UART circuitry divides the machine cycle frequency by 32.
Explain in detail the serial communication registers of the 8051. (NOV 2009)
SBUF:
It is an 8-bit register used for serial communication.
For a byte data to be transferred via the TxD line:
Byte must be placed in the SBUF register.
Bytes are framed with the start and stop bits and transferred serially via the TxD line.
SBUF holds the byte of data when it is received by 8051 RxD line.
When the bits are received serially via RxD.
8051 de-frames byte by eliminating the stop and start bits.
SCON:
It is an 8-bit register used to program the start bit, stop bit and data bits of data framing.
SM0 SM1 SM2 REN TB8 RB8 TI RI
35
ET3491 EMBEDDED SYSTEM AND IOT DESIGN Unit-1
1. TMOD register is loaded with the value 20H, indicating the use of timer 1 in mode 2 (8-bit auto-
reload) to set baud rate.
2. The TH1 is loaded with one of the values to set baud rate for serial data transfer.
3. The SCON register is loaded with the value 50H, indicating serial mode 1, where an 8-bit data is framed
with start and stop bits.
4. TR1 is set to 1 to start timer 1
5. TI is cleared by CLR TI instruction.
6. The character byte to be transferred serially is written into SBUF register.
7. The TI flag bit is monitored with the use of instruction JNB TI, xx, to see if the character has been
transferred completely.
8. To transfer the next byte, go to step 5.
Write a program for the 8051 to transfer letter “A” serially at 4800 baud, continuously.
Solution:
36
ET3491 EMBEDDED SYSTEM AND IOT DESIGN Unit-1
By checking the TI flag bit, we know whether or not the 8051 is ready to transfer another byte
It must be noted that TI flag bit is raised by 8051 itself when it finishes data transfer
It must be cleared by the programmer with instruction CLR TI
If we write a byte into SBUF before the TI flag bit is raised, we risk the loss of a portion of the byte
being transferred
The TI bit can be checked by the instruction JNB TI,xx Using an interrupt.
Write a program for the 8051 to transfer “YES” serially at 9600 baud, 8-bit data, 1 stop bit do this
continuously. (May 2006)
Solution:
MOV TMOD, #20H ;timer 1, mode 2 (auto reload)
MOV TH1, #-3 ;9600 baud rate
MOV SCON, #50H ;8-bit, 1 stop, REN enabled
SETB TR1 ;start timer 1
AGAIN: MOV A, # “Y” ;transfer “Y”
ACALL TRANS
MOV A, # “E” ;transfer “E”
ACALL TRANS
MOV A, # “S” ;transfer “S”
ACALL TRANS
37
ET3491 EMBEDDED SYSTEM AND IOT DESIGN Unit-1
Write a program for the 8051 to receive bytes of data serially and put them in P1, set the baud rate at 4800, 8-
bit data and 1 stop bit. (NOV 2016)
Solution:
MOV TMOD, #20H ;timer 1, mode 2 (auto reload)
MOV TH1, #-6 ;4800 baud rate
MOV SCON, #50H ;8-bit, 1 stop, REN enabled
SETB TR1 ;start timer 1
HERE: JNB RI, HERE ;wait for char to come in
MOV A, SBUF ;saving incoming byte in A
MOV P1, A ;send to port 1
CLR RI ;get ready to receive next byte
SJMP HERE ;keep getting data
38
ET3491 EMBEDDED SYSTEM AND IOT DESIGN Unit-1
In receiving bit via its RxD pin, 8051 goes through the following steps.
1. It receives the start bit
Indicating that the next bit is the first bit of the character byte it is about to receive
2. The 8-bit character is received one bit at time
3. The stop bit is received
When receiving the stop bit 8051 makes RI = 1,indicating that an entire character byte has
been received.
5. After the SBUF contents are copied into a safe place.
The RI flag bit must be forced to 0 by CLR RI in order to allow the next received character byte to be
placed in SBUF.
Failure to do this causes loss of the received character.
There are two ways to increase the baud rate of data transfer
To use a higher frequency crystal
To change a bit in the PCON register
PCON
PCON register is an 8-bit register
When 8051 is powered up, SMOD is zero.
We can set it to high by software and thereby double the baud rate.
GF1, GF0: General flag bits
PD: Power down mode
IDL: Ideal mode
39
ET3491 EMBEDDED SYSTEM AND IOT DESIGN Unit-1
The 8051 microcontroller is available as a 40 pin DIP chip and it works at +5 volts DC.
Among the 40 pins, a total of 32 pins are allotted for the four parallel ports P0, P1, P2 and P3 i.e each
port occupies 8-pins.
The remaining pins are VCC, GND, XTAL1, XTAL2, RST, EA ,PSEN.
XTAL1, XTAL2:
These two pins are connected to Quartz crystal oscillator which runs the on-chip oscillator.
The quartz crystal oscillator is connected to the two pins along with a capacitor of 30pF as shown in the
circuit.
If use a source other than the crystal oscillator, it will be connected to XTAL1 and XTAL2 is left
unconnected.
RST:
40
ET3491 EMBEDDED SYSTEM AND IOT DESIGN Unit-1
41
ET3491 EMBEDDED SYSTEM AND IOT DESIGN Unit-1
P0.0- P0.7(AD0-AD7) :
The port 0 pins multiplexed with Address/data pins.
If the microcontroller is accessing external memory, these pins will act as address/data pins, otherwise
they are used for Port 0 pins.
P2.0- P2.7 (A8-A15) :
The port2 pins are multiplexed with the higher order address pins.
When the microcontroller is accessing external memory, these pins provide the higher order address
byte, otherwise they act as Port 2 pins.
P1.0- P1.7 :
These 8-pins are dedicated to perform input or output port operations.
P3.0- P3.7:
These 8-pins are meant for Port3 operations and also for some control operations like read, Write,
Timer0, Timer1, INT0, INT1, RxD and TxD.
Program 1: Using timers in 8051 write a program to generate square wave 100ms, 50%
duty cycle. (NOV 2014, May 2016, May 2012)
MOV TMOD, #01
Here: MOV TL0, #D7
MOV TH0, #B4
CPL P1.3
SETB TRO
Again: JNB TF0, Again
CLR TR0
CLR TF0
SJMP Here
42
ET3491 EMBEDDED SYSTEM AND IOT DESIGN Unit-1
Program 2: Write an 8051 ALP to multiply the given number 48H and 30H. (April 2017)
Mnemonics Comments
Opcode Operand
MOV A,#48 ;Store data1 in accumulator
MOV B,#30 ;Store data2 in B register
MUL AB ;Multiply both
MOV DPTR,#4500 ;Initialize memory location
MOVX @DPTR,A ;Store lower order result
INC DPTR ;Go to next memory location
MOV A,B ;Store higher order result
MOVX @DPTR,A
L1: SJMP L1 ;Stop the program
Program 3: Write a program to add two 16 bit numbers. The numbers are 8C8D and
8D8C. Place the sum in R7 and R6. R6 should have the lower byte. (NOV 2010)
Mnemonics Comments
Opcode Operand
MOV A, #8D ;Store LSB data1 in accumulator
MOV B, #8C ;Store LSB data2 in B register
ADD A, B ;Add both
MOV R6, A ;Store LSB result
MOV A, #8C ;Store MSB data1 in accumulator
MOV B, #8D ;Store MSB data2 in B register
ADD A, B ;Add both
MOV R7, A ;Store MSB result
L1: SJMP L1 ;Stop the program
43