Continue to Site

# question on "ON INTERRUPT" in PicBasic Pro

Status
Not open for further replies.

#### sp

##### Full Member level 6
picbasic pro interrupt

main page: https://www.microengineeringlabs.com/resources/pbpmanual/
the below: https://www.microengineeringlabs.com/resources/pbpmanual/5_42-5_47.htm#544

Code:
ON INTERRUPT

ON INTERRUPT GOTO Label

ON INTERRUPT allows the handling of microcontroller interrupts by a PICBASIC PRO™ subroutine.

There are 2 ways to handle interrupts using the PICBASIC PRO™ Compiler. The first is to write an assembly language interrupt routine. This is the way to handle interrupts with the shortest latency and lowest overhead. This method is discussed under advanced topics in a later section.

The second method is to write a PICBASIC PRO™ interrupt handler. This looks just like a PICBASIC PRO™ subroutine but ends with a RESUME.

When an interrupt occurs, it is flagged. As soon as the current PICBASIC PRO™ statement=s execution is complete, the program jumps to the BASIC interrupt handler at Label. Once the interrupt handler is complete, a RESUME statement sends the program back to where it was when the interrupt occurred, picking up where it left off.

DISABLE and ENABLE allow different sections of a PICBASIC PRO™ program to execute without the possibility of being interrupted. The most notable place to use DISABLE is right before the actual interrupt handler. Or the interrupt handler may be placed before the ON INTERRUPT statement as the interrupt flag is not checked before the first ON INTERRUPT in a program.

Latency is the time it takes from the time of the actual interrupt to the time the interrupt handler is entered. Since PICBASIC PRO™ statements are not re-entrant (i.e. you cannot execute another PICBASIC PRO™ statement while one is being executed), there can be considerable latency before the interrupt routine is entered.

PBP will not enter the BASIC interrupt handler until it has finished executing the current statement. If the statement is a PAUSE or SERIN, it could be quite a while before the interrupt is acknowledged. The program must be designed with this latency in mind. If it is unacceptable and the interrupts must be handled more quickly, an assembly language interrupt routine must be used.

Overhead is another issue. ON INTERRUPT will add an instruction after every statement to check whether or not an interrupt has occurred. DISABLE turns off the addition of this instruction. ENABLE turns it back on again. Usually the additional instruction will not be much of a problem, but long programs in small microcontrollers could suffer.

More than one ON INTERRUPT may be used in a program.

ON INTERRUPT GOTO myint	' Interrupt handler is myint
INTCON = %10010000	' Enable RB0 interrupt

. . .

DISABLE		' Disable interrupts in handler
myint: 	led = 1		' Turn on LED when interrupted
ENABLE		' Enable interrupts after handler

To turn off interrupts permanently (or until needed again) once ON INTERRUPT has been used, set INTCON to $80: INTCON =$80

wad i wanna ask is tht after the RESUME... it will return to the main prog... so the ENABLE wont b able to run... y it is there anyway??... or it should b put b4 the RESUME so it can turn on the interrupt again...

thank you

my regards,
sp

#### metal

##### Full Member level 6
picbasic interrupt

Hello

Enable and Disable are used for PICBasic Pro interupt enabling and disabling respectively...

Just to let you know that PICBasic Pro supports interrupts in a different way from assembler, it adds a special code for interrupt, so when you use disable, it simply tells the compiler not to put a code that is dedicated for interrupts handling, so that when you are in the interrupt service routine, serving an interrupt, you have to use disable to disallow any incomming interrupts from disturbing the routine, you have to use enable directly after the resume command to avoid disturbing the resume command too, as this command not only returns from interrupt routine, but also turns the GIE bit high, so that is a problem if you think you can put enable before resume command, also you have to use enable after resume command to allow the compiler to put that interrupt handling code after the resume directly, not before it...

Good luck

### sp

Points: 2

#### sp

##### Full Member level 6
on interrupt picbasic

thank you...

but this type of programming language is sequential running,,, tht's mean running line by line...

after the "resume" command, the program return to the main program,,,, is the enable get executed?...

tht is my question.... actually i just learn this pisbasic pro language.... i am NOOB...

thank you

my regards,
sp

#### metal

##### Full Member level 6
picbasic pro interrupts

Hello

Enable and Disable are not executed, as I told you before --> They are commands that tell the compiler where to put and where not to put the iterrupt handling code, if you read the last pages in the manual regarding the interrupt issues I think all problems will be healed...

