Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Calling Subroutines inside the ISR

Status
Not open for further replies.

Suraj143

Active Member
Hi this is the update routine of a clock.To adjust the time also I'm taking this code.I'm placing this code in the main routine.

Can I call this update routine inside ISR? Because for every second it must update inside the ISR.


Code:
Update	incf	M1,F
	movf	M1,W
	xorlw	.10
	btfss	STATUS,Z
	return
	clrf	M1
	incf	M10,F
	movf	M10,W
	xorlw	.6
	btfss	STATUS,Z
	return
	clrf	M10
	incf	H1,F
	----
	----
	----
	return
 
That would be a colossal mistake. Imagine the chaos if the interrupt occurred while you were in the routine.

There are some solutions. The best one is to duplicate the code using different file registers if you need temporary storage.

The general rule is:
"You cannot modify any variable in an interrupt routine and the main program unless you disable interrupts in the main program while you modify the variable"
 
Last edited:
Papabravo said:
The general rule is:
"You cannot modify any variable in an interrupt routine and the main program unless you disable interrupts in the main program while you modify the variable"

Hi Papabravo when an interrupt occurs it goes to the ISR & it will disable upcoming interrupts automatically.

And when I'm going to the edit mode I'll disable interrupts as well by turning off timers.So inside the edit mode no more going to the ISR.:)

I'm wondering why cant I use the same variables & same routine in the ISR.
 
You need to prevent interrupts while your main routine is processing the variable.

Search on 'reentrant'.
 
A simple way to make this so you can use the routine in your main code and the ISR would be to zero the seconds variable before you call update from the main code. This will ensure that the ISR will never call the code at the same time as the background code.

Mike.
 
An extra concern as well - it's a BAD idea to call subroutines in an ISR at all, because you've only got a VERY limited stack size - so calling subroutines in an ISR means you must reduce stack useage even further in your main program.

So if you do it, watch the depth of your subroutine calls in the main program!.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top