You are on page 1of 35

W4.

E4 8051 Keil –Assembly programming with Timers/Counters

W4.E4 8051 Keil –Assembly programming with


Timers/Counters

Course: BMHA204P – Microprocessors and Microcontrollers LAB


-Dr Richards Joe Stanislaus
Assistant Professor - SENSE
Email: richards.stanislaus@vit.ac.in
W4.E4 8051 Keil –Assembly programming with Timers/Counters

Important NOTE
•TAKE NOTES IN RECORD (Can strike
also, no marks will be reduced)
•Enter your registration number and
Full Name next to
1) all the plots and
2) all the programs.
W4.E4 8051 Keil –Assembly programming with Timers/Counters

LAB record instructions:


For the lab experiment,
• Write the Aim.
• Complete the Software/Hardware components used.
• Write the mnemonics / commands used and one line description
• Draw the Diagramatic representation of circuit connection / port
interfacing
• Write the special pins and components used and brief description
• Connect the inputs and outputs. write the same in the lab copy(inputs
and outputs section).
• Write the output as observed and displayed.
• Write a concluding statement for each program / sub-experiment.
• Submit the document’s hard copy on time by next week.
W4.E4 8051 Keil –Assembly programming with Timers/Counters

ASSEMBLY
PROGRAMMING WITH
Timers/Counters OF
8051
W4.E4 8051 Keil –Assembly programming with Timers/Counters

Timers/Counters
The 8051 has two timers/counters (T0 & T1) , they can be used
either as
To generate waveform signals with specific frequency
Timers to generate a time delay
Event counters to count events happening outside the
microcontroller

‰
Both Timer 0 and Timer 1 are 16 bits wide
W4.E4 8051 Keil –Assembly programming with Timers/Counters

Timers/Counters
How timer works?
W4.E4 8051 Keil –Assembly programming with Timers/Counters

Timers/Counters
Registers associated with 8051 timers are:

Timer/counter register TH & TL (16-bit) – To hold the value for

generating time delay

TMOD Register (8-bit) - to define mode of timer operation

TCON (8-bit) – To control the timer operation


W4.E4 8051 Keil –Assembly programming with Timers/Counters

Timers/Counters
Timer registers (T0 & T1)
W4.E4 8051 Keil –Assembly programming with Timers/Counters

Timers/Counters
 To calculate the values to be loaded into the TL and TH registers
(Assume XTAL = 11.0592 MHz)
1. Divide the desired time delay by 1.085 us
2. Perform 65536 – n, where n is the decimal value we got in
Step1
3. Convert the result of Step2 to hex, where yyxx is the initial hex
value to be loaded into the timer’s register
4. Set TL = xx and TH = yy To generate a time delay
W4.E4 8051 Keil –Assembly programming with Timers/Counters

Timers/Counters
Example: 250us time delay

Step1: 250us/1.085 us = 230pulses

Step2: P=65536-230=65306

Step3: 65306 converted by hexa decimal =FF1A

Step4: TH1=0xFF; TL1=0x1A;


W4.E4 8051 Keil –Assembly programming with Timers/Counters

Timers/Counters
TMOD Register
W4.E4 8051 Keil –Assembly programming with Timers/Counters

Timers/Counters
TCON Register
W4.E4 8051 Keil –Assembly programming with Timers/Counters

Timers/Counters
To generate a time delay
1. Load the TMOD value register indicating which timer (timer 0
or timer 1) and timer mode (0 or 1) is selected
2. Load registers TL and TH with initial count value
3. Start the timer using SETB TRx
4. Keep monitoring the timer flag (TF) with the JNB TFx,target
instruction
5. Stop the timer using CLR TRx
6. Clear the TF flag for the next round
7. Go back to Step 2 to load TH and TL again
W4.E4 8051 Keil –Assembly programming with Timers/Counters
W4.E4 8051 Keil –Assembly programming with Timers/Counters
W4.E4 8051 Keil –Assembly programming with Timers/Counters

MODE 1

MODE 2
W4.E4 8051 Keil –Assembly programming with Timers/Counters

Lab Task – 4.1


Write an 8051 assembly language program using timers to
generate a frequency of 2kHz on pin port pin P2.7. Assume the
crystal frequency as 11.0592 MHz.
Solution:
Look at the following steps.
(a) T = 1 / f = 1 / 2 kHz = 500 us the period of square wave.
(b) 1 / 2 of it for the high and low portion of the pulse is 250 us.
(c) 250 us / 1.085 us = 230 and 65536 – 230 = 65306 which in hex is
FF1AH.
(d) TL = 1A and TH = FF, all in hex. The program is as follow.
W4.E4 8051 Keil –Assembly programming with Timers/Counters

Lab Task – 4.1 PROGRAM


ORG 0000H
MOV TMOD, #01H
BACK: MOV TL0, #01AH
MOV TH0, #0FFH
SETB TR0
AGAIN: JNB TF0, AGAIN
CLR TR0
CPL P2.7
CLR TF0
SJMP BACK
W4.E4 8051 Keil –Assembly programming with Timers/Counters

Lab Task 4.1 Steps to Check output


1. To set the clock frequency, exit debug mode,
Select options for target as shown in figure,

Enter the clock frequency


in Xtal(MHz) : 11.0592
W4.E4 8051 Keil –Assembly programming with Timers/Counters

Lab Task 4.1 Steps to Check output


2. After setting clock frequency,
Save, Translate, Build, Go to debug mode
• Port used in program is P2 Pin 7 : P2.7
• Timer registers: Timer 0 : TH0 and TL0
• TMOD register TCON register
Select Peripherals and select IO port - Port 2 and
Timer -> Timer 0 which are used in program.
W4.E4 8051 Keil –Assembly programming with Timers/Counters

