Sunteți pe pagina 1din 8

'Section 9.

1: Programming 8051 Timers


1. How many timers do we have in the 8051? Two timers: Timer 1 and Timer 0 each
being 16-bits.

2. The timers of the 8051 are 16-bit and are designated as Timer 0 and Timer 1

3. The registers of Timer 0 are accessed as low byte (TL0) and high byte(TH0).

4. The register of Timer 1 are accessed as low byte (TL1) and high byte(TH1).

5. In questions 3 and 4, are the registers bit-addressable? No (look at the sheet Dale gave
us that has the addresses of all the registers). We can’t do things like “CLR TL1.1” or
“SETB TH0.5” Correct
6. The TMOD register is a()__8__-bit register.
7. What is the job of the TMOD register? Sets various timer mode operations
8. True or false. TMOD is a bit-addressable register? True
9. Find the TMOD value for both Timer 0 and Timer 1, mode 2, software start/stop(GATE
=0), with the clock coming from the 8051 crystal. Set TMOD to #22H (or #00100010b).
10. Find the frequency and period used by the timer if the crystal attached to the 8051 has
the following values
(a) XTAL = 11.0592MHz (b) XTAL = 20MHz
(c)XTAL = 24MHz (d) XTAL = 30MHz
A). F= (1/12 machine cycles ) * XTAL = (1/12) * 11.0592e6 HZ = .92160 MHz
Period = T= 1/F = 1/.92160 MHZ = 1.085 microSec
B). F = (1/12) * 20e6 = 1.67 MHz; T = 0.6 microSec
C). F = (1/12) * 24e6 = 2MHZ; T = 1/(2e6) = .5 microSec
D). F = (1/12) * 30e6 = 2.5 MHZ; T= 1/(2.5e6) =.4 microSec
11. Indicate the size of the timer for each of the following modes.
(a) mode 0 (b) mode 1 (c) mode 2
a. 13 bit b. 16 bit c. 8-bit auto-reload
12. Indicate the rollover value(in hex and decimal) of the timer for each of the following for each
of the following modes.
(a) mode 0 (b) mode 1 © mode 2
a. 1FFFH b. FFFFH c. FFH

13. Indicate when the TF1 flag is raised for each of


the following modes.
(a) mode 0 (b) mode 1 © mode 2
a).when it rolls over from 1FFFH to 0
b). when it rolls over from FFFFH to 0
c). when it rolls over from FFH to 0
14. True or false. Both Timer 0 and Timer 1 have their own TF. True
15. True or false. Both Timer 0 and Timer 1 have their own timer start (TR). True
16. Assuming XTAL = 11.0592MHz, indicate when the TF0 flag is raised for the following
program.
MOV TMOD, #01H
MOV TL0, #12H
MOV TH0, #1CH
SETB TR0
The number of machine cycle elapsed :
2^16 = 65,536
1C12H = 7186
65,536– 1C12H= 58,350
65,536-7186=58,350
Time taken for TF0 flag is raised = 58,350 * (1.085) us = .063309 = 63.309ms
(2^16-LOADVALUE)*(1/(CLOCKFREQUENCY/12))=TIMEDELAY

do you get it now just convert it from hex to decimal to get the value you need to multiply
by (oh I get that!!)- Angel lol
17. Assuming that XTAL = 16MHz, indicate when the TF0 flag is raised for the following
program.
MOV TMOD, #01H
MOV TL0, #12H
MOV TH0, #1CH
SETB TR0
The number of machine cycle elapsed :
2^16 = 65,536
1C12H = 7186
65,536– 1C12H = 58,350
Time taken for TF0 flag is raised = 58,350 * (12/16) us = 43.7625ms

18. Assuming that XTAL = 11.0592MHz, indicate when the TF0 flag is raised for the following
program.
MOV TMOD, #01H ; TIMER 0, MODE 1
MOV TL0, #10H
MOV TH0, #0F2H
SETB TR0
The number of machine cycle elapsed :
2^16 = 65,536
F210H = 61968
65,536– F210H = 3,568
Time taken for TF0 flag is raised = 3,568 * (1.085) us = 3.87us

