Download as pdf or txt
Download as pdf or txt
You are on page 1of 38

Digital Assignment-3

Course Name-Microcontroller and Microprocessor


Name:-Deepak Kumar Sharma
Reg no:-22BEC0769
Slot:-L39+L40
Faculty:-Dr.Somasundaram D
Q1. Making an led connected to port0 to glow for 100us using timer0,mode1
Sol:-
Aim:-
To make an LED connected to Port 0 glow for 100 microseconds using Timer 0
in Mode 1.
Components required:-
https://www.keil.com/download/pro
duct/ http://www.edsim51.com/
https://www.electronicshub.org/8051
-simulators/
https://www.edgefxtech.com/blog/8051-microcontroller-
simulator-and- emulator/
https://www.onworks.net/software/app-gsim51-8051-
microcontroller- simulator/

Algorithm:-
1.Initialize Timer 0 in Mode 1 with an appropriate reload value to generate a
100 microsecond delay.
2.Configure Port 0 as an output port to control the LED.
3.Turn on the LED by setting the corresponding bit in Port 0.
4.Start Timer 0.
5.Wait for Timer 0 to complete its 100 microsecond delay.
6.Turn off the LED by clearing the corresponding bit in Port 0.
7.End the program.
FlowChart:-

Start

Initialize the timer0


and mode 1

Configure Port 0 as an
output port to control
the LED.Turn on the LED
by setting the
corresponding bit in
Port 0.

Wait for Timer 0 to complete


its 100 microsecond
delay..Turn off the LED by
clearing the corresponding
bit in Port 0.

END

Code:-
ORG 0000H
LJMP MAIN
ORG 00BH
CPL P0.0
RETI
ORG 0030H
MAIN: MOV TMOD,#01H
MOV P0,#0FFH;
MOV TH0,#0A4H
MOV IE,#82H
SETB TR0
BACK: MOV A,P0
SJMP BACK
END
Output:-
Before Execution:

After Execution:-
Inference:-
The algorithm uses Timer 0 in Mode 1 to generate a precise delay of 100
microseconds, allowing the LED to glow for that duration before turning off.
Q2. Making an led connected to port0 to glow for 500us using timer0,mode1
or to generate a square wave of 2kHz using timer0 and mode1
Sol:-
Aim:-
To make an LED connected to Port 0 glow for 500 microseconds using Timer 0
in Mode 1 and generating square wave for 2KHz
Components required:-
https://www.keil.com/download/pro
duct/ http://www.edsim51.com/
https://www.electronicshub.org/8051
-simulators/
https://www.edgefxtech.com/blog/8051-microcontroller-
simulator-and- emulator/
https://www.onworks.net/software/app-gsim51-8051-
microcontroller- simulator/
Algorithm:-
1 Initialize Timer 0 in Mode 1 with a suitable reload value to generate a 500
microsecond delay.
2.Configure Port 0 as an output port to control the LED.
3.Turn on the LED by setting the corresponding bit in Port 0.
4.Start Timer 0.
5.Wait for Timer 0 to complete its 500 microsecond delay.
6.Turn off the LED by clearing the corresponding bit in Port 0.
7.End the program.

FlowChart:-

Start
Initialize the timer0
and mode 1

Configure Port 0 as an
output port to control the
LED.Turn on the LED by
setting the corresponding
bit in Port 0 and analyse
the wave

Wait for Timer 0 to complete


its 500 microsecond
delay..Turn off the LED by
clearing the corresponding
bit in Port 0.

END

Code:-
ORG 0000H
LJMP MAIN
ORG 00BH
CPL P0.0
RETI
ORG 0030H
MAIN: MOV TMOD,#01H
MOV P0,#0FEH;
MOV TH0,#34H
MOV IE,#82H
SETB TR0
BACK:MOV A,P0
SJMP BACK
END
Output:-

