SlideShare a Scribd company logo
SHRI MADHWA VADIRAJA INSTITUTE OF
TECHNOLOGY AND MANAGEMENT
Microcontroller
18EC46
Venugopala Rao A S
Dept. of CSE
venugopalrao.cs@sode-edu.in
8051 Stack, I/O Port Interfacing and Programming:
• Module-3: 8051 Stack, I/O Port Interfacing and Programming:
• 8051 Stack [T1: 2.7],
• Stack and Subroutine instructions [T2: 8.3],
• Assembly language program examples on subroutine and involving loops.[T1:
Ex:3-11, 3-12],
• Assembly language program examples on subroutine and involving loops.[T1:
Ex:4-7],
• Interfacing simple switch and LED to I/O ports to switch on/off LED with respect
to switch status.
2
8/3/2022 18EC46
8051 Stack, I/O Port Interfacing and Programming:
• 8051 Stack
• The stack is a section of RAM used by the CPU to store information temporarily
• This information could be data or an address
• The register used to access the stack is called the SP (stack pointer) register
• The stack pointer in the 8051 is 8 bit wide, hence it can take value of 00 to FFH
• When the 8051 is powered up, the SP register contains value 07
• RAM location 08 is the first location begin used for the stack by the 8051
3
8/3/2022 18EC46
8051 Stack, I/O Port Interfacing and Programming:
• The storing of a CPU register in the stack is called ___
• PUSH
• SP is pointing to the __
• last used location of the stack
• As we push data onto the stack, the SP is ____
• incremented by one
• Loading the contents of the stack back into a CPU register is called a ___
• POP
• With every pop, the top byte of the stack is copied to the register specified by the
instruction and the stack pointer is decremented once
4
8/3/2022 18EC46
8051 Stack, I/O Port Interfacing and Programming:
• Show the stack and stack pointer from the following. Assume the default stack
area.
• MOV R6, #25H
• MOV R1, #12H
• MOV R4, #0F3H
• PUSH R6
• PUSH R1
• PUSH R4
• Solution:
5
8/3/2022 18EC46
8051 Stack, I/O Port Interfacing and Programming:
• Examining the stack, show the contents of the register and SP after execution of
the following instructions. All value are in hex.
• POP R3 ; POP stack into R3
• POP R5 ; POP stack into R5
• POP R2 ; POP stack into R2
6
8/3/2022 18EC46
8051 Stack, I/O Port Interfacing and Programming:
• The CPU also uses the stack to save the address of the instruction just below the
CALL instruction
• This is how the CPU knows where to resume when it returns from the called
subroutine.
• Why to increment SP after push?
• To make sure that the stack is growing from lower to upper addresses (to 7FFH)
• If the stack pointer were decremented after push ???
• We would be using RAM locations 7, 6, 5, etc. which belong to R7 to R0 of bank
0, the default register bank
• When 8051 is powered up, register bank 1 and the stack are using the same
memory space
• We can reallocate another section of RAM to the stack
7
8/3/2022 18EC46
8051 Stack, I/O Port Interfacing and Programming:
• Call and RET
• Already covered
• Interrupts and Returns
• An interrupt is a hardware-generated call.
• Similar to a call opcode which can automatically access a subroutine, certain pins
on the 8051 can cause a call when external electrical signals on them go to a low
state.
• Internal operations of the timers and the serial port can also cause an interrupt call
to take place.
• The subroutines called by an interrupt are located at fixed hardware addresses and
are called as Interrupt Service Routine (ISR).
8
8/3/2022 18EC46
8051 Stack, I/O Port Interfacing and Programming:
• When an interrupt call takes place, hardware interrupt disable flip-flops are set.
• This is to prevent another interrupt of the same priority level from taking place.
• Disabled Hardware interrupts can be enabled back by executing an interrupt
return instruction. (Generally written at the end of the interrupt subroutine.)
9
8/3/2022 18EC46
INTERRUPT ADDRESS (HEX) CALLED
IE0 0003
TF0 0008
IEl 0013
TFl 0018
SERIAL 0023
8051 Stack, I/O Port Interfacing and Programming:
• RETI
• Pops two bytes from the stack into the program counter(PC) and reset the interrupt
enable Flip-Flops
• The only difference between the RET and RETI instructions is the enabling of the
interrupt logic when RETI is used.
• RET is used at the ends of subroutines called by an opcode. RETI is used by
subroutines called by an interrupt.
10
8/3/2022 18EC46
8051 Stack, I/O Port Interfacing and Programming:
• TIME DELAY CALCULATION IN 8051:
• Machine Cycle in 8051 :
• The CPU takes a certain number of clock cycles to execute an instruction.
• These clock cycles are referred to as machine cycles.
• The length of the machine cycle depends on the frequency of the crystal
oscillator connected to the 8051 system.
• The crystal oscillator frequency can vary from 4MHz to 30MHz.
• To make the 8051 system compatible with the serial port of the personal computer
PC, 11.0592MHz crystal oscillators is used.
11
8/3/2022 18EC46
8051 Stack, I/O Port Interfacing and Programming:
• In the 8051, one machine cycle lasts 12 oscillator periods.
• So to calculate the machine cycle, we take 1/12 of the crystal frequency, then take
the inverse of it results in time period. i.e frequency = 1/time period.
• E.g.: Find the time period of the machine cycle in each case for the following
crystal frequency of different 8051 based systems: 11.0592 MHz, 16 MHz, 20
MHz
• Solution:
• Crystal Frequency: 11.0592 MHz:
• 11.0592/12 = 921.6 KHz
• Machine cycle = 1/921.6 KHz = 1.085us [us=microsecond]
12
8/3/2022 18EC46
8051 Stack, I/O Port Interfacing and Programming:
• Crystal Frequency: 16 MHz:
• 16MHz/12 = 1.333 MHz
• Machine cycle = 1/1.333 MHz = 0.75us [us=microsecond]
• Crystal Frequency: 20MHz:
• 20MHz/12 = 1.66 MHz
• Machine Cycle = 1/1.66 MHz = 0.60us [us=microsecond]
13
8/3/2022 18EC46
8051 Stack, I/O Port Interfacing and Programming:
• Now let us find how long it takes to execute each of the following instructions, for
a crystal frequency of 11.0592 MHz. The machine cycle of a system of 11.0592
MHz is 1.085 us.
14
8/3/2022 18EC46
INSTRUCTION MACHINE CYCLE TIME TO EXECUTE
MOV R2,#55H 1 1x1.085 us = 1.085 us
DEC R2 1 1x1.085 us = 1.085 us
DJNZ R2,target 2 2x1.085 us = 2.17 us
LJMP 2 2x1.085 us = 2.17 us
SJMP 2 2x1.085 us = 2.17 us
NOP 1 1x1.085 us = 1.085 us
MUL AB 4 4x1.085 us = 4.34 us
8051 Stack, I/O Port Interfacing and Programming:
• How to Calculate Exact Time Delay in 8051 microcontroller?
• The delay subroutine consists of 2 parts: Setting a counter and creating a loop.
• Let us assume that the crystal frequency of 11.0592 MHz is connected
• Consider the following code
• MOV A,#55H
• AGAIN: MOV P1,A
• ACALL DELAY
• CPL A
• SJMP AGAIN
• ;-----------------Time Delay
• DELAY: MOV R3,#225
• HERE: DJNZ R3,HERE
• RET
15
8/3/2022 18EC46
8051 Stack, I/O Port Interfacing and Programming:
• Machine cycles for each instruction of the DELAY subroutine is as shown below.
• Therefore, we have a time delay of [(255 x 2) + 1 + 1] x 1.085 us = 555.52 us
• Very often we used to calculate the time delay based on the instructions inside the
loop and ignore the clock cycles associated with the instructions outside the loop.
16
8/3/2022 18EC46
DELAY: MOV R2,#255 Machine Cycle = 1
HERE: DJNZ R2,HERE Machine Cycle = 2
RET Machine Cycle = 1
8051 Stack, I/O Port Interfacing and Programming:
• NOP Instruction:
• NOP instruction is used to increase the delay in the loop. NOP means "No
Operation" simply wastes time.
• Loop Inside a Loop Delay:
• This method is used to get a larger delay i.e. is used to loop inside a loop, which is
also called a nested loop.
• E.g.: Lets find the time delay for the following subroutine with 11.0592 MHz
crystal frequency is connected to the 8051 system.
17
8/3/2022 18EC46
8051 Stack, I/O Port Interfacing and Programming:
• The time delay inside the HERE loop [255(1+1+1+1+2)] x 1.085 us = 1660.05 us
• The time delay of the two instructions outside the loop is:
• 1660.05 us+[1+1]x1.085 us = 1662.67 us
18
8/3/2022 18EC46
DELAY: MOV R2,#255 Machine Cycle = 1
HERE: NOP Machine Cycle = 1
NOP Machine Cycle = 1
NOP Machine Cycle = 1
NOP Machine Cycle = 1
DJNZ R2,HERE Machine Cycle = 2
RET 1
8051 Stack, I/O Port Interfacing and Programming:
• E.g.: For a crystal frequency of 11.0592 MHz, find the time delay in the following
subroutine. The machine cycle is 1.085 us.
• 'HERE' Loop Calculations: 1+1+2, so [(1+1+2)x250] x 1.085 us = 1085 us.
19
8/3/2022 18EC46
DELAY: MOV R2,#200
AGAIN: MOV R3,#250
HERE: NOP
NOP
DJNZ R3,HERE
DJNZ R2,AGAIN
RET
Machine Cycle = 1
Machine Cycle = 1
Machine Cycle = 1
Machine Cycle = 1
Machine Cycle = 2
Machine Cycle = 2
Machine Cycle = 1
8051 Stack, I/O Port Interfacing and Programming:
• 'AGAIN' Loop Calculations:
• In this loop "MOV R3,#250" and "DJNZ R2,AGAIN" at the begining and end of
the AGAIN loop give delay of [(1+2)x200]x1.085 us = 651us
• The AGAIN loop repeats the HERE loop 200 times so 200x1085 us = 217000 us.
• The total time delay is 217000 us + 651 us = 217651 us or 217.651milliseconds.
• The time is approximate as we have ignored the first and the last instructions in
the subroutine i.e. DELAY: "MOV R2,#200" and "RET".
20
8/3/2022 18EC46
8051 Stack, I/O Port Interfacing and Programming:
• I/O Port Interfacing and Programming
21
8/3/2022 18EC46
8051 Stack, I/O Port Interfacing and Programming:
• The four 8-bit I/O ports P0, P1, P2 and P3 each uses 8 pins
• All the ports upon RESET are configured as input, ready to be used as input ports
• When the first 0 is written to a port, it becomes an output
• To reconfigure it as an input, a 1 must be sent to the port
• Port 0
• Port 0 can be used for input or output, 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
• Open drain is a term used for MOS chips in the same way
that open collector is used for TTL chips
22
8/3/2022 18EC46
8051 Stack, I/O Port Interfacing and Programming:
• The following code will continuously send out to port 0 the alternating value 55H
and AAH
• BACK: MOV A,#55H
• MOV P0,A
• ACALL DELAY
• MOV A,#0AAH
• MOV P0,A
• ACALL DELAY
• SJMP BACK
• In order to make port 0 an input, the port must be programmed by writing 1 to all
the bits
23
8/3/2022 18EC46
ORG 300H ;put DELAY at address 300H
DELAY:MOV R5,#0FFH ; R5=255 counter
AGAIN:DJNZ R5,AGAIN ; stay here until R5 = 0
RET ;return to caller (when R5 =0)
END ;end of asm file
8051 Stack, I/O Port Interfacing and Programming:
• E.g.: Program to configure Port 0 as an input port and then receive data from that
port and sent to P1
• MOV A,#0FFH ;A=FF hex
• MOV P0,A ;make P0 an i/p port by writing it all 1s
• BACK: MOV A,P0 ;get data from P0
• MOV P1,A ;send it to port 1
• SJMP BACK ;keep doing it
• Port 1
• Port 1 can be used as input or output
• 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 input port
• To make port 1 an input port, it must be programmed by writing 1 to all its bits
24
8/3/2022 18EC46
8051 Stack, I/O Port Interfacing and Programming:
• Port 1 to Port 3 Structures and their operation:
• All the ports have the following components in them
• D latch
• Output driver
• Input buffer
• Port 1 Structure:
• In the figure, Internal FET pull-up is the internal load for P1 which is present in
P2 and P3 as well but not found in P0.
• 8051 ports have both latch and buffer.
• While reading the data, it can be either reading from the pin or from the latch
25
8/3/2022 18EC46
8051 Stack, I/O Port Interfacing and Programming:
• Reading the input pin:
• To make any port pin as input, we must write 1(Logic High) to that pin.
• Writing High (1) to the port bit means that the High is given to the Latch (D flip-
flop) input.
• This makes Q to go high and Q` to go low
• Since Q` is low and connected to gate of Transistor M1, M1 goes to OFF state.
• When M1 is OFF, it blocks any path to ground and hence input signal directly
goes to the tri-state buffer.
• When we read the input port through instructions such as MOV A, P1 we are
reading the data present at the input pin. This instruction activates the read pin of
the tri-state buffer and lets the data at the pin to go into CPU’s internal bus
26
8/3/2022 18EC46
8051 Stack, I/O Port Interfacing and Programming:
• Writing zero to port:
• To make any bits of any ports as output port we must write ZERO onto it.
• If we write a ZERO to any port bit, then Q=0 and Q`=1 which makes transistor to
go to ON state.
• This provides a path to ground for both load and input pin.
• So when we try to read from input pin it always gives a LOW irrespective of the
status of the input bit
•
27
8/3/2022 18EC46
8051 Stack, I/O Port Interfacing and Programming:
• Port 2 and Port 3
• Port 2 can be used as input or output
• Just like P1, port 2 also does not need any pull-up resistors
• Upon reset, port 2 is configured as an input port
• To make port 2 an input port, it must be programmed by writing 1 to all its bits.
• Port 3 can be used as input or output
• Port 3 does not need any pull-up resistors
• Port 3 is configured as an input port upon reset.
28
8/3/2022 18EC46
8051 Stack, I/O Port Interfacing and Programming:
29
8/3/2022 18EC46
8051 Stack, I/O Port Interfacing and Programming:
• E.g.: Program to continuously send out to port 1 the alternating value 55H and
AAH
• MOV A,#55H
• BACK: MOV P1,A
• ACALL DELAY
• CPL A
• SJMP BACK
30
8/3/2022 18EC46
8051 Stack, I/O Port Interfacing and Programming:
• E.g.: Program to configure Port 1 first as an input port then receive data from that
port and save it in R7 and R5
• MOV A,#0FFH ;A=FF hex
• MOV P1,A ;make P1 an input port by writing it all 1s
• MOV A,P1 ;get data from P1
• MOV R7,A ;save it to in reg R7
• ACALL DELAY ;wait
• MOV A,P1 ;another data from P1
• MOV R5,A ;save it to in reg R5
31
8/3/2022 18EC46
8051 Stack, I/O Port Interfacing and Programming:
• I/O Programming- Different ways of Accessing Entire 8 Bits
• Program to access the entire 8 bits of Port 1
• BACK: MOV A,#55H
• MOV P1, A
• ACALL DELAY
• MOV A,#0AAH
• MOV P1,A
• ACALL DELAY
• SJMP BACK
• Can we rewrite the code in a more efficient manner ?
• Access the port directly without going through the accumulator
32
8/3/2022 18EC46
BACK:MOV P1,#55H
ACALL DELAY
MOV P1,#0AAH
ACALL DELAY
SJMP BACK
MOV A,#55H
BACK:MOV P1,A
ACALL DELAY
CPL A
SJMP BACK
8051 Stack, I/O Port Interfacing and Programming:
• Write a program to perform the following:
• (a) Keep monitoring the P1.2 bit until it becomes high
• (b) When P1.2 becomes high, write value 45H to port 0
• (c) Send a high to low (H to L) pulse to P2.3
• SETB P1.2 ;make P1.2 an input
• MOV A,#45H ;A=45H
• AGAIN: JNB P1.2,AGAIN ;get out when P1.2=1
• MOV P0,A ;issue A to P0
• SETB P2.3 ;make P2.3 high
• CLR P2.3 ; make P2.3 low for H to L
33
8/3/2022 18EC46
8051 Stack, I/O Port Interfacing and Programming:
• Write a program to toggle the bits of port1 with delay which depends on the value
of a number in R0.
• ORG 0
• BACK:MOV A,#0H
• MOV P1,A ; Send 0h to port P1
• MOV R0, #30H ; count value for generating delay
ACALL DELAY ; Call the subroutine DELAY
• CPL A ; Complement A to toggle
• MOV P1,A ; Send AAh to port1
• MOV R0, #0FFH ; count value for generating delay
ACALL DELAY ; Call the subroutine DELAY
• SJMP BACK ; Keep doing this indefinitely
34
8/3/2022 18EC46
8051 Stack, I/O Port Interfacing and Programming:
• ------Delay Subroutine---------
• ORG 300H
• DELAY: NOP ;Do nothing
• AGAIN: DJNZ R0,AGAIN ;Stay here until R0 becomes 0
• RET ;Return to the Called program
• END
35
8/3/2022 18EC46
8051 Stack, I/O Port Interfacing and Programming:
• Assume that P2.3 is an input port pin and represents the condition of an oven.
Monitor the bit continuously. If it goes high, it means that the oven is hot.
Whenever it goes high, send a high-to-low pulse to port P1.5 to turn on a buzzer.
• Solution:
• ORG 00H
• 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
36
8/3/2022 18EC46
8051 Stack, I/O Port Interfacing and Programming:
• 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
• Solution:
• SETB P1.7 ;make P1.7 an input
• AGAIN: JB P1.7,SENDY ;jump if P1.7=1
• MOV P2,#’N’ ;SW=0, issue ‘N’ to P2
• SJMP AGAIN ;keep monitoring
• SENDY: MOV P2,#’Y’ ;SW=1, issue ‘Y’ to P2
• SJMP AGAIN ;keep monitoring
37
8/3/2022 18EC46
8051 Stack, I/O Port Interfacing and Programming:
• A switch is connected to pin P1.0 and an LED to pin P2.7. Write a program to get
the status of switch and send it the LED.
• Solution:
• SETB P1.0 ;make P1.0 an input
• AGAIN: MOV C, P1.0 ;Read the switch status in to CF
• MOV P2.7,C ;Send the switch status on to LED
• SJMP AGAIN ;keep monitoring
38
8/3/2022 18EC46
8051 Stack, I/O Port Interfacing and Programming:
• A switch is connected to pin P1.0 and an LED to pin P2.7. Write a program to get
the status of switch and send it the LED.
• SETB P1.0 ;make P1.0 an input
• AGAIN: MOV C,P1.0 ;Read the switch status in to CF
• MOV P2.7,C ;Send the switch status on to LED
• SJMP AGAIN ;keep monitoring
39
8/3/2022 18EC46

More Related Content

PPT
Microcontroller 8051
PPTX
MICROPROCESSOR AND MICROCONTROLLER ARCHITECTURE
PPT
8051 microcontroller and it’s interface
PPTX
PPTX
PPTX
8051 Microcontroller Overview by Venkatrao Ramisetti
PPTX
Microcontroller Electronics Engineering Sem 4MODULE 1.pptx
PPT
Power Point Presentation Microcontrollers
Microcontroller 8051
MICROPROCESSOR AND MICROCONTROLLER ARCHITECTURE
8051 microcontroller and it’s interface
8051 Microcontroller Overview by Venkatrao Ramisetti
Microcontroller Electronics Engineering Sem 4MODULE 1.pptx
Power Point Presentation Microcontrollers

Similar to VTU 4th Semester ECE dept Microcontroller lecture slides module 3 (20)

PPT
MEMORY.ppt 8051/8052 MEMORY MANEGEMENT MEMORY DESCRIPTION
PDF
Architecture and Programmingpart1_Microcontroller
PPTX
1st unit - microcontroller architechture and pin diagram
PPT
MicrocontrollersII.ppt
PPT
introduction to Microcontrollers 8051.ppt
PDF
Unit3_all timer interfacing in microcontroller
PPTX
pic microcontroller unit 5 detailed .pptx
PDF
Lecture SOHIL LUHAR
PPTX
8051 microcontroller by K. Vijay Kumar
PPT
UNIT-5.ppt
PPTX
8051 microcontroller lecture EEE 3209.pptx
PPTX
8085 microprocessor(1)
PDF
EMBEDDED SYSTEMS AND IOT lab manual for enginnering students
PPTX
Microcontroller (8051) by K. Vijay Kumar
PDF
8051 Architecture, System Bus, Advantages
PPTX
8051 microcontroller
PDF
UNIT 1 8 BIT EMBEDDED PROCESSOR CS3691 6TH SEM CSE
PDF
Microcontroller .pdf
PPT
8051-MICROCONTROLLER all contents with diagram.ppt
PPT
janakiraman egsp collage I msc 4 unit
MEMORY.ppt 8051/8052 MEMORY MANEGEMENT MEMORY DESCRIPTION
Architecture and Programmingpart1_Microcontroller
1st unit - microcontroller architechture and pin diagram
MicrocontrollersII.ppt
introduction to Microcontrollers 8051.ppt
Unit3_all timer interfacing in microcontroller
pic microcontroller unit 5 detailed .pptx
Lecture SOHIL LUHAR
8051 microcontroller by K. Vijay Kumar
UNIT-5.ppt
8051 microcontroller lecture EEE 3209.pptx
8085 microprocessor(1)
EMBEDDED SYSTEMS AND IOT lab manual for enginnering students
Microcontroller (8051) by K. Vijay Kumar
8051 Architecture, System Bus, Advantages
8051 microcontroller
UNIT 1 8 BIT EMBEDDED PROCESSOR CS3691 6TH SEM CSE
Microcontroller .pdf
8051-MICROCONTROLLER all contents with diagram.ppt
janakiraman egsp collage I msc 4 unit
Ad

Recently uploaded (20)

PDF
R24 SURVEYING LAB MANUAL for civil enggi
PPTX
CH1 Production IntroductoryConcepts.pptx
PPTX
Current and future trends in Computer Vision.pptx
PPTX
UNIT 4 Total Quality Management .pptx
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PPTX
bas. eng. economics group 4 presentation 1.pptx
PPTX
Geodesy 1.pptx...............................................
PDF
Well-logging-methods_new................
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PDF
composite construction of structures.pdf
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PPTX
Internet of Things (IOT) - A guide to understanding
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
DOCX
573137875-Attendance-Management-System-original
R24 SURVEYING LAB MANUAL for civil enggi
CH1 Production IntroductoryConcepts.pptx
Current and future trends in Computer Vision.pptx
UNIT 4 Total Quality Management .pptx
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
bas. eng. economics group 4 presentation 1.pptx
Geodesy 1.pptx...............................................
Well-logging-methods_new................
Foundation to blockchain - A guide to Blockchain Tech
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
Embodied AI: Ushering in the Next Era of Intelligent Systems
CYBER-CRIMES AND SECURITY A guide to understanding
composite construction of structures.pdf
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
Internet of Things (IOT) - A guide to understanding
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
573137875-Attendance-Management-System-original
Ad

VTU 4th Semester ECE dept Microcontroller lecture slides module 3

  • 1. SHRI MADHWA VADIRAJA INSTITUTE OF TECHNOLOGY AND MANAGEMENT Microcontroller 18EC46 Venugopala Rao A S Dept. of CSE venugopalrao.cs@sode-edu.in
  • 2. 8051 Stack, I/O Port Interfacing and Programming: • Module-3: 8051 Stack, I/O Port Interfacing and Programming: • 8051 Stack [T1: 2.7], • Stack and Subroutine instructions [T2: 8.3], • Assembly language program examples on subroutine and involving loops.[T1: Ex:3-11, 3-12], • Assembly language program examples on subroutine and involving loops.[T1: Ex:4-7], • Interfacing simple switch and LED to I/O ports to switch on/off LED with respect to switch status. 2 8/3/2022 18EC46
  • 3. 8051 Stack, I/O Port Interfacing and Programming: • 8051 Stack • The stack is a section of RAM used by the CPU to store information temporarily • This information could be data or an address • The register used to access the stack is called the SP (stack pointer) register • The stack pointer in the 8051 is 8 bit wide, hence it can take value of 00 to FFH • When the 8051 is powered up, the SP register contains value 07 • RAM location 08 is the first location begin used for the stack by the 8051 3 8/3/2022 18EC46
  • 4. 8051 Stack, I/O Port Interfacing and Programming: • The storing of a CPU register in the stack is called ___ • PUSH • SP is pointing to the __ • last used location of the stack • As we push data onto the stack, the SP is ____ • incremented by one • Loading the contents of the stack back into a CPU register is called a ___ • POP • With every pop, the top byte of the stack is copied to the register specified by the instruction and the stack pointer is decremented once 4 8/3/2022 18EC46
  • 5. 8051 Stack, I/O Port Interfacing and Programming: • Show the stack and stack pointer from the following. Assume the default stack area. • MOV R6, #25H • MOV R1, #12H • MOV R4, #0F3H • PUSH R6 • PUSH R1 • PUSH R4 • Solution: 5 8/3/2022 18EC46
  • 6. 8051 Stack, I/O Port Interfacing and Programming: • Examining the stack, show the contents of the register and SP after execution of the following instructions. All value are in hex. • POP R3 ; POP stack into R3 • POP R5 ; POP stack into R5 • POP R2 ; POP stack into R2 6 8/3/2022 18EC46
  • 7. 8051 Stack, I/O Port Interfacing and Programming: • The CPU also uses the stack to save the address of the instruction just below the CALL instruction • This is how the CPU knows where to resume when it returns from the called subroutine. • Why to increment SP after push? • To make sure that the stack is growing from lower to upper addresses (to 7FFH) • If the stack pointer were decremented after push ??? • We would be using RAM locations 7, 6, 5, etc. which belong to R7 to R0 of bank 0, the default register bank • When 8051 is powered up, register bank 1 and the stack are using the same memory space • We can reallocate another section of RAM to the stack 7 8/3/2022 18EC46
  • 8. 8051 Stack, I/O Port Interfacing and Programming: • Call and RET • Already covered • Interrupts and Returns • An interrupt is a hardware-generated call. • Similar to a call opcode which can automatically access a subroutine, certain pins on the 8051 can cause a call when external electrical signals on them go to a low state. • Internal operations of the timers and the serial port can also cause an interrupt call to take place. • The subroutines called by an interrupt are located at fixed hardware addresses and are called as Interrupt Service Routine (ISR). 8 8/3/2022 18EC46
  • 9. 8051 Stack, I/O Port Interfacing and Programming: • When an interrupt call takes place, hardware interrupt disable flip-flops are set. • This is to prevent another interrupt of the same priority level from taking place. • Disabled Hardware interrupts can be enabled back by executing an interrupt return instruction. (Generally written at the end of the interrupt subroutine.) 9 8/3/2022 18EC46 INTERRUPT ADDRESS (HEX) CALLED IE0 0003 TF0 0008 IEl 0013 TFl 0018 SERIAL 0023
  • 10. 8051 Stack, I/O Port Interfacing and Programming: • RETI • Pops two bytes from the stack into the program counter(PC) and reset the interrupt enable Flip-Flops • The only difference between the RET and RETI instructions is the enabling of the interrupt logic when RETI is used. • RET is used at the ends of subroutines called by an opcode. RETI is used by subroutines called by an interrupt. 10 8/3/2022 18EC46
  • 11. 8051 Stack, I/O Port Interfacing and Programming: • TIME DELAY CALCULATION IN 8051: • Machine Cycle in 8051 : • The CPU takes a certain number of clock cycles to execute an instruction. • These clock cycles are referred to as machine cycles. • The length of the machine cycle depends on the frequency of the crystal oscillator connected to the 8051 system. • The crystal oscillator frequency can vary from 4MHz to 30MHz. • To make the 8051 system compatible with the serial port of the personal computer PC, 11.0592MHz crystal oscillators is used. 11 8/3/2022 18EC46
  • 12. 8051 Stack, I/O Port Interfacing and Programming: • In the 8051, one machine cycle lasts 12 oscillator periods. • So to calculate the machine cycle, we take 1/12 of the crystal frequency, then take the inverse of it results in time period. i.e frequency = 1/time period. • E.g.: Find the time period of the machine cycle in each case for the following crystal frequency of different 8051 based systems: 11.0592 MHz, 16 MHz, 20 MHz • Solution: • Crystal Frequency: 11.0592 MHz: • 11.0592/12 = 921.6 KHz • Machine cycle = 1/921.6 KHz = 1.085us [us=microsecond] 12 8/3/2022 18EC46
  • 13. 8051 Stack, I/O Port Interfacing and Programming: • Crystal Frequency: 16 MHz: • 16MHz/12 = 1.333 MHz • Machine cycle = 1/1.333 MHz = 0.75us [us=microsecond] • Crystal Frequency: 20MHz: • 20MHz/12 = 1.66 MHz • Machine Cycle = 1/1.66 MHz = 0.60us [us=microsecond] 13 8/3/2022 18EC46
  • 14. 8051 Stack, I/O Port Interfacing and Programming: • Now let us find how long it takes to execute each of the following instructions, for a crystal frequency of 11.0592 MHz. The machine cycle of a system of 11.0592 MHz is 1.085 us. 14 8/3/2022 18EC46 INSTRUCTION MACHINE CYCLE TIME TO EXECUTE MOV R2,#55H 1 1x1.085 us = 1.085 us DEC R2 1 1x1.085 us = 1.085 us DJNZ R2,target 2 2x1.085 us = 2.17 us LJMP 2 2x1.085 us = 2.17 us SJMP 2 2x1.085 us = 2.17 us NOP 1 1x1.085 us = 1.085 us MUL AB 4 4x1.085 us = 4.34 us
  • 15. 8051 Stack, I/O Port Interfacing and Programming: • How to Calculate Exact Time Delay in 8051 microcontroller? • The delay subroutine consists of 2 parts: Setting a counter and creating a loop. • Let us assume that the crystal frequency of 11.0592 MHz is connected • Consider the following code • MOV A,#55H • AGAIN: MOV P1,A • ACALL DELAY • CPL A • SJMP AGAIN • ;-----------------Time Delay • DELAY: MOV R3,#225 • HERE: DJNZ R3,HERE • RET 15 8/3/2022 18EC46
  • 16. 8051 Stack, I/O Port Interfacing and Programming: • Machine cycles for each instruction of the DELAY subroutine is as shown below. • Therefore, we have a time delay of [(255 x 2) + 1 + 1] x 1.085 us = 555.52 us • Very often we used to calculate the time delay based on the instructions inside the loop and ignore the clock cycles associated with the instructions outside the loop. 16 8/3/2022 18EC46 DELAY: MOV R2,#255 Machine Cycle = 1 HERE: DJNZ R2,HERE Machine Cycle = 2 RET Machine Cycle = 1
  • 17. 8051 Stack, I/O Port Interfacing and Programming: • NOP Instruction: • NOP instruction is used to increase the delay in the loop. NOP means "No Operation" simply wastes time. • Loop Inside a Loop Delay: • This method is used to get a larger delay i.e. is used to loop inside a loop, which is also called a nested loop. • E.g.: Lets find the time delay for the following subroutine with 11.0592 MHz crystal frequency is connected to the 8051 system. 17 8/3/2022 18EC46
  • 18. 8051 Stack, I/O Port Interfacing and Programming: • The time delay inside the HERE loop [255(1+1+1+1+2)] x 1.085 us = 1660.05 us • The time delay of the two instructions outside the loop is: • 1660.05 us+[1+1]x1.085 us = 1662.67 us 18 8/3/2022 18EC46 DELAY: MOV R2,#255 Machine Cycle = 1 HERE: NOP Machine Cycle = 1 NOP Machine Cycle = 1 NOP Machine Cycle = 1 NOP Machine Cycle = 1 DJNZ R2,HERE Machine Cycle = 2 RET 1
  • 19. 8051 Stack, I/O Port Interfacing and Programming: • E.g.: For a crystal frequency of 11.0592 MHz, find the time delay in the following subroutine. The machine cycle is 1.085 us. • 'HERE' Loop Calculations: 1+1+2, so [(1+1+2)x250] x 1.085 us = 1085 us. 19 8/3/2022 18EC46 DELAY: MOV R2,#200 AGAIN: MOV R3,#250 HERE: NOP NOP DJNZ R3,HERE DJNZ R2,AGAIN RET Machine Cycle = 1 Machine Cycle = 1 Machine Cycle = 1 Machine Cycle = 1 Machine Cycle = 2 Machine Cycle = 2 Machine Cycle = 1
  • 20. 8051 Stack, I/O Port Interfacing and Programming: • 'AGAIN' Loop Calculations: • In this loop "MOV R3,#250" and "DJNZ R2,AGAIN" at the begining and end of the AGAIN loop give delay of [(1+2)x200]x1.085 us = 651us • The AGAIN loop repeats the HERE loop 200 times so 200x1085 us = 217000 us. • The total time delay is 217000 us + 651 us = 217651 us or 217.651milliseconds. • The time is approximate as we have ignored the first and the last instructions in the subroutine i.e. DELAY: "MOV R2,#200" and "RET". 20 8/3/2022 18EC46
  • 21. 8051 Stack, I/O Port Interfacing and Programming: • I/O Port Interfacing and Programming 21 8/3/2022 18EC46
  • 22. 8051 Stack, I/O Port Interfacing and Programming: • The four 8-bit I/O ports P0, P1, P2 and P3 each uses 8 pins • All the ports upon RESET are configured as input, ready to be used as input ports • When the first 0 is written to a port, it becomes an output • To reconfigure it as an input, a 1 must be sent to the port • Port 0 • Port 0 can be used for input or output, 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 • Open drain is a term used for MOS chips in the same way that open collector is used for TTL chips 22 8/3/2022 18EC46
  • 23. 8051 Stack, I/O Port Interfacing and Programming: • The following code will continuously send out to port 0 the alternating value 55H and AAH • BACK: MOV A,#55H • MOV P0,A • ACALL DELAY • MOV A,#0AAH • MOV P0,A • ACALL DELAY • SJMP BACK • In order to make port 0 an input, the port must be programmed by writing 1 to all the bits 23 8/3/2022 18EC46 ORG 300H ;put DELAY at address 300H DELAY:MOV R5,#0FFH ; R5=255 counter AGAIN:DJNZ R5,AGAIN ; stay here until R5 = 0 RET ;return to caller (when R5 =0) END ;end of asm file
  • 24. 8051 Stack, I/O Port Interfacing and Programming: • E.g.: Program to configure Port 0 as an input port and then receive data from that port and sent to P1 • MOV A,#0FFH ;A=FF hex • MOV P0,A ;make P0 an i/p port by writing it all 1s • BACK: MOV A,P0 ;get data from P0 • MOV P1,A ;send it to port 1 • SJMP BACK ;keep doing it • Port 1 • Port 1 can be used as input or output • 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 input port • To make port 1 an input port, it must be programmed by writing 1 to all its bits 24 8/3/2022 18EC46
  • 25. 8051 Stack, I/O Port Interfacing and Programming: • Port 1 to Port 3 Structures and their operation: • All the ports have the following components in them • D latch • Output driver • Input buffer • Port 1 Structure: • In the figure, Internal FET pull-up is the internal load for P1 which is present in P2 and P3 as well but not found in P0. • 8051 ports have both latch and buffer. • While reading the data, it can be either reading from the pin or from the latch 25 8/3/2022 18EC46
  • 26. 8051 Stack, I/O Port Interfacing and Programming: • Reading the input pin: • To make any port pin as input, we must write 1(Logic High) to that pin. • Writing High (1) to the port bit means that the High is given to the Latch (D flip- flop) input. • This makes Q to go high and Q` to go low • Since Q` is low and connected to gate of Transistor M1, M1 goes to OFF state. • When M1 is OFF, it blocks any path to ground and hence input signal directly goes to the tri-state buffer. • When we read the input port through instructions such as MOV A, P1 we are reading the data present at the input pin. This instruction activates the read pin of the tri-state buffer and lets the data at the pin to go into CPU’s internal bus 26 8/3/2022 18EC46
  • 27. 8051 Stack, I/O Port Interfacing and Programming: • Writing zero to port: • To make any bits of any ports as output port we must write ZERO onto it. • If we write a ZERO to any port bit, then Q=0 and Q`=1 which makes transistor to go to ON state. • This provides a path to ground for both load and input pin. • So when we try to read from input pin it always gives a LOW irrespective of the status of the input bit • 27 8/3/2022 18EC46
  • 28. 8051 Stack, I/O Port Interfacing and Programming: • Port 2 and Port 3 • Port 2 can be used as input or output • Just like P1, port 2 also does not need any pull-up resistors • Upon reset, port 2 is configured as an input port • To make port 2 an input port, it must be programmed by writing 1 to all its bits. • Port 3 can be used as input or output • Port 3 does not need any pull-up resistors • Port 3 is configured as an input port upon reset. 28 8/3/2022 18EC46
  • 29. 8051 Stack, I/O Port Interfacing and Programming: 29 8/3/2022 18EC46
  • 30. 8051 Stack, I/O Port Interfacing and Programming: • E.g.: Program to continuously send out to port 1 the alternating value 55H and AAH • MOV A,#55H • BACK: MOV P1,A • ACALL DELAY • CPL A • SJMP BACK 30 8/3/2022 18EC46
  • 31. 8051 Stack, I/O Port Interfacing and Programming: • E.g.: Program to configure Port 1 first as an input port then receive data from that port and save it in R7 and R5 • MOV A,#0FFH ;A=FF hex • MOV P1,A ;make P1 an input port by writing it all 1s • MOV A,P1 ;get data from P1 • MOV R7,A ;save it to in reg R7 • ACALL DELAY ;wait • MOV A,P1 ;another data from P1 • MOV R5,A ;save it to in reg R5 31 8/3/2022 18EC46
  • 32. 8051 Stack, I/O Port Interfacing and Programming: • I/O Programming- Different ways of Accessing Entire 8 Bits • Program to access the entire 8 bits of Port 1 • BACK: MOV A,#55H • MOV P1, A • ACALL DELAY • MOV A,#0AAH • MOV P1,A • ACALL DELAY • SJMP BACK • Can we rewrite the code in a more efficient manner ? • Access the port directly without going through the accumulator 32 8/3/2022 18EC46 BACK:MOV P1,#55H ACALL DELAY MOV P1,#0AAH ACALL DELAY SJMP BACK MOV A,#55H BACK:MOV P1,A ACALL DELAY CPL A SJMP BACK
  • 33. 8051 Stack, I/O Port Interfacing and Programming: • Write a program to perform the following: • (a) Keep monitoring the P1.2 bit until it becomes high • (b) When P1.2 becomes high, write value 45H to port 0 • (c) Send a high to low (H to L) pulse to P2.3 • SETB P1.2 ;make P1.2 an input • MOV A,#45H ;A=45H • AGAIN: JNB P1.2,AGAIN ;get out when P1.2=1 • MOV P0,A ;issue A to P0 • SETB P2.3 ;make P2.3 high • CLR P2.3 ; make P2.3 low for H to L 33 8/3/2022 18EC46
  • 34. 8051 Stack, I/O Port Interfacing and Programming: • Write a program to toggle the bits of port1 with delay which depends on the value of a number in R0. • ORG 0 • BACK:MOV A,#0H • MOV P1,A ; Send 0h to port P1 • MOV R0, #30H ; count value for generating delay ACALL DELAY ; Call the subroutine DELAY • CPL A ; Complement A to toggle • MOV P1,A ; Send AAh to port1 • MOV R0, #0FFH ; count value for generating delay ACALL DELAY ; Call the subroutine DELAY • SJMP BACK ; Keep doing this indefinitely 34 8/3/2022 18EC46
  • 35. 8051 Stack, I/O Port Interfacing and Programming: • ------Delay Subroutine--------- • ORG 300H • DELAY: NOP ;Do nothing • AGAIN: DJNZ R0,AGAIN ;Stay here until R0 becomes 0 • RET ;Return to the Called program • END 35 8/3/2022 18EC46
  • 36. 8051 Stack, I/O Port Interfacing and Programming: • Assume that P2.3 is an input port pin and represents the condition of an oven. Monitor the bit continuously. If it goes high, it means that the oven is hot. Whenever it goes high, send a high-to-low pulse to port P1.5 to turn on a buzzer. • Solution: • ORG 00H • 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 36 8/3/2022 18EC46
  • 37. 8051 Stack, I/O Port Interfacing and Programming: • 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 • Solution: • SETB P1.7 ;make P1.7 an input • AGAIN: JB P1.7,SENDY ;jump if P1.7=1 • MOV P2,#’N’ ;SW=0, issue ‘N’ to P2 • SJMP AGAIN ;keep monitoring • SENDY: MOV P2,#’Y’ ;SW=1, issue ‘Y’ to P2 • SJMP AGAIN ;keep monitoring 37 8/3/2022 18EC46
  • 38. 8051 Stack, I/O Port Interfacing and Programming: • A switch is connected to pin P1.0 and an LED to pin P2.7. Write a program to get the status of switch and send it the LED. • Solution: • SETB P1.0 ;make P1.0 an input • AGAIN: MOV C, P1.0 ;Read the switch status in to CF • MOV P2.7,C ;Send the switch status on to LED • SJMP AGAIN ;keep monitoring 38 8/3/2022 18EC46
  • 39. 8051 Stack, I/O Port Interfacing and Programming: • A switch is connected to pin P1.0 and an LED to pin P2.7. Write a program to get the status of switch and send it the LED. • SETB P1.0 ;make P1.0 an input • AGAIN: MOV C,P1.0 ;Read the switch status in to CF • MOV P2.7,C ;Send the switch status on to LED • SJMP AGAIN ;keep monitoring 39 8/3/2022 18EC46