CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Fast Interrupts for Timer0, Timer1 and Timer2

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
thushi



Joined: 22 Feb 2006
Posts: 14

View user's profile Send private message

Fast Interrupts for Timer0, Timer1 and Timer2
PostPosted: Wed Mar 22, 2006 12:29 am     Reply with quote

Hi all,

I would like to implement similar application like Microchip AN843, for that I require Fast Interrupts for Timer0, Timer1 and Timer2 and Slow interrupts for RB and A to D converter.

Please help me to write fast interrupts (For Timer0, Timer 1 and Timer2) using PIC C. (Device is PIC18F452)

Assembly code inside PIC C would be okay.

I wrote it using #org command but it doesnt work properly.

my code as follows:

#ORG 0X000008, 0X000180
VOID MY_ISR()
{
#ASM

MOVWF hWTemp
MOVFF STATUS,hSTemp
MOVFF BSR,hBTemp
BRA HighIntSelect
NOP



MOVWF lWTemp
MOVFF STATUS,lSTemp
MOVFF BSR,lBTemp
BRA lowIntSelect ;Jump to interrupt polling routine

HIGH_CONTEXT_RESTORE:
MOVFF hBTemp,BSR
MOVF hWTemp,W
MOVFF hSTemp,STATUS
RETFIE 1

lowContextRestore:
MOVFF lBTemp,BSR
MOVF lWTemp,W
MOVFF lSTemp,STATUS
RETFIE 1;


HighIntSelect:
btfsc TMR2IF ;Timer2 to PR2 match?
bra TIMER2_PR2_Match
btfsc TMR1IF ;Timer1 overflow Interrupt?
bra TIMER1_OVERFLOW
btfsc TMR0IF ;Timer0 overflow Interrupt?
bra TIMER0_OVERFLOW ;Yes
BRA HIGH_CONTEXT_RESTORE


LOW_CONTEXT_SAVE:
MOVWF lWTemp
MOVFF STATUS,lSTemp
MOVFF BSR,lBTemp
BRA lowIntSelect ;Jump to interrupt polling routine


TIMER2_PR2_Match:

bcf TMR2IF ;
tstfsz PWM3_DUTYCYCLE ;If Software PWM duty cycle=0, then
bra PWM3_NOT_0 ;no need to set the PWM3 port pin
BRA HIGH_CONTEXT_RESTORE

TIMER1_OVERFLOW:
BCF PWM3_PORT ;PWM3 pin cleared after the duty cycle time expires
BCF TMR1IF
BRA HIGH_CONTEXT_RESTORE

TIMER0_OVERFLOW:

movff FREQ_REF_H,TMR0H ;Load the Higher byte of SpeedCommand to TMR0H
movff FREQ_REF_L,TMR0L ;Load the Lower byte of SpeedCommand to TMR0L

BCF TMR0IF
BSF TIMER0_OV_FLAG
BRA HIGH_CONTEXT_RESTORE

lowIntSelect:
btfsc RBIF ;RB interrupt?
bra CHECK_FAULT ;Yes
btfsc ADIF
bra AD_CONV_COMPLETE
BRA lowContextRestore

CHECK_FAULT:
BCF RBIF
BRA lowContextRestore

AD_CONV_COMPLETE:
movff ADRESH,FREQUENCY
movlw 0x14 ;Minimum Frequency set to 5Hz (scaling factor X4)
cpfsgt FREQUENCY
movwf FREQUENCY
movlw 0xF0 ;Limiting V/F to F= 60Hz (scaling factor X4)
cpfslt FREQUENCY
movwf FREQUENCY

bcf ADIF ;ADIF flag is cleared for next interrupt
BRA lowContextRestore

PWM3_NOT_0:
movlw 0xFF ;Higher byte of Timer1 loaded with FFh
movwf TMR1H
movff PWM3_DUTYCYCLE,TMR1L ;Lower byte of Timer1 loaded with Duty cycle
bsf PWM3_PORT ;PWM3 pin set high
BRA HIGH_CONTEXT_RESTORE



#ENDASM
}


Thank you,

Best Regards,

Thushi
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Wed Mar 22, 2006 2:44 am     Reply with quote

First, when posting source code use the 'Code' button. This will keep the formatting of your code intact.

