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 CCS Technical Support

Dumb question - delay_ms disables interrupts?

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



Joined: 15 Jun 2006
Posts: 8

View user's profile Send private message

Dumb question - delay_ms disables interrupts?
PostPosted: Fri Jul 05, 2013 12:56 pm     Reply with quote

This is an odd one.

I have a 60Hz signal going to the RB0/INT0 pin. The following code works; it gives me a pulse on LED1 60 times a second.

Code:

#include <18F67K22.h>
#device ADC=12
#include <regdefs_18F67K22.inc>


#use delay (clock=40000000)
#use RS232 (stream=term, baud=115200, UART1)
#use RS232 (stream=GPS, baud=9600, UART2)

. . . .

#int_ext
void ext_isr() {
   bit_clear(INTCON,1);
   output_high(LED1);
   delay_ms(1);
   output_low(LED1);
   
}

. . .

main() {
   
   OSCCON=0x00;         //default osc
   bit_set(OSCTUNE,6);      //PLL on
   hw_init();

   enable_interrupts(int_ext);
   enable_interrupts(global);


   while(1) {
   }
}



However if I change the last part of the code to this:

Code:

        while(1) {
        delay_ms(100);
        }


then I get ten pulses a second. Is delay_ms disabling interrupts? It seems like that would be a very bizarre thing for it to do.
_________________
--bill von
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Jul 05, 2013 1:08 pm     Reply with quote

Quote:
#int_ext
void ext_isr() {
bit_clear(INTCON,1);
output_high(LED1);
delay_ms(1);
output_low(LED1);

}


main() {

while(1) {
delay_ms(100);
}

Is delay_ms disabling interrupts? It seems like that would be a very
bizarre thing for it to do.



Turn on compiler warnings. You will see a warning message that tells
you why it's done.
temtronic



Joined: 01 Jul 2010
Posts: 9226
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Fri Jul 05, 2013 1:08 pm     Reply with quote

Yes, it has to in order for the PIC to 'twiddle it's thumbs' while it's delaying.....


If you dump out both of the listings you'll see how and why this happens.

For anything other than very simple programs, it's best to use some kind of timer/interrupt scheme for delays that won't prevent the PIC from handling interrupts.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Jul 05, 2013 1:11 pm     Reply with quote

This FAQ article shows one way to do a work-around:
http://www.ccsinfo.com/faq.php?page=delay_in_interrupt
billvon



Joined: 15 Jun 2006
Posts: 8

View user's profile Send private message

PostPosted: Fri Jul 05, 2013 1:35 pm     Reply with quote

PCM programmer wrote:
Turn on compiler warnings. You will see a warning message that tells you why it's done.


There it is: "Interrupts disabled during call to prevent re-entrancy: (@delay_ms1)" I missed it in all the warnings for "variable never used."

Thanks.
_________________
--bill von
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Fri Jul 05, 2013 1:47 pm     Reply with quote

clever programmers set a flag variable - and do delays outside the ISR

i would switch the led -set a flag -
then do the delay in MAIN and release the LED



Very Happy Very Happy Very Happy
billvon



Joined: 15 Jun 2006
Posts: 8

View user's profile Send private message

PostPosted: Fri Jul 05, 2013 2:43 pm     Reply with quote

asmboy wrote:
clever programmers set a flag variable - and do delays outside the ISR

i would switch the led -set a flag -
then do the delay in MAIN and release the LED


Thanks for the suggestion. The delay within the ISR is for testing only and will be removed once I ensure reliable interrupts (I have three time-critical interrupts to deal with.) The final code in the ISR's will just increment a set of time registers then exit; all displaying/LED blinking etc will be in mainline code.
_________________
--bill von
Gabriel



Joined: 03 Aug 2009
Posts: 1067
Location: Panama

View user's profile Send private message

PostPosted: Fri Jul 05, 2013 2:53 pm     Reply with quote

interrupts do just that... they ... interrupt.

if you hang inside the ISR with a delay, you are not interrupting... you are creating a mess... and testing you your interrupts will be dificult probably.

Quote:
the delay within the ISR is for testing only and will be removed once I ensure reliable interrupts


i think you will only ensure you are interrupting wrong.

Quote:
I have three time-critical interrupts to deal with


.... errrr... thus you should take the delays out.


G.
_________________
CCS PCM 5.078 & CCS PCH 5.093
billvon



Joined: 15 Jun 2006
Posts: 8

View user's profile Send private message

PostPosted: Fri Jul 05, 2013 4:00 pm     Reply with quote

Works fine now with the delays in place. Thanks for the help everyone.
_________________
--bill von
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