1 minute read
3.4.2 FINDING VALUES TO BE LOADED INTO TMR0H AND TMR0L
We should load TMR0H first and then load TMR0L because the value for TMR0H is kept in a
temporary register and written to TMR0H when TMR0L is loaded. This will prevent any error
Advertisement
in counting if the TMR0ON flag is set HIGH.
Program code using Timer0 to generate time delay (16-bit mode):
T0CON=0b_____ ; // Configure the T0CON register TMR0H=0x_____; // initial count values (high byte). TMR0L=0x_____; // initial count values (low byte). TMR0ON= 1; // Start the timer while(TMR0IF== 0); // Keep monitoring the TMR0IF to see if it is raised TMR0ON= 0; // Stop the timer TMR0IF= 0; //Clear TMR0IF
Program code using Timer0 to generate time delay (8-bit mode):
T0CON=0b______; // Configure the T0CON register TMR0L=0x_____; // initial count values TMR0ON= 1; // Start the timer while(TMR0IF== 0); // Keep monitoring the TMR0IF to see if it is raised TMR0ON= 0; // Stop the timer TMR0IF= 0; //Clear TMR0IF
3.4.2 Finding values to be loaded into TMR0H and TMR0L
The following are the steps to calculate the values to be loaded into TMR0H and TMR0L
registers:
a. Find the time for one clock period (Tc): TC = 1/ (FOSC /4/prescaler) s
b. Calculate the number of count (NOC) to get the required time delay:
NOC = Time Delay = Td Time Period Tc c. Get the value to be loaded into TMR0:
i. TMR0 = 65535 – count + 1 (16-bit mode) or TMR0 = 65536 – count
ii. TMR0= 255 – count + 1 (8-bit mode) or TMR0= 256 – count