8051 Mode 2 Programming
8051 Mode 2 Programming
8051 Mode 2 Programming
(http://www.webclasses.in/wp-content/uploads/2015/08/mode-2-programming.jpg)
mode 2 programming
Load the TMOD value register indicating which timer (timer 0 or timer 1) is to be used and which
timer mode 2 is selected.
Load registers TH with initial count value
Start the timer.
Keep monitoring the timer flag (TF).
Get out of the loop when TF becomes high.
Stop the timer.
Clear the TF flag for the next round.
Go back to Step 4, since mode 2 is auto-reload
Example:
In the following program, we create a square wave of 50% duty cycle (with equal portions high and low)
on the P1.5bit. Timer 0 is used to generate the time delay.
1 #include<reg51.h>
2
(http://www.webclasses.in/)
3 sbit toggle=P1^5;
4
5 void delay()
6
7 {
8
9 TMOD=0x20H;
10
11 TH0=0x05H;
12
13 TR0=1;
14
15 while(TF==0);
16
17 TR0=0;
18
19 TF0=0;
20
21 }
22
23 void main()
24
25 {
26
27 while(1)
28
29 {
30
31 toggle =~ toggle;
32
33 delay();
34
35 }
36
37 }
Previous (http://www.webclasses.in/8051-mode-1-programming/)
Next (http://www.webclasses.in/8051-counter/)
(http://www.webclasses.in/)
Webclasses.in is optimized for learning, testing, and training. Examples might be simplified to improve reading
and basic understanding. Tutorials, references, and examples are constantly reviewed to avoid errors, but we
(https://www.facebook.com/pages/WebClassesin/525736977570928)
(https://plus.google.com/u/0/109119486751129691344)