Вы находитесь на странице: 1из 6

Microcontrollers And its Applications

Lab Task 4

Shivaum Heranjal L27+28


17BEC0315 PADMINI T N
TASK 4

Question 1: Write an 8051 assembly program using timer 0 to generate


a 500 Hz and timer 1 to 7 kHz square wave frequency on P1.0 and P1.1
respectively using Interrupts. Then examine the frequency using the KEIL
IDE inbuilt Logic Analyzer.

Note: Calculations for time delay and the general format of the control
register used in the program should be specified.

Soln.
First we calculate the initial values for both the timers:
We use both the timers in mode 1(16-bit timer mode) where TH and TL are
cascaded.
Desired frequency of wave generated by Timer 0= 500Hz; i.e time period=1/500=2ms
Desired frequency of wave generated by Timer 1= 7kHz,
i.e time period=1/7000=0.14286ms

Since in the programming, we will be complimenting the specified port (p1.0/p1.1)


every time our timer resets, the delay required is half the time period.

In mode 1,
(65535 − X + 1) * 1.085u = delay (where X is the value with which timer
will be initialised)
delay
⇒ (65535 − X + 1) = 1.085u

For Timer 0, RHS=922


For Timer 1, RHS=66

Therefore T H 0 T L0 = F C66H and T H 1 T L1 = F F BE H

Special Function Registers used:


● TMOD- 8 bit register which determines usage of timer 0/1 as counter/timer
and mode of operation. For timer 1 and 0 to be in mode 1, TMOD will be set
as 00010001b.

● TCON-8 bit register which determines when the timers 0/1 are set on/off(TR0
and TR1), also consists of timer overflow flags(TF0 and TF1) and lower 4 bits
control interrupt bits. In our program, we assign a high bit to TR0 and TR1 to
start the timers.

● IE- 8 bit register which enables/disables interrupts from 2 timer overflow


interrupts(3 for 8952 microcontroller), 2 external interrupts and 1 serial port
interrupt. Once it detects an interrupt, it jumps to a fixed location in memory
depending on the interrupt.In our program, instead of checking TF0/TF1, we
check for timer overflow interrupts for compliment our port values. we
assign IE=10001010, IE(7) is high to enable the interrupt register, IE(1)(jumps
to 000BH location) and IE(3)(jumps to 001BH location) enable Timer 0 and
Timer 1 interrupt respectively.

PROGRAM:
Result:-

Green-P1.0; Blue-P1.1
Port Desired Desired Obtained Obtained
Frequency Time Period Frequency Time Period
P1.0 500Hz 2ms 497.62Hz 2.00955ms
P1.1 7kHz 0.14286ms 6.678kHz 0.14974ms

Inference:-
Since there are other instructions which take up machine cycles to
execute, the practical delay is more than what is required. To combat
this, we can take the aforementioned extra delay into account and
tweak the initial values of the timers to get more closer to the desired
frequency square waves.

Вам также может понравиться