Jump to content
Sign in to follow this  
DavidT

Interrupts - Possible Constraints

Recommended Posts

Hi. I have some questions about using interrupts please:

1. Is there any limit to how much code you can put in the interrupt function?

2. Interrupts have to be at address location 4. Does this limit how many functions/code size that can precede the interrupt function so I can call them from the interrupt?

3. Related to 2, how do I do function prototyping so I can call functions that are written 'after' the interrupt function?

I'm a novice at programming but do have have many simple programs working successfully and can make basic interrupts work. More complex ones are causing me some grief hence my questions.

Thanks, David.

Share this post


Link to post
Share on other sites

Hi David

 

Hi. I have some questions about using interrupts please:

1. Is there any limit to how much code you can put in the interrupt function?

 

Yes. The actual limit is a function of interrupt frequency and clock speed - the processor must be able to excecute all the interrupt code (and exit the interrupt) before the next one occurs. In real life you never want to get anywhere near this limit and must strive to keep your interrupt code 'short and sweet'. How you achieve this is dependent on the nature of the project but have a look at NOVO - Sourceboost's real-time OS for an elegant solution.

 

2. Interrupts have to be at address location 4. Does this limit how many functions/code size that can precede the interrupt function so I can call them from the interrupt?

 

This is hardware-related. The idea of programming in C is to distance you from these hardware issues, though in the world of microcontrollers that distance is necessarily quite short! The Linker will sort it all out for you so the sequence of functions in the source code is immaterial and should be designed for ease of human consumption rather than for the hardware.

 

3. Related to 2, how do I do function prototyping so I can call functions that are written 'after' the interrupt function?

[\quote]

 

Something along the lines of:

 

int bert( char person );	/* the prototype */

void interrupt(void)
{
int tom = bert( '$' );	/* call a function yet to be defined */
...
}

int bert( char person )	 /* define the function */
{
...
return people[person];
}

 

Take a look at ra68qi's guide for beginners; it will set you off on the right path.

 

Regards

 

richard.

Share this post


Link to post
Share on other sites

Thanks, Richard. This interrupt will be an input state change triggered by a push-button so I don't have another on it's way if I debounce it adequately and disable interrupts while in the interrupt function itself. I would like to measure the button press duration to select modes, write to EEPROM, make 12 LED's flash in special patterns, etc. This quickly builds up into code of some substance.

 

I will take another look at ra68qi's post; its been helpful before. Thanks for the prototyping example.

David.

Share this post


Link to post
Share on other sites

Then your reading the PWM capture application notes from Microchip i take it?

Share this post


Link to post
Share on other sites
Thanks, Richard. This interrupt will be an input state change triggered by a push-button so I don't have another on it's way if I debounce it adequately and disable interrupts while in the interrupt function itself. I would like to measure the button press duration to select modes, write to EEPROM, make 12 LED's flash in special patterns, etc. This quickly builds up into code of some substance.

 

I will take another look at ra68qi's post; its been helpful before. Thanks for the prototyping example.

David.

 

Hi DavidT,

 

It may be difficult to work with interrupts in your case ( due to key bouncing). Why don't you try some thing like this..

Lets say you have connected the switch to portb.0 & based on the duration you have pressed you decide what activity to do.

if(portb.0==0)
{
 delay_ms(100);  // wait for the key to settle down.
if(portb.0==0)
{
  for(x=1;x<1000;x++)
{
	y=x;
	delay_ms(1);
	if(portb.0==1)
	   {
		   break;
		}
 }

  if(y<=500)
  {
  write to eeprom;
}
  if((y>500)&&(y<750))
  {
 light 12 leds;
  }
  if((y>750)&&(y<999))
  {
 do some thing else;
}
  y=0;
  x=0;
}
}

 

hope this would be useful.

 

Regards

Raghunathan.

Share this post


Link to post
Share on other sites

If he uses RB3 he can use CCP2 and do edge or PWM captures to decide what to do, less code and easier to implement.

Since all he would have to do is measure the first two pulses and subrtact to get the width.

Share this post


Link to post
Share on other sites

I have been making a temperature monitoring device to help set up, tune and cool model aeroplane motors, 2 and 4 cylinder petrol engines 150/300cc. Temperatures are displayed via 12 LEDs, 3 per cylinder. 'Short' interrupts toggle between 'hold' and 'real-time' displays, and the 'long' interrupt clears EEPROM just before flight. These features are working well now, thank you. MMC cards next!

 

I'm using the MCLR pin to detect a change in state from the push-button. And yes, I have put the simple 100ms delay at the start of my interrupt to debounce the switch. If the switch is still being pressed at the end of that, and while it continues to be pressed, I increment a counter every 100ms within a 'while' statement.

 

Having this counter as a global variable I can make decisions both within the interrupt and elsewhere. So at the moment I make the hold/real-time mode decision in the interrupt and flash the LEDs in a pattern to reveal that as you suggest. I've moved the initialisation of memory and various buffers to main(). I believe my main problem was I was trying to clear some variables that had probably been saved by the interrupt. When I stopped doing this my lockups went away. No surprise I guess!

Regards, David.

Share this post


Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...