Frequently Asked Questions

How can I use a delay_XX() routine in an interrupt without disabling interrupts?

First, it is not good design practice to call delay_XX() routines inside your interrupt. Try to design your programs so interrupts take as little time as possible.

Second, read the FAQ about how the compiler prevents accidental reentrancy.

The internal CCS functions, such as the RS232 and delay_XX() functions, cannot be defined twice like normal C functions that are defined by the users. However, here is a work around to define two #use delay(clock=XXXXX) which in turn will create two delay_XX() functions in code space so there would be no reentrancy problems with interrupts:

(This is very similar to creating two serial ports).

#include <18F452.h>
#fuses HS,NOWDT,NOLVP,NODEBUG,NOPUT,NOBROWNOUT
#use rs232(baud=9600, xmit=PIN_C6,rcv=PIN_C7)

#use delay(clock=20000000)

#int_YOURINTERRUPT
void isr(void) {
   /*
     YOUR CODE HERE
   */

   delay_ms(10);
}

#use delay(clock=20000000)

void main(void) {
   enable_interrupts(int_YOURINTERRUPT);
   enable_interrupts(GLOBAL);
   
   while(TRUE) {
      delay_ms(100);
      
      /*
         YOUR CODE HERE
      */
      
   }
}

C-Aware IDE Demo
Embedded C Learners Kit
C Workshop Compiler