Code:
#ORG 0X000008, 0X000180
Don't do this. This will map your fast interrupt handler over the start address 0x0018 of the normal interrupt. In CCS you create an interrupt handler by specifying the #int_xxx identifier, for Fast interrupts add the FAST keyword.

Code:
 MOVWF hWTemp
MOVFF STATUS,hSTemp
MOVFF BSR,hBTemp
You don't need to do this. On entering an interrupt the PIC18 processors will save the contents of these three registers to hardware shadow registers. On exiting the interrupt use RETFIE with parameter 1 to restore the contents of these register.
thushi



Joined: 22 Feb 2006
Posts: 14

View user's profile Send private message

Fast Keyword
PostPosted: Wed Mar 22, 2006 4:15 am     Reply with quote

Dear Mr. Ckielstra,

Thank you for your help.

I coudnt use FAST keyword for all three interrupt service routines . (T0, T1 and T2). This is the problem I have. I want to service the interrupts as soon as possible. T0, T1, T2 set to higher priority.

thank you

ckielstra wrote:
First, when posting source code use the 'Code' button. This will keep the formatting of your code intact.

Code:
#ORG 0X000008, 0X000180
Don't do this. This will map your fast interrupt handler over the start address 0x0018 of the normal interrupt. In CCS you create an interrupt handler by specifying the #int_xxx identifier, for Fast interrupts add the FAST keyword.

Code:
 MOVWF hWTemp
MOVFF STATUS,hSTemp
MOVFF BSR,hBTemp
You don't need to do this. On entering an interrupt the PIC18 processors will save the contents of these three registers to hardware shadow registers. On exiting the interrupt use RETFIE with parameter 1 to restore the contents of these register.
Mad Mad Shocked
Ttelmah
Guest







PostPosted: Wed Mar 22, 2006 4:43 am     Reply with quote

You cheat!....
Declare _one_ interrupt as fast. Write your complete handler, including the tests for which interrupt has occured, and the routines for the other interrupts inside this handler.
Then simply set the 'high priority' bits, for the other two interrupts.

Best Wishes
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Wed Mar 22, 2006 7:20 am     Reply with quote

Quote:
I coudnt use FAST keyword for all three interrupt service routines . (T0, T1 and T2). This is the problem I have. I want to service the interrupts as soon as possible. T0, T1, T2 set to higher priority.
CCS support for the FAST interrupts is 'minimal'...

Write CCS-C code like you are going to create a FAST interrupt for just a single interrupt.
The trick is to not only enable the interrupt for the specified FAST interrupt but also set the high priority flags for all other FAST interrupts (requires direct writing to the priority registers, no CCS command exists for this).

For the normal priority interrupts CCS can create a good dispatcher for you automatically, you don't have to write this code yourself.

It will look something like this:
Code:
#include <18F458.H>
#fuses HS, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#DEVICE HIGH_INTS=TRUE


#byte INTCON2 = 0xFF1
#byte IPR1    = 0xF9F


#INT_TIMER0 FAST        // Note the FAST keyword to make this a high priority interrupt
void my_fast_isr(void)
{
  // Put here your FAST interrupt code
  // Note that it is your responsibility to save and restore all modified registers,
  // except for Wreg, Status and BSR which are all saved to shadow registers
  // automatically by hardware.
}
                     
#INT_TBE
void tbe_isr(void)
{
  // place here your UART Tx code.
}

#INT_AD
void ad_isr(void)
{
  // put here your AD converter code.
}

//===============================
void main()
{
  // bit_set(INTCON2, 1);       // Set Timer0 as high priority
                                // Not required, the compiler will do this because
                                // we already defined this as a FAST interrupt

  bit_set(IPR1, 0);             // Set Timer1 as high priority
  bit_set(IPR1, 1);             // Set Timer2 as high priority

  enable_interrupts(INT_TIMER0);
  enable_interrupts(INT_TIMER1);
  enable_interrupts(INT_TIMER2);
  enable_interrupts(INT_TBE);
  enable_interrupts(INT_AD);
  enable_interrupts(GLOBAL);

  while(1);
}
thushi



Joined: 22 Feb 2006
Posts: 14

View user's profile Send private message

PostPosted: Wed Mar 22, 2006 8:06 am     Reply with quote

