Interrupt on timer

Markd77

Joined Sep 7, 2009
2,806
There should be a general one. I am using the pic12 and 16s and the relevant document for those is Mid-Range MCU Family Reference Manual. I had a quick look but couldn't find the right one for 18F
 

Tahmid

Joined Jul 2, 2008
343
There should be one general one. I can't seem to find it in there site. Anyway, search for DS39500A. That's their reference manual.
 

Tahmid

Joined Jul 2, 2008
343
Rich (BB code):
#pragma code high_vector=0x08
void interrupt_at_high_vector(void)
{
  _asm GOTO high_isr _endasm
}
#pragma interrupt high_isr
void high_isr(void)
{
    LATC = 1; //RC0 HIGH
    INTCONbits.TMR0IF = 0; //CLEAR TMR0 INT FLAG
}
Hi,
In ASM this would be:
Rich (BB code):
   org 0x08
   goto interrupt_service_routine
interrupt_service_routine
   bsf   PORTC, 1
   bcf   INTCON, TMR0IF
   retfie
or, simply
Rich (BB code):
   org 0x08
   bsf   PORTC, 1
   bcf   INTCON, TMR0IF
   retfie
It works like this:
Everytime an interrupt occurs, the PIC moves the program counter to the interrupt vector, which in this case is 0x08. ORG tells compiler that this is from location 0x08. So the ISR is directly carried out using the ORG directive.
However in C, "#pragma code" is used to place code at a specific ROM address. But the C18 compiler does not place an ISR at the interrupt vector table automatically. That's why we use Assembly language instruction GOTO at the interrupt vector to transfer control to the ISR. This is done by:
Rich (BB code):
#pragma code high_vector=0x08
void interrupt_at_high_vector(void)
{
  _asm GOTO high_isr _endasm
}
Now we redirect it from adress location 0x08 to another subroutine / function to the ISR. This is done with the help of keyword interrupt as follows:
Rich (BB code):
#pragma interrupt high_isr
void high_isr(void)
{
    LATC = 1; //RC0 HIGH
    INTCONbits.TMR0IF = 0; //CLEAR TMR0 INT FLAG
}
The interrupt keyword tells that high_isr is the interrupt service routine, so then interrupt action is carried out. Notice, however that retfie (return from interrupt) is not required in C. This is because the compiler places this instruction at the end, as now it knows that high_isr is the interrupt service routine, as you have used the interrupt keyword.
Hope this helps.
 

Tahmid

Joined Jul 2, 2008
343
Yea, I think that's the one.
So, hopefully, now you're pretty clear regarding the basics of interrupts(there's a lot more to learn if this is your starting point).
 
Top