Timers and Interrupts (PIC16F84A)

Anish June 5, 2012 1 Comment

Timer Module:

A Timer is a simple module included along with almost most of the microcontrollers. A timer can be used as a timer/counter. The size of the timer varies from controller to controller. The PIC16F84A has one 8 bit timer module. The very basic operation of a timer is to count and nothing else!. In the timer mode, the module will increment automatically for every instruction cycle (instruction cycle is not same as the clock cycle) and in the counter mode, it increments on every rising/falling edge of the pulse given at pin RA4 (3rd pin). Since it is a 8 bit timer, it can count from 00h to FFh (00000000 to 11111111). After FFh it informs the controller that a overflow has occurred (Raises an interrupt) and then rolls back to 00h and again continues.


The prescalar is also a 8 bit counter. It is neither readable nor writable but it is setup by configuration bits of the OPTION_REG register. In simple words, it can be used to scale the timer module like we scale our graph sheets. For example, the timer increments for every instruction cycle and we do know that the microcontroller is capable of executing some million instructions per second. Since the timer is a 8 bit counter, it overflows sooner than we think! for this purpose, perscalar is used, for a prescalar of 1:8, the timer increments for every 8 instruction cycle. So, the timer will overflow slower than the previous scenario.

Timer interrupt:

It is now obvious that the timer raises the interrupt when the count goes from FFh to 00h. To show that a overflow has occured, the timer sets the second bit of the INTCON register. But this bit has to be cleared in the coming ISR(Interrupt service routine) before the interrupt can be re-enabled.


Generally, an interrupt can be assumed as a high priority work for the microcontroller. If the microcontroller is executing a program and if a interrupt arises, then it will service the interrupt and continue with the program.(Similar to when you are watching a movie in your computer, and a mail man calls you – what you do? Pause the movie, go, get your mail and come back and resume the movie. Like this you can get many interrupts.)  For example, consider that your microcontroller is executing a long program. But a peripheral interfaced to the controller urgently needs a result of some funky math, now the peripheral will raise a interrupt by giving the starting memory address of the location of its problem. The controller will then do that computing work for the peripheral(This is called a ISR – Interrupt Service Routine) and then goes to continue to execute its program from where it left. Now, how does the controller know where it left the program? Here, your microcontroller is intelligent! It saves the contents of its program counter (memory address of the next instruction to be executed) on to a stack and when it finishes the interrupt service routine which is always terminated with RETURN, the controller retrieves the next instruction’s memory address and continues the execution of the program.

The 16F84A has four interrupt sources.

  • External interrupt via RB0 pin
  • Timer overflow interrupt
  •  Port B change interrupt (pins RB4 to RB7)
  • Data EEPROM write complete interrupt.

There is a register called INTCON for controlling (enable/disable) and recording these interrupts.

The RB0 interrupt can be enabled/disabled by setting/clearing the fourth bit of the INTCON register. When there is a valid edge triggered interrupt on RB0, Then the first bit of the INTCON register will be set (This must be cleared in the ISR before enabling this interrupt again).

As said earlier, the timer overflow will set the second bit of the INTCON register. This interrupt can be enabled/disabled by setting/clearing the fifth bit of the INTCON register.

An Input change at the PortB pins 7 to 4 will set the zeroth bit of the INTCON register and this interrupt can be controlled by the third bit of the INTCON register.

After completion of the data EEPROM write cycle, a interrupt flag is set. This interrupt is controlled by the sixth pin of the INTCON register.


  • ash
    April 23, 2012

    wow..good one!!!1

Leave a Reply to ash Cancel reply

Your email address will not be published. Required fields are marked *