ON INTERRUPT
(Created page with "= <span class="f_Header">Action</span> = Execute subroutine when the specified interrupt occurs. <span style="font-family: Arial;"> </span> <span style="font-family: A...") |
|||
Line 9: | Line 9: | ||
= <span class="f_Header">Syntax</span> = | = <span class="f_Header">Syntax</span> = | ||
− | <span class="f_Syntax">ON</span> interrupt label [NOSAVE|SAVE|SAVEALL] | + | <span class="f_Syntax">ON</span> interrupt label [NOSAVE|SAVE|SAVEALL] |
<span style="font-family: Arial;"> </span> | <span style="font-family: Arial;"> </span> | ||
Line 39: | Line 39: | ||
| valign="top" style="border: 1px solid rgb(0, 0, 0);" | | | valign="top" style="border: 1px solid rgb(0, 0, 0);" | | ||
The label to jump to if the interrupt occurs. | The label to jump to if the interrupt occurs. | ||
+ | |||
+ | When using a label, you need to use a RETURN to resume the main program. | ||
+ | |||
+ | The label may also be a sub routine. When using a sub routine, the sub routine needs to end with END SUB like any normal sub routine. | ||
|- style="vertical-align: top;" | |- style="vertical-align: top;" | ||
Line 85: | Line 89: | ||
| | ||
− | [[File:Notice.jpg| | + | [[File:Notice.jpg|notice]]When the AVR has extended IO-space (for example ATMega48, 88 or 168, see datasheet at the end: Registersummary), the compiler uses <span style="font-weight: bold;">R23</span>for a number of operations. So Push and Pop R23 as well when using the NOSAVE-option when using these AVR's with extended IO-space. |
|- style="vertical-align: top;" | |- style="vertical-align: top;" | ||
Line 94: | Line 98: | ||
This is the default and is the same as when no parameter is provided. The most common used registers, SREG, and RAMPZ are saved and restored. | This is the default and is the same as when no parameter is provided. The most common used registers, SREG, and RAMPZ are saved and restored. | ||
− | Saved | + | Saved : SREG , R31 to R16 and R11 to R0 with exception of R6,R8 and R9. |
If RAMPZ exists, it will be saved as well. | If RAMPZ exists, it will be saved as well. | ||
Line 109: | Line 113: | ||
<span style="font-family: Arial;"> </span> | <span style="font-family: Arial;"> </span> | ||
− | + | When using a label you must return from the interrupt routine with the [[RETURN]] statement. | |
<span style="font-family: Arial;"> </span> | <span style="font-family: Arial;"> </span> | ||
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. | 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. | ||
<span style="font-family: Arial;"> </span> | <span style="font-family: Arial;"> </span> | ||
Line 123: | Line 131: | ||
<span style="font-family: Arial;"> </span> | <span style="font-family: Arial;"> </span> | ||
− | What are interrupts good for? | + | = <span class="f_Header">What are interrupts good for?</span> = |
<span style="font-family: Arial;"> </span> | <span style="font-family: Arial;"> </span> | ||
Line 143: | Line 151: | ||
<span style="font-family: Arial;"> </span> | <span style="font-family: Arial;"> </span> | ||
− | To allow the use of interrupts you must set the global interrupt switch with | + | 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! |
<span style="font-family: Arial;"> </span> | <span style="font-family: Arial;"> </span> | ||
Line 159: | Line 167: | ||
<span style="font-family: Arial;"> </span> | <span style="font-family: Arial;"> </span> | ||
− | The compiler normally generates a RETI instruction | + | The compiler normally generates a RETI instruction at these addresses so that in the event that an interrupt occurs, it will return immediately. |
<span style="font-family: Arial;"> </span> | <span style="font-family: Arial;"> </span> | ||
Line 167: | Line 175: | ||
<span style="font-family: Arial;"> </span> | <span style="font-family: Arial;"> </span> | ||
− | When an interrupt is | + | 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. |
<span style="font-family: Arial;"> </span> | <span style="font-family: Arial;"> </span> | ||
Line 173: | Line 181: | ||
It is not possible to give interrupts a priority. The interrupt with the lowest address has the highest interrupt! | It is not possible to give interrupts a priority. The interrupt with the lowest address has the highest interrupt! | ||
− | + | | |
<span style="font-family: Arial;"> </span> | <span style="font-family: Arial;"> </span> | ||
− | Finally some tips | + | Finally some tips : |
<span style="font-family: Arial;"> </span> | <span style="font-family: Arial;"> </span> | ||
− | *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. | + | * 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. |
<span style="font-family: Arial;"> </span> | <span style="font-family: Arial;"> </span> | ||
− | *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. | + | * 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. |
<span style="font-family: Arial;"> </span> | <span style="font-family: Arial;"> </span> | ||
− | *Since you can not PUSH a hardware register, you need to load it first: | + | * Since you can not PUSH a hardware register, you need to load it first: |
<span style="font-family: Arial;"> </span> | <span style="font-family: Arial;"> </span> | ||
− | PUSH R24 | + | PUSH R24 ; since we are going to use R24 we better save it |
<span style="font-family: Arial;"> </span> | <span style="font-family: Arial;"> </span> | ||
− | IN r24, SREG | + | IN r24, SREG ; get content of SREG into R24 |
− | PUSH R24 | + | PUSH R24 ; we can save a register |
<span style="font-family: Arial;"> </span> | <span style="font-family: Arial;"> </span> | ||
Line 205: | Line 213: | ||
;here goes your asm code | ;here goes your asm code | ||
− | POP R24 | + | POP R24 ;get content of SREG |
<span style="font-family: Arial;"> </span> | <span style="font-family: Arial;"> </span> | ||
− | OUT SREG, R24 | + | OUT SREG, R24 ; save into SREG |
− | POP R24 | + | 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|$frameprotect]]=1 to actiavate this protection. | ||
+ | |||
+ | | ||
<span style="font-family: Arial;"> </span> | <span style="font-family: Arial;"> </span> | ||
Line 217: | Line 231: | ||
= <span class="f_Header">See Also</span> = | = <span class="f_Header">See Also</span> = | ||
− | [[ | + | [[ON_VALUE|On VALUE]] |
<span style="font-family: Arial;"> </span> | <span style="font-family: Arial;"> </span> | ||
Line 223: | Line 237: | ||
<span style="font-family: Arial;"> </span> | <span style="font-family: Arial;"> </span> | ||
− | = <span class="f_Header">Partial Example</span> = | + | = <span class="f_Header">Partial Example using label</span> = |
− | < | + | <span style="color: rgb(0, 128, 0); font-family: 'Courier New'; font-size: 10pt;"> </span> |
+ | |||
+ | <span style="font-family: 'Courier New'; color: rgb(0, 128, 0);"> </span> | ||
+ | |||
+ | = <span class="f_Header">Partial Example using Sub</span> = | ||
+ | |||
+ | |||
+ | |||
+ | <span style="font-family: 'Courier New'; color: rgb(0, 128, 0);"> </span> | ||
+ | |||
+ | As you can see, using a Sub is more flexible because you can include local routines using a label/return. | ||
+ | |||
+ | |||
+ | |||
+ | <source lang="bascomavr"> | ||
Enable Interrupts | Enable Interrupts | ||
Enable Int0 'enable the interrupt | Enable Int0 'enable the interrupt | ||
Line 241: | Line 269: | ||
Return 'generates a RETI because it is the first RETURN | Return 'generates a RETI because it is the first RETURN | ||
Return 'generates a RET because it is the second RETURN | Return 'generates a RET because it is the second RETURN | ||
− | </source> | + | </source> |
+ | |||
+ | {{Languages}} | ||
[[Category:BASCOM Language Reference]] | [[Category:BASCOM Language Reference]] |
Revision as of 11:49, 24 August 2013
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
Partial Example using Sub
As you can see, using a Sub is more flexible because you can include local routines using a label/return.
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
Languages | English • Deutsch |
---|