View previous topic :: View next topic |
Author |
Message |
billvon
Joined: 15 Jun 2006 Posts: 8
|
Dumb question - delay_ms disables interrupts? |
Posted: Fri Jul 05, 2013 12:56 pm |
|
|
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
|
|
Posted: Fri Jul 05, 2013 1:08 pm |
|
|
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: 9221 Location: Greensville,Ontario
|
|
Posted: Fri Jul 05, 2013 1:08 pm |
|
|
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
|
|
|
billvon
Joined: 15 Jun 2006 Posts: 8
|
|
Posted: Fri Jul 05, 2013 1:35 pm |
|
|
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
|
|
Posted: Fri Jul 05, 2013 1:47 pm |
|
|
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
|
|
|
billvon
Joined: 15 Jun 2006 Posts: 8
|
|
Posted: Fri Jul 05, 2013 2:43 pm |
|
|
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
|
|
Posted: Fri Jul 05, 2013 2:53 pm |
|
|
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
|
|
Posted: Fri Jul 05, 2013 4:00 pm |
|
|
Works fine now with the delays in place. Thanks for the help everyone. _________________ --bill von |
|
|
|