In my program, I have a periodic timer interrupt written in C. What I need to achieve is to have the interrupt service routine jump to another function after the interrupt service routine is executed. However, the catch is that the address of the function (which the program is suppose to jump) is stored as a variable (which is not fixed each time the interrupt occurs). Im looking for advice on how can this possibly be done. Thank you.
use a function pointer? but beware of the limitations of function pointers in C51: http://www.8052.com/forum/read.phtml?id=47543
More specifically, what I need to do is for this particular function is be called once the interrupt routine has been executed. It would have to be different from the usual behaviour of an interrupt where the interrupted program will resume. I understand that I can use a function pointer to call the function from the interrupt routine. But unfortuntately, this function concerned has a infinite loop which loops forever. For example, void loop_function (void){ for (;;) {} } As such, I cannot simply call the function from the interrupt routine. This is because the program will be stuck in the infinite loop. Hence, the next periodic timer interrupt would not set in as the previous interrupt is considered to be still running. Could what I want possibly be achieved in C? Or do I have to go into assembly?
If you were to explain why you are trying to do this it might be easier to help. So far it looks as though you don't ever want execution to return to the 'main' program after this interrupt handler has been executed, you want your endless loop function to run, however you want all ISRs to continue executing in response to interrupts. This sounds quite strange - perhaps you could design your program differently? Stefan
"But unfortuntately, this function concerned has a infinite loop" Why?! "I cannot simply call the function from the interrupt routine. This is because the program will be stuck in the infinite loop." Of course it will - that's the nature of infinite loops! That's why they're called "infinite!" It sounds like your design is fundamentally flawed: if you have these infinite loops, then you will get stuck in them - no matter how you get into them! I think you probably don't want infinite loops at all; I suspect what you really want is loops that run until the next timer event, or something like that?
Yes you are right. The program would not return to 'main'. Instead it will jump from function to function. The purpose of doing this is to create an application which can run different tasks (functions) in a round robin fashion. These tasks can execute forever until they are pre-empted by another task when the timer interrupt sets in. What I describe may sound a bit pointless but it is actually part of an overall effort to build a context switching routine for a very basic real-time OS in 8051.
Hi Try this code
#include <REG51.h> char idata *STACKPointer; char data Low_adr; char data High_adr; //Replace this function to our Interrupt function void Init8051(void) { STACKPointer = (char *)SP; //Get Stack STACKPointer--; //Remove calling function STACKPointer--; //Return to new function STACKPointer[0]=High_adr; STACKPointer[1]=Low_adr; }
The purpose of doing this is to create an application which can run different tasks (functions) in a round robin fashion. These tasks can execute forever until they are pre-empted by another task when the timer interrupt sets in. To make a preemptive multi-tasker on an 8051, I don't think you'll get by with writing everything in C. You'll have to code at least some of the core pieces in assembly. You'll have to manipulate the stack on each context switch triggered by the timer interrupt. I would strongly suggest to re-evaluate that plan. Preemptive multitasking is hard to do on an 8051, and harder still if you want to stick with C as the implementation language. Cooperative multitasking may very well be a better choice.
View all questions in Keil forum