Documente Academic
Documente Profesional
Documente Cultură
VIT, VELLORE
Reg.No 16BEC0198
L43+L44, E1
Course Code ECE3003 Slot & Semester
FALL ~2018-19
Date of
Date of Exp. 22nd October, 2018 22nd October, 2018
Submission
Faculty A.KARTHIKEYAN
SUBMISSION:
MAIL ID : AKECE3003E1@GMAIL .COM,
MAIL SUBJECT : SL.NO. _ L + L _REG.NUMBER FALL_1819_ NAME_ E XP.NO. __
FILE NAME AND FORMAT : REG.NUMBER.DOCX (DOC) - ONLY
MAIL YOUR, DIGITAL ASSIGNMENT , LAB PROGRAMS TO THE ABOVE SAID MAIL ID
And also LOAD YOUR DOCUMENTS BEFORE THE DEAD LINE ON THE INTRANET
Reg no: 16BEC0198 FALL~2018-19 ECE3003 - 𝜇C and its applications
ASSIGNMENT QUESTION
2. For Question 1, indicate the largest value (in decimal) that each register can contain.
3. For Question 1, indicate the largest value (in hex) that each register can contain.
4. Find the value of the CY flag after the execution of the following code.
INC A CY=1
Ans: SP= 07
6. Upon pushing data onto the stack, the SP register is _____________ (decremented,
incremented).
Ans: incremented
7. Upon popping data from the stack, the SP register is ___________ (decremented,
incremented).
Ans: decremented
8. Can you change the value of the SP register? If yes, explain why you would want
to do that.
Ans: Yes, the SP register value can be changed. We would want to do this as we might need more
than 24 bytes of memory which is normally used in stacks. So we change the SP to point to RAM
location 30-7FH using “MOV SP,#xx.”
MOVC A,@A+DPTR
MOV A,@R0
Ans: the 1st one is indexed addressing mode and the 2nd one is register indirect addressing mode.
Ans: b is incorrect
MOV A,40H
MOV A,#40H
Ans: The 1st instruction is in direct addressing mode and the 2nd instruction is in immediate
addressing mode.
MOV 40H,A
MOV 40H,#0A
Ans: The 1st instruction is in direct addressing mode and the 2nd instruction is in immediate
addressing mode.
15. Explain the difference between the ADD and ADDC instructions.
Ans: The add instruction tells the CPU to add the source byte to register A and put the result n
register A,the ADD instruction is used for both signed and unsigned numbers while ADDC
instruction will add the source byte to A in addition to the CY flag,if CY=1 prior to the instruction
,CY is also added to A .If CY=0 prior to the instruction source I added to the destination plus 0.
CLR C
17. True or False. "DA A" must be used for adding BCD data only.
Ans: TRUE
18. Can we use the "DA A" instruction to convert data such as 9CH into BCD without
Ans: :- YES ,we can use the instruction DA A to convert 9CH into BCD without performing ADD
instruction because the conditions are:
Ans: :-
Ans:
MOV R1, A
21. In the 8051, when dividing a byte by a byte what is the largest value for the numerator?
What about the denominator? Give the value in both hex and decimal.
Ans: When dividing byte by byte, the numerator must be in register A and denominator must be in
register B. A and B are 8 bit register so the size of both numerator and denominator is 255 in
decimal or FF in hex.
22. In the 8051, when dividing a byte by a byte what is the largest value for the quotient?
What about the remainder? Give the value in both hex and decimal.
Ans: In bye by byte division one operand must be register A while The other should be B. Both are
8 bit registers, the quotient is stored in A and remainder is stored in B , so size of each number is
255 in dec or FF in hex.
23. In the 8051, when multiplying two numbers what is the largest value for each number?
Ans: In bye by byte multiplication one operand must be register A while The other should be B
.Both are 8 bit registers, so size of each number is 255 in dec or FF in hex
24. Explain the role of the OV flag in division and multiplication instructions.
Ans: It makes the OV flag=0 and CY flag=0 if denominator is not equal to 0 .Otherwise OV=1
indicates an error and CY=0.
25. In generating a square of numbers between 0 and 9 there are two methods: (a) using the MUL
instruction, or (b) using a look-up table. Which one is faster in the 8051?
26. Upon reset, all the ports of the 8051 are configured as ______ (input, output).
Ans: Input
27. To make all the bits of a port an input port we must write ____ hex to it.
SENSE, VIT Page 5 of 43
Reg no: 16BEC0198 FALL~2018-19 ECE3003 - 𝜇C and its applications
Ans: 0FFH
Ans: The ports can be accessed by Read –Modify-Write Technique. The feature saves many lines
of the code by combining in a single instruction all 3 actions 1) Reading the port 2) modifying the
value 3)writing to the port.
30. Write a program to monitor P2.4 continuously. When it becomes low, it sends 55H to P1.
Ans:
ORG 0000H
HERE:JB P2.4,HERE
MOV A,#55H
MOV P1,A
31. Upon reset, all ports of the 8051 are configured as _____________ (output, input).
Ans: Input
33. Which ports of the 8051 require the connection of external pull-up resistors in order to be
Ans: Port 0.
34. In the 8051, explain why we must write "1" to a port in order for it to be used for input.
Ans: When the port is used as an input port, ‘1’ is written to the latch. In this situation both the
output MOSFETs are ‘off’. Hence the output pin have floats hence whatever data written on pin is
directly read by read pin.
35. Explain why we need to buffer the switches used as input in order to avoid damaging the
8051 port.
Ans: :- Two reasons spring to mind. This is assuming your 8051 is a CMOS part, which they
probably all are nowadays.
(1) Applying a logic 1 voltage to an input on a circuit which is not powered will cause current to
flow through the protection diode from the input to the positive supply rail. Basically the 8051 gets
powered through its own input pin. This can destroy the input.
SENSE, VIT Page 6 of 43
Reg no: 16BEC0198 FALL~2018-19 ECE3003 - 𝜇C and its applications
(2) Buffering protects the inputs from static discharge onto the connector pins. You may damage a
buffer chip but that's usually easier to diagnose replace than a damaged microprocessor.
You wouldn't normally buffer switches unless they were a long way from the processor where
voltage spikes could get onto the input lines.
36. What is the maximum frequency that can be generated using Mode 1 if the crystal frequency
then frequency=1/T=infinite
37. What is the maximum frequency that can be generated using Mode 2 if the crystal
f=1/T =infinite
38. What is the lowest frequency that can be generated using Mode 1 if the crystal frequency is
39. What is the lowest frequency that can be generated using Mode 2 if the crystal frequency is
Ans: When it rolls from FFFFH to 0000H it sets high a flag bit called TFx.
Ans: When it rolls from FFH to 00H it sets high a flag bit called TFx.
42. How does the LCD distinguish data from instruction codes when receiving information at its
SENSE, VIT Page 7 of 43
Reg no: 16BEC0198 FALL~2018-19 ECE3003 - 𝜇C and its applications
data pin?
Ans: There are two very important registers inside the LCD. The RS pin is used for their selection
as follows. If RS = 0, the instruction command code register is selected, allowing the user to send a
command such as clear display, cursor at home, etc. If RS = 1 the data register is selected, allowing
the user to send data to be displayed on the LCD.
43. To send the instruction code 01 to clear the display, we must make RS = ___.
Ans: 0
44. To send letter 'A' to be displayed on the LCD, we must make RS = ____.
Ans: 1
45. What is the purpose of the E line? Is it an input or an output as far as the LCD is
concerned?
46. When is the information (code or data) on the LCD pin latched into the LCD?
47. The 8051 TxD and RxD signals ________ (are, are not) TTL-compatible.
Ans: Are
48. In this lab, what is the role of the MAX233 (MAX232) chip?
Ans: To save board space, some designers use MAX233 chip from Maxim
1. MAX233 performs the same job as MAX232 but eliminates the need for capacitors.
2. Notice that MAX233 and MAX232 are not pin compatible.
49. With XTAL=11.0592 MHz, what is the maximum baud rate for the 8051?
Ans: 240000
Ans: TI (transfer interrupt) is raised when the last bit of the framed data, the stop bit, is transferred,
indicating that the SBUF register is ready to transfer the next byte. RI (received interrupt), is raised
when the entire frame of data, including the stop bit, is received. In other words, when the SBUF
register has a byte, RI is raised to indicate that the received byte needs to be picked up before it is
lost (overrun) by new incoming serial data. As far as serial communication is concerned, all the
above concepts apply equally when using either polling or an interrupt. The only difference is in
how the serial communication needs are served. In the polling method, we wait for the flag (TI or
RI) to be raised; while we wait we cannot do anything else. In the interrupt method, we are notified
when the 8051 has received a byte, or is ready to send the next byte; we can do other things while
the serial communication needs are served.
In the 8051 only one interrupt is set aside for serial communication. This interrupt is used to both
send and receive data. If the interrupt bit in the IE register (IE.4) is enabled, when RI or TI is raised
the 8051 gets interrupted and jumps to memory address location 0023H to execute the ISR. In that
ISR we must examine the TI and RI flags to see which one caused the interrupt and respond
accordingly.
Ans: True
53. For full duplex, what are the absolute minimum signals needed between the 8051 and the
54. Name all of the interrupts in the 8051 and their vector table addresses.
Ans: The 8051 microcontroller can recognize five different events that cause the main program to
interrupt from the normal execution. These five sources of interrupts in 8051are:
The Timer and Serial interrupts are internally generated by the microcontroller, whereas the
external interrupts are generated by additional interfacing devices or switches that are externally
connected to the microcontroller. These external interrupts can be edge triggered or level triggered.
When an interrupt occurs, the microcontroller executes the interrupt service routine so that
memory location corresponds to the interrupt that enables it. The Interrupt corresponding to the
memory location is given in the interrupt vector table below.
Ans: The TF0 causes an interrupt when it becomes one, which happens when the timer rolls over
from 65535 to 0.
Ans: The TF0 causes an interrupt when it becomes one, which happens when the timer rolls over
from 255 to 0
57. On reset, INT0 (and INT1) are _________ (edge, level) triggered.
Ans: level
59. True or False. There is only a single interrupt for the serial data transfer.
Ans: True
60. Name all of the interrupts in the 8051 and their vector table addresses.
Ans:
Ans: The TF0 causes an interrupt when it becomes one, which happens when the timer rolls over
from 65535 to 0
Ans: The TF0 causes an interrupt when it becomes one, which happens when the timer rolls over
from 255 to 0
63. Draw TMOD ,TCON,SCON,PCON,IE Control word format and PSW bits.
64. On reset, INT0 (and INT1) are _________ (edge, level) triggered.
Ans: level.
66. With XTAL=11.0592 MHz, what is the maximum baud rate for the 8051?
Ans: 24000
67. To send letter 'A' to be displayed on the LCD, we must make RS = ____.
SENSE, VIT Page 10 of 43
Reg no: 16BEC0198 FALL~2018-19 ECE3003 - 𝜇C and its applications
Ans: 1
68. How does the LCD distinguish data from instruction codes when receiving information at its data pin?
Ans: When RS=0, instruction codes are fed in and when RS=1, data is fed in.
69. What is the lowest frequency that can be generated using Mode 1 if the crystal frequency is 11.0592
MHz? Show your calculation.
70. Upon reset, all ports of the 8051 are configured as _____________ (output, input).
Ans: input
71. To send the instruction code 01 to clear the display, we must make RS = ___.
Ans: 0
72. Upon pushing data onto the stack, the SP register is _____________ (decremented,
incremented).
Ans: incremented
73. Can you change the value of the SP register? If yes, explain why you would want
to do that.
74. The stack uses the same area of RAM as bank ______.
Ans: bank 1
Ans: SP=07
-----------------------------------------------------------------------------------------------------------------------------
--
1. Show the status of the ACC,CY, AC and P flag after the addition of 38H and 2FH in the following
instructions.
MOV A, #38H
ADD A, #2FH ;
Ans:
Memo Lab Mnemon Operan Addressi Machi Memo Type of Comment Flags
ry el ics ds ng mode ne ry byte instructi s affected
Addre used cycle requir on by
ss requir ed instructio
e ns
mov A,#38H immedia 1 2 Data ;move NONE
te transfer 38h to
accumula
tor
add A,2FH 1 2 Data ;add the AC,CY
transfer contents
of
accumula
tor and
2FH
2. Show the status of the CY, AC and P flag after the addition of 9CH and 64H in the following
instructions.
MOV A, #9CH
ADD A, #64H ;
Ans:
Memo Lab Mnemon Operan Addressi Machi Memo Type of Comment Flags
ry el ics ds ng mode ne ry byte instructi s affected
Addre used cycle requir on by
ss requir ed instructio
ed ns
mov A,#9CH immedia 1 2 Data ;move NONE
te transfer 9Ch to
accumula
tor
add A,64H 1 2 Data ;add the AC,CY
transfer contents
of
accumula
tor and
64H
3. Show the status of the CY, AC and P flag after the addition of 88H and 93H in the following
instructions.
MOV A, #88H
ADD A, #93H ;
Ans:
Memo Lab Mnemon Operan Addressi Machi Memo Type of Comment Flags
ry el ics ds ng mode ne ry byte instructi s affected
Addre used cycle requir on by
ss requir ed instructio
e ns
mov A,#88H immedia 1 2 Data ;move NONE
te transfer 88h to
accumula
tor
add A,93H 1 2 Data ;add the AC,CY
transfer contents
of
accumula
tor and
93H
4. Show the stack and stack pointer from the following. Assume the default stack area.
Memo Lab Mnemoni Operan Addressi Machi Memo Type of Comme Flags
ry el cs ds ng mode ne ry byte instructi nts affected
Addres used cycle requir on by
s requir ed instructio
e ns
MOV R6, immedia 1 2 Data ;move NONE
#25H te transfer the
value
25H to
R6
MOV R1, Immedia 1 2 Data ;move NONE
#12H te transfer the
value
SENSE, VIT Page 14 of 43
Reg no: 16BEC0198 FALL~2018-19 ECE3003 - 𝜇C and its applications
12H to
R1
MOV R4, Immedia 1 2 Data ;move NONE
#0F3H te transfer the
value
F3H to
R4
PUSH 6 2 2 Data Push the NONE
transfer contents
at
location
6
location
to stack
PUSH 1 2 2 Data Push the NONE
transfer contents
at
location
1
location
to stack
PUSH 4 2 2 Data Push the NONE
transfer contents
at
location
4
location
to stack
5. Examining the stack, show the contents of the register and SP after execution of the following
instructions. All value are in hex.
POP 3 ;
POP 5 ;
POP 2 ; assume OB – 54, OA – F9, 09 -76,08-6C.
Ans:
Memo Lab Mnemoni Operan Addressi Machi Memo Type of Comme Flags
ry el cs ds ng mode ne ry byte instructi nts affected
Addres used cycle requir on by
s requir ed instructio
e ns
POP 3 2 2 Data ;pop the NONE
transfer contents
pointed
by stack
pointer
to
location
3
POP 5 2 2 Data ;pop the NONE
transfer contents
pointed
by stack
pointer
to
location
5
POP 2 2 2 Data ;pop the NONE
transfer contents
pointed
by stack
pointer
to
location
2
6. Examining the stack, show the contents of the register and SP after
execution of the following instructions. All value are in hex.
MOV SP, #5FH ;make RAM location 60H
;first stack location
MOV R2, #25H
MOV R1, #12H
MOV R4, #0F3H
PUSH 2
PUSH 1
PUSH 4
Memo Lab Mnemoni Operan Addressi Machi Memo Type of Comme Flags
ry el cs ds ng mode ne ry byte instructi nts affected
Addres used cycle requir on by
s requir ed instructio
ed ns
MOV SP, immedia 1 2 Data ;move NONE
#5FH te transfer the stack
pointer
to 5FH
MOV R2, immedia 1 2 Data ;move NONE
#25H te transfer the
value
25H to
R2
MOV R1, immedia 1 2 Data ;move NONE
#12H te transfer the
value
12H to
R1
MOV R4, immedia 1 2 Data ;move NONE
#0F3H te transfer the
value
F3H to
R4
PUSH 2 2 2 Data Push to NONE
transfer contents
at
location
2 to
stack
PUSH 1 2 2 Data Push to NONE
transfer contents
at
location
1 to
stack
PUSH 4 2 2 Data Push to NONE
transfer contents
at
location
4 to
stack
7. Write a program to (a) load the accumulator with the value 55H, and (b) complement the ACC 700
times.
Memo Label Mnemon Operan Addressi Machi Memo Type of Comment Flags
ry ics ds ng mode ne ry instructi s affected
Addre used cycle byte on by
ss requir requir instructi
e ed ons
MOV A,#55H immedi 1 2 Data ;move the NONE
ate transfer value 55H
to
accumula
tor
MOV R0,#7D Immedi 1 2 Data ;move the NONE
ate transfer value 7D
to R0
LOOP MOV R1,#10 Immedi 1 2 Data ;move the NONE
2: 0D ate transfer value
100D to
R1
LOOP CPL A 1 1 Logical ;complim NONE
1: Instructi ent
ons accumula
tor
DJNZ R1,LOO 2 2 Branch ; NONE
P1 decreme
nt R1 and
jump tp
loop1 if
non zero
DJNZ R0,LOO 2 2 Branch ; NONE
P2 decreme
nt R0 and
jump tp
loop2 if
non zero
9. Find the period of the machine cycle for 11.0592 MHz crystal frequency
Ans: T= 1.085 us
10. For 8051 system of 11.0592 MHz, find how long it takes to execute each instruction.
(a) MOV R3,#55 = 1.085 us(1mc) (b) DEC R3=1.085us(1mc) (c) DJNZ R2 target= 2.17us(2mc)
(d) LJMP = 2.17us(e) SJMP=2.17us(2mc) (f) NOP=1.085us(1mc) (g) MUL AB=4.34us(4mc)
11. Find the size of the delay in following program, if the crystal frequency is 11.0592MHz.
MOV A,#55H
AGAIN: MOV P1,A
ACALL DELAY
SENSE, VIT Page 19 of 43
Reg no: 16BEC0198 FALL~2018-19 ECE3003 - 𝜇C and its applications
CPL A
SJMP AGAIN
;---time delay-------
DELAY: MOV R3,#200
HERE: DJNZ R3,HERE
RET
Ans: 403*1.085 us = 437.255 us
12. Find the size of the delay in following program, if the crystal frequency is 11.0592MHz.
Machine Cycle
DELAY: MOV R3,#250 1
HERE: NOP 1
NOP 1
NOP 1
NOP 1
DJNZ R3,HERE 2
RET 2
Ans: 1.63ms
13. Find the size of the delay in following program, if the crystal frequency is 11.0592MHz.
Machine Cycle
DELAY: MOV R2,#200 1
AGAIN: MOV R3,#250 1
HERE: NOP 1
NOP 1
DJNZ R3,HERE 2
DJNZ R2,AGAIN 2
RET 2
Ans: 200603*1.085us = 0.2176 s
14. Write the following programs. Create a square wave of 50% duty cycle on bit 0 of port 1.
Ans: ORG 0000H
HERE: CPL P1.0
ACALL DELAY
SJMP HERE
DELAY: MOV R0,#255
BACK: DJNZ R0,BACK
RET
END
CLR P2.3
ACALL DELAY
SJMP AGAIN
DELAY: MOV R0,#255
BACK: DJNZ R0, BACK
RET
END
16. Assume that bit P2.3 is an input and represents the condition of an oven. If it goes high, it means that
the oven is hot. Monitor the bit continuously. Whenever it goes high, send a high-to-low pulse to port
P1.5 to turn on a buzzer.
Ans:
ORG 0000H
HERE: JNB P1.2, HERE
SETB P1.5
ACALL DELAY
CLR P1.5
ACALL DELAY
SJMP HERE
DELAY: MOV R0,#255
AGAIN: DJNZ R0, AGAIN
RET
END
17. A switch is connected to pin P1.7. Write a program to check the status of SW and perform the
following: (a) If SW=0, send letter ‘N’ to P2 (b) If SW=1, send letter ‘Y’ to P2
Ans:
SETB P1.7 ;make P1.7 an input
AGAIN:
JB P1.7,OVER ;jump if P1.7=1
MOV P2,#’N’ ;SW=0, issue ‘N’ to P2
SJMP AGAIN ;keep monitoring
OVER:
MOV P2,#’Y’ ;SW=1, issue ‘Y’ to P2
18. A switch is connected to pin P1.7. Write a program to check the status of SW and perform the
following:
(a) If SW=0, send letter ‘N’ to P2 (b) If SW=1, send letter ‘Y’ to P2 Use the carry flag to check the switch
status.
Ans:
SETB P1.7 ;make P1.7 an input
AGAIN:
MOV C,P1.7 ;read SW status into CF
JC OVER ;jump if SW=1
MOV P2,#’N’ ;SW=0, issue ‘N’ to P2
SJMP AGAIN ;keep monitoring
OVER:
MOV P2,#’Y’ ;SW=1, issue ‘Y’ to P2
SJMP AGAIN ;keep monitoring
19. A switch is connected to pin P1.0 and an LED to pin P2.7. Write a program to get the status of the
switch and send it to the LED.
Ans:
SENSE, VIT Page 21 of 43
Reg no: 16BEC0198 FALL~2018-19 ECE3003 - 𝜇C and its applications
20. Write code to send 55H to ports P1 and P2, using (a) their names (b) their addresses
Ans:a) Using names
ORG 0000H
MOV A,#55H
MOV P1,A
MOV P2,A
END
b) Using addresses
ORG 0000H
MOV 90H,#55H
MOV A0H,#55H
END
21. Show the code to push R5 and A onto the stack and then pop them back them into R2 and B, where B
= A and R2 = R5
Ans: ORG 0000H
PUSH R5
PUSH A
POP B
POP R2
END
22. Write a program to copy the value 55H into RAM memory locations 40H to 41H using
(a) direct addressing mode, (b) register indirect addressing mode without a loop, and (c) with a loop
Ans: a) ORG 0000H
MOV 40H,#55H
MOV 41H,#55H
b) ORG 0000H
MOV R1,#40H
MOV R2,#41H
MOV @R1,#55H
MOV @R2,#55H
END
C) ORG 0000H
MOV R1,#40H
MOV R0,#2
HERE: MOV @R1,#55H
SENSE, VIT Page 22 of 43
Reg no: 16BEC0198 FALL~2018-19 ECE3003 - 𝜇C and its applications
INC R1
DJNZ R0,HERE
END
23. Write a program to clear 16 RAM locations starting at RAM address 60H
Ans: ORG 0000H
MOV R0,#16
MOV R1,#60H
BACK: CLR A
MOV @R1,A
INC R1
DJNZ R0, BACK
END
24. Write a program to copy a block of 10 bytes of data from 35H to 60H.
Ans: ORG 0000H
MOV R0,#10
MOV R1,#35H
MOV R2,#60H
BACK: MOV A,@R1
MOV @R2,A
INC R1
INC R2
DJNZ R0, BACK
END
25. Write a program to get the x value from P1 and send x2 to P2, continuously.
Ans: ORG 0000H
HERE:MOV A,P1
MOV P2,A
SJMP HERE
END
26. Write a program to toggle P1 a total of 200 times. Use RAM location 32H to hold your counter value
instead of registers R0 – R7.
Ans: ORG 0000H
MOV 32H,#200
HERE: CPL P1
DJNZ 32H, HERE
END
28. While there are instructions such as JNC and JC to check the carry flag bit (CY), there are no such
instructions for the overflow flag bit (OV).How would you write code to check OV?
Ans: using JNB PSW.2, HERE
29. While a program to save the status of bit P1.7 on RAM address bit 05.
Ans:
ORG 0000H
MOV C,P1.7
MOV 05,C
END
30. Write a program to see if the RAM location 37H contains an even value. If so, send it to P2. If not,
make it even and then send it to P2.
Ans:
ORG 0000H
MOV A,37H
JNB ACC.0, HERE
INC A
HERE: MOV P2,A
END
31. The status of bits P1.2 and P1.3 of I/O port P1 must be saved before they are changed. Write a program
to save the status of P1.2 in bit location 06 and the status of P1.3 in bit location 07.
Ans:ORG 0000H
CLR 06 ;clear bit addr. 06
CLR 07 ;clear bit addr. 07
NEXT: JNB P1.2,OVER ;check P1.2, if 0 then jump
SETB 06 ;if P1.2=1,set bit 06 to 1
OVER: JNB P1.3,NEXT ;check P1.3, if 0 then jump
SETB 07 ;if P1.3=1,set bit 07 to 1
END
32. A switch is connected to pin P1.7 and an LED to pin P2.0. Write a program to get the status of the
switch and send it to the LED.
Ans:
1. If SW=0, LED is OFF
2. If SW=1, LED os ONDS
Ans: ORG 0000H
SETB P1.7 ;make P1.7 an input
AGAIN:
JB P1.7,OVER ;jump if P1.7=1
CLR P2.0 ;SW=0, LED is switched OFF
SJMP AGAIN ;keep monitoring
OVER:
SETB P2.0 ;SW=1, LED is switched ON
SJMP AGAIN
END
33. Assume that bit P2.3 is an input and represents the condition of an oven. If it goes high, it means that
the oven is hot. Monitor the bit continuously. Whenever it goes high, send a high-to-low pulse to port
P1.5 to turn on a buzzer.
Ans: ORG 0000H
HERE: JNB P2.3,HERE ;keep monitoring for high
SETB P1.5 ;set bit P1.5=1
CLR P1.5 ;make high-to-low
SJMP HERE ;keep repeating
END
34. A switch is connected to pin P1.7. Write a program to check the status of the switch and make the
following decision.
(a) If SW = 0, send “0” to P2 (b) If SW = 1, send “1“ to P2
Solution:
SW EQU P1.7
MYDATA EQU P2
HERE: MOV C,SW
JC OVER
MOV MYDATA,#’0’
SJMP HERE
OVER: MOV MYDATA,#’1’
SJMP HERE
END
35. Assume that the on-chip ROM has a message. Write a program to copy it from code space into the
upper memory space starting at address 80H. Also, as you place a byte in upper RAM, give a copy to
P0.
Ans: ORG 0
MOV DPTR,#MYDATA
MOV R1,#80H ;access the upper memory
B1: CLR A
MOVC A,@A+DPTR ;copy from code ROM
MOV @R1,A ;store in upper memory
MOV P0,A ;give a copy to P0
JZ EXIT ;exit if last byte
INC DPTR ;increment DPTR
INC R1 ;increment R1
SJMP B1 ;repeat until last byte
EXIT: SJMP $ ;stay here when finished
;---------------
ORG 300H
MYDATA: DB “The Promise of World Peace”,0
END
36. Assume that RAM locations 40 – 44H have the following values. Write a program to find the sum of
the values. At the end of theprogram, register A should contain the low byte and R7 the high byte.
40 = (7D) 41 = (EB) 42 = (C5) 43 = (5B) 44 = (30) MOV R0,#40H ;load pointer
MOV R2,#5; load counter
Ans:
CLR A ; A=0
MOV R7,A ;clear R7
AGAIN: ADD A,@R0 ;add the byte ptr to by R0
JNC NEXT ;if CY=0 don’t add carry
INC R7 ;keep track of carry
NEXT: INC R0 ;increment pointer
DJNZ R2,AGAIN ;repeat until R2 is zero
37. Write a program to add two 16-bit numbers. Place the sum in R7 and R6; R6 should have the lower
byte.
Solution: -
CLR C
SENSE, VIT Page 25 of 43
Reg no: 16BEC0198 FALL~2018-19 ECE3003 - 𝜇C and its applications
MOV A, #0E7H
ADD A, #80H
MOV R6, A ; low byte
MOV A, #3CH
ADDC A, #3BH
MOV R7, A ; high byte
38. Assume that 5 BCD data items are stored in RAM locations starting at 40H, as shown below. Write a
program to find the sum of all the numbers. The result must be in BCD.
40=(71) 41=(11) 42=(65) 43=(59) 44=(37)
Ans:
Solution: -
MOV R0, #40H ; R0 is a pointer
MOV R2, #5 ; 5 iterations
CLR A
MOV R7, A
Again: ADD A, @R0 ; loop
DA A ; adjust for BCD
JNC Next ; Jump if C=0
INC R7 ; C=1
Next: INC R0 ; C=0
DJNZ R2, Again
39. (a) Write a program to get hex data in the range of 00 – FFH from port 1 and convert it to decimal.
Save it in R7, R6 and R5.
(b) Assuming that P1 has a value of FDH for data, analyze program.
Ans:
MOV A,#0FFH
MOV P1,A ;make P1 an input port
MOV A,P1 ;read data from P1
MOV B,#10 ;B=0A hex
DIV AB ;divide by 10
MOV R7,B ;save lower digit
MOV B,#10
DIV AB ;divide by 10 once more
MOV R6,B ;save the next digit
MOV R5,A ;save the last digit
40. Write a program to read the temperature and test it for the value 75. According to the test results, place
the temperature value into the registers indicated by the following.
If T = 75 then A = 75 If T < 75 then R1 = T If T > 75 then R2 = T
Solution:
MOV P1,#0FFH ;make P1 an input port
MOV A,P1 ;read P1 port
CJNE A,#75,OVER ;jump if A is not 75
SJMP EXIT ;A=75, exit
OVER:
JNC NEXT ;if CY=0 then A>75
MOV R1,A ;CY=1, A<75, save in R1
SJMP EXIT ; and exit
NEXT:
MOV R2,A ;A>75, save it in R2
EXIT: ...
41. Write a program to transfer value 41H serially (one bit at a time) via pin P2.1. Put two highs at the
start and end of the data. Send the byte LSB first.
Solution:
MOV A,#41H
SETB P2.1 ;high
SETB P2.1 ;high
MOV R5,#8
AGAIN:
RRC A
MOV P2.1,C ;send CY to P2.1
DJNZ R5,AGAIN
SETB P2.1 ;high
SETB P2.1 ;high
42. Write a program to bring in a byte of data serially one bit at a time via pin P2.7 and save it in register
R2. The byte comes in with the LSB first.
Solution:
MOV R5,#8
AGAIN:
MOV C,P2.7 ;bring in bit
RRC A
DJNZ R5, AGAIN
MOV R2,A ;save it
43. Assume that bit P2.2 is used to control an outdoor light and bit P2.5 a light inside a building. Show
how to turn on the outside light and turn off the inside one.
Solution:
SETB C ;CY = 1
ORL C,P2.2 ;CY = P2.2 ORed w/ CY
MOV P2.2,C ;turn it on if not on
CLR C ;CY = 0
ANL C,P2.5 ;CY = P2.5 ANDed w/ CY
MOV P2.5,C ;turn it off if not off
DJNZ R7,AGAIN
45. Assume that the lower three bits of P1 are connected to three switches. Write a program to send the
following ASCII characters to P2 based on the status of the switches.
000 ‘0’ 001 ‘1’ 010 ‘2’ 011 ‘3’ 100 ‘4’ 101 ‘5’ 110 ‘6’ 111 ‘7’
Solution: -
ORG 0000H ;
MOV DPTR, 300H ;
MOV A, P1 ; GET STATUS OF P1
ANL A, 07H ; MASK ALL BUT LOWER 3 BITS
MOV A, @A+DPTR ; GET CORRESPONDING VALUE
MOV P2, A ; MOVE VALUE TO P2 DISPLAY IT
ORG 0300H ;
DB '0','1','2','3','4','5','6','7';
END
46. a. The following program generates a square wave on P1.5 continuously using timer 1 for a time delay.
Find the frequency of the square wave if XTAL = 11.0592 MHz. In your calculation do not include the
overhead due to Instructions in the loop.
MOV TMOD,#10;Timer 1, mod 1 (16-bitmode)
AGAIN: MOV TL1,#34H ;TL1=34H, low byte of timer
MOV TH1,#76H ;TH1=76H, high byte timer
SETB TR1 ;start the timer 1
BACK: JNB TF1,BACK ;till timer rolls over
CLR TR1 ;stop the timer 1
CPL P1.5 ;comp. p1. to get hi, lo
CLR TF1 ;clear timer flag 1
SJMP AGAIN ;is not auto-reload
Solution: -
Since FFFFH – 7634H = 89CBH + 1 = 89CCH and 89CCH = 35276 clock count and 35276 × 1.085
us = 38.274 ms for half of the square wave. The frequency = 13.064Hz. Also notice that the high
portion and low portion of the square wave pulse are equal. In the above calculation, the overhead
due to all the instruction in the loop is not included.
Finding the Loaded Timer Values
To calculate the values to be loaded into the TL and TH registers, look at the following example
• Assume XTAL = 11.0592 MHz, we can use the following steps for finding the TH, TL registers’
values
1. Divide the desired time delay by 1.085 us
2. Perform 65536 – n, where n is the decimal value we got in Step1
3. Convert the result of Step2 to hex, where yyxx is the initial hex value to be loaded into the
timer’s register
4. Set TL = xx and TH = yy
b.. Assume that XTAL = 11.0592 MHz. What value do we need to load the timer’s register if we want to
have a time delay of 5 ms (milliseconds)? Show the program for timer 0 to create a pulse width
of 5 ms on P2.3.
Solution:
Since XTAL = 11.0592 MHz, the counter counts up every 1.085 us. This means that out of many
1.085 us intervals we must make a 5 ms pulse. To get that, we divide one by the other. We need 5
ms / 1.085 us = 4608 clocks. To Achieve that we need to load into TL and TH the value 65536 – 4608
= EE00H. Therefore, we have TH = EE and TL = 00.
CLR P2.3 ;Clear P2.3
MOV TMOD,#01 ;Timer 0, 16-bitmode
SENSE, VIT Page 28 of 43
Reg no: 16BEC0198 FALL~2018-19 ECE3003 - 𝜇C and its applications
HERE:
MOV TL0,#0 ;TL0=0, the low byte
MOV TH0,#0EEH ;TH0=EE, the high byte
SETB P2.3 ;SET high P2.3
SETB TR0 ;Start timer 0
AGAIN:
JNB TF0,AGAIN ;Monitor timer flag 0
CLR TR0 ;Stop the timer 0
CLR TF0 ;Clear timer 0 flag
47.a. Assume that XTAL = 11.0592 MHz, write a program to generate a square wave of 2 kHz frequency
on pin P1.5.
Solution:
This is similar to Examples above, except that we must toggle the bit to generate the square wave.
Look at the following steps.
(a) T = 1 / f = 1 / 2 kHz = 500 us the period of square wave.
(b) 1 / 2 of it for the high and low portion of the pulse is 250 us.
(c) 250 us / 1.085 us = 230 and 65536 – 230 = 65306 which in hex is FF1AH.
(d) TL = 1A and TH = FF, all in hex. The program is as follow.
47.b. Assume XTAL = 11.0592 MHz, write a program to generate a square wave of 50 kHz frequency on
pin P2.3.
Solution:
Look at the following steps.
(a) T = 1 / 50 = 20 ms, the period of square wave.
(b) 1 / 2 of it for the high and low portion of the pulse is 10 ms.
(c) 10 ms / 1.085 us = 9216 and 65536 – 9216 = 56320 in decimal, and in hex it is DC00H.
(d) TL = 00 and TH = DC (hex).
48.a. Examine the following program and find the time delay in seconds. Exclude the overhead due to the
instructions in the loop.
MOV TMOD,#10H ;Timer 1, mod 1
MOV R3,#200 ;cnter for multiple delay
AGAIN: MOV TL1,#08H ;TL1=08,low byte of timer
MOV TH1,#01H ;TH1=01,high byte
SETB TR1 ;Start timer 1
BACK: JNB TF1,BACK ;until timer rolls over
CLR TR1 ;Stop the timer 1
CLR TF1 ;clear Timer 1 flag
DJNZ R3,AGAIN ;if R3 not zero then ;reload timer
Solution:
200*70.820ms=14.164s
48.b. Assume XTAL = 11.0592 MHz, find the frequency of the square wave generated on pin P1.0 in the
following program
MOV TMOD,#20H ;T1/8-bit/auto reload
Solution: -
MOV TM0D,#01100000B ;counter 1, mode 2,
;C/T=1 external pulses
MOV TH1,#0 ;clear TH1
SETB P3.5 ;make T1 input
SENSE, VIT Page 30 of 43
Reg no: 16BEC0198 FALL~2018-19 ECE3003 - 𝜇C and its applications
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, we make P3.5 an input port by making it high. In other words, we
must configure (set high) the T1 pin (pin P3.5) to allow pulses to be fed into it.
Answers:
1.
2.
a) ORG 0000H
MOV R0,#50H
MOV A,#99H
ADDC A,R0
MOV R1,A
END
c) ORG 0000H
MOV R0,#23H
MOV R1,#06H
HERE: INC R0
DJNZ R1,HERE
END
3. a) ORG 0000H
MOV P2,00H ;CONFIGURING PORT2
AS OUTPUT PORT
SETB P2.2
CLR P2.5
END
b) ORG 0000H
MOV A,P0
ANL A,P1
MOV P2,A
END
4.
ORG 0000H
SETB P1.7;make P1.7
an input
AGAIN: JB
P1.2,OVER ;jump if
P1.7=1
5.
ORG 0000H
MOV TMOD,#01;Timer 0, mode 1(16-bit mode)
HERE:MOV TL0,#0F2H;TL0=F2H, the low byte
MOV TH0,#0FFH;TH0=FFH, the high byte
CPL P1.5;toggle P1.5
ACALL DELAY
SJMP HERE
DELAY: SETB TR0;start the timer 0
AGAIN:JNB TF0,AGAIN;monitor timer flag 0 ;until it rolls over
CLR TR0;stop timer 0
CLR TF0;clear timer 0 flag
RET
END
6. ORG 0000H
MOV TMOD, #01H
HERE: MOV TL0, #01AH
MOV TH0, #0FFH
CPL P1.5
ACALL DELAY
SJMP HERE
DELAY: SETB TR0
AGAIN: JNB TF0, AGAIN
CLR TR0
CLR TF0
RET
END
7. ORG 000H
LJMP MAIN
ORG 00BH
CPL P2.1
RETI
ORG 0030H
MAIN: MOV P0, #0FFH
MOV TMOD, #02H
MOV TH0, #-92
MOV SCON, #50H
MOV IE, #10000010B
SETB TR0
HERE: MOV A, P0
MOV P1, A
SJMP HERE
END
8. a ORG 000H
MOV P2, #0FFH
MOV TMOD, #20H
MOV TH1,-3
MOV SCON, #50H
SETB TR1
MOV DPTR, #MYDATA
LOOP1: CLR A
MOVC A,@A+DPTR
JZ LOOP2
ACALL SEND
INC DPTR
SJMP LOOP1
LOOP2: MOV A, P2
ACALL SEND
ACALL RECV
MOV P1, A
SJMP LOOP2
1. Calculate the amount of time required to execute the Programs used for developing your Project
Based Component (Embedded) with Microcontroller and its Applications.
2. Write the addressing Modes, Control word formats used in the Programs used for developing your
Project Based Component (Embedded) with Microcontroller and its Applications.
3. Calculate the amount of Memory required to store the Programs used for developing your Project
Based Component (Embedded) with Microcontroller and its Applications.
4. List the Type of Instruction used in the the Programs used for developing your Project Based
Component (Embedded) with Microcontroller and its Applications based on different parameters.
5. Draw the block diagram, Pin diagram, Register organization and Program status word, Special
function registers of IC used in your project and compare them with Intel 8051 IC diagrams.
ASSEBLY PROGRAM
Start:
MOV P3,#0FFH; ir
CLR P2.0;connected to IN1 of L293D
CLR P2.1;connected to IN2 of L293D
CheckIR: ACALL DELAY
MOV R7,P3;IR (1) read.
MOV A,R7
RRC A
JNC MotorForward; ;Jump when CY=0 i.e. IR is blocked.
SJMP CheckIR
BACK1:MOV R7, P3; IR(2) read.
MOV A,R7
RRC A
RRC A
JNC SecondIRDetected
SJMP BACK1MotorForward:SETB P2.0;This loop closes the gate.
CLR P2.1
ACALL DELAY1
ACALL DELAY1
ACALL DELAY
CLR P2.0
SJMP BACK1
SecondIRDetected:MOV R6,P3;Train is cutting IR(2) and is going to pass through it.
MOV A,R6
RRC A
RRC A
JC MotorReverse_Check_FirstIR; Check IR (1) when train crosses IR (2)
SJMP SecondIRDetected; Wait till train crosses IR (2)
MotorReverse_Check_FirstIR:MOV R5,P3
MOV A,R5
RRC A
JNC MotorReverse_Check_FirstIR;Keep looping when IR (1) is cut. Go down once train crosses IR (1)
SecondIR_Check:MOV R4,P3;Check IR (2) after making sure that train has crossed IR(1)
MOV A,R4
RRC A
RRC A
JNC SecondIR_Check;Keep looping when IR(2) is cut. Go down when train crosses IR(2)
CLR P2.0;This loop opens the gate.
SETB P2.1
SENSE, VIT Page 37 of 43
Reg no: 16BEC0198 FALL~2018-19 ECE3003 - 𝜇C and its applications
ACALL DELAY1
ACALL DELAY1
ACALL DELAY
CLR P2.1
SJMP Start
DELAY:MOV R7,#0FFH;Delay program 1
TOP:MOV R6,#0FFH
MOV R5,#0FFH
BACK:DJNZ R5,BACK
UP:DJNZ R6,UP
DJNZ R7,TOP
RET
DELAY1:MOV R4,#14H;Delay program 2
OneSec:MOV TMOD,#01H
MOV TL0,#0AFH
MOV TH0,#3CH
SETB TR0
WAIT:JNB TF0,WAIT
CLR TR0
CLR TF0
DJNZ R4, OneSec
RET
END
Label Mne monics Operands addressing Machine cycle Required Memory Type of
mode used Byte Instruction
Required
2 2
branch
ACALL DELAY
1 1
Bit
CLR P2.0 manipulator
2 2
branch
SJMP BACK1
SECOND REGISTER 1 1
MOV A,P3 Data
IRDETE
transfer
CTED:
1 1
logical
RRC A
1 1
logical
RRC A
2 2
Bit
JC MOTORREV manipulator
ERSE
2 2
branch
SJMP SECONDIRD
ETECTED
MOTOR REGISTER 1 1
MOV A,P3 Data
REVERS
transfer
E
1 1
logical
RRC A
2 2
Bit
JNC MOTORREV manipulator
ERSE
SECOND REGISTER 1 1
MOV A,P3 Data
IRCHEC
transfer
K
1 1
logical
RRC A
1 1
logical
RRC A
2 2
Bit
JNC SECONDIRC manipulator
HECK
1 1
Bit
CLR P2.0 manipulator
1 1
Bit
SETB P2.1 manipulator
2 2
branch
ACALL DELAY1
2 2
branch
ACALL DELAY
1 1
Bit
CLR P2.1 manipulator
2 2
branch
SJMP START
= 16581440*1.085us
=17.990s
Features
• Compatible with MCS®-51 Products
• 8K Bytes of In-System Programmable (ISP) Flash Memory – Endurance: 10,000 Write/Erase Cycles
• 4.0V to 5.5V Operating Range
• Fully Static Operation: 0 Hz to 33 MHz
• Three-level Program Memory Lock
• 256 x 8-bit Internal RAM
• 32 Programmable I/O Lines
• Three 16-bit Timer/Counters
• Eight Interrupt Sources
• Full Duplex UART Serial Channel
• Low-power Idle and Power-down Modes
• Interrupt Recovery from Power-down Mode
• Watchdog Timer
• Dual Data Pointer
• Power-off Flag
• Fast Programming Time
• Flexible ISP Programming (Byte and Page Mode)
Pin Diagram
Block diagram
A map of the on-chip memory area called the Special Function Register (SFR) space is shown in Table 5-1.
Note that not all of the addresses are occupied, and unoccupied addresses may not be implemented on the chip. Read
accesses to these addresses will in general return random data, and write accesses will have an indeterminate effect.
User software should not write 1s to these unlisted locations, since they may be used in future products to invoke new
features. In that case, the reset or inactive values of the new bits will always be 0.
Timer 2 Registers: Control and status bits are contained in registers T2CON (shown in Table 5- 2) and T2MOD (shown in Table
10-2) for Timer 2. The register pair (RCAP2H, RCAP2L) are the Capture/Reload registers for Timer 2 in 16-bit capture mode or
16-bit auto-reload mode.
Interrupt Registers: The individual interrupt enable bits are in the IE register. Two priorities can be set for each of the six
interrupt sources in the IP register.
Dual Data Pointer Registers: To facilitate accessing both internal and external data memory, two banks of 16-bit Data
Pointer Registers are provided: DP0 at SFR address locations 82H-83H and DP1 at 84H-85H. Bit DPS = 0 in SFR AUXR1 selects
DP0 and DPS = 1 selects DP1. The user should ALWAYS initialize the DPS bit to the appropriate value before accessing the
respective Data Pointer Register.
Power Off Flag: The Power Off Flag (POF) is located at bit 4 (PCON.4) in the PCON SFR. POF is set to “1” during power
up. It can be set and rest under software control and is not affected by reset.