Advantages of ISR in assembly

Discussion in 'Embedded Systems and Microcontrollers' started by Tera-Scale, Mar 3, 2012.

  1. Tera-Scale

    Thread Starter Active Member

    Jan 1, 2011
    I will use an ISR that is restricted to 2μs. I will use PIC18F/PIC24F.
    In this ISR the uC will be:

    -Firing timer 1 (system clock)
    -Stop CTMU current source (1 clock cycle)
    -Perford ADC connversion (will use system clock)
    -Save ADC to somewhere else (not in the original buffer)
    -Drain ADC sampling cap (will only take 2 clock cyles)
    -Switch on CTMU (will only take 1 clock cycle)

    -I will then have to wait for the other rising edge(new input cycle) to stop the timer. How much latency will there be?

    This whole cycle will happen only once - it is not repetitive.

    If I will use a 20Mhz crystal, I will have 20clock cycles in 2μs. Am i right?

    What advantages would I have if I where to code the ISR in assembly language.

    In C, MPLAB SIM I usually use stop watch to calculate exactly the clock cycles.

    Any hints would be appreciated a lot.
  2. atferrari

    AAC Fanatic!

    Jan 6, 2004
    I am not conversant in C.

    In assembler you can be reasonably sure of latency and duration of your ISR.
    Tera-Scale likes this.
  3. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    The one advantage as atferrari pointed out is you will know exactly what you are getting. But if you are not well educated in assembler then the frustration factor is worth considering. Also a decent C optimizer will beat a newbie assembler programmer any day. Not that someone can't manage to turn out some pretty poor C code too. :)
    Tera-Scale likes this.
  4. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    AFAIK if parts of your program are in C then the latency while still being determinate, can vary between several extremes. C employs a software stack that pulls information on and off the hardware stack: while doing such interrupts are disabled.

    Once inside the ISR then a C implimentation will have extra duties to perform as the full C context must be stored. This can entail several different items, and is compiler dependent.
  5. Tera-Scale

    Thread Starter Active Member

    Jan 1, 2011
    I tried a simple interrupt in C. This just goes to the ISR and do just one asm("nop"); apart from clearing the flag bit. The process from completing the last instruction till returning to the next (going through the ISR) took 16 instruction cycles. From what I learned these are too much.

    From completing the main instruction to going to the 'bracket' of the interrupt function, it takes 5 instruction cycles in MPLAB SIM which is accaptable. But from the bracket of the interrupt function to the first instruction it takes another 5 instruction cycles (from disassembly : 0D98 F80034 push.w 0x0034). Then at the end after the "nop" (1 cycle) and Clear Flag (1 cycle), it takes another 4 cycles to return ( 0DA6 F90034 pop.w 0x0034 0DA8 064000 retfie).

    Would I see some improvement with using assembly? Where can I find some examples?

    Code ( (Unknown Language)):
    2. int main()
    4. {
    6. TRISB=0x4000;
    7. AD1PCFG=0xFFFF;
    9. IntInit();    //init interrupts
    11. while(1)
    12. {
    14. asm("nop");
    15. asm("nop");
    16. asm("nop");
    17. asm("nop");
    18. }
    20. return(0);
    21. }
    23. void IntInit(void)
    24. {
    25.    INTCON2 = 0x0000;   /*Setup INT0, INT1, INT2, interupt on falling edge*/
    26.    IFS1bits.INT1IF = 0;    /*Reset INT1 interrupt flag */
    27.    IEC1bits.INT1IE = 1;    /*Enable INT1 Interrupt Service Routine */
    28.    IPC5bits.INT1IP = 1;     /*set low priority*/
    29. }
    31. //_INT1Interrupt() is the INT1 interrupt service routine (ISR).
    32. void __attribute__((__interrupt__)) _INT1Interrupt(void);
    33. void __attribute__((__interrupt__, auto_psv)) _INT1Interrupt(void)
    34. {
    35.         asm("nop");
    36.     IFS1bits.INT1IF = 0;    //Clear the INT1 interrupt flag or else
    37. }
  6. stahta01


    Jun 9, 2011
    FYI: On the 16F/18F series an 20MHz osc/clock gives a 5MHz machine cycle time.

    So I get 10 machine cycles in 2uS.

    Tim S.
    Tera-Scale likes this.