Interrupt Timer PDF
Interrupt Timer PDF
Tutorial (Part 2)
Microchip believes that its family of products is one of the most secure families of its kind on the market today, when used in the
intended manner and under normal conditions.
There are dishonest and possibly illegal methods used to breach the code protection feature. All of these methods, to our
knowledge, require using the Microchip products in a manner outside the operating specifications contained in Microchips Data
Sheets. Most likely, the person doing so is engaged in theft of intellectual property.
Microchip is willing to work with the customer who is concerned about the integrity of their code.
Neither Microchip nor any other semiconductor manufacturer can guarantee the security of their code. Code protection does not
mean that we are guaranteeing the product as unbreakable.
Code protection is constantly evolving. We at Microchip are committed to continuously improving the code protection features of our
products. Attempts to break Microchips code protection feature may be a violation of the Digital Millennium Copyright Act. If such acts
allow unauthorized access to your software or other copyrighted work, you may have a right to sue for relief under that Act.
NOTICE TO CUSTOMERS
All documentation becomes dated, and this manual is no exception. Microchip tools and
documentation are constantly evolving to meet customer needs, so some actual dialogs
and/or tool descriptions may differ from those in this document. Please refer to our web site
(www.microchip.com) to obtain the latest documentation available.
Documents are identified with a DS number. This number is located on the bottom of each
page, in front of the page number. The numbering convention for the DS number is
DSXXXXXA, where XXXXX is the document number and A is the revision level of the
document.
INTRODUCTION
This chapter contains general information that will be useful to know before using the
Timers Tutorial. Items discussed in this chapter include:
Document Layout
The Microchip Web Site
Customer Support
Document Revision History
DOCUMENT LAYOUT
This document provides an introduction to Timer0.
CUSTOMER SUPPORT
Users of Microchip products can receive assistance through several channels:
Distributor or Representative
Local Sales Office
Field Application Engineer (FAE)
Technical Support
Development Systems Information Line
Customers should contact their distributor, representative or field application engineer
(FAE) for support. Local sales offices are also available to help customers. A listing of
sales offices and locations is included in the back of this document.
Technical support is available through the web site at: http://support.microchip.com
OBJECTIVES
At the end of this lab you should be able to:
1. Develop application firmware to generate TMR0 overflow interrupts for specified
time periods.
2. Develop application firmware using an external clock source with the Timer0
module.
3. Develop external Timer0 clock source applications that meet PIC16F690
Electrical Specifications.
PREREQUISITES
In order to successfully complete this lab you should:
1. Understand basic circuit theory.
2. Understand basic digital electronic components such as gates, multiplexers and
memory registers.
3. Understand binary numbering systems and basic binary arithmetic.
4. Have some programming experience in the C Language.
5. Have completed the Introduction to MPLAB IDE/PICC-Lite Compiler
Tutorial (DS41322).
6. Have completed Timers: Timer0 Tutorial (Part1) (DS51682).
EQUIPMENT REQUIRED
This lab has been developed so that no hardware is required other than a PC. However,
you will need the following:
1. You will need to download the free MPLAB Integrated Development Environment
available at the following url:
http://www.microchip.com
When prompted, unzip the contents of the file into a temporary folder on your
desktop and then install.
2. Install the free HI-TECH PICC-LITE compiler (refer to the download instruc-
tions).
3. Once both programs are installed, complete the Introduction to MPLAB
IDE/PICC-Lite Compiler Tutorial (DS41322) if you havent already. This lab
assumes that you have done so and will expand on that knowledge.
4. It is also recommended that you download a copy of the PIC16F690 data sheet
(DS41262) from www.microchip.com.
TIMER0 INTERRUPT
In the previous lab we incremented a variable, counter, whenever the Timer0 value
register TMR0 overflowed from 255 to 0. To do this, a Polling algorithm was used
where the Timer0 Interrupt Flag (T0IF) was checked periodically to see if it was set to
1. This indicated that the TMR0 register had overflowed and that the counter
variable should be incremented. Now, you may notice that this type of algorithm of
periodically checking the T0IF ties up the processor for however long it takes to perform
the check. This may be acceptable for some applications, however, there will be times
when you would like the processor to devote its attention to a different task and only
take care of, or service, incrementing the counter variable when the T0IF flag over-
flows without needing to constantly check its status. This is easily accomplished on
mid-range PIC microcontrollers, such as the PIC16F690, using interrupts which serve
as an alarm, signaling that a particular event has occurred (such as when the T0IF flag
is set). In Figure 1-1, the left-hand flowchart represents the polling algorithm used in
the previous lab while the right-hand flowchart represents an alternative approach in
the form of an interrupt routine.
The PIC16F690 can be configured to perform a specific task when an interrupt occurs.
This is called the Interrupt Service Routine or ISR for short. When any interrupt
occurs, and there could be more than one, the processor will immediately stop what it
is doing and jump to the ISR to service the interrupt. Once completed, the processor
returns to what it was doing in code, prior to being interrupted. If multiple peripheral
interrupts are used, a prioritization algorithm will need to be included in the ISR to
determine which interrupt is serviced first. This lab will concentrate on Timer0 interrupts
only and not introduce any others.
INTCON
7 6 5 4 3 2 1 0
There are basically three primary Configuration bits used to configure any interrupt.
First, the Global Interrupt Enable bit (GIE) acts as a sort of Master Switch that must
be set to enable interrupt capability on the PIC mid-range microcontroller. The GIE will
automatically clear to 0 whenever an interrupt occurs, ensuring that no other interrupts
can occur during execution of the ISR. Therefore, once the ISR is completed, the GIE
must be set again to enable future interrupts. Next, each peripheral will have individual
interrupt enable bits. These individual interrupt enable bits may be contained within a
separate Peripheral Interrupt Register (PIRx). However, the Timer0 peripheral interrupt
enable bit is contained within the INTCON register. The Timer0 Overflow Interrupt Flag
bit (T0IF) is set, and remains set until cleared in software, when a Timer0 overflow has
occurred. This bit needs to be cleared if further interrupts are required for this
peripheral. The following recommended sequence should be used when configuring
any interrupt and following any ISR:
1. Clear the interrupt flag (Timer0 Overflow Interrupt Flag).
2. Enable the individual peripheral interrupt (set the Timer0 Overflow Interrupt
Enable bit).
3. Enable PIC mid-range MCU interrupt capability by setting the Global Interrupt
Enable bit.
Interrupt configuration using these steps will ensure that interrupts do not occur during
initialization, causing unexpected results.
PROCEDURE
Part 1: Configuring Timer0 Interrupts
1. Create a new project in MPLAB IDE using the following:
a) Select the PIC16F690 as the device.
b) Select HI-TECH PICC-LITE as the Language Toolsuite.
c) Create a folder on your C:\ drive and store the project there.
2. In the MPLAB IDE workspace, create a new file and copy the code in
Example 1-2 into it.
//Configure device
__CONFIG(INTIO & WDTDIS & PWRTDIS & MCLRDIS &
UNPROTECT & BORDIS & IESODIS & FCMDIS);
//-----------------------DATA MEMORY
//----------------------PROGRAM MEMORY
/*----------------------------------------------------------
Subroutine: Timer0_ISR
Parameters: none
Returns: nothing
Synopsys: This is the Interrupt Service Routine for
Timer0 overflow interrupts. On TMR0 overflow
the counter variable is incremented by 1
----------------------------------------------------------*/
void interrupt Timer0_ISR(void)
{
if (T0IE && T0IF) //are TMR0 interrupts enabled and
//is the TMR0 interrupt flag set?
{
T0IF=0; //TMR0 interrupt flag must be
//cleared in software
//to allow subsequent interrupts
++counter; //increment the counter variable
//by 1
}
}
OPTION = 0B00001000;
T0IE = 1; //enable TMR0 overflow interrupts
GIE = 1; //enable Global interrupts
/*----------------------------------------------------------
Subroutine: main
Parameters: none
Returns: nothing
Synopsys: Main program function
-----------------------------------------------------------*/
main(void)
{
Init(); //Initialize the relevant registers
Note: The specific line number in your code may differ from that shown.
Setting the breakpoint here will allow the Stopwatch to analyze the time interval
between successive counter variable increments.
Since the TMR0 register is 8-bits wide, it will take 256 internal instruction cycles for an
overflow to happen (28 = 256). Since the PIC16F690 is configured to run off of the 8 MHz
internal oscillator, we can use Equation 1-2 to determine TMR0 overflow.
On any TMR0 overflow, the Interrupt Service Routine (Timer0_ISR()) will execute by
clearing T0IF and then increment the counter variable. Lets check to see if this is
what happens.
4. Press the Reset button on the simulator toolbar.
5. Press the Run button to execute the program up until the breakpoint is
encountered. The Stopwatch window should resemble Figure 1-4.
Notice the Stopwatch indicates it took 139.5 S to reach the breakpoint. This doesnt
agree with Equation 1-2. However, dont forget that there is some extra code that the
central processing unit (CPU) will need to execute before configuring the Timer0
peripheral such as device configuration, variable declarations and so on.
6. Press the Zero button in the Stopwatch window. This clears the Stopwatch to
zero without resetting the CPU. Press the Run button once again in the simulator
toolbar. The Stopwatch should now resemble Figure 1-5.
The Stopwatch should now indicate that it has taken precisely 128 S to reach the
breakpoint as per Equation 1-2. In Timers: Timer0 Tutorial (Part1) (DS51682), the
time it took using polling instead of interrupts to increment the counter variable was
close but not exactly what was calculated. Why do you think that is?
It can be concluded that in timing sensitive applications, its a good idea to utilize TMR0
interrupts. In this way, if TMR0 overflows, the processor immediately stops whatever it
is doing, services the interrupt (executes the interrupt subroutine) and then resumes its
previous task.
Data Bus
FOSC/4
0 8
T0CKI 0 Sync 2
1 cycles
8-bit TMR0
PRESCALER 1
T0CS 3
PSA Set T0IF on
T0SE PS<2:0> overflow
BIT VALUE TMR0 RATE
000 1:2
001 1:4
010 1:8
011 1:16
100 1:32
101 1:64
110 1:128
111 1:256
OPTION
7 6 5 4 3 2 1 0
Note: Configuring the Timer0 module using the OPTION register is discussed in greater
detail in Timers: Timer0 Tutorial (Part1) (DS51682).
VDD 1 20 VSS
RA5/T1CK1/OSC1/CLKIN 2 19 RA0/AN0/C1IN+/ICSPDAT/ULPWU
RA4/AN3/T1G/OSC2/CLKOUT 3 18 RA1/AN1/C12IN0-/VREF/ICPSPCLK
RA2/AN2/T0CKI/INT/C1OUT
PIC16F690
RA3/MCLR/VPP 4 17
RC5/CCP1/P1A 5 16 RC0/AN4/C2IN+
RC4/C2OUT/P1B 6 15 RC1/AN5/C12IN1-
RC3/AN7/C12IN3-/P1C 7 14 RC2/AN6/C12IN2-/P1D
RC6/AN8/SS 8 13 RB4/AN10/SDI/SDA
RC7/AN9/SD0 9 12 RB5/AN11/RX/DT
RB7/TX/CK 10 11 RB6/SCK/SCL
The AN2 feature of this pin means it can be used for either digital or analog signals.
The PIC16F690, has been designed so that the analog pins (i.e., ANx) will default to
analog when the PIC MCU powers up. Since this pin will be used for a digital signal,
analog functionality is disabled using a Special Function Register called the Analog
Select Register (ANSEL) as shown in Figure 1-9.
Referring to the pin-out diagram for the PIC16F690 in Figure 1-8, notice that there are
actually 12 analog configurable pins (i.e., AN0 AN11). The Analog Select High
(ANSELH) register can be configured as well if needed for these pins. However, in this
application, the only pin of interest is the T0CKI/AN2 pin. These other pins will be
discussed in greater detail in other labs and as always, for more information on this or
any other feature of this product, refer to the data sheet.
When using an external input signal of any kind, it is important to pay particular
attention to electrical specifications and timing parameters listed in the data sheet. The
2-cycle synchronization block shown in Figure 1-6 samples the input signal on the
T0CKI pin and synchronizes it with the clock used by PIC16F690. Therefore, there are
some important equations to know when not using the prescaler for Timer0 (see
Equation 1-3 and Equation 1-4):
2
T = --------------------------------------------------------------------------------------- + 20nS = minimum HIGH T0CK1 signal pulse width
T0 H PIC MCU OscillatorFrequency
Example:
If using the 8 MHz internal oscillator, use Equation 1-4 and Equation 1-5.
2
T = --------------- + 20nS = a minimum HIGH pulse of 270nS
T0H 8MHz
2
T = --------------------------------------------------------------------------------------- + 20nS = minimum LOW T0CK1 signal pulse width
T0H PIC MCU OscillatorFrequency
Example:
If using the 8 MHz internal oscillator, the minimum low pulse width can be calculated
as shown in Equation 1-6.
2
T T 0 H = --------------- + 20nS = a minimum LOW pulse width of 270nS
8MHz
The internal sampling that occurs on the T0CKI signal takes two clock cycles of the PIC
microcontrollers oscillator. Divide 2 by the oscillator frequency in Hz to obtain an
answer in seconds (same as multiplying the oscillator frequency in seconds by 0.5).
The 20 nS added at the end of the equations represents a small 20 nS RC delay
present within the device. In this lab, the 8 MHz internal oscillator is used. Therefore, it
is necessary to ensure that the incoming signal stays High and/or Low for a minimum
of 270 nS when not using the prescaler. If the incoming signal is a TTL square wave,
this means the period can be no less than 270nS + 270nS = 540nS or a frequency of
(1/540nS) = 1.8 MHz
To use the prescaler on the T0CKI source signal, Equation 1-7 is used.
4
--------------------------------------------------------------------------------------------------------------------------- + 40nS
PIC MCU OSCILLATORFREQUENCY ( Hz )
T T 0 H = 20nS OR ------------------------------------------------------------------------------------------------------------------------------------------------------ whichever is
Prescale value (i.e. 2,4...256)
greater
= minimum T0CKI signal period
Example:
If using the 8 MHz internal oscillator and a Timer0 prescale value of 64, the minimum
T0CKI signal period is calculated as shown in Equation 1-8.
4
-------------- - + 40nS
8MHz
T = ------------------------------------------- = 8.4nS which is less than 20nS.
T0H 64 Therefore, use a minimum period of 20 nS
In Equation 1-5, if the calculated value is less than 20 nS, a minimum period of 20 nS
must be maintained. Otherwise, maintain a minimum period at the calculated value.
Procedure:
OPTION = 0B00101000;
T0IE = 1;//enable TMR0 overflow interrupts
GIE = 1; //enable Global interrupts
4. Next, select the Asych tab in the Stimulus window. The Stimulus window should
resemble Figure 1-11.
6. Click the cell immediately under the Action Tab and select Pulse High (see
Figure 1-13).
The Stimulus tool is now configured so that during a simulation Run, pressing the Fire
button next to the T0CKI cell will pulse that pin input High.
7. Open the Watch window and add the TMR0 Special Function Register as well as
the counter symbol (see Figure 1-14).
Note that the TMR0 register has a value of 5 corresponding to the number of Fire
button presses.
Try pressing the Fire button enough times to generate a TMR0 overflow interrupt that
will increment the counter variable.
EXERCISES
1. Using the code and Init() function from Lab 1, configure the prescaler to
generate a TMR0 interrupt that will increment the counter variable for each of the
following periods exactly assuming that the PIC16F690 internal 8 MHz oscillator
is used:
a. 8.192 mS
b. 1.024 mS
c. 15.616 mS
2. Using Equation 1-2, develop a new equation that determines the prescaler value
based off the required overflow period. Develop the equation further to determine
a value to preload into TMR0 to generate an interrupt that doesnt fit neatly into
a specific prescaler value.
3. Configure the application code used in question 1 to increment the counter
variable every 1 second.
4. Calculate the minimum external clock source periods on the T0CKI pin for the
following (these assume you are using the PIC16F690):
a. Using an external crystal oscillator of 20 MHz and a Timer0 prescaler value of:
i. 32
ii. 64
b. Using an external crystal oscillator of 20 MHz with the Timer0 prescaler
disabled.
c. Using the internal 32 kHz oscillator with the Timer0 prescaler set to 128.
5. Refer to the PIC16F690 data sheet (DS41262), Table 17-5 in the Electrical
Specifications section. Suppose that all equations have been performed
correctly. What is an external condition that could affect the synchronous
operation of the application using an external source on the T0CKI pin?
NOTES:
01/02/08