19. Assuming that XTAL = 20MHz, indicate when the TF0 flag is raised for the following
program.
MOV TMOD, #01H
MOV TL0, #12H
MOV TH0, #1CH
SETB TR0
The number of machine cycle elapsed :
2^16 = 65,536
1C12H = 7186
65,536– 1C12H = 58,350
Time taken for TF0 flag is raised = 58,350 * (12/20) us = 35.01us
20. Assume that XTAL = 11.0592MHz. Find the TH1, TL1 value to generate a time delay of
2ms. Timer 1 is programmable in mode 1.
The number of machine cycle elapsed :
2^16 = 65,536
Time taken for TF1 flag is raised = 2ms
N= (2ms)/(1.085us)=1843
65,536–N
65,536–1844 (ROUNDED UP) Why did you round up??? I got 1843.3 so shouldnt it be
1843?? Agree it should be 1843 (I got 1843.2 from 2ms / 1.085069444 microseconds). But
maybe the programmer doesn’t want to produce a delay that’s less than 2ms so rounding up
would be appropriate in that sense.

63692
in hex = F8CC
so TH1=F8
TL1=CC

21. Assume that XTAL = 16MHz. Find the TH1, TL1 value to generate a time delay of 5ms.
Timer 1 is programmable in mode 1.

16MHz / 12 = 1.333MHz 1/1.333MHz = 750ns


N = (5ms) / (750ns) = 6667 -> 65536 – 6667 = 58869d -> E5F5h
The number of machine cycle elapsed :
2^16 = 65,536
Time taken for TF1 flag is raised = 5ms
N= (5ms) / ((12/16)us)=6667 // 5ms / 750ns = 6667
65,536–N
65,536–6667 (ROUNDED UP)
58869
in hex = E5F5
so TH1=E5
TL1=F5
22. Assuming that XTAL = 11.0592MHz, program Timer 0 to generate a time delay of 2.5ms.
MOV TMOD, #01H; Timer 0 Mode 1 (16-bit)
MOV TL0, XXH
MOV TH0, XXH
SETB TR0
The number of machine cycle elapsed :
2^16 = 65,536
Time taken for TF1 flag is raised = 2.5ms
N= (2.5ms)/(1.085us)=2304.147
65,536–N
65,536–2304(ROUNDED UP)
63232
in hex = F700
so TH1=F7
TL1=00 //verified by David :) Approved by Daniel M

23. Assuming that XTAL = 11.0592MHz, program Timer 1 to generate a time delay of 0.2ms.
0.2m/1.085e-6=184.32
65536-184.32=65352=FF48
MOV TMOD, #10H
MOV TL1, #48H
MOV TH1, #0FFH
AND NOT A SINGLE FUCK WAS GIVEN :P

ALL ABOUT THAT:


http://www.facebook.com/photo.php?fbid=207124616089413&set=a.169058749896000.
39608.165518323583376&type=1&theater

QUIZ QUESTION
Assuming the crystal is 12Mhz, indicate when TF0 flag is raised in the following
program. it is assumed your answer is in usec. Where is the following program??
12MHz/12 = 1MHz 1/1MHz = 1us
INSERT ANSWER HERE → 1us

24. Assuming that XTAL = 20MHz, program Timer 1 to generate a time delay of 100ms.
Number of machine cycles elapsed: 2^16 = 65536
1/12*20e6 = 1.6 microsec 12/20e6= 6e-7 (why is it this?) because its the same as
12/11.0592e6=1.085e-6 oh ok I see
what I did wrong!
100ms/ 1.6 =62500 100ms/6e-7=166666.666667
65536-62500 =3096 =0C18 65536-166666.666667=63896=F998

MOV TMOD, #10H


MOV TL1, #18H MOV TL1, #98H
MOV TH1, #0C MOV TH1, #0F9H

----is this correct???

; DAVID’S - THE CORRECT VERSION LOL


MOV TMOD, #10H; Timer 1 Mode 1 (16-bit)
MOV TL0, #F5H
MOV TH0, #74H
SETB TR0

