8051 Mode 2 Programming

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

8051 Micro-controller

8051 Overview (http://www.webclasses.in/8051-microcontroller/)


8051 Internal Organization of Computers (http://www.webclasses.in/8051-internal-organization-of-
computers/)
8051 Microcontrollers and embedded processors with 8051 family (http://www.webclasses.in/8051-
microcontrollers-and-embedded-processors-with-8051-family/)
8051 Compiler (http://www.webclasses.in/8051-compiler/)
8051 FLAG BITS AND PSW REGISTER (http://www.webclasses.in/8051-flag-bits-and-psw-register/)
8051 REGISTER BANKS AND STACK (http://www.webclasses.in/8051-register-banks-and-stack/)
8051 I/O Port , Reset , Oscillator (http://www.webclasses.in/8051-io-port-reset-oscillator/)
8051 WHY PROGRAM 8051 IN C (http://www.webclasses.in/8051-why-program-8051-in-c/)
8051 DATA TYPES (http://www.webclasses.in/8051-data-types/)
8051 TIME DELAY (http://www.webclasses.in/8051-time-delay/)
8051 Logical and Bit-wise operators (http://www.webclasses.in/8051-logical-and-bit-wise-operators/)
8051 TIMER PROGRAMMING (http://www.webclasses.in/8051-timer-programming/)
8051 TMOD Register (http://www.webclasses.in/8051-tmod-register/)
8051 Mode 1 Programming (http://www.webclasses.in/8051-mode-1-programming/)
8051 Mode 2 Programming (http://www.webclasses.in/8051-mode-2-programming/)
8051 COUNTER (http://www.webclasses.in/8051-counter/)

8051 Mode 2 Programming

Characteristics and operations of mode 1:


It is an 8-bit timer; therefore, it allows only values of 00 to FFH to be loaded into the timer’s
register TH.
After TH is loaded with the 8-bit value, the 8051 gives a copy of it to TL, the timer must be
started i.e. This is done by SET TR0=1 for timer 0 and SETB TR1=1 for timer 1.
After the timer is started, it starts to count up until it reaches its limit of FFH.
To stop the timer0, TR0 must be set low and to stop the timer1, TR1 must be set low
it rolls over from FFH to 00, it sets high a flag bit called TF (timer flag).
Each timer has its own timer flag: TF0 for timer 0, and TF1 for timer 1.
When the TL register rolls from FFH to 0 and TF is set to 1, TL is reloaded automatically with the
original value kept by the TH register.
To repeat the process, we must simply clear TF and let it go without any need by the
programmer to reload the original value.
This makes mode 2 an auto-reload, in contrast with mode 1 in which the programmer has to
reload TH and TL.

(http://www.webclasses.in/wp-content/uploads/2015/08/mode-2-programming.jpg)

mode 2 programming

To generate a time delay

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

cannot warrant full correctness of all content.

Proudly powered by Phonate Technologies (http://www.phonatetech.com/) | Copyright ©Webclasses.in

(https://www.facebook.com/pages/WebClassesin/525736977570928)

(https://plus.google.com/u/0/109119486751129691344)

You might also like