Inference:-
The LED glow algorithm uses Timer 0 in Mode 1 to generate a precise delay of
500 microseconds, allowing the LED to glow for that duration before turning
off. The square wave algorithm utilizes Timer 0 in Mode 1 to generate a square
wave of 2 kHz on Port 0..
Q3. Making an led connected to port0 to glow for 150us and then off for
400us using timer1 in mode1
Sol:-
Aim:-
To make an LED connected to Port 0 glow for 150 microseconds using Timer 1
in Mode 1 and then off for 400us
Components required:-
https://www.keil.com/download/pro
duct/ http://www.edsim51.com/
https://www.electronicshub.org/8051
-simulators/
https://www.edgefxtech.com/blog/8051-microcontroller-
simulator-and- emulator/
https://www.onworks.net/software/app-gsim51-8051-
microcontroller- simulator/

Algorithm:-
1.Initialize Timer 1 in Mode 1 with suitable reload values to generate a 150
microsecond delay and a 400 microsecond delay.
2.Configure Port 0 as an output port to control the LED.
3.Turn on the LED by setting the corresponding bit in Port 0.
4.Start Timer 1 for the 150 microsecond delay.
5.Wait for Timer 1 to complete the 150 microsecond delay.
6.Turn off the LED by clearing the corresponding bit in Port 0.
7.Start Timer 1 again for the 400 microsecond delay.
8.Wait for Timer 1 to complete the 400 microsecond delay.
9.Repeat the process from step 3 to create the desired blinking pattern.

FlowChart:-
Start

Initialize the timer0


and mode 1

Configure Port 0 as an
output port to control the
LED..Turn on the LED by
setting the corresponding
bit in Port 0.Start Timer 1
for the 150 microsecond
delay.

Sta rt Ti mer 1 for the 150


mi cros econd delay..Wait for Timer
1 to compl ete the 150 mi crosecond
del ay.Turn off the LED by cl earing
the corresponding bit i n Port 0.Start
Ti mer 1 a gain for the 400
mi cros econd delay.

8.Wait for Timer 1 to


complete the 400
microsecond delay.
END

Code:-
DELAY_ON equ 150 ; 150 microseconds delay for LED on
DELAY_OFF equ 400 ; 400 microseconds delay for LED off
ORG 0000h ; Start of the program
MAIN:
; Initialize Timer 1 in Mode 1
MOV TMOD, #01h ; Timer 1, Mode 1 (16-bit timer, mode 1)
MOV TH1, #0 ; Initialize Timer 1 high byte
MOV TL1, #0 ; Initialize Timer 1 low byte
MOV P0, #0FFh ; Set all bits of Port 0 as output
LOOP:
; Turn on the LED (set bit 0 of Port 0)
SETB P0.0 ; Set bit 0 of Port 0
MOV TL1, #LOW(DELAY_ON)
MOV TH1, #HIGH(DELAY_ON)
SETB TR1 ; Start Timer 1
LOOP1:
JNB TF1, LOOP1 ; Wait for Timer 1 to overflow
CLR P0.0 ; Clear bit 0 of Port 0
MOV TL1, #LOW(DELAY_OFF)
MOV TH1, #HIGH(DELAY_OFF)
SETB TR1 ; Start Timer 1
LOOP2:
JNB TF1, LOOP2 ; Wait for Timer 1 to overflow
SJMP LOOP ; Repeat the loop indefinitely
END
Output:-
Before Execution:
After Execution:-

Inference:-
This algorithm will create the specified LED blinking pattern with the desired
durations of glow and off time using Timer 1 in Mode 1.
Q4. Making an led connected to port0 to glow for 100us in mode2 of timer1
Sol:-
Aim:-
To make an LED connected to Port 0 glow for 100 microseconds using Timer 1
in Mode 2
Components required:-
https://www.keil.com/download/pro
duct/ http://www.edsim51.com/
https://www.electronicshub.org/8051
-simulators/
https://www.edgefxtech.com/blog/8051-microcontroller-
simulator-and- emulator/
https://www.onworks.net/software/app-gsim51-8051-
microcontroller- simulator/

Algorithm:-
1.Initialize Timer 1 in Mode 2 with an appropriate reload value to generate a
100 microsecond delay.
2.Configure Port 0 as an output port to control the LED.
3.Turn on the LED by setting the corresponding bit in Port 0.
4.Start Timer 1.
5.Wait for Timer1 to complete its 100 microsecond delay.
6.Turn off the LED by clearing the corresponding bit in Port 0.
7.End the program.
FlowChart:-

Start
Initialize the timer1
and mode 2