Dear Mr.Ttelmah

Thanks for your reply.

Can you please give me a sample code to do that. I coudnt understand what you wrote here. Is it some think like this?

Code:

#int_timer0 fast
void timer0_isr()
{
//my timer 0 interrupt routines goes here

---> should I check Interrupt flags for other pheripherals????
}





Question Question Question Rolling Eyes [/code]
thushi



Joined: 22 Feb 2006
Posts: 14

View user's profile Send private message

I require FAST Interrupts for T0, T1 and T2
PostPosted: Wed Mar 22, 2006 8:14 am     Reply with quote

Dear Ckielstra,

Thanks again..

I am doing motor control application so that time is more critical. Timer0 is used for set the frequency of the motor. Timer2 is used to generate PWM (Two channels) and Timer 1 is used for software PWM. so that Timer2 and Timer 1 interrupts have to be serviced as soon as possible. I need STATUS, Wreg and BSR to save. nothing other than that.

I set prority bits already. If I set Timer 2 interrupt FAST then Timer 1 is become slow, because it is saves all the contexts. How can I do that???

Question Question Question Exclamation Question Question

Thank you

Best Regards,

Thushi


ckielstra wrote:
Quote:
I coudnt use FAST keyword for all three interrupt service routines . (T0, T1 and T2). This is the problem I have. I want to service the interrupts as soon as possible. T0, T1, T2 set to higher priority.
CCS support for the FAST interrupts is 'minimal'...

Write CCS-C code like you are going to create a FAST interrupt for just a single interrupt.
The trick is to not only enable the interrupt for the specified FAST interrupt but also set the high priority flags for all other FAST interrupts (requires direct writing to the priority registers, no CCS command exists for this).

For the normal priority interrupts CCS can create a good dispatcher for you automatically, you don't have to write this code yourself.

It will look something like this:
Code:
#include <18F458.H>
#fuses HS, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#DEVICE HIGH_INTS=TRUE


#byte INTCON2 = 0xFF1
#byte IPR1    = 0xF9F


#INT_TIMER0 FAST        // Note the FAST keyword to make this a high priority interrupt
void my_fast_isr(void)
{
  // Put here your FAST interrupt code
  // Note that it is your responsibility to save and restore all modified registers,
  // except for Wreg, Status and BSR which are all saved to shadow registers
  // automatically by hardware.
}
                     
#INT_TBE
void tbe_isr(void)
{
  // place here your UART Tx code.
}

#INT_AD
void ad_isr(void)
{
  // put here your AD converter code.
}

//===============================
void main()
{
  // bit_set(INTCON2, 1);       // Set Timer0 as high priority
                                // Not required, the compiler will do this because
                                // we already defined this as a FAST interrupt

  bit_set(IPR1, 0);             // Set Timer1 as high priority
  bit_set(IPR1, 1);             // Set Timer2 as high priority

  enable_interrupts(INT_TIMER0);
  enable_interrupts(INT_TIMER1);
  enable_interrupts(INT_TIMER2);
  enable_interrupts(INT_TBE);
  enable_interrupts(INT_AD);
  enable_interrupts(GLOBAL);

  while(1);
}
Ttelmah
Guest







PostPosted: Wed Mar 22, 2006 8:29 am     Reply with quote

Don't set timer2 fast.
What is happening, is an implication of the chip. If _any_ other interrupt is set as a high priority, then timer0, is automatically set to high priority _as well_. Hence if you select 'timer1', or 'timer2' as 'fast', while timer0, is also enabled, the compiler 'sees' this, and will automatically add the vectoring code, and saving code for multiple interrupts.
Do exactly what Ckielstra shows. Have two 'dummy' handlers for timer1, and timer2 (these are never used), and the real handler for timer0. Set this one as 'fast'. Then in this handler, check at the start which interrupt bit is set, jump to the required handler code, clear the corresponding interrupt flag, and return.
In the main code, simply set the 'high priority' bits for timer1, and timer2, which will then vector to the timer0 handler (which effectively becomes an 'int_global' for the high priority interrupt).
As a comment though, you make no mention of actually using other interrupts?. If not, then forget using high priority!... The 'point' about high priority, is that it allows one interrupt to interrupt another. If you only want 'quick' handling of the interrupts, then simply write an 'int_global' handler, and use the retfie 1 ability from this. This will be as 'fast', as the high priority interrupts, and avoids you having to fiddle.

