0% found this document useful (0 votes)
6 views194 pages

8081 microcontroller timer counter

The document outlines a course on Microprocessors and Microcontrollers, specifically focusing on the 8051 microcontroller and its peripherals, including I/O ports and timers. It provides detailed assembly language programming examples for adding 8-bit numbers, toggling port bits, and manipulating data between ports and registers. Additionally, it includes objectives for understanding the 8051's ports, their dual roles, and coding techniques for I/O handling.

Uploaded by

manish.ddu1976
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views194 pages

8081 microcontroller timer counter

The document outlines a course on Microprocessors and Microcontrollers, specifically focusing on the 8051 microcontroller and its peripherals, including I/O ports and timers. It provides detailed assembly language programming examples for adding 8-bit numbers, toggling port bits, and manipulating data between ports and registers. Additionally, it includes objectives for understanding the 8051's ports, their dual roles, and coding techniques for I/O handling.

Uploaded by

manish.ddu1976
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 194

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

MODULE-4 :Microcontroller 8051 Peripherals


I/O Ports (Parallel Ports), Timers-Counters

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

• MOV A, P1 ; transfering data from P1 to A


• Transfering 1 bit data

• MOV C, P1.5 ;Transfering 1 bit from P1.5 to


carry flag
• MOV A, P1.3 ; not a valid instruction

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

 L1:MOV R1, #00H // Initializes Register R1


 MOV B,P0 // Moves content of P0 to B
 MOV A,P1 // Moves content of P1 to A
 CLR C // Clears carry flag
 ADD A,B // Add the content of A and B and store result in A
 JNC L2 // If carry is not set, jump to label L2
 INC R1 // Increment Register R1 if carry present

 L2: MOV P2, R1 // Moves the content from Register R1 to Port2


 MOV P3,A // Moves the content from A to Port3
 SJMP L1 // Jumps to label L1
 END
10
Explanation:
 ORG 00H is the starting address of the program.
 Giving the values as #0FFH and #00H initializes the ports as input and
output ports respectively.
 R1 register is initialized to 0 so as to store any carry produced during the
sum.
 MOV B, P0 moves the value present in P0 to the B register.
 MOV A, P1 moves the value present in P1 to Accumulator.
 ADD AB adds the values present in Accumulator and B register and stores
the result in Accumulator.
 JNC L2 refers to jump to label L2 if no carry is present by automatically
checking whether the carry bit is set or not.
 If the carry bit is set to increment register R1.
 MOV P2, R1, and MOV P3, refers to moving the carry bit to P2 and result
in Accumulator to P3.

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

• Using a simulator for the program 1, examine


the registers of the delay subroutine and make
the delay shorter or longer by changing the
register values.

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

• Using a simulator, write a program to get a byte of data from P1


and send it to P0 and P2. Also, give a copy of it to registers R0,
R1, and R2. Single-step the program and examine the ports and
registers.

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

Upon completion of this chapter, you will be able to:

>> List the 4 ports of the 8051


>> Describe the dual role of port 0 in providing both data and addresses
>> Code Assembly language to use the ports for input or output
>> Explain the dual role of port 0 and port 2
>> Code 8051 instructions for I/O handling
>> Code I/O bit-manipulation programs for the 8051
Figure 4-1. 8051 Pin Diagram
23
24
25
26
27
Figure 4-2. Port 0 with Pull-Up Resistors
Table 4-1: Port 3 Alternate Functions
Table 4-2: Reset Value of Some 8051 Ports
Table 4-3: Single-Bit Addressability of Ports
32
33
34
35
36
37
38
39
40
Table 5-1: 8051 Special Function Register (SFR) Addresses
Table 5-1: 8051 Special Function Register (SFR) Addresses
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
Solution:
Example 4-1 ;Tested for the DS89C4x with XTAL = 11.0592 MHz.
ORG 0
BACK: MOV A,#55H
MOV P0,A
MOV P1,A
MOV P2,A
ACALL QSDELAY ;Quarter of a second delay
MOV A,#0AAH
MOV P0,A
MOV P1,A
Write a test program for the MOV P2,A
ACALL QSDELAY
DS89C4x0 chip to toggle all the SJMP BACK
bits of P0, P1, and P2 every 1/4 ;-----------1/4 SECOND DELAY
of a second. Assume a crystal QSDELAY:
MOV R5, #11
frequency of 11.0592 MHz. H3: MOV R4, #248
H2: MOV R3, #255
H1: DJNZ R3, H1 ;4 MC for DS89C4x0
DJNZ R4, H2
DJNZ R5, H3
RET
END
Delay = 11 x 248 x 255 x 4 MC x 90 ns = 250,430 µs
Use an oscilloscope to verify the delay size.
Example 5-5

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:

