Sunteți pe pagina 1din 5

Timers & Interrupts in PIC 16F84

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.
Prescalar:
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.
Interrupts:
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
instructions 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.

Interrupts
Interrupts allow a microcontroller to respond to some events at the moment
they occur, regardless of what the microcontroller is doing at the time. This
provides a connection between a microcontroller and the external
environment. Generally, each interrupt changes the programme flow,
interrupts it and after executing an interrupt service routine continues on
from that same interrupt point.

The PIC16F84 four interrupt sources:


1. Termination of writing data to EEPROM
2. TMR0 interrupt caused by timer overflow
3. Interrupt during alteration on RB4, RB5, RB6 and RB7 pins of port B.
4. External interrupt from RB0/INT pin of microcontroller

Generally speaking, each interrupt source has two bits associated with it.
One enables interrupts, and the other detects when interrupts occur. There
is one common bit called GIE which can be used to disable or enable all
interrupts simultaneously. This bit is very useful when writing a programme
because it allows for all interrupts to be disabled for a period of time, so
that execution of some important part of a programme would not be
interrupted. When the instruction which resets GIE bit is executed (GIE=0,
all interrupts disallowed), any interrupt that remained unsolved would be
ignored. Interrupts which remained unsolved and were ignored, are
processed when GIE bit (GIE=1, all interrupts allowed) would be cleared.
When interrupt was answered, GIE bit was cleared so that any additional
interrupts would be disabled, return address was pushed onto stack and
address 0004h was written in programme counter - only after this does
replying to an interrupt begin! After interrupt is processed, the bit which
caused an interrupt must be cleared, or the interrupt routine would
automatically be processed over again during a return to the main
programme.

External interrupt on RB0/INT pin of microcontroller


External interrupt on RB0/INT pin is triggered by rising signal edge (if bit
INTEDG=1 in OPTION<6> register), or falling edge (if INTEDG=0). When
correct signal appears on INT pin, INTF bit is set in INTCON register. INTF bit
(INTCON<1>) must be cleared in interrupt routine, so that interrupt wouldn't
occur again while going back to the main programme. This is an important
part of the programme which programmemer must not forget, or programme
will constantly go into interrupt routine. Interrupt can be turned off by
resetting INTE control bit (INTCON<4>). Possible application of this interrupt
could be measuring the impulse width or pause length, i.e. input signal
frequency. Impulse duration can be measured by first enabling the interrupt
on rising edge, and upon its appearing, starting the timer and then enabling
the interrupt on falling edge. Timer should be stopped upon the appearing
of falling edge - measured time period represents the impulse duration.
Interrupt during a TMR0 counter overflow
Overflow of TMR0 counter (from FFh to 00h) will set T0IF (INTCON<2>) bit.
This is very important interrupt because many real problems can be solved
using this interrupt. One of the examples is time measurement. If we know
how much time counter needs in order to complete one cycle from 00h to
FFh, then a number of interrupts multiplied by that amount of time will
yield the total of elapsed time. In interrupt routine some variable would be
incremented in RAM memory, value of that variable multiplied by the
amount of time the counter needs to count through a whole cycle, would
yield total elapsed time. Interrupt can be turned on/off by setting/resetting
T0IE (INTCON<5>) bit.

Interrupt upon a change on pins 4, 5, 6 and 7 of port B


Change of input signal on PORTB <7:4> sets RBIF (INTCON<0>) bit. Four pins
RB7, RB6, RB5 and RB4 of port B, can trigger an interrupt which occurs when
status on them changes from logic one to logic zero, or vice versa. For pins
to be sensitive to this change, they must be defined as input. If any one of
them is defined as output, interrupt will not be generated at the change of
status. If they are defined as input, their current state is compared to the
old value which was stored at the last reading from port B.

Interrupt upon finishing write-subroutine to EEPROM


This interrupt is of practical nature only. Since writing to one EEPROM
location takes about 10ms (which is a long time in the notion of a
microcontroller), it doesn't pay off to a microcontroller to wait for writing
to end. Thus interrupt mechanism is added which allows the microcontroller
to continue executing the main programme, while writing in EEPROM is
being done in the background. When writing is completed, interrupt informs
the microcontroller that writing has ended. EEIF bit, through which this
informing is done, is found in EECON1 register. Occurrence of an interrupt
can be disabled by resetting the EEIE bit in INTCON register.

S-ar putea să vă placă și