CHANGING ISR RETURN ADDRESS........

Go To Last Post
14 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

so I just want to return to a specific address when ISR ends and the only solution in my mind is software reset but it is even good to reset my controller again and again? 

I know you guys will be thinking why I need this thing.smiley so I am working on fingerprint lock system, which displays stuff on LCD. in the ISR I will be changing passwords and enrolling new finger (have to clear LCD).and in the while loop it will ask the user to write a password. so when interrupt ISR ends there is a blank screen or just the last thing I wrote in ISR. that's why I want that when ISR ends it will start from while loop(more prefered then reset) so that it displays stuff even after ISR.

is there any solution?

 

controller: atmega8      doing programming in C 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Hassan6432 wrote:
I know you guys will be thinking why I need this thing

Indeed!

 

It is a very common question and there is (almost) always a better way.

 

Hassan6432 wrote:
that's why I want that when ISR ends it will start from ...

No, that is no reason for the ISR to return to a different place!

 

You haven't said which particular Interrupt you're talking about. But, in general, any ISR should just do the minimum to handle the event and notify the "main loop" - the ISR should not be doing any advanced stuff like setting passwords.

 

The "main loop" should be handling logic like clearing screens, displaying prompts, etc. 

 

Hassan6432 wrote:
is there any solution?

Yes - It sounds like you need to re-thing the design of your system.

 

EDIT

 

Maybe a State Machine would help ... ?

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Thu. Jul 12, 2018 - 07:07 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 2

Hassan6432 wrote:

...in the ISR I will be changing passwords and enrolling new finger...

 

That is not how you normally do things. In general the advice is to keep all ISRs short. If you need to do anything more you set a flag in the ISR and use that to cause additional code in your main() to execute.

 

#1 This forum helps those that help themselves

#2 All grounds are not created equal

#3 How have you proved that your chip is running at xxMHz?

#4 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand." - Heater's ex-boss

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

so there is no way??

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Did you not read the replies?

 

It's not that there is no way - it's that it is almost certainly the wrong way!

 

Image result for "you didn't want to do that"

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Thu. Jul 12, 2018 - 08:36 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ok then is there any method i can use to do it without interrupts? (that if i press that button and menu will pop up)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hassan6432 wrote:

so there is no way??

 

There is a way, in fact there are several ways. I hesitate to tell you how to do it because I know that you will be back with a 'my code doesn't work' post within a few days but the way to do it is...

 

 

1) At the point you wish to return to a different address you will need to write your own epilogue code to clean up the stack and restore the saved registers. This will need to match the compiler generated prologue code.

2) You will push the new return address onto the stack.

3) You execute a RETI instruction.

 

HOWEVER, any changes you make to your ISR will almost certainly mean you end up rewriting step 2) as the changes to the stack and saved registers will change on each compilation.

 

There is a good reason you've been advised to do it properly.

 

#1 This forum helps those that help themselves

#2 All grounds are not created equal

#3 How have you proved that your chip is running at xxMHz?

#4 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand." - Heater's ex-boss

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 2

Hassan6432 wrote:

ok then is there any method i can use to do it without interrupts? (that if i press that button and menu will pop up)

 

 

uint8_t gFlag;

ISR() {
    
    //process interrupt here
    ;
    ;
    
    gFlag = TRUE;
}

main() {
    
    while (1){
    
        if (gFlag) {
            display_menu();
        } else {
            do_something_else();
        }
    }
    
    
}

 

#1 This forum helps those that help themselves

#2 All grounds are not created equal

#3 How have you proved that your chip is running at xxMHz?

#4 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand." - Heater's ex-boss

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Brian Fairchild wrote:
1) At the point you wish to return to a different address you will need to write your own epilogue code to clean up the stack and restore the saved registers. This will need to match the compiler generated prologue code.

2) You will push the new return address onto the stack.

3) You execute a RETI instruction.

A cleaner and easier way to do it is to use setjmp/longjmp.

 

But as already said by others: changing the design to avoid it is a MUCH better solution.

Stefan Ernst

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

What triggers the interrupt? Why is it even there?

 

Like others have said I would take a step back and design this program properly. I can't help thinking you are where you are because you have started with some small test and then it has grown organically into the solution you have now. That's not a great way to design software. Sure you can write short test s of various aspects of the system (the scanner, the LCD, maybe button inputs and so on) but once you have a clear picture of how everything works start with a clean design where you map out the main things that have to happen and consider (for anything that has to have very quick response) the use of interrupts. Then put all the building blocks together.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

sternst wrote:

A cleaner and easier way to do it is to use setjmp/longjmp.

 

Does that even work in an ISR? It makes my head hurt just thinking about it.

 

We've been here before with the whole ISR and different exits thing ...

 

https://www.avrfreaks.net/forum/...

#1 This forum helps those that help themselves

#2 All grounds are not created equal

#3 How have you proved that your chip is running at xxMHz?

#4 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand." - Heater's ex-boss

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hassan6432 wrote:
ok then is there any method i can use to do it without interrupts?

Nobody said you had to do it without interrupts!

 

It's just the "return to a different address" thing which is a Really Bad Idea.

 

I suggest that you take a step back, and spend some time to understand how people usually handle interrupts in microcontroller code - you will find that messing with the ISR return address is not the usual way to do it!

 

You still haven't said which particular interrupt(s) it is that you are using.

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Brian Fairchild wrote:
 Does that even work in an ISR? It makes my head hurt just thinking about it. 

Yes, it does. I am not completely sure about the I-flag, so perhaps you need a sei() at the jump target.

 

Your solution makes more headaches. wink

Think about function calls in the while loop (explicit, or implicit like a division), and what happens when the interrupt occurred within such subroutine.

Stefan Ernst

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I wrote:
It is a very common question and there is (almost) always a better way.
 

Brian Fairchild wrote:
We've been here before with the whole ISR and different exits thing ...

Many, many times; and not just here - the same thing keeps cropping up on other forums for other microcontrollers.

 

The whole thing is down to a basic lack of understanding of the normal way to handle interrupts.

 

Only in very special cases - where the normal way is demonstrably inapplicable - should such things be attempted.

 

Almost certainly, this is not such a case!

 

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...