Embedded System Applications 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 in counting if the TMR0ON flag is set HIGH.
Program code using Timer0 to generate time delay (16-bit mode): T0CON=0b_____ ; TMR0H=0x_____; TMR0L=0x_____; TMR0ON= 1; while(TMR0IF== 0); TMR0ON= 0; TMR0IF= 0;
// Configure the T0CON register // initial count values (high byte). // initial count values (low byte). // Start the timer // Keep monitoring the TMR0IF to see if it is raised // Stop the timer //Clear TMR0IF
Program code using Timer0 to generate time delay (8-bit mode): T0CON=0b______; TMR0L=0x_____; TMR0ON= 1; while(TMR0IF== 0); TMR0ON= 0; TMR0IF= 0;
// Configure the T0CON register // initial count values // Start the timer // Keep monitoring the TMR0IF to see if it is raised // Stop the timer //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 ii. TMR0= 255 β count + 1
(16-bit mode) or TMR0 = 65536 β count (8-bit mode) or TMR0= 256 β count 40