ON INTERRUPT
Contents |
Action
Execute subroutine when the specified interrupt occurs.
Syntax
ON interrupt label [NOSAVE|SAVE|SAVEALL]
Remarks
When using a label you must return from the interrupt routine with the RETURN statement.
The first RETURN statement that is encountered that is outside a condition will generate a RETI instruction. You may have only one such RETURN statement in your interrupt routine because the compiler restores the registers and generates a RETI instruction when it encounters a RETURN statement in the ISR. All other RETURN statements are converted to a RET instruction.
While the label is supported because the old GW-BASIC supported it, it is best to use a Sub routine which you can end with End Sub.
The possible interrupt names can be looked up in the selected microprocessor register file. 2313def.dat for example shows that for the compare interrupt the name is COMPARE1. (look at the bottom of the file)
What are interrupts good for?
An interrupt will halt your program and will jump to a specific part of your program. You can make a DO .. LOOP and poll the status of a pin for example to execute some code when the input on a pin changes.
But with an interrupt you can perform other tasks and when then pin input changes a special part of your program will be executed. When you use INPUT "Name ", v for example to get a user name via the RS-232 interface it will wait until a RETURN is received. When you have an interrupt routine and the interrupt occurs it will branch to the interrupt code and will execute the interrupt code. When it is finished it will return to the Input statement, waiting until a RETURN is entered.
Maybe a better example is writing a clock program. You could update a variable in your program that updates a second counter. But a better way is to use a TIMER interrupt and update a seconds variable in the TIMER interrupt handler.
There are multiple interrupt sources and it depends on the used chip which are available.
To allow the use of interrupts you must set the global interrupt switch with an ENABLE INTERRUPTS statement. This only allows that interrupts can be used. You must also set the individual interrupt switches on!
ENABLE TIMER0 for example allows the TIMER0 interrupt to occur.
With the DISABLE statement you turn off the switches.
When the processor must handle an interrupt it will branch to an address at the start of flash memory. These addresses can be found in the DAT files.
The compiler normally generates a RETI instruction at these addresses so that in the event that an interrupt occurs, it will return immediately.
When you use the ON ... LABEL statement, the compiler will generate code that jumps to the specified label. The SREG and other registers are saved at the LABEL location and when the RETURN is found the compiler restores the registers and generates the RETI so that the program will continue where it was at the time the interrupt occurred.
When an interrupt is serviced no other interrupts can occur because the processor(not the compiler) will disable all interrupts by clearing the master interrupt enable bit. When the interrupt is serviced the interrupt is also cleared so that it can occur again when the conditions are met that sets the interrupt.
It is not possible to give interrupts a priority. The interrupt with the lowest address has the highest interrupt!
Finally some tips :
- when you use a timer interrupt that occurs each 10 uS for example, be sure that the interrupt code can execute in 10 uS. Otherwise you would loose time.
- it is best to set just a simple flag in the interrupt routine and to determine it's status in the main program. This allows you to use the NOSAVE option that saves stack space and program space. You only have to Save and Restore R24 and SREG in that case.
- Since you can not PUSH a hardware register, you need to load it first:
PUSH R24 ; since we are going to use R24 we better save it
IN r24, SREG ; get content of SREG into R24
PUSH R24 ; we can save a register
- here goes your asm code
POP R24 ;get content of SREG
OUT SREG, R24 ; save into SREG
POP R24 ; get r24 back
- When you call user functions or sub routines which passes variables from your interrupt, you need to enable frame protection. Use$frameprotect=1 to actiavate this protection.
See Also
Partial Example using label
Enable Interrupts Enable Int0 'enable the interrupt On Int0 Label2 Nosave 'jump to label2 on INT0 Do'endless loop nop Loop End Label2: Dim A As Byte If A > 1 Then Return 'generates a RET because it is inside a condition End If Return 'generates a RETI because it is the first RETURN Return 'generates a RET because it is the second RETURN
Partial Example using Sub
Declare Sub Label2() Dim A As Byte Enable Interrupts Enable Int0 'enable the interrupt On Int0 Label2 Nosave 'jump to label2 on INT0 Do'endless loop nop Loop End Sub Label2() If A > 1 Then exit sub Else gosub test End If exit sub Test: print "test" Return End Sub 'generates a RETI
As you can see, using a Sub is more flexible because you can include local routines using a label/return.
Languages | English • Deutsch |
---|