Configure Port 0 as an
output port to control
the LED.Turn on the LED
by setting the
corresponding bit in
Port 0.

Wait for Timer 1 to complete


its 100 microsecond
delay..Turn off the LED by
clearing the corresponding
bit in Port 0.

END

Code:-
ORG 0000H
LJMP MAIN
ORG 00BH
CPL P0.0
RETI
ORG 0030H
MAIN: MOV TMOD,#20H
MOV P0,#0FFH;
MOV TH0,#0A4H
MOV IE,#82H
SETB TR0
BACK: MOV A,P0
SJMP BACK
END
Output:-
Before Execution:

After Execution:-
Inference:-
The algorithm uses Timer 1 in Mode 2 to generate a precise delay of 100
microseconds, allowing the LED to glow for that duration before turning off.
Q5.Make P2.7 glow and off for 1000us in mode1 of timer0
Sol:-
Aim:-
To make an LED connected to Port 2.7 glow for 1000 microseconds using Timer
0 in Mode 1.
Components required:-
https://www.keil.com/download/pro
duct/ http://www.edsim51.com/
https://www.electronicshub.org/8051
-simulators/
https://www.edgefxtech.com/blog/8051-microcontroller-
simulator-and- emulator/
https://www.onworks.net/software/app-gsim51-8051-
microcontroller- simulator/

Algorithm:-
1.Initialize Timer 0 in Mode 1 with an appropriate reload value to generate a
1000 microsecond delay.
2.Configure Port 2.7 as an output port to control the LED.
3.Turn on the LED by setting the corresponding bit in Port 0.
4.Start Timer 0.
5.Wait for Timer 0 to complete its 1000 microsecond delay.
6.Turn off the LED by clearing the corresponding bit in Port 2.7
7.End the program.
FlowChart:-
Start

Initialize the timer0


and mode 1

Configure Port 2.7 as an


output port to control
the LED.Turn on the LED
by setting the
corresponding bit in
Port 2.7.

Wait for Timer 0 to complete


its 1000 microsecond
delay..Turn off the LED by
clearing the corresponding
bit in Port 2.7.

END

Code:-
ORG 0000H
LJMP MAIN
ORG 00BH
CPL P2.7
RETI
ORG 0030H
MAIN: MOV TMOD,#01H
MOV P2,#0FCH;
MOV TH0,#68H
MOV IE,#82H
SETB TR0
BACK: MOV A,P2
SJMP BACK
END
Output:-
Before Execution:

After Execution:-
Inference:-
The algorithm uses Timer 0 in Mode 1 to generate a precise delay of 1000
microseconds on port 2.7 pin, allowing the LED to glow for that duration
before turning off.
Q6. Make P0.0 and P1.0 LEDs glow and off for 200 microseconds using Timer
0 in Mode1
Sol:-
Aim:-
To make both P0.0 and P1.0 LEDs glow for 200 microseconds and then turn off
for 200 microseconds using Timer 0 in Mode 1.
Components required:-
https://www.keil.com/download/pro
duct/ http://www.edsim51.com/
https://www.electronicshub.org/8051
-simulators/
https://www.edgefxtech.com/blog/8051-microcontroller-
simulator-and- emulator/
https://www.onworks.net/software/app-gsim51-8051-
microcontroller- simulator/

Algorithm:-
1.Initialize Timer 0 in Mode 1.
2.Configure Port 0 and Port 1 as output ports to control the LEDs.
3.Set both P0.0 and P1.0 to turn on (glow) simultaneously.
4.Start Timer 0 for 200 microseconds.
5.Wait for Timer 0 to complete the 200-microsecond delay.
6.Clear both P0.0 and P1.0 to turn off simultaneously.
7.Start Timer 0 again for another 200 microseconds.
8.Repeat the process from step 3 to create the desired blinking pattern..

FlowChart:-

Start
Initialize the timer0
and mode 1

Configure Port 0 and Port


1 as output ports to
control the LEDs..Set both
P0.0 and P1.0 to turn on
simultaneously.Start
Timer 0 for 200
microseconds.

Wa i t for Timer 0 to complete the


200-mi cros econd delay.Cl ear both
P0.0 a nd P1.0 to turn off
s i multaneously..Start Ti mer 0 a gain
for a nother 200 mi croseconds

