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

interrupt and delay problem

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



Joined: 04 Feb 2010
Posts: 37

View user's profile Send private message

interrupt and delay problem
PostPosted: Thu Aug 12, 2010 5:00 am     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Aug 12, 2010 5:07 am     Reply with quote

Code:

...


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

View user's profile Send private message

PostPosted: Thu Aug 12, 2010 7:55 am     Reply with quote

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

View user's profile Send private message Visit poster's website

PostPosted: Thu Aug 12, 2010 8:43 am     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Aug 12, 2010 9:16 am     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Aug 13, 2010 2:03 am     Reply with quote

thanks so much guys for answering. Smile i will apply your suggestions and write here the results.
RoGuE_StreaK



Joined: 02 Feb 2010
Posts: 73

View user's profile Send private message

Re: interrupt and delay problem
PostPosted: Sun Aug 15, 2010 10:51 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Aug 16, 2010 2:07 am     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Aug 16, 2010 6:25 am     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Aug 16, 2010 7:02 am     Reply with quote

I've applied my code and trying all the functions but now I have another problem. Embarassed 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 ?
Code:

...


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

View user's profile Send private message

PostPosted: Wed Aug 18, 2010 12:20 am     Reply with quote

Problem Solved. Thanks so much. Very Happy
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