8081 microcontroller timer counter
8081 microcontroller timer counter
1
Parallel Ports
2
Experiment -5
Parallel Ports
3
Parallel Ports
• Initialize the Input Ports
8 bit port
eg. Mov P1, #0FFH
1 bit port
eg. SETB P1.3
NOTE:
OUTPUT PORTS ARE NOT INITALIZED
4
• Transfering 8 bit data
5
Subroutine
---------
ACALL DELAY
--------- Main program
---------
H1:SJMP H1
DELAY: ---------
---------- SUBROUTINE
RET
6
Problem: To write an assembly language program to add two 8 bit numbers in 8051 microcontroller using ports.
Example:
7
Block diagram:
8
Problem: To write an assembly language program to add two 8 bit numbers in 8051 microcontroller using ports.
Algorithm:
Initialize Ports P0 and P1 as input ports.
Initialize Ports P2 and P3 as output ports.
Initialize the R1 register.
Move the contents from Port 0 to B register.
Move the contents from Port 1 to A register.
Add contents in A and B.
If carry is present increment R1.
Move contents in R1 to Port 2.
Move the sum in step 6 to Port 3.
9
Problem: To write an assembly language program to add two 8 bit numbers in 8051 microcontroller using ports.
Program:
ORG 00H // Indicates starting address
MOV P0,#0FFH // Initializes P0 as input port
MOV P1,#0FFH // Initializes P1 as input port
MOV P2,#00H // Initializes P2 as output port
MOV P3,#00H // Initializes P3 as output port
11
Program number: 1
• Write and assemble a program to toggle all the bits of P0, P1, and P2
continuously by sending 55H and AAH to these ports. Put a time delay
between the "on" and "off" states. Then using the simulator, single-
step through the program and examine the ports. Do not single-step
through the time delay call.
12
COMPLIMENT
55 H = 0 1 0 1 0 1 0 1 B
AA H = 1 0 1 0 1 0 1 0 B
13
Program number: 1
• Org 0000h
• Here : MOV P0,#55H
• MOV P1,#55H
• MOV P2,#55H
• ACALL DELAY
• MOV P0,#0AAH
• MOV P1,#0AAH
• MOV P2,#0AAH
• ACALL DELAY
• SJMP HERE
• Delay : MOV R1,#04H
• BACK: MOV R2,#20H
• AGAIN: DJNZ R2,AGAIN
• DJNZ R1,BACK
• RET
• END
14
Program number: 2
15
• Org 0000h
Program number: 2
• Here : MOV P0,#55H
• MOV P1,#55H
• MOV P2,#55H
• ACALL DELAY1
• MOV P0,#0AAH
• MOV P1,#0AAH
• MOV P2,#0AAH
• ACALL DELAY2
• SJMP HERE
• Delay1 : MOV R1,#04H
• BACK1: MOV R2,#20H
• AGAIN1: DJNZ R2,AGAIN1
• DJNZ R1,BACK1
• RET
• Delay2 : MOV R1,#08H
• BACK2: MOV R2,#50H
• AGAIN2: DJNZ R2,AGAIN2
• DJNZ R1,BACK2
• RET
• END 16
Program number: 3
17
Program number: 3
• ORG 0000H
• MOV P1,#0FFH
• MOV A,P1
• MOV P0,A
• MOV P2,A
• MOV R0,A
• MOV R1,A
• MOV R2,A
• END
•
18
19
I/O PORT
PROGRAMMING
Chapter 4
Objectives
Write a program to copy a block of 10 bytes of data from RAM locations starting
at 35H to RAM locations starting at 60H.
Solution:
Assuming that ROM space starting at 250H contains “America”, write a program to transfer the bytes into
RAM locations starting at 40H.
Solution:
;(a) This method uses a counter
ORG 0000
MOV DPTR,#MYDATA ;load ROM pointer
MOV R0,#40H ;load RAM pointer
MOV R2,#7 ;load counter
BACK: CLR A ;A = 0
MOVC A,@A+DPTR ;move data from code space
MOV @R0,A ;save it in RAM
INC DPTR ;increment ROM pointer
INC R0 ;increment RAM pointer
DJNZ R2,BACK ;loop until counter=0
HERE: SJMP HERE
Assuming that ROM space starting at 250H contains “America”, write a program to transfer the bytes into
RAM locations starting at 40H.
Solution:
;(b) This method uses null char for end of string
ORG 0000
MOV DPTR,#MYDATA ;load ROM pointer
MOV R0,#40H ;load RAM pointer
BACK:CLR A ;A=0 Notice the null
MOVC A,@A+DPTR ;move data from code space character, 0,
JZ HERE ;exit if null character indicating the
MOV @R0,A ;save it in RAM end of the
INC DPTR ;increment ROM pointer string, and how
INC R0 ;increment RAM pointer we use the JZ
SJMP BACK ;loop instruction to
HERE: SJMP HERE detect that.
;----------On-chip code space used for storing data
ORG 250H
MYDATA: DB "AMERICA",0 ;notice null char for
;end of string
END
Example 5-8
Write a program to get the x value from P1 and send x2 to P2, continuously.
Solution:
ORG 0
MOV DPTR,#300H ;load look-up table address
MOV A,#0FFH ;A=FF
MOV P1,A ;configure P1 as input port
BACK: MOV A,P1 ;get X
MOVC A,@A+DPTR ;get X squared from table
MOV P2,A ;issue it to P2
SJMP BACK ;keep doing it
ORG 300H
Notice that the first
XSQR_TABLE:
instruction could be
DB 0,1,4,9,16,25,36,49,64,81 replaced with “MOV
END DPTR,#XSQR_TABLE”
Example 5-9
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.
Solution:
Find out to which byte each of the following bits belongs. Give the address of
the RAM byte in hex.
(a) SETB 42H ;set bit 42H to 1 (d) SETB 28H ;set bit 28H to 1
(b) CLR 67H ;clear bit 67 (e) CLR 12 ;clear bit 12 (decimal)
(c) CLR 0FH ;clear bit 0FH (f) SETB 05
Solution:
For each of the following instructions, state to which port the bit belongs. Use
Table 5-3.
(a) SETB 86H (b) CLR 87H (c) SETB 92H (d) SETB 0A7H
Solution:
Solution:
CLR PSW.3
SETB PSW.4
MOV R7,A
Example 5-14
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?
Solution:
The OV flag is PSW.2 of the PSW register. PSW is a bit-addressable register; therefore,
we can use the following instruction to check the OV flag.
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.
Solution:
Assume that bit P2.3 is an input and represents the condition of a door. If it goes high, it means that the door is open.
Monitor the bit continuously. Whenever it goes high, send a low-to-high pulse to port P1.5 to turn on a buzzer.
Solution:
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.
Solution:
CLR 06 ;clear bit address 06
CLR 07 ;clear bit address 07
JNB P1.2,OVER ;check bit P1.2,if 0 then jump
SETB 06 ;if P1.2=1,set bit location 06 to 1
OVER:JNB P1.3,NEXT ;check bit P1.3, if 0 then jump
SETB 07 ;if P1.3=1, set bit location 07 to 1
NEXT:...
Example 5-18
Write a program to save the status of bit P1.7 on RAM address bit 05.
Solution:
Write a program to get the status of bit pin P1.7 and send it to pin P2.0.
Solution:
HERE: MOV C,P1.7 ;get bit from port
MOV P2.0,C ;send bit to port
SJMP HERE ;repeat forever
Example 5-20
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 low-to-high pulse to port P1.5 to turn on a buzzer.
Solution:
OVEN_HOT BIT P2.3
BUZZER BIT P1.5
An LED is connected to pin P1.7. Write a program to toggle the LED forever.
Solution:
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.
Solution:
SW BIT P1.7 ;assign bit
LED BIT P2.0 ;assign bit
HERE: MOV C,SW ;get the bit from the port
MOV LED,C ;send the bit to the port
SJMP HERE ;repeat forever
Example 5-23
Assume that RAM bit location 12H holds the status of whether there has been a phone call or not. If it is high,
it means there has been a new call since it was checked the last time. Write a program to display “New
Messages” on an LCD if bit RAM 12H is high. If it is low, the LCD should say “No New Messages”.
Solution:
PHONBIT BIT 12H
MOV C,PHONBIT ;copy bit location 12H to carry
JNC NO ;check to see if is high
MOV DPTR,#400H ;yes, load address of message
LCALL DISPLAY ;display message (see Chap. 12)
SJMP EXIT ;get out
NO: MOV DPTR,#420H ;load the address of No message
LCALL DISPLAY ;display it
EXIT: ;exit
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 “NO” to P2. (b) If SW = 1, send “YES” to P2.
Solution:
SW EQU P1.7
MYDATA EQU P2
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.
Use EQU to designate the I/O ports.
Solution:
SW EQU 97H ;P1.7 bit address
MYDATA EQU 0A0H ;P2 address
HERE: MOV C,SW
JC OVER
MOV MYDATA,#'0' ;00110000 to P2
SJMP HERE
OVER: MOV MYDATA,#'1’ ;00110001 to P2
SJMP HERE
END
Figure 5-4. 8052 On-Chip RAM
Address Space
Example 5-26
Write a program for the 8052 to put 55H into the upper RAM locations of 90 - 99H.
Solution:
ORG 0
MOV A,#55H
MOV R2,#10
MOV R0,#90H ;access the upper 128 bytes of on-chip RAM
BACK:MOV @R0,A ;use indirect addressing mode
INC R0
DJNZ R2,BACK ;repeat for all locations
SJMP $ ;stay here
END
Run the above program on your simulator and examine the upper
memory to see the result. (See Figures 5-5 and 5-6 for screen
shots.)
Example 5-27
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.
Solution:
ORG 0
MOV DPTR,#MYDATA
MOV R1,#80H ;access the upper 128 bytes of on-chip RAM
B1: CLR A
MOVC A,@A+DPTR ;copy from code ROM space
MOV @R1,A ;store in upper RAM space
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 the last byte
EXIT: SJMP $ ;stay here when finished
;-----------
ORG 300H
MYDATA: DB "The Promise of World Peace",0
END
113
114
115
116
117
118
119
120
Welcome to the
Course-Microprocessors and Microcontrollers
Code-BECE204L
Dr Arun Dev Dhar Dwivedi
Associate Professor (Sr.), Department of Micro and Nano Electronics
School of Electronics Engineering (SENSE)
Email: [email protected], [email protected]
Phone: 8209517661, 9450547267 (WhatsApp)
Cabin-SJT313A11A
121
Port programming
Implementation of Digital Circuits
Parallel Ports
• Initialize the Input Ports
8 bit port
eg. MOV P1, #0FFH
1 bit port
eg. SETB P1.5
NOTE:
OUTPUT PORTS ARE NOT INITALIZED
• Transfering 8 bit data
126
Digital circuit
• Assume
• INPUT
A=P1.0
B=P1.1
C=P1.2
• OUTPUT
Q=P2.0
127
Digital circuit
• Org 0000h
• SETB P1.0
• SETB P1.1
• SETB P1.2
• MOV C,P1.0
• ANL C, P1.1
• MOV ACC.7,C
• MOV C, P1.1
• ORL C, P1.2
• MOV ACC.6,C
• MOV C,P1.2
• ANL C,P1.1
• ANL C,ACC.6
• ORL C,ACC.7
• MOV P2.0,C
• END 128
Digital circuit Program
• Org 0000h
• SETB P1.0;INITIALIZE
• SETB P1.1;INITIALIZE
• SETB P1.2;INITALIZE
• MOV C,P1.0
• ANL C,P1.1
• MOV ACC.7,C
• MOV C,P1.1
• ORL C,P1.2
• MOV ACC.6,C
• MOV C, P1.2
• ANL C,P1.1
• ANL C, ACC.6
• ORL C,ACC.7
• MOV P2.0,C
• END
129
TASK 1: Implementation of digital circuits using KEIL
for an 8051 Microcontroller
ORG 0000H
SETB ACC.0; Input A
SETB ACC.1; Input B
MOV C, ACC.1
CPL C
ANL C, ACC.0
MOV ACC.2, C; Output C
MOV C, ACC.0
ANL C, ACC.1
MOV ACC.3, C; Output D
H: SJMP H
END
133
RESULT:
S.NO A B Output (C) Output(D)
1 0 0
2 0 1
3 1 0
4 1 1 134
Experiment -6
135
Program 1
Write a program using timer 0 to generate a 500 Hz square wave frequency on one of the pins of P1.0. Then
examine the frequency using the KEIL IDE inbuilt Logic Analyzer.
//Progam1
ORG 0000H
Initial value calculation:
MOV TMOD,#01H
HERE: MOV TL0,#66H
Frequency f=500Hz
MOV TH0,#0FCH
Total time period T=2ms
CPL P1.0
Required delay=TON=TOFF=1ms
ACALL DELAY
[(FFFF-XXXX)+1]*1.085µs = 1ms
SJMP HERE
(65536-X)* 1.085µs = 1ms
DELAY: SETB TR0
X=(64614)D
AGAIN: JNB TF0, AGAIN
X=XXXX = FC66H
CLR TR0
CLR TF0
RET
END
136
Program 2
Write a program using timer 1 to generate a 1 kHz square wave frequency on one of the pins of P1. Then examine
the frequency using the oscilloscope.
137
PROGRAM 3-TIMER OPERATION MODE2
Write a program using timer 1 to generate a 2 KHz square wave
frequency on one of the pins of P1.0.
Then examine the frequency using the KEIL IDE inbuilt Logic
Analyzer.
ORG 0000H
MOV TMOD, #20H
HERE: MOV TH1, #19H
BACK: SETB TR1
AGAIN: JNB TF1, AGAIN
CPL P1.0
CLR TF1
SJMP BACK
END
138
Program 4 (Counter Programming)
Assume a room can accommodate 10 people. Write an 8051 program to count the number of people
entering the room. If the count reaches 10 turn on LED.
ORG 0000H
MOV TMOD,#06h; counter 0 in mode2
CLR P0.1;TURN OFF LED
SETB P3.4; intilalize i/p port
MOV TH0,#00
SETB TR0; Start Counter0
AGAIN:MOV A, TL0
MOV P1,A
CJNE A,#10D ,AGAIN
SETB P0.1; turn ON LED.
END
139
Assembly Program: Program 5
COUNTER PROGRAM
ORG 0000H
MOV TMOD, #60H Use Counter 1 in mode 2 and after 10 number of counts on TL1, generate a
MOV TH1, #0f5H SQUARE waveform of 1 KHz on P1.2 by using Timer 0 in mode 1, show the
MOV TL1, #0F5H counts in TL1 on port 2.
SETB P3.5
SETB TR1
BACK: MOV A, TL1
MOV P2, A
JNB TF1, BACK
CLR TR1
CLR TF1
MOV TMOD, #01H
HERE: MOV TL0, #32H
MOV TH0, #0FEH
CPL P1.2
ACALL DELAY
SJMP HERE
DELAY: SETB TR0
AGAIN: JNB TF0, AGAIN
CLR TR0
CLR TF0
RET
140
END
Assuming that clock pulses are fed into pin T1,write a program
PROGRAM 6-COUNTER OPERATION 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.
141
8051 Assembly Code for Timer 0 Mode 0
142
8051 Assembly Code for Timer 1 in Mode 0
143
144
145
146
147
148
149
150
151
152
153
154
155
156
mode1:
157
158
Program 1
Write a program using timer 0 to generate a 500 Hz square wave frequency on one of the pins of P1.
Then examine the frequency using the KEIL IDE inbuilt Logic Analyzer.
//Progam1
ORG 0000H
Initial value calculation:
MOV TMOD,#01H
HERE: MOV TL0,#66H
Frequency f=500Hz
MOV TH0,#0FCH
Total time period T=2ms
CPL P1.0
Required delay=TON=TOFF=1ms
ACALL DELAY
[(FFFF-XXXX)+1]*1.085µs = 1ms
SJMP HERE
(65536-X)* 1.085µs = 1ms
DELAY: SETB TR0
X=(64614)D
AGAIN: JNB TF0, AGAIN
X=XXXX = FC66H
CLR TR0
CLR TF0
RET
END
159
Program 2
Write a program using timer 1 to generate a 1 kHz square wave frequency on one of the pins of P1. Then examine
the frequency using the oscilloscope.
160
161
162
163
164
165
166
167
168
169
170
171
//Write a program using timer 0 to generate a 500 Hz wave with 50% duty Cycle
//frequency on one of the pins of P1.0
//Then examine the frequency using the KEIL IDE inbuilt Logic Analyzer.
ORG 0000H
MOV TMOD,#01H
AGAIN:MOV TL0,#66H
MOV TH0,#0FCH
DELAY:SETB TR0
BACK:JNB TF0,BACK
CLR TR0
CPL P1.0
CLR TF0
SJMP AGAIN
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
Program 3 (Counter Programming)
Assume a room can accommodate 10 people. Write an 8051 program to count the number of people
entering the room. If the count reaches 10 turn on LED.
ORG 0000H
MOV TMOD,#06h; counter 0 in mode2
CLR P0.1;TURN OFF LED
SETB P3.4; intilalize i/p port
MOV TH0,#00
SETB TR0; Start Counter0
AGAIN:MOV A, TL0
MOV P1,A
CJNE A,#10D ,AGAIN
SETB P0.1; turn ON LED.
END
194