View previous topic :: View next topic |
Author |
Message |
Dean
Joined: 08 Jun 2017 Posts: 15
|
Interrupt PIC18F4520 |
Posted: Thu Jun 15, 2017 5:28 am |
|
|
Hi everyone
I have a question regarding interrupts in PIC18F4520.
1. Using Timer0 overflow interrupt what happens if I am calling an endless loop from that interrupt handling routine ?
2. Can anyone provide a full example for external interrupt. I have an application with 3 push buttons connected as inputs with pull up resistors. I want an interrupt to handle pressing any of these three buttons regardless what the processor is doing.
Dean _________________ Find a way Or Make one . |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19553
|
|
Posted: Thu Jun 15, 2017 8:14 am |
|
|
If the interrupt triggers, and contains an infinite loop, the only thing that can save you is the watchdog. Otherwise the processor is hung.
General rule is interrupt handlers should only ever do the minimum necessary to handle the interrupt. Nothing else.
If you are triggering an interrupt on buttons, then you need to have hardware debounce for the buttons. Otherwise you can get multiple triggers. Alternative, just have a 'tick' interrupt (useful for anything else that wants time), and just poll the keys in this, with it needing the keys to be steady for a couple of ticks to give debounce.
A search here will find many examples, or look in the example folder with the compiler. ex_sleep.c shows a button being used to wake a processor. |
|
|
Dean
Joined: 08 Jun 2017 Posts: 15
|
|
Posted: Thu Jun 15, 2017 8:42 am |
|
|
The infinite loop here is not a hang up, its a part of the code where lcd is flashing continuously till a button is pressed. I am using external interrupt to detect the push button whenever pressed. It is supposed to exit the flashing text loop by the code. The problem is that once the loop is started the external interrupt does not see any signal from external side, it is supposed to interrupt the loop right ? _________________ Find a way Or Make one . |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19553
|
|
Posted: Thu Jun 15, 2017 8:56 am |
|
|
No.
If the code is inside or called by the interrupt, then it can't be interrupted by the interrupt.
Basically it is an error to have any such code inside an interrupt. Your keyboard interrupt routine should just set flags to say what you want done, and the LCD code etc., should all be outside the interrupt.
The interrupt should _just_ handle the event it is meat to record. Nothing else. |
|
|
Dean
Joined: 08 Jun 2017 Posts: 15
|
|
Posted: Thu Jun 15, 2017 9:33 am |
|
|
Thank you Ttelmah
I understand the limitation of the interrupt, it looks even timer and timer overflow interrupt behaves the same way. Even if the loop is triggered by a flag outside the interrupt routine the same result is achieved. I wonder if the watch dog timer can be used here, I never used it before, or any other command which can be placed inside the loop and force the system to listen to interrupts . In C# there is a function doevent() which forces the program to do so, I am looking then for something similar. _________________ Find a way Or Make one . |
|
|
Dean
Joined: 08 Jun 2017 Posts: 15
|
|
Posted: Thu Jun 15, 2017 10:01 am |
|
|
By the way, is there a method built in LCD 16X2 to flash text without having to go to a loop ? _________________ Find a way Or Make one . |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19553
|
|
Posted: Thu Jun 15, 2017 11:45 am |
|
|
I think you need to learn how to program in the PIC....
All you need is your LCD loop to be a subroutine, and have it at some point return if a flag is set. Then in the interrupt set the flag.
_Provided_ your interrupts (all of them) only handle the hardware events, have the required interrupt set the flag, and the loop will then exit. The problem will arrive if any of your interrupts attempts to do more, and use any routines that are also used in the external code. You will then get a warning (asusming this is not turned off) 'interrupts disabled to prevent re-entrancy', and your problems will start.
This is at heart a hardware limitation of the PIC(12/16/18). It does not have a variable stack, so routines cannot be called from inside themselves. Hence put any routine in the interrupt that is also used outside, and this will start to happen.... |
|
|
newguy
Joined: 24 Jun 2004 Posts: 1911
|
|
Posted: Thu Jun 15, 2017 12:24 pm |
|
|
Dean wrote: | By the way, is there a method built in LCD 16X2 to flash text without having to go to a loop ? |
Yes, consult the data sheet for the LCD's controller. The standard LCD driver that comes with the compiler sets up the LCD with flash off, cursor off. From memory, you just change the set up (initialization) code to turn the flash or cursor on/off.
PCM's flexible LCD driver (check the code library) might support the flash. You'll have to check. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19553
|
|
Posted: Thu Jun 15, 2017 2:43 pm |
|
|
No.
On the standard LCD's the flash is for the cursor only, not text..... |
|
|
|