MOV R0,#35H ;source pointer


MOV R1,#60H ;destination pointer
MOV R3,#10 ;counter
BACK:MOV A,@R0;get a byte from source
MOV @R1,A;copy it to destination
INC R0 ;increment source pointer
INC R1 ;increment destination pointer
DJNZ R3,BACK ;keep doing it for all ten bytes
Example 5-6 Solution:
ORG 0000H ;burn into ROM starting at 0
MOV DPTR,#200H ;DPTR=200H look-up table address
CLR A ;clear A(A=0)
MOVC A,@A+DPTR ;get the char from code space
In this program, assume
MOV R0,A ;save it in R0
that the word “USA” is INC DPTR ;DPTR=201 pointing to next char
burned into ROM CLR A ;clear A(A=0)
locations starting at MOVC A,@A+DPTR ;get the next char
200H, and that the MOV R1,A ;save it in R1
program is burned into INC DPTR ;DPTR=202 pointing to next char
ROM locations starting CLR A ;clear A(A=0)
at 0. Analyze how the MOVC A,@A+DPTR ;get the next char
program works and MOV R2,A ;save it in R2
HERE:SJMP HERE ;stay here
state where “USA” is
stored after this ;Data is burned into code space starting at 200H
program is run. ORG 200H
MYDATA: DB "USA"
END ;end of program
Example 5-6 Solution:

In the above program ROM locations 200H - 202H have


the following contents.
In this program, assume
200=('U') 201=('S') 202=('A'’)
that the word “USA” is We start with DPTR = 200H, and A = 0. The instruction
burned into ROM “MOVC A, @A+DPTR” moves the contents of ROM location
locations starting at 200H (200H + 0 = 200H) to register A. Register A
200H, and that the contains 55H, the ASCII value for “U”. This is moved
program is burned into to R0. Next, DPTR is incremented to make DPTR =
ROM locations starting 201H. A is set to 0 again to get the contents of the
at 0. Analyze how the next ROM location 201H, which holds character “S”.
program works and After this program is run, we have R0 = 55H, R1 =
53H, and R2 = 41H, the ASCII values for the
state where “USA” is
characters “U”, “S” and “A”.
stored after this
program is run.
Example 5-7

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

;----------On-chip code space used for storing data


ORG 250H
MYDATA: DB "AMERICA"
END
Example 5-7

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

Answer the following questions for Example 5-8.


(a) Indicate the content of ROM locations 300 - 309H.
(b) At what ROM location is the square of 6, and what value should be there?
(c) Assume that P1 has a value of 9: What value is at P2 (in binary)?
Solution:
(a) All values are in hex.
300 = (00) 301 = (01) 302 = (04) 303 = (09)
304 = (10) 4x 4 = 16 = 10 in hex
305 = (19) 5x 5 = 25 = 19 in hex
306 = (24) 6x 6 = 36 = 24H
307 = (31) 308 = (40) 309 = (51)

(b) 306H; it is 24H


(c) 01010001B, which is 51H and 81 in decimal (9² = 81).
Example 5-10

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:

MOV P1,#55H ;P1=55H


MOV 32H,#200;load counter value into RAM loc 32h
LOP1: CPL P1 ;toggle P1
ACALL DELAY
DJNZ 32H,LOP1;repeat 200 times
Example 5-11

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:

(a) RAM bit address of 42H belongs to D2 of RAM location 28H.


(b) RAM bit address of 67H belongs to D7 of RAM location 2CH.
(c) RAM bit address of 0FH belongs to D7 of RAM location 21H.
(d) RAM bit address of 28H belongs to D0 of RAM location 25H.
(e) RAM bit address of 12 belongs to D4 of RAM location 21H.
(f) RAM bit address of 05 belongs to D5 of RAM location 20H.
Figure 5-1. 16 Bytes of Internal RAM.
Note: They are both
bit- and byte-accessible.
Table 5-2: Single-Bit Instructions
Figure 5-2. SFR RAM Address
(Byte and Bit)
Table 5-3: Bit Addresses for All Ports
Example 5-12

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:

(a) SETB 86H is for SETB P0.6.


(b) CLR 87H is for CLR P0.7.
(c) SETB 92H is for SETB P1.2.
(d) SETB 0A7H is for SETB P2.7.
Figure 5-3. Bits of the PSW Register
Example 5-13

Write a program to save the Accumulator in R7 of bank 2.

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.

JB PSW.2,TARGET ;jump if OV=1


Example 5-15

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:

MOV A,37H ;load RAM location 37H into accumulator