FREQ -->20MHz / 12 = 1.667MHz PER-->1/1.667MHz = 600ns


N = (100ms) / (600ns) = 166667 --> (2^16)=>65536 – 166667 = -101131d = FFFE_74F5h //
negative number

25. Assuming that XTAL = 11.0592MHz, and we are generating a square wave on pin P1.2, find
the lowest square wave frequency that we can generate using mode 1. Lowest frequency =
Highest time period since t = 1/f. The highest time period is created by loading 0 into TLx and
THx (timer 0 or 1 isn’t specified in the problem, it doesn’t matter). (65536-0)*(1.085069444
usec)=.0711 seconds. 1/.0711=14.0625 Hz
2 * 65536 * 1.085usec = .1422 1/.1422 = 7.03 Hz ???? // Yes, verified by David
26. Assuming that XTAL = 11.0592MHz, and we are generating a square wave on pin P1.2, find
the highest square wave frequency that we can generate using mode 1. Highest frequency =
Lowest time period since t = 1/f. The lowest time period is created by loading FFFEH into TLx
and THx. (65536-65535)*(1.085069444 usec)=1.085069444 usec. to get the period you
multiply 1.085 micro by 2 = 2.17micro. frequency of period = 460.82KHz 1/1.085069444
usec=921600 Hz
David:
Delay = (2^16 – 0) x 1.085us = 71.1065ms
Freq = 1/71.1065ms = 14.0634Hz

27. Assuming that XTAL = 16MHz, and we are generating a square wave on pin P1.2, find the
lowest square wave frequency that we can generate using mode 1.
65536-0 = 65536
t= 65536*(12/(16e6))=.049152 *2 = .098304
1/f = t, f=1/t=1/.098304=10.1725Hz
28. Assuming that XTAL = 16MHz, and we are generating a square wave on pin P1.2, find the
highest square wave frequency that we can generate using mode 1.
1/((65536-65535) * (12/16e6) )= 1.3 MHZ

29. In mode 2 assuming that TH1 = F1H, indicate which states timer 2 goes through until TF1 is
raised. How many states is that? 14 since FF-F1=E

30. Program Timer 1 to generate a square wave of 1kHz. Assume that XTAL = 11.0592MHz.
MOV TMOD, #10H
MOV TH1,#0FEH
MOV TL1,#33H
SETB TR1 ;INITIALIZE TIMER
1/1K=.001s/2=5e-4/1.085e-6=460.829
65536-460.829=65075.171=FE33

T= 1/f = 1/1000 = 100 microsec


50 microseconds/1.085 microsec = 46
65536-46 = 65490 = FFD2H
MOV TMOD, #10H
MOV TL1, #D2H
MOV TH1, # FFH

31. Program Timer 0 go generate a square wave of 3kHz. Assume that XTAL = 11.0592MHz.
MOV TMOD, #01H
MOV TH0, #0FFH
MOV TL0, #66H
SETB TR0

T= 1/3000 = 333 usec /2 = 167 usec/1.085usec = 154


65536 - 154= 65382 = FF66
32. Program Timer 0 to generate a square wave of 0.5kHz. Assume that XTAL = 20MHz.
1/0.5K=.002/2=.001/(12/20M)=1666.67
65536-1666.67=63869.4=F97D
MOV TMOD, #01H
MOV TH0, #0F9H
MOV TL0, #7DH
1/(20MHz/12)= .6 usec
T=1/f=1/0.5e3=.002 thats for a full wave so we then divide that by half .002/2=.001
.001/1.085e-6=921.6589862 .001/.6e-6=1666.67
65536-921.6589862=64614.34101=FC66 65536-1666.67=63869.33=F97D
MOV TMOD, #01H ;TIMER 0 MODE 1
HERE: MOV TL0, #66H #7DH
MOV TH0, #0FCH #0F9H
CPL P1.5
ACALL DELAY
SJMP HERE,nv mn A
DELAY: SETB TR0
AGAIN: JNB TF0, AGAIN
CLR TR0
CLR TF0
RET

