Beruflich Dokumente
Kultur Dokumente
used in desktops. So their in depth and clear knowledge is required for successful system software
engineers. This guide will explain the interrupt system in general and their application using PIC18
architecture. We will also learn about handling of interrupts in HI-TECH C for PIC18.
Enable Bit - The are suffixed with IE (Interrupt Enable) example TMR0IE stands for TIMER0
Interrupt Enable. It can be used to enable/disable the related interrupt. When set to '1' it enables the
interrupt.
Flag Bit - It is set automatically by the related hardware when the interrupt condition occurs. It
is generally suffixed with IF (Interrupt Fag). When it is set to '1' we know that interrupt has occurred.
For example when TMR0IF is set by TIMER0, it indicates that TIMER0 has overflowed.
Priority Bit - We can leave this for now to keep things simple. We won't be using interrupt
priority feature of PIC18.
In global level their are following bits to control the interrupts globally.
For example in above code snipped. void interrupt ISR() is the main service routine where the CPU jumps
for any interrupt (TIMER,A/D, INTx etc). As soon as we enter the ISR we check for the source of interrupt.
This done using the line
if(TMR0IE && TMROIF)
the above line check is the source of interrupt was TIMER0 (so it checks if TMR0IF is set or not). It also
checks TMR0IE (TIMER0 interrupt enable bit) to make sure this interrupt is enabled or not. It execute the
TIMER0 ISR only of both the condition are true. One more line on interest is line 23 where we clear the
flag, this is necessary otherwise the interrupt is triggered again.
You should note one more point the GIE (explained above) is cleared automatically when PIC18 enters
ISR, this make sure that interrupts cannot be interrupted them self. It also automatically sets GIE on
return from interrupt, which re enable interrupts.
In the above code (from line 9 to 20), you can see that for each overflow of timer (like alarm from an
alarm clock) we increment a counter and as soon as this counter reaches 76 we toggle an LED. And also
clear the counter (line 19). This gives us a LED that is toggled ever 1sec. So you see that LED is controlled
fully by the TIMER while CPU can keep on doing other jobs but LED will always blink at its predefined rate.
This example give a kind of illustration of multitasking, the LED take care of itself. You don't have to write
anything on the main() function to control the LED. Your main function could go on normally, for example
control a line follower robot. The TIMER will take care of the LED and toggle it every second. The LED
could be used for decoration of the robot.
Handling interrupt in C
Here I will show you how we can write programs in HI-TECH C that utilizes interrupt feature of PIC MCU.
The interrupt handling is very clean and straightforward in HI-TECH C (as compare to Microchip C18
Compiler). Please note that interrupt handling is not a standard feature of C language, so their is
significant diffenence between different compiler in handling interrupts.
Please note that to simplify the situtation, we are not using interrupt priority feature of PIC18(because this
is a guide for first time user of interrput). In HI-TECH C for PIC18 an ISR (interrupt service routine, that is
called when interrupt condition occur) is defined like this
1
/***************************************************************
**
2
3 A simple example to demonstrate the use of PIC Timers. In
this
4 example the TIMER0 is configured as follows.
5
6 *8 Bit Mode
7 *Clock Source from Prescaler
8 *Prescaler = FCPU/256 (Note: FCPU= Fosc/4)
9 *Over flow INT enabled
10
11 As our FCPU=20MHz/4 (We are running from 20MHz XTAL)
12 =5MHz
13
14 Time Period = 0.2uS
15 Prescaller Period = 0.2 x 256 = 51.2uS
16 Overflow Period = 51.2 x 256 = 13107.2 uS
17 = 0.0131072 sec
18
19 So we need 1/0.0131072 Over Flows to count for 1 sec
20 = 76.2939 Overflows
21
22 So we keep a counter to keep track of overflows.
23
24 When an over flow occurs the PIC jumps to ISR where we
25 increment counter. And when counter becomes 76 we toggle
26 RB1 pin. This pin is connected to LED. Therefore we
27 have a LED which is ON for 1 sec and Off for 1sec.
28
29
30 Target Chip: PIC18F4520
31 Target Compiler: HI-TECH C For PIC18
(http://www.htsoft.com/)
32 Project: MPLAP Project File
33
34 Author: Avinash Gupta
35 Copyright (c) 2008-2010
36 eXtreme Electronics, India
37 www.eXtremeElectronics.co.in
38
39 NOTICE
40 -------------
41 NO PART OF THIS WORK CAN BE COPIED, DISTRIBUTED OR
PUBLISHED WITHOUT A
42 WRITTEN PERMISSION FROM EXTREME ELECTRONICS INDIA. THE
LIBRARY, NOR ANY PART
43 OF IT CAN BE USED IN COMMERCIAL APPLICATIONS. IT IS
INTENDED TO BE USED FOR
44 HOBBY, LEARNING AND EDUCATIONAL PURPOSE ONLY. IF YOU WANT
TO USE THEM IN
45 COMMERCIAL APPLICATION PLEASE WRITE TO THE AUTHOR.
46
47
****************************************************************
*/
48
49 #include <htc.h>
50
51
52 //Chip Settings
53 __CONFIG(1,0x0200);
54 __CONFIG(2,0X1E1F);
55 __CONFIG(3,0X8100);
56 __CONFIG(4,0X00C1);
57 __CONFIG(5,0XC00F);
58
59 unsigned char counter=0;//Overflow counter
60
61 void main()
62 {
63 //Setup Timer0
64 T0PS0=1; //Prescaler is divide by 256
65 T0PS1=1;
66 T0PS2=1;
67
68 PSA=0; //Timer Clock Source is from Prescaler
69
70 T0CS=0; //Prescaler gets clock from FCPU (5MHz)
71
72 T08BIT=1; //8 BIT MODE
73
74 TMR0IE=1; //Enable TIMER0 Interrupt
75 PEIE=1; //Enable Peripheral Interrupt
76 GIE=1; //Enable INTs globally
77
78 TMR0ON=1; //Now start the timer!
79
80 //Set RB1 as output because we have LED on it
81 TRISB&=0B11111101;
82
83 while(1); //Sit Idle Timer will do every thing!
84 }
85
86 //Main Interrupt Service Routine (ISR)
87 void interrupt ISR()
88 {
89 //Check if it is TMR0 Overflow ISR
90 if(TMR0IE && TMR0IF)
91 {
92 //TMR0 Overflow ISR
93 counter++; //Increment Over Flow Counter
94
95 if(counter==76)
96 {
97 //Toggle RB1 (LED)
98 if(RB1==0)
99 RB1=1;
100 else
101 RB1=0;
102
103 counter=0; //Reset Counter
104 }
105 //Clear Flag
106 TMR0IF=0;
107 }
108 }