JNB ACC.0,YES ;is D0 of reg A 0? if so jump
INC A ;it is odd, make it even
YES: MOV P2,A ;send it to P2
Example 5-16

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:

HERE: JNB P2.3,HERE ;keep monitoring for high


CLR P1.5 ;Clear bit (P1.5 = 0)
ACALL DELAY
SETB P1.5 ;P1.5=1 (low-to-high pulse)
ACALL DELAY
SJMP HERE
Example 5-17

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:

MOV C,P1.7 ;get bit from port


MOV 05,C ;save bit
Example 5-19

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

HERE:JNB OVEN_HOT,HERE ;keep monitoring for HOT


ACALL DELAY ;
CPL BUZZER ;sound the buzzer
ACALL DELAY ;
SJMP HERE
This is similar to Example 5-16, except the use of BIT directive allows us to assign the OVEN_HOT
and BUZZER bit to any port. This way you do not have to search the program for them.
Example 5-21

An LED is connected to pin P1.7. Write a program to toggle the LED forever.

Solution:

LED BIT P.7 ;using BIT directive


HERE: CPL LED ;toggle LED
LCALL DELAY ;delay
SJMP HERE ;repeat forever
Example 5-22

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

;—————————————data to be displayed on LCD


ORG 400H
YES_MG: DB "New Messages",0
ORG 420H
NO_MG: DB "No New Messages",0
Example 5-24

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

HERE: MOV C,SW


JC OVER
MOV MYDATA,#'N’ ;SW=0, send "NO"
MOV MYDATA,#'O'
SJMP HERE
OVER: MOV MYDATA,#'Y’’ ;SW=1, send "YES"
MOV MYDATA,#'E'
MOV MYDATA,#'S'
SJMP HERE
END
Example 5-25

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

MODULE-4 :Microcontroller 8051 Peripherals


Port programming: Implementation of Digital Circuits

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

• MOV A, P1 ; transfering data from P1 to A


• Transfering 1 bit data

• MOV C, P1.5 ;Transfering 1 bit from P1.5 to


carry flag
• MOV A, P1.3 ; not a valid instruction
Digital circuit

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 Objective: To develop an assembly code for 8051 Microcontroller,


SETB ACC.0; input A to implement the given digital circuit using KEIL development
SETB ACC.1; input B tool.
CLR ACC.2; input C
SETB ACC.3; input D
MOV C, ACC.0
ANL C, ACC.1
ORL C, ACC.2
CPL C
MOV ACC.7, C
MOV C, ACC.2
CPL C
ANL C, ACC.3
CPL C
ANL C, ACC.7; Final output (F)
HALT: SJMP HALT
END
130
131
RESULT:

S.NO A B C D Output (F)


1 0 0 0 0
2 0 0 0 1
3 0 0 1 0
4 0 0 1 1
5 0 1 0 0
6 0 1 0 1
7 0 1 1 0
8 0 1 1 1
9 1 0 0 0
10 1 0 0 1
11 1 0 1 0
12 1 0 1 1
13 1 1 0 0
14 1 1 0 1
132
15 1 1 1 0
DIGITAL CIRCUIT PROGRAM 2

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

Timer and Counter Programming

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.

Initial value calculation: //Program2


Frequency f=1 KHz ORG 0000H
Total time period T=1ms MOV TMOD,#10H
Required delay=TON=TOFF=0.5ms HERE: MOV TL1,#33H
[(FFFF-XXXX)+1]*1.085µs = 0.5ms MOV TH1,#0FEH
XXXX = FE33H CPL P1.0
ACALL DELAY
SJMP HERE
DELAY: SETB TR1
AGAIN: JNB TF1,AGAIN
CLR TR1
CLR TF1
RET
END

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.

MOV TMOD, #01100000B ; counter 1, mode 2, ;C/T=1


external pulses
MOV TH1, #0 ; clear TH1
SETB P3.5 ; make T1 input
AGAIN: SETB TR1 ; start the counter
BACK: MOV A, TL1 ; get copy of TL
MOV P2, A ; display it on port 2
JNB TF1, BACK ; keep doing, if TF = 0
CLR TR1 ; stop the counter 1
CLR TF1 ; make TF=0
SJMP AGAIN

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.

Initial value calculation: //Program2


Frequency f=1 KHz ORG 0000H
Total time period T=1ms MOV TMOD,#10H
Required delay=TON=TOFF=0.5ms HERE: MOV TL1,#33H
[(FFFF-XXXX)+1]*1.085µs = 0.5ms MOV TH1,#0FEH
XXXX = FE33H CPL P1.0
ACALL DELAY
SJMP HERE
DELAY: SETB TR1
AGAIN: JNB TF1,AGAIN
CLR TR1
CLR TF1
RET
END

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

You might also like