33. Program Timer 1 to generate a square wave of 10kHz. Assume that XTAL = 20MHz
MOV TMOD, #10H
MOV TH1,
T= 1/f = 1/10e3 = 0.0001 ;50 microsec
50e-6/(12/20e6) = 50e-6/0.6e-6 = 83.33
65536 - 83.3 = 65454 (rounded up) = FFAE
MOV TMOD, #10H Timer 1 Mode 1
MOV TL1, #AEH
MOV TH1, #FFH

34. Assuming that XTAL = 11.0592MHz, show a program to generate a 1-second time delay.
Use any timer that you want.
1/1.085e-6 =921659
65536 - 921659 = -856123 = 2_F000H
MOV TMOD, #10H
MOV TH1, #0F0H
MOV TL1, #00H

35. Assuming that XTAL = 16MHz, show a program to generate a 0.25sec time delay. Use any
timer you want.
0.25sec/(12/16e6) = 333333
65536 -333333 =
This doesn’t make sense for some reason am know I am missing something but what>????
Never gonna give you up, never gonna let you down...
36. Assuming that XTAL = 11.0592MHz and that we are generating a square wave on pin P1.3,
find the lowest square wave frequency that we can generate using mode 2.
37. Assuming that XTAL = 11.0592MHz and that we are generating a square wave on pin P1.3,
find the highest square wave frequency that we can generate using mode 2.
38. Assuming that XTAL = 16MHz and that we are generating a square wave on pin P1.3, find
the lowest square wave frequency that we can generate using mode 2.
39. Assuming that XTAL = 16MHz and that we are generating a square wave on pin P1.3, find
the highest square wave frequency that we can generate using mode 2.
40. Find the value (in hex) loaded into TH in each of the following.
(a) MOV TH0, # -12 (b) MOV TH0, # -22 © MOV TH0, # -34 (d) MOV TH0, # -92
(e) MOV TH1, # -120 (f) MOV TH1, # -104 (g) MOV TH1, # -222 (h) MOV TH1, # -67
Does this mean just convert the decimal value to hex value???? yup
a. F4 b. EA c. DE d. A4 e. 88 f. 98 g. 22 h. BD
41. In Problem 40, indicate by what number the machine cycle frequency of
921.6kHz(11.0592MHz) is divided. SAY WHAT!!!!!!!! Wouldn’t the answer be 32?? and to get a
doubled rate. Change SMOD and then it will be divided by 16? I think this question is just
worded terribly - I think it’s asking, “By what number is the crystal frequency (11.0592 MHz)
divided by? 12 (p. 242) Oh so the machine cycles??

42. In Problem 41, find the time delay for each case from the time the timer starts to the time the
TF flag is raised.
Section 9.2: Counter Programming
43. To use the timer as an event counter we must set the C/T bit in the TMOD register to
_____HIGH____(low, high). ; MOV TMOD, 40H; → 0101_0000 Timer 1 Mode 1 w/couter
44. Can we use both of the timers as event counters? YES
45. For counter 0, which pin is used to input clocks? P3.4 (P. 255)
46. For counter 1, which pin is used to input clocks? P3.5
47. Program Timer 1 to be an event counter. Use mode 1 and display the binary count on P1
and P2 continuously. Set the initial count to 20, 000.
MOV TMOD, 01010000B
MOV TH1, #4EH
MOV TL1, #20H
MOV P1, #0FFH
MOV P2, #0FFH
SETB TR1
JNB
48. Program Timer 0 to be an event counter. Use mode 2 and display the binary count on P2
continuously. Set the initial count to 20.
49. Program Timer 1 to be an event counter. Use mode 2 and display the decimal count on P2,
P1, P0 continuously. Set the initial count to 99.
50. The TCON register is a(n)_______-bit register. eight
51. True or false. The TCON register is not a bit-addressable register. true
52. Give another instruction to perform the action of “SETB TR0”. SETB TCON.4
IGNORE 9.3 since it’s n C

S-ar putea să vă placă și