Unit 3
Unit 3
Application of microprocessor
Contents
• Interfacing of A/D converters, Interfacing of D/A converters,
Waveform generators, Multiplexed seven segment LED display
systems, Measurement of frequency, phase angle and power
factor, Traffic light controller, Stepper motor control
Counter and Time delay in 8085
• Counters are used to keep track of events.
• Time delays are important in setting up reasonably accurate
timing between two events.
Counters
• A Counter is designed simply by
loading an appropriate number
into one of the registers and
using the INR (Increment by
one) or the DCR (Decrement by
one) instructions.
• A loop is established to update
the count, and each count is
checked to determine whether it
has reached the final number; if
not, the loop is repeated.
Time delays
• The procedure used to design a
specific delay is similar to that
used to set up a counter.
• A register is loaded with a number,
depending on the time delay
required, and then the register is
decremented until it reaches zero
by setting up a loop with a
conditional jump instruction.
• The loop causes the delay,
depending upon the clock period of
the system.
Calculating time delays
• Each instruction passes through different combinations of Opcode
Fetch, Memory Read, and Memory Write cycles.
• Knowing the combinations of cycles, one can calculate how long
such an instruction would require to complete.
• Number of Bytes
• Number of Machine Cycles
• Number of T-State.
Calculating time delays …
• Knowing how many T-States an instruction requires, and keeping
in mind that a T-State is one clock cycle long, we can calculate the
time delay using the following formula:
Time Delay = No. of T-States * Clock Period
• For example,
• MVI instruction uses 7 T-States.
• Therefore, if the Microprocessor is running at 2 MHz, the
instruction would require 3.5 µS to complete.
Time delay design techniques
• Using One Register.
• Using a Register Pair.
• Using a Loop with in a Loop
Time delay design techniques …
• Using one register
• A count is loaded in a register, and We can use a loop to produce a
certain amount of time delay in a program.
• The following is an example of a delay using one register:
MVI C, FFH ;7 T-States
LOOP: DCR C ;4 T-States
JNZ LOOP ;10 T-States
• The first instruction initializes the loop counter and is executed only
once requiring only 7 T-States.
• The following two instructions form a loop that requires 14 T-States to
execute and is repeated 255 times until C becomes 0.
Time delay design techniques …
• Using one register …
• We need to keep in mind though that in the last iteration of the
loop, the JNZ instruction will fail and require only 7 T-States
rather than the 10.
• Therefore, we must deduct 3 T-States from the total delay to get
an accurate delay calculation.
Time delay design techniques …
• Using one register …
• To calculate the delay, we use the following formula:
Tdelay = TO + TL
Tdelay = Total delay
TO = delay outside the loop
TL = delay of the loop
TO is the sum of all delays outside the loop.
TL is calculated using the formula
TL = T * Loop T-States * N (no. of iterations)
Time delay design techniques …
• Using one register …
• Using these formulas, we can calculate the time delay for the
MVI C, FFH ;7 T-States
LOOP: DCR C ;4 T-States
JNZ LOOP ;10 T-States
• Assuming f = 2 MHz
• TO = 7 T-States (Delay of the MVI instruction)
• TL = (14 X 255) - 3 = 3567 T-States
• 14 T-States for the 2 instructions repeated 255 times
• FF16 = 25510 reduced by the 3 T-States for the final JNZ.)
Time delay design techniques …
• Using one register …
• TDelay = [(TO + TL ) / f]
= (7 + 3567) / 2MHz
= (3574) X 0.5 mSec
= 1.787 mSec
Time delay design techniques …
• Using a register pair
• Using a single register, one can repeat a loop for a maximum
count of 255 times.
• It is possible to increase this count by using a register pair for
the loop counter instead of the single register.
• A minor problem arises in how to test for the final count since
DCX and INX do not modify the flags.
• However, if the loop is looking for when the count becomes
zero, we can use a small trick by ORing the two registers in the
pair and then checking the zero flag.
Time delay design techniques …
• Using a register pair
• The following is an example of a delay loop set up with a
register pair as the loop counter.
No Is this
Final
Count?
Yes
Initialize Counter
Load Delay Register
Display
Body of loop
Time Delay
Update Count
Is this
Final
Is Count?
No Count No
Complete?
Yes
Yes
Time delay design techniques …
• Counter Design with Time Delay
Initialize Counter
Initialize Counter
Display Count
Is No
No Count Time Delay
Complete?
Go back
Yes
End
End
MVI B,00H
NEXT: DCR B
MVI C,COUNT
DELAY: DCR C
JNZ DELAY
MOV A,B
OUT PORT#
JMP NEXT
Illustrative program 1 …
• Delay loop includes two instructions:
• DCR C and JNZ with 14 T-states.
• Therefore the time delay TL in the loop (without accounting
for the fact that JNZ requires 7 T-States in the last cycle,
because count will remain same even if the calculations take
into account the difference of 3 T-States) is:
TL = 14 T-states x T (Clock period) x Count
= 14 x (0.5 x 10-6) x Count
= (7.0 x 10-6) x Count
Illustrative program 1 …
• The Delay outside the loop includes the following instructions:
Illustrative program 2 …
• Modulo TEN counter (0-9)
• Write a program to count from 0 – 9 with a one - second delay
between each count. At count of 9, the counter should reset
itself to 0 and repeat the sequence continuously. Use register
pair H-L to set up the delay, and display each count at one of
the output ports. Assume the clock frequency of the micro
computer is 1 MHz.
Illustrative program 2 …
START: MVI B,00H 7
MOV A,B 4
DISPLAY: OUT PORT# 10
LXI H,16-Bit 10
LOOP: DCX H 6
MOV A,L 4
ORA H 4
JNZ LOOP 10/7
INR B 4
MOV A,B 4
CPI 0AH 7
JNZ DISPLAY 10/7
JZ START 10/7
Illustrative program 2 …
• Time Delay Calculation
• The major delay between two counts is provided by the 16-bit
number in the delay register HL(inner loop in flow chart).
• This delay is set up by using a register pair.
Loop Delay TL= 24 T-states *T * Count 1
second= 24*1.0 *10-6 *Count
Count= 1 = 41666 = A2C2H
24 x 10-6
• A2C2H would provide approx 1 sec delay between two counts.
To achieve higher accuracy in the delay, the instructions
outside the loop must be accounted for delay calculation. (will
be 41665).
Illustrative program 3 …
• Generating Pulse waveforms
• Write a program to generate a continuous square wave with the
period of 500 micro sec. Assume the system clock period is
325 ns, and use bit D0 to output the square wave.
Illustrative program 3 …
MVI D,AA 7T
ROTATE: MOV A,D 4T
RLC 4T
MOV D,A 4T
ANI 01H 7T //mask MSB 7 Bits
OUT PORT1 10T
MVI B,COUNT
DELAY: DCR B 4T
JNZ DELAY 10/7 T
JMP ROTATE 10T
A 10101010
After RLC 01010101
A AND 01H 00000001
200C INX H 23
Sine Wave Generation Using DAC …
address Label Mnemonic Machine code Comments
200D DCR C 0D
200E JNZ POS C2, 09, 20
2011 MVI A,24H 0E, 24
2013 NEG DCX H 2B
2014 MOV A,M 7E
2015 OUT 00H D3, PORT A [[H-L]] is outputted through portA.
2017 DCR C 0D
2018 JNZ NEG C2, 13, 20
201B JMP START C3, 04, 20
Measurement of frequency
Measurement of frequency …
Measurement of frequency …
Traffic light controller
Traffic light controller …
• Port A is used to control lights on N-S road and Port B is used to
control lights on W-E road.
Traffic light controller …
Traffic light controller …
Traffic light controller …
Traffic light controller …
MVI A, 80H : Initialize 8255, port A and port B
OUT 83H (CR) : in output mode
START: MVI A, 09H
OUT 80H (PA) : Send data on PA to glow R1 and R2
MVI A, 24H
OUT 81H (PB) : Send data on PB to glow G3 and G4
MVI C, 28H : Load multiplier count (40D) for delay
CALL DELAY : Call delay subroutine
MVI A, 12H
OUT 80H : Send data on Port A to glow Y1 and Y2
OUT 81H : Send data on port B to glow Y3 and Y
Traffic light controller …
MVI C, 0AH : Load multiplier count (10D) for delay
CALL DELAY : Call delay subroutine
MVI A, 24H
OUT 80H : Send data on port A to glow G1 and G2
MVI A, 09H
OUT 81H : Send data on port B to glow R3 and R4
MVI C, 28H : Load multiplier count (40D) for delay
CALL DELAY : Call delay subroutine
MVI A, 12H
OUT 80H : Send data on port A to glow Y1 and Y2
OUT 81H : Send data on port B to glow Y3 and Y4
Traffic light controller …
MVI C, 0AH : Load multiplier count (10D) for delay
CALL DELAY : Call delay subroutine
JMP START
Delay Subroutine:
DELAY: LXI D, Count : Load count to give 0.5 sec delay
BACK: DCX D : Decrement counter
MOV A, D
ORA E : Check whether count is 0
JNZ BACK : If not zero, repeat
DCR C : Check if multiplier zero, otherwise repeat
JNZ DELAY
RET : Return to main program
Traffic light controller …
Stepper motor controller …
• A stepper motor is a digital motor.
• It can be driven by digital signal.
Stepper motor controller …
• Motor shown in the circuit has two phases, with center-tap
winding.
• The center taps of these windings are connected to the 12V
supply.
• Due to this, motor can be excited by grounding four terminals of
the two windings.
• Motor can be rotated in steps by giving proper excitation sequence
to these windings.
• The lower nibble of port A of the 8255 is used to generate
excitation signals in the proper sequence.
Stepper motor controller …
• These excitation signals are buffered using driver transistors.
• The transistors are selected such that they can source rated current
for the windings.
• Motor is rotated by 1.80 per excitation.
Stepper motor controller …
Source Program
Stepper motor controller …
Stepper motor subroutine
Stepper motor controller …
Delay subroutine
Stepper motor controller …
As port A is used as an output port, control word for 8255 is 80H.
Stepper Motor Control Program:
2500H Excite code DB 03H, 06H, 09H, OCH : This is the code sequence for clockwise
rotation
Subroutine to rotate a stepper motor clockwise by 360° - Set the counts:
MVI C, 32H : Set repetition count to 50ıο
START: MVI B, 04H : Counts excitation sequence
LXI H, 2500H : Initialize pointer
BACK1: MOV A, M : Get the Excite code
OUT PORTA : Send Excite code
CALL DELAY : Wait
INX H : Increment pointer
Stepper motor controller …
DCR B : Repeat 4 times
JNZ BACK l
DCR C
JNZ START : Repeat 50 times
RET
Stepper motor controller …
Delay subroutine:
Delay: LXI D, Count
Back: DCX D
MOV A, D
ORA E
JNZ Back
RET