Page 255 - Mechatronics with Experiments
P. 255

MICROCONTROLLERS  241
                             Example: Timer0 Interrupt      Below is a sample code which sets up Timer0 to
                             generate interrupt. Timer0 is defined as a low priority interrupt. Interrupt vector table
                             location for low priority interrupts is 000018h . As in the previous example, a shell form of
                             low priority ISR for a low priority interrupt address is placed at the 0x18 vector address,
                             and “goto” statement is used to direct the program execution to the Timer0 ISR.


                             #include <p18f452.h> /∗ Header file for PIC 18F452 register declarations ∗/
                             #include <timer.h>   /∗ For timer interrupt  ∗/

                             /∗ Interrupt Service Routine (ISR) logic ∗/
                             void Timer0_ISR(void) ;

                             #pragma code LOW_INTERRUPT_VECTOR = 0x18
                                        /∗ Specify where the program address to be stored ∗/

                             void Low_ISR(void)
                             {
                               _asm
                                    goto Timer0_ISR
                               _endasm
                             }
                             #pragma code  /∗ Restore the default program memory allocation. ∗/




                             #pragma interruptlow Timer0_ISR
                             void Timer0_ISR(void)
                             {
                                static unsigned char led_display = 0;

                                INTCONbits.TMR0IF = 0;
                                led_display = ˜led_display & 0x0F  /∗ toggle LED display ∗/
                                PORTB = led_display ;

                             }

                             /∗ Setup of the ISR ∗/
                             void EnableInterruptTimer0(void)
                             {
                                TRISB = 0;
                                PORTB = 0;
                                OpenTimer0 (TIMER_INT_ON & TO_SOURCE_INT & TO_16BIT) ;
                                INTCONbits.GIE = 1;
                             }


                             void main (void)
                             {

                                EnableInterruptTimer0() ;
                                while (1) ;   /∗ Wait indefinitely.  ∗/
                                              /∗ when the interrupt occurs,
                                                the corresponding ISR will be executed ∗/
                             }
   250   251   252   253   254   255   256   257   258   259   260