END

Code:-
DELAY_ON equ 200 ; 200 microseconds delay for LED on
DELAY_OFF equ 200 ; 200 microseconds delay for LED off

ORG 0000h ; Start of the program

MAIN:
MOV TMOD, #01h ; Timer 0, Mode 1 (16-bit timer, mode 1)
MOV TH0, #0 ; Initialize Timer 0 high byte
MOV TL0, #0 ; Initialize Timer 0 low byte
MOV P0, #0FFh ; Set all bits of Port 0 as output
MOV P1, #0FFh ; Set all bits of Port 1 as output
LOOP:
SETB P0.0 ; Set bit 0 of Port 0
SETB P1.0 ; Set bit 0 of Port 1
MOV TL0, #LOW(DELAY_ON)
MOV TH0, #HIGH(DELAY_ON)
SETB TR0 ; Start Timer 0

LOOP1:
JNB TF0, LOOP1 ; Wait for Timer 0 to overflow
CLR P0.0
CLR P1.0 ; Clear bit 0 of Port 1
MOV TL0, #LOW(DELAY_OFF)
MOV TH0, #HIGH(DELAY_OFF)
SETB TR0 ; Start Timer 0

LOOP2:
JNB TF0, LOOP2 ; Wait for Timer 0 to overflow
SJMP LOOP ; Repeat the loop indefinitely
END
Output:-
Inference:-
The algorithm will use Timer 0 in Mode 1 to generate precise delays of 200
microseconds for LED glow and LED off cycles. This will create a blinking
pattern where both LEDs will alternately glow and turn off with a 200-
microsecond duration for each state.
Q7. Make inputs from P0.2,P0.1,P0.0 as switch and led connected to P1.0 is to
be glow as (001-1kHz),(010-2kHz),(100-4kHz),delay is given using
timer0,mode1
Sol:-
Aim:-
To use inputs from P0.2, P0.1, and P0.0 as switches to control the frequency of
the LED connected to P1.0 (001 - 1 kHz, 010 - 2 kHz, 100 - 4 kHz) using Timer 0
in Mode 1.
Components required:-
https://www.keil.com/download/pro
duct/ http://www.edsim51.com/
https://www.electronicshub.org/8051
-simulators/
https://www.edgefxtech.com/blog/8051-microcontroller-
simulator-and- emulator/
https://www.onworks.net/software/app-gsim51-8051-
microcontroller- simulator/

Algorithm:-
1. Initialize Timer 0 in Mode 1.
2. Configure Port 0 (P0) with P0.2, P0.1, and P0.0 as input switches and
Port 1 (P1) with P1.0 as the output for the LED.
3. Continuously monitor the states of P0.2, P0.1, and P0.0 to determine the
switch combinations.
4. If the switch combination is 001, set Timer 0 to generate a 1 kHz square
wave for the LED on P1.0.
5. If the switch combination is 010, set Timer 0 to generate a 2 kHz square
wave for the LED on P1.0.
6. If the switch combination is 100, set Timer 0 to generate a 4 kHz square
wave for the LED on P1.0.
7. Wait for Timer 0 to complete the delay for the specified frequency.
8. Repeat the process to continuously update the LED frequency based on
the switch inputs.

FlowChart:-
Start

Initialize the timer0


and mode 1

Configure Port 0 (P0) with P0.2,


P0.1, and P0.0 as input switches
and Port 1 (P1) with P1.0 as the
output for the LED.Continuously
monitor the states of P0.2, P0.1,
and P0.0 to determine the switch
combinations.If the switch
combination is 001, set Timer 0
to generate a 1 kHz square wave
for the LED on P1.0

6.If the s witch combination is 100,


s et Ti mer 0 to generate a 4 kHz
s quare wave for the LED on
P1.0.Wa i t for Ti mer 0 to complete
the delay for the s pecified
frequency

END

