Simple example of this process is the digital wristwatch. The role of this electronic system is to display time in a very precise manner and change the display every second (for seconds), every minute (for minutes) and so on. To perform the steps we've listed, the system must use a timer, which needs to be very accurate in order to take necessary actions.The clock is actually a core of any electronic system.

We can use these timers for various important purposes. So far we used “delay procedure” to implement some delay in the program, that was counting up to a specific value, before the program could be continued. "Delay procedure" had two disadvantages:

  • we could not say exactly how long the Delay procedure was in progress
  • we could not perform any further steps while the program executes the "delay procedure"

Now, using Timers we can build a very precise time delays which will be based on the system clock and allow us to achieve our desired time delay well-known in advance. 
In order for us to know how to work with these timers, we need to learn some things about each one of them. We will study each one separately.

PIC Timer0 tutorial

The Timer0 module timer/counter has the following features:

  • 8-bit timer/counter
  • Readable and writable
  • 8-bit software programmable prescaler
  • Internal (4 Mhz) or external clock select
  • Interrupt on overflow from FFh to 00h
  • Edge select (rising or falling) for external clock

Let’s explain the features of PIC Timer0 we have listed above:

Timer0 has a register called TMR0 Register, which is 8 bits of size. 
We can write the desired value into the register which will be increment as the program progresses. Frequency varies depending on the Prescaler. Maximum value that can be assigned to this register is 255. 

TMR0IF - TMR0 Overflow Interrupt Flag bit. 
The TMR0 interrupt is generated when the TMR0 register overflows from FFh to 00h. This overflow sets bit TMR0IF (INTCON<2>). You can initialize the value of this register to what ever you want (not necessarily "0").
We can read the value of the register TMR0 and write into. We can reset its value at any given moment (write) or we can check if there is a certain numeric value that we need (read).

Prescaler - Frequency divider.

 

We can use Prescaler for further division of the system clock. The options are:

 

  • 1:2
  • 1:4
  • 1:8
  • 1:16
  • 1:32
  • 1:64
  • 1:128
  • 1:256

The structure of the OPTION_REG register

We perform all the necessary settings with OPTION_REG Register. The size of the register is 8 bits.Click the link to explore the relevant bits of OPTION_REG Register

OPTION REGISTER

Initializing the OPTION_REG register

The following is an example how we can initialize the OPTION_REG:

  1. PSA=0; // Prescaler is assigned to the Timer0 module
  2. PS0=1; // Prescaler rate bits
  3. PS1=1; // are set to “111”
  4. PS2=1; // which means divide by 256
  5. TOSE=0; // rising edge
  6. TOCS=0; // Internal instruction cycle clock