Best Wishes
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Wed Mar 22, 2006 8:29 am     Reply with quote

Here is an example that uses multiple high and low interrupts:
http://www.ccsinfo.com/forum/viewtopic.php?t=21092&start=15
Ttelmah
Guest







PostPosted: Wed Mar 22, 2006 8:41 am     Reply with quote

As an example of the 'int_global' approach, the following is some code I used for this. This was for a different 'set' of interrupts to you, but shows how to handle it.
Code:

#int_global
void myint(void) {
#ASM
   //Here for maximum speed, I test the RB interrupt - since it is allways
   //enabled, I don't have to test the enable bit
   BTFSS   INTCON,RBIF
   GOTO    NXT
#endasm
   quad();              //Quadrature handler.
#asm
   BCF     INTCON,RBIF
   GOTO    FEXIT        //Use the fast stack for exit
NXT:
   //Test for the external interrupt (power fail).
   BTFSS    INTCON,INT0E
   GOTO     NEXTA
   BTFSC    INTCON,INT0
#endasm
   pfail();
#asm
NEXTA:
   //Now for Timer 2
   BTFSS     PIE1,TMR2
   GOTO      NEXT1
   BTFSC     PIR1,TMR2
#endasm
   TIMER2_isr();
#asm
NEXT1:
   //Receive data available
   BTFSS     PIE1,RCIE
   GOTO      NEXT2
   BTFSC     PIR1,RCIE
#endasm
   RDA_isr();
#asm
NEXT2:
   //Transmit buffer empty
   BTFSS     PIE1,TXIE
   GOTO      FEXIT
   BTFSC     PIR1,TXIE
#endasm
   TBE_isr();
#asm
   //Here the 'fast' exit.
FEXIT:
   RETFIE  1
#ENDASM
}

The interrupt handlers each clear their own interrupt flag (except in my code the 'quad' handler), and the compiler will insert them normally as 'inline'. I my code, I save/restore any extra registers needed in these routines.

Best Wishes
thushi



Joined: 22 Feb 2006
Posts: 14

View user's profile Send private message

PostPosted: Thu Mar 23, 2006 4:08 am     Reply with quote

Ttelmah wrote:
As an example of the 'int_global' approach, the following is some code I used for this. This was for a different 'set' of interrupts to you, but shows how to handle it.
Code:

#int_global
void myint(void) {
#ASM
   //Here for maximum speed, I test the RB interrupt - since it is allways
   //enabled, I don't have to test the enable bit
   BTFSS   INTCON,RBIF
   GOTO    NXT
#endasm
   quad();              //Quadrature handler.
#asm
   BCF     INTCON,RBIF
   GOTO    FEXIT        //Use the fast stack for exit
NXT:
   //Test for the external interrupt (power fail).
   BTFSS    INTCON,INT0E
   GOTO     NEXTA
   BTFSC    INTCON,INT0
#endasm
   pfail();
#asm
NEXTA:
   //Now for Timer 2
   BTFSS     PIE1,TMR2
   GOTO      NEXT1
   BTFSC     PIR1,TMR2
#endasm
   TIMER2_isr();
#asm
NEXT1:
   //Receive data available
   BTFSS     PIE1,RCIE
   GOTO      NEXT2
   BTFSC     PIR1,RCIE
#endasm
   RDA_isr();
#asm
NEXT2:
   //Transmit buffer empty
   BTFSS     PIE1,TXIE
   GOTO      FEXIT
   BTFSC     PIR1,TXIE
#endasm
   TBE_isr();
#asm
   //Here the 'fast' exit.
FEXIT:
   RETFIE  1
#ENDASM
}

The interrupt handlers each clear their own interrupt flag (except in my code the 'quad' handler), and the compiler will insert them normally as 'inline'. I my code, I save/restore any extra registers needed in these routines.

Best Wishes



Hi,

Thanks lot for the code you provided.

I coudnt see "#int_global" command in CCS C compiler help. Why they avoid it? I have PCW 3.242 Editor and compiler.

Should I store W, STATUS and BSR registers when I use #int_global ?


Thank you,

Best Regards,

Thushi..
Very Happy
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group