Code:-
SWITCH_1KHZ equ 001b ; Switch combination for 1 kHz
SWITCH_2KHZ equ 010b ; Switch combination for 2 kHz
SWITCH_4KHZ equ 100b ; Switch combination for 4 kHz
ORG 0000h ; Start of the program
MAIN:
MOV TMOD, #01h ; Timer 0, Mode 1 (16-bit timer, mode 1)
MOV TH0, #0 ; Initialize Timer 0 high byte
MOV TL0, #0 ; Initialize Timer 0 low byte
MOV P0, #0FFh ; Set all bits of Port 0 as input
CLR P1.0 ; Clear bit 0 of Port 1 for LED output
LOOP:
MOV A, P0 ; Load switch inputs into accumulator
CJNE A, #SWITCH_1KHZ,LOOP1
MOV TH0, #0 ; Set Timer 0 high byte for 1 kHz
MOV TL0, #250 ; Set Timer 0 low byte for 1 kHz (1 ms delay)
SJMP start_timer
LOOP1:
CJNE A, #SWITCH_2KHZ, LOOP2
MOV TH0, #0 ; Set Timer 0 high byte for 2 kHz
MOV TL0, #125 ; Set Timer 0 low byte for 2 kHz (0.5 ms delay)
SJMP start_timer
LOOP2:
CJNE A, #SWITCH_4KHZ, LOOP ; If not 4 kHz, repeat loop
MOV TH0, #0 ; Set Timer 0 high byte for 4 kHz
MOV TL0, #62 ; Set Timer 0 low byte for 4 kHz (0.25 ms delay)
start_timer:
SETB TR0 ; Start Timer 0
timer_loop:
JNB TF0, timer_loop ; Wait for Timer 0 to overflow
CPL P1.0 ; Complement bit 0 of Port 1 for LED output
SJMP loop ; Repeat the loop indefinitely
END
Output:-
Inference:-
The algorithm will monitor the states of P0.2, P0.1, and P0.0 to determine the
desired frequency (1 kHz, 2 kHz, or 4 kHz) for the LED connected to P1.0. Timer
0 in Mode 1 will be used to generate the corresponding delays to achieve the
specified frequencies.

Answer the following question:-


Q1. Explain all Timer registers.
Ans:-In the context of the 8051 microcontroller, Timer registers are essential
for controlling the timing and generating delays or timing intervals. Here's an
explanation of the Timer registers commonly used in 8051 assembly language
programming:
1. TMOD (Timer Mode Register):
 This register controls the operating modes of Timer 0 and Timer 1.
 Each 4-bit nibble in TMOD corresponds to one of the timers (T0
and T1).
 TMOD is used to set the mode of operation for Timer 0 and Timer
1. For example, setting TMOD to 01h configures Timer 0 in Mode
1 (16-bit timer mode with auto-reload), while setting it to 20h
configures Timer 1 in Mode 2 (8-bit auto-reload timer with 8-bit
prescaler).
2. TCON (Timer Control Register):
 TCON contains control bits for Timer 0 and Timer 1, including
start/stop control, timer overflow flags, and external interrupt
flags.
 TF0 (Timer 0 Overflow Flag) and TF1 (Timer 1 Overflow Flag)
indicate when Timer 0 or Timer 1 overflows, respectively.
 TR0 (Timer 0 Run Control) and TR1 (Timer 1 Run Control) are used
to start or stop Timer 0 and Timer 1.
 TF0 and TF1 must be manually cleared in software after handling
timer overflow conditions.
3. TH0/TL0 and TH1/TL1 (Timer 0/1 High/Low Byte Registers):
 These register pairs are used to set the initial value and reload
value for Timer 0 and Timer 1.
 THx/TLx together form a 16-bit register where THx holds the high
byte and TLx holds the low byte.
 In Timer modes where auto-reload is enabled (such as Mode 1),
THx/TLx are loaded with the reload value when the timer
overflows, allowing continuous timing operations.
4.Timer 0 and Timer 1 Modes:
 Timer 0 and Timer 1 can operate in various modes, including:
 Mode 0: 13-bit timer with auto-reload.
 Mode 1: 16-bit timer with auto-reload.
 Mode 2: 8-bit timer with auto-reload and 8-bit prescaler.
 Mode 3: Two 8-bit timers operating independently.
These Timer registers collectively enable precise timing control and generation
of timing intervals for tasks such as delay generation, frequency measurement,
and event timing in 8051 assembly language programming.
-------------------------------------------THE END--------------------------------------------------

You might also like