View previous topic :: View next topic |
Author |
Message |
koray_duran
Joined: 04 Feb 2010 Posts: 37
|
interrupt and delay problem |
Posted: Thu Aug 12, 2010 5:00 am |
|
|
Hi,
My problem is about to delay in interrupt. I need to create sound and for that i should use delays. But program gives warnings about delays disables interrupts and my program crashs. when i deactivete " if(rcv_data[1]==0x20) { exit_time(); }" line, program starts to work again.
Pls tell me a solution for this issue.
Code: |
case ENTER:
led_control(RED);
output_high(BUZZER);
delay_ms(100);
output_low(BUZZER);
delay_ms(50);
output_high(BUZZER);
delay_ms(100);
output_low(BUZZER);
break;
void exit_time()
{
send(adress,0x20);
z=0;
while(z<=40)
{
z++;
stt_control(ENTER);
readrf();
}
if(z==40) { armmode=1; alrmmode=1; }
}
#INT_RDA
// rs485 receive data
void receiving_data() {
rcv_data[0]=getc(RS);
rcv_data[1]=getc(RS);
if(rcv_data[0]==adress)
{
if(rcv_data[1]==0x10) { send(adress,(read_eeprom(254))); comm=1; }
if(rcv_data[1]==0x20) { exit_time(); }
if(rcv_data[1]==0xA0) { send(adress,0xA0); stt_control(PASS); }
if(rcv_data[1]==0xA1) { send(adress,0xA1); stt_control(ARM); }
if(rcv_data[1]==0xA2) { send(adress,0xA2); stt_control(NIGHT); }
if(rcv_data[1]==0xA3) { send(adress,0xA3); stt_control(PRG); }
if(rcv_data[1]==0xA4) { send(adress,0xA4); alrmmode=1; }
}
}
|
|
|
|
koray_duran
Joined: 04 Feb 2010 Posts: 37
|
|
Posted: Thu Aug 12, 2010 5:07 am |
|
|
Last edited by koray_duran on Wed Aug 18, 2010 12:35 am; edited 1 time in total |
|
|
koray_duran
Joined: 04 Feb 2010 Posts: 37
|
|
Posted: Thu Aug 12, 2010 7:55 am |
|
|
and also i try kbhit instead of interrupt but it crahes after a certain time or process. i dont think that kbhit can solve this issue. |
|
|
collink
Joined: 08 Jan 2010 Posts: 137 Location: Michigan
|
|
Posted: Thu Aug 12, 2010 8:43 am |
|
|
koray_duran wrote: | and also i try kbhit instead of interrupt but it crahes after a certain time or process. i dont think that kbhit can solve this issue. |
Never, under any circumstances, should you ever call any functions that in turn call delay_* (any delay function) nor should delay_* be found in interrupt handlers. Instead, make your RDA interrupt handler get the incoming byte and save it to an array. Then have your main code be in an infinite loop that checks to see if any data has been received and stored in the buffer by the interrupt handler.
The cardinal rule is this: Do as little as possible in the interrupt handler. Get the byte, get out. |
|
|
rnielsen
Joined: 23 Sep 2003 Posts: 852 Location: Utah
|
|
Posted: Thu Aug 12, 2010 9:16 am |
|
|
In your INT_RDA you are trying to receive two characters. When this interrupt is entered it means that ONE character has been received and needs to be taken care of. If you need to receive multiple characters then you will need to take care of them one at a time, each time the interrupt is entered.
Like it was stated, never, ever place delays in an interrupt. Interrupts should be short and sweet. If you do need delays use timers to create the delays without causing your code to be 'stuck' inside an interrupt.
In this interrupt, retrieve your character, stuff it someplace and then get out. Once you have received all of the characters that you are looking for, manipulate them outside of the interrupt.
One last note, if you do post code don't post the entire thing. If most people are like me, they are not going to study the massive code to find out what you are doing. Post code that you are having problems with and trim it down to the basic problem code.
Ronald |
|
|
koray_duran
Joined: 04 Feb 2010 Posts: 37
|
|
Posted: Fri Aug 13, 2010 2:03 am |
|
|
thanks so much guys for answering. i will apply your suggestions and write here the results. |
|
|
RoGuE_StreaK
Joined: 02 Feb 2010 Posts: 73
|
Re: interrupt and delay problem |
Posted: Sun Aug 15, 2010 10:51 pm |
|
|
koray_duran wrote: | I need to create sound and for that i should use delays. | No you shouldn't. For sound you want a consistent frequency no matter what is happening, so it should be interrupt-driven, and interrupt only. One of the issues I had when trying to make sound is that virtually every "tutorial" for making sound (including the Roman Black one) uses delays to set the frequency; this is maybe OK if sound is all your project is doing, but if you are doing other processing then it's quickly going to go to hell in a handbasket. |
|
|
koray_duran
Joined: 04 Feb 2010 Posts: 37
|
|
Posted: Mon Aug 16, 2010 2:07 am |
|
|
thats very good information, thanks RoGuE_StreaK. May i ask you some small example for that issue. that can really save me.
And by the way when i get out this codes from interrupt, program started to work but now i have a problem about communication between two devices. sometimes it is crashing maybe this is related to this delaying issue. |
|
|
RoGuE_StreaK
Joined: 02 Feb 2010 Posts: 73
|
|
Posted: Mon Aug 16, 2010 6:25 am |
|
|
Not really too useful as it doesn't show the audio section, but if you check out the following two threads of issues I have had getting it operational you might should be able to figure something out.
Array for audio storage:
http://www.ccsinfo.com/forum/viewtopic.php?p=128459#128459
(I converted my 8bit wavs to hex using a text editor, will have a look tomorrow which one, I don't have it with me)
Getting the right interrupt rate:
http://www.ccsinfo.com/forum/viewtopic.php?p=135808&highlight=#135808
Basically I interrupt at the frequency of the wav, advance the pointer in the array, and set the PWM duty cycle to whatever value is at that location in the array.
For simplicity I adjusted timer2 so it natively gives a resolution of 256 (8bit), you want it running as fast as possible while keeping as much resolution as you need.
Interrupt has been changed to:
setup_timer_0(RTCC_INTERNAL|RTCC_8_bit|RTCC_DIV_2);
and on interrupt, the timer is preloaded with a value of about 102 (not in any of those threads), which brings my interrupt frequency to almost exactly 16kHz.
(Using a 20MHz OSC, RTCC_DIV_1 gives an interrupt every ~52uS, RTCC_DIV_2 gives an interrupt every ~104uS, and to get 16kHz I need ~62uS, so go for the bigger number and preload it to make it overflow earlier)
Do a search (here and elsewhere) for the likes of "state machines", gives a good method of prioritising workloads, doing the important things exactly when they need to be done, and the less important things in any free time. |
|
|
koray_duran
Joined: 04 Feb 2010 Posts: 37
|
|
Posted: Mon Aug 16, 2010 7:02 am |
|
|
I've applied my code and trying all the functions but now I have another problem. In the main loop there is a "readrf();" function that reads rfid transponders. It doesn't work while timer_2 works and there is nothing related to timer_2 which I use for creating sound. I give my codes and em4095 driver codes.
What could happen ?
Last edited by koray_duran on Wed Aug 18, 2010 12:34 am; edited 1 time in total |
|
|
koray_duran
Joined: 04 Feb 2010 Posts: 37
|
|
Posted: Wed Aug 18, 2010 12:20 am |
|
|
Problem Solved. Thanks so much. |
|
|
|