These are commands for the compiler in deed, not for the PIC to sequentioally execute them as you think, in the background, they simply prevent the compiler form putting that code that enables the PIC to detect whether there is an interrupt or not, so when the compiler finds Disable, it simply disables the PIC's ability to serve interrupts.

Look what will happen if you put Enable before Resume:

Suppose that the execution arrived to a point that it will execute Resume command, at this moment an interrupt ocuured, what will really occur is that it will return back to the start of the interrupt Service Routine "ISR", because you used the Enable command before Resume command, you have enbled the interrupts in deed before the Resume command, leading to a possible infinite loop of ISR execution without knowing why...As you know, its a matter of 3 µSeconds to execute a resume command, I think this is absoltely enough to make the PIC run in an infinite loop if the interrupt it self, or another interrupt is pending for service.

Good luck

### sp

Points: 2

#### Buzzz

##### Newbie level 5
on interrupt goto

Enable will be "executed" be it that "executed" is not the right term. As metal said, it is only for PicBasic compiler to indicate that the actual interrupt subroutine is finished. You _have_ to put it after the resume command.

### sp

Points: 2

#### sp

##### Full Member level 6
picbasic interrupts

okok,, thank you metal for the detail explanation... i understand now...

thanks to buzz as well....

as usual.... i hav many question to ask...

https://www.microengineeringlabs.com/resources/pbpmanual/5_4-5_6.htm#56
Code:
BUTTON

BUTTON Pin,Down,Delay,Rate,BVar,Action,Label

Read Pin and optionally performs debounce and auto-repeat. Pin is automatically made an input. Pin may be a constant, 0-15, or a variable that contains a number 0-15 (e.g. B0) or a pin name (e.g. PORTA.0).
------------------------------------------------------------------------------------
Down: State of pin when button is pressed (0..1).

Delay: Cycle count before auto-repeat starts (0..255). If 0, no debounce or auto-repeat is performed. If 255, debounce, but no auto-repeat, is performed.

Rate: Auto-repeat rate (0..255).

BVar: Byte-sized variable used internally for delay/repeat countdown. It must be initialized to 0 prior to use and not used elsewhere in the program.

Action: State of button to act on (0 if not pressed, 1 if pressed).

Label: Execution resumes at this label if Action is true.
------------------------------------------------------------------------------------

example:
' Goto notpressed if button not pressed on Pin2
BUTTON PORTB.2,0,100,10,B2,0,notpressed

BUTTON needs to be used within a loop for auto-repeat to work properly.

BUTTON accomplishes debounce by delaying program execution for a period of milliseconds to wait for the contacts to settle down. The default debounce delay is 10ms. To change the debounce to another value, use DEFINE:

> Set button debounce delay to 50ms

DEFINE BUTTON_PAUSE 50

Be sure that BUTTON_PAUSE is all in upper case.

In general, it is easier to simply read the state of the pin in an IF..THEN than to use the BUTTON command as follows:

If PORTB.2 = 1 Then notpressed

Example program:

INCLUDE "modedefs.bas" 	' Include serial modes
SO 	Con 	0 	' Define serial output pin
Bpin 	Con 	2 	' Define Button input pin
B0 	Var 	Byte

B0 = 0 		' Zero Button working buffer

loop: BUTTON Bpin,1,10,5,B0,0,notp 	' Check button(skip if not pressed)
Serout SO,N2400,["Press",13,10] 	' Indicate button pressed

notp: Serout SO,N2400,[#B0,13,10] 	' Show working variable

Pause 100 	' Wait a little
Goto loop 	' Do it forever

i hav question on the "BUTTON" command now...

Code:
Delay: Cycle count before auto-repeat starts (0..255). If 0, no debounce or auto-repeat is performed. If 255, debounce, but no auto-repeat, is performed.

1. how does the cycle count work?... does this (0..255) mean there is only 2 options either 0 or 255.. or we can put any value btween? if we put 10, tht's mean 10 cycle count? one cycle is how long in term of time... does this depend on external osc?

2. another question is wad is debounce mean?.... does it mean tht after everytime u press the button (indicate by "pin") it automatically bounce back n count as 1 press and depress?

Code:
Rate: Auto-repeat rate (0..255).

3. wad is tht?... how much is 255 if we use 255?... 255 cycle?? same question again... we can use any value btween 0 to 255? is 0 is the fastest or 255? confuse

Code:
Action: State of button to act on (0 if not pressed, 1 if pressed).

4. tht is the most i dont understand... is the "action" and "down" is the same?... or it has nth to do wth each other?

many many many thanks... for all the professional help...

warm regards,
sp

Status
Not open for further replies.