Lab Task 4.1 Steps to Check output


3. Click Run (F5) and then
Select logic analyzer
Setup in Logic Analyzer->
New (insert) and type P2.7 and close
W4.E4 8051 Keil –Assembly programming with Timers/Counters

Lab Task 4.1 Steps to Check output


4. RUN and Click Auto below Min/Max, and In or Out below Zoom.
5. Click on Cursor check box in right. Then Click Stop in Update screen
or stop execution
W4.E4 8051 Keil –Assembly programming with Timers/Counters

Lab Task 4.1 Steps to Check output


6. Click at one end of the wave form, and hold the mouse, move the
mouse pointer to other end of the waveform to see the difference
value as shown. Take screenshot in your computer with the difference
value displayed, for one cycle.

Verify the approx time period of the output waveform.


W4.E4 8051 Keil –Assembly programming with Timers/Counters

Lab Task – 4.2


Write an 8051 assembly language program to toggle bit of P1.7
for every 1 second. Assume the crystal frequency as 11.0592
MHz.
W4.E4 8051 Keil –Assembly programming with Timers/Counters

Lab Task 4.2


Making TH and TL both zero means that the timer will count from
0000 to FFFF, and then roll over to raise the TF flag. As a result, it
goes through a total Of 65536 states. Therefore, we have delay =
(65536 - 0) × 1.085 us = 71.1065ms.
71.1065ms * X =1000ms

where X represents the number of times the whole range from 0000
to FFFF needs to be repeated to generated an overall delay of 1
second (1000 ms).

X = 1000/71.1065 = 14
W4.E4 8051 Keil –Assembly programming with Timers/Counters

Lab Task 4.2: PROGRAM


ORG 0000H
MOV TMOD, #10H
REPEAT: MOV R0,#14
CPL P1.7
BACK: MOV TL1, #00H
MOV TH1, #00H
SETB TR1
AGAIN: JNB TF1, AGAIN
CLR TR1
CLR TF1
DJNZ R0, BACK
SJMP REPEAT
W4.E4 8051 Keil –Assembly programming with Timers/Counters

Lab Task 4.2 Steps to Check output


• Repeat the steps as given in 9.1 for checking output,
Note:
Port is P1.7
• Timer is Timer 1 with TH1 and TL1 as input.
• R0 register is used as counter
• Calculate the one time period of the output waveform.
W4.E4 8051 Keil –Assembly programming with Timers/Counters

Lab Task 4.3


Assuming that clock pulses are fed into pin T1 (P3.5), write an 8051
assembly language program for counter 1 in mode 2 to count the
pulses and display the state of the TL1 count on P1, which connects
to 8 LEDs.
W4.E4 8051 Keil –Assembly programming with Timers/Counters

Lab Task 4.3 PROGRAM


ORG 000H
MOV TMOD, #60H
MOV TH1, #0
SETB P3.5 ; Set input by clicking in Port 3.5 in peripherals
AGAIN: SETB TR1
BACK:MOV A, TL1
MOV P1, A ; For each on-off in P3.5: A and P1 are incremented
JNB TF1, BACK
CLR TR1
CLR TF1
SJMP AGAIN
W4.E4 8051 Keil –Assembly programming with Timers/Counters

Lab Task 4.3 Steps to Check output


• Open P3 in Peripherals – Port 3,
For every on-off click on checkbox corresponding to P3.5,
Observe increment in A and
Observe increment in P1 (P1 in peripherals - Port 1). Take screenshot
W4.E4 8051 Keil –Assembly programming with Timers/Counters

Challenging Tasks - 1
Write an 8051 assembly language program using timers to generate
a frequency of 10kHz on pin port pin P2.7. Assume the crystal
frequency as 11.0592 MHz.
W4.E4 8051 Keil –Assembly programming with Timers/Counters

Challenging task - 2
Write an 8051 assembly language program using timers to blink the
LED connected at P1.7 TON of 0.75 seconds and TOFF of 0.25
seconds. Assume the crystal frequency as 11.0592 MHz.
W4.E4 8051 Keil –Assembly programming with Timers/Counters

Challenging Tasks - 3
Write an 8051 assembly language program to
implement a counter for counting pulses of an input signals.
Assume the crystal frequency as 11.0592 MHz. Configure TIMER 1 to
generate a clock pulse for every one seconds and TIMER 0 as a
counter which receives input pulses at P3.4 from P3.5. Display final
count values in port P1 (TL0) & P2(TH0).
W4.E4 8051 Keil –Assembly programming with Timers/Counters

Challenging task 4 – Analyze the program


ORG 000H BACK: JNB TF1, BACK
REPEAT: MOV TMOD, #15H CLR TF1
SETB P3.4 CLR TR1
MOV TL0, #00 DJNZ R0, AGAIN
MOV TH0, #00 MOV A, TL0
SETB TR0 MOV P1,A
MOV R0,#21 MOV A, TH0
AGAIN: MOV TL1,#00 MOV P2,A
MOV TH1, #00 SJMP REPEAT
SETB TR1 END
W4.E4 8051 Keil –Assembly programming with Timers/Counters

LAB record instructions:


For the lab experiment,
• Write the Aim.
• Complete the Software/Hardware components used.
• Write the mnemonics / commands used and one line description
• Draw the Diagramatic representation of circuit connection / port
interfacing
• Write the special pins and components used and brief description
• Connect the inputs and outputs. write the same in the lab copy(inputs
and outputs section).
• Write the output as observed and displayed. GET OUTPUT VERIFIED
• Write a concluding statement for each program / sub-experiment.
• Submit the document’s hard copy on time by next week.

You might also like