Mode 1 Programming
1. Load the TMOD register a value indicating which timer (Timer0 or Timer1) is to be used and timer Mode 1 selected (M1=0, M0=1).
2. Load THx and TLx with initial count values.
To calculate the values to be loaded to THx and TLx, you can use the formula:
Where:
delay = desired delay in seconds
YYXX = values to be loaded to THx and TLx ( YY = THx, XX = TLx)
Example:
Suppose you want to create a 10ms delay using Timer0 and 11.0592MHz crystal. What are the values to be loaded to TH0 and TL0?
Solution:
Therefore:
TH0 = DC;
TL0 = 00;
3. Start timer by setting TRx (TRx=1).
4. Keep monitoring the timer flag (TFx) to see if it is raised (TFx=1).
5. Stop timer (TRx=0).
6. Clear the timer flag (TF=0) for the next round.
7. Go back to step 2 to load THx and TLx again.
Example of Mode 1
Write a program that will generate a square wave with a period of 20ms on pin P3.0. Assume that the crystal frequency is 11.0592MHz.
Solution:
The period of the square wave is 20ms. This means that the high and low portions are 10ms in length. We can use either Timer0 or Timer1 for this purpose. Let us say, we are going to use Timer1 and mode1.
#include<reg51.h> sbit pulse = P3^0;void main(void) { while(1) { TMOD = 0b00010000; //Timer 1, mode 1 (16-bit) TL1=0x00; // load TL with 0x00 TH1=0xDC; //load TH with 0xDC TR1=1; //start Timer1 while(!TF1){;} //wait for timer1 overflow TR1=0; //stop timer1 pulse = ~pulse; //complement P3.0 TF1=0; //clear timer1 flag } }















THIS IS AN EXCELLENT JOB. AS EXPLICIT AS IT CAN BE!