ik zou graag een prog maken met een paar interupts maar verstaat niet goed hoe je er een moet maken graag een beetje uitleg hieromtrend

de helpfile van Proton gebruiken?


ON_INTERRUPT 12 14 16 
 
Syntax

ON_INTERRUPT {Goto} Label 
ON_HARDWARE_INTERRUPT {Goto} Label

Note that the Goto text is optional.

Overview

Jump to a subroutine when a HARDWARE interrupt occurs 
 
Operators
Label - a valid identifier 
Example

 ' Flash an LED attached to PORTB.0 at a different rate to the 
 ' LED attached to PORTB.1 
 
 DEVICE 16F84 
 ON_HARDWARE_INTERRUPT GOTO Flash 
 ' Assign some Interrupt associated aliases 
 SYMBOL T0IE = INTCON.5         ' TMR0 Overflow Interrupt Enable 
 SYMBOL T0IF = INTCON.2         ' TMR0 Overflow Interrupt Flag 
 SYMBOL GIE = INTCON.7          ' Global Interrupt Enable 
 SYMBOL PS0 = OPTION_REG.0      ' Prescaler ratio bit-0 
 SYMBOL PS1 = OPTION_REG.1      ' Prescaler ratio bit-1 
 SYMBOL PS2 = OPTION_REG.2      ' Prescaler ratio bit-2  
 SYMBOL PSA = OPTION_REG.3      ' Prescaler Assignment (1=assigned to WDT 0=assigned to oscillator)  
 SYMBOL T0CS = OPTION_REG.5     ' Timer0 Clock Source Select (0=Internal clock 1=External PORTA.4) 
 SYMBOL LED = PORTB.1 
 
 GOTO Over_interrupt            ' Jump over the interrupt subroutine 

 ' Interrupt routine starts here 
Flash: 

 ' XOR PORTB with 1, Which will turn on with one interrupt and turn off with the next the LED connected to PORTB.0 
 PORTB = PORTB ^ 1 
 T0IF = 0                        ' Clear the TMR0 overflow flag 
 CONTEXT RESTORE                 ' Restore the registers and exit the interrupt 
 
Over_interrupt : 
 TRISB = %00000000               ' Configure PORTB as outputs 
 PORTB = 0                       ' Clear PORTB 
 ' Initiate the interrupt 
 GIE = 0                         ' Turn off global interrupts 
 PSA = 0                         ' Assign the prescaler to external oscillator 
 PS0 = 1                         ' Set the prescaler 
 PS1 = 1                         ' to increment TMR0 
 PS2 = 1                         ' every 256th instruction cycle 
 T0CS = 0                        ' Assign TMR0 clock to internal source 
 TMR0 = 0                        ' Clear TMR0 initially 
 T0IE = 1                        ' Enable TMR0 overflow interrupt 
 GIE = 1                         ' Enable global interrupts 
Inf: 
 LOW LED 
 DELAYMS 500 
 HIGH LED 
 DELAYMS 500 
 GOTO Inf 

Initiating a TMR0 Interrupt on a 14-bit Core Device.

Before we can change any bits that correspond to interrupts we need to make sure that global interrupts are disabled. This is done by clearing the GIE bit of INTCON (INTCON.7). Sometimes an interrupt may occur while the GIE bit is being cleared, which means that the bit is not actually cleared and global interrupts are not disabled. To make sure that the GIE bit is actually cleared we must poll it. This can be accomplished by a simple loop: -


 GIE = 0          ' Disable global interrupts 
 WHILE GIE = 1    ' Make sure they are off 
 GIE = 0          ' Continue to clear GIE 
 WEND             ' Exit when GIE is clear 

The prescaler attachment to TMR0 is controlled by bits 0:2 of the OPTION_REG (PS0, 1, 2). The table below shows their relationship to the prescaled ratio applied. But before the prescaler can be calculated we must inform the PICmicro as to what clock governs TMR0. This is done by setting or clearing the PSA bit of OPTION_REG (OPTION_REG.3). If PSA is cleared then TMR0 is attached to the external crystal oscillator. If it is set then it is attached to the watchdog timer, which uses the internal RC oscillator. This is important to remember; as the prescale ratio differs according to which oscillator it is attached to.


 PS2 PS1 PS0 PSA=0 (External crystal OSC) PSA=1 (Internal WDT OSC)
 0   0   0   1 : 2                        1 : 1
 0   0   1   1 : 4                        1 : 2
 0   1   0   1 : 8                        1 : 4
 0   1   1   1 : 16                       1 : 8
 1   0   0   1 : 32                       1 : 16
 1   0   1   1 : 64                       1 : 32
 1   1   0   1 : 128                      1 : 64
 1   1   1   1 : 256                      1 : 128

TMR0 Prescaler Ratio Configurations

As can be seen from the above table, if we require TMR0 to increment on every instruction cycle (4/OSC) we must clear PS2..0 and set PSA, which would attach it to the watchdog timer. This will cause an interrupt to occur every 256us (assuming a 4MHz crystal). If the same values were placed into PS2..0 and PSA was cleared (attached to the external oscillator) then TMR0 would increment on every 2nd instruction cycle and cause an interrupt to occur every 512us.

There is however, another way TMR0 may be incremented. By setting the T0CS bit of the OPTION_REG (OPTION_REG.5) a rising or falling transition on PORTA.0 will also increment TMR0. Setting T0CS will attach TMR0 to PORTA.0 and clearing TOCS will attach it to the oscillators. If PORTA.0 is chosen then an associated bit, T0SE (OPTION_REG.4) must be set or cleared. Clearing T0SE will increment TMR0 with a low to high transition, while setting T0SE will increment TMR0 with a high to low transition.

The prescaler's ratio is still valid when PORTA.0 is chosen as the source, so that every nth transition on PORTA.0 will increment TMR0. Where n is the prescaler ratio.

Before the interrupt is enabled, TMR0 itself should be assigned a value, as any variable should be when first starting a program. In most cases clearing TMR0 will suffice. This is necessary because, when the PICmicro is first powered up the value of TMR0 could be anything from 0 to 255

We are now ready to allow TMR0 to trigger an interrupt. This is accomplished by setting the T0IE bit of INTCON (INTCON.5). Setting this bit will not cause a global interrupt to occur just yet, but will inform the PICmicro that when global interrupts are enabled, TMR0 will be one possible cause. When TMR0 overflows (rolls over from 255 to 0) the T0IF (INTCON.2) flag is set. This is not important yet but will become crucial in the interrupt handler subroutine.

The final act is to enable global interrupts by setting the GIE bit of the INTCON register (INTCON.7).

Format of the Interrupt Handler on a 14-bit Core Device

The interrupt handler subroutine must always follow a fixed pattern. First, the contents of the STATUS, PCLATH,
FSR, and Working register (WREG) must be saved, this is termed context saving, and is performed automatically by
the compiler, and variable space is automatically allocated for the registers in the shared portion of
memory located at the top of BANK 0.

When the interrupt handler was called, the GIE bit was automatically cleared by hardware, disabling any more interrupts. If this were not the case, another interrupt might occur while the interrupt handler was processing the first one, which would lead to disaster.

Now the T0IF (TMR0 overflow) flag becomes important. Because, before exiting the interrupt handler it must be cleared to signal that we have finished with the interrupt and are ready for another one.

T0IF = 0 ' Clear the TMR0 overflow flag

But this only applies if a (TMR0 overflow triggered the interrupt in the first place.

The STATUS, PCLATH, FSR, and Working register (WREG) must be returned to their original conditions (context restoring). The CONTEXT RESTORE command may be used for this. i.e. CONTEXT RESTORE. The CONTEXT RESTORE command also returns the PICmicro back to the main body code where the interrupt was called from. In other words it performs a RETFIE instruction.

Note that on PICmicro devices that contain only 1 PAGE of code memory, the PCLATH register is not saved or restored. This is because it is not required to point to pages above the first.

ICD Disturbance with 14-bit Core Device

If ICD2 support is enabled along with a hardware interrupt, the compiler cannot allocate context saving/restoring space in the shared portion of RAM because the ICD requires this memory for its own exclusive use. So the compiler adopts a different RAM allocation strategy.

The first 4 bytes of RAM in BANK0 are left clear for saving the WREG, STATUS, FSR, and PCLATH. Subsequentially, every first byte of RAM in banks 1,2 or 3 (if available) are also reserved for WREG saving. This is required because an interrupt may occur while the PICmicro is pointing to a BANK other than 0.

The RAM usage will be reflected in the amount of RAM available on the PICmicro.

Format of the Interrupt Handler on a 16-bit Core Device

The interrupt handler subroutine on a 16-bit core device follows the same rules as on a 14-bit device, but there are some subtle differences. First, the contents of the STATUS, BSR, WREG (Working register), and sometimes FSR0L and FSR0H, may need to be saved, this is termed context saving, and unlike the 14-bit core devices, it is NOT performed automatically by the compiler. Issuing the CONTEXT SAVE command will store the registers in Access RAM. I say "may need saving" because the 16-bit core devices can automatically save the STATUS, BSR and WREG in shadow registers before entering an interrupt handler, and restore them upon exiting. See the appropriate PICmicro's datasheet for more details on this, because Microchip, in their wisdom, may change its operation in future devices (it has happened before!).

When the interrupt handler is called, the GIE bit is automatically cleared by hardware, disabling any more interrupts. If this were not the case, another interrupt might occur while the interrupt handler was processing the first one, which would lead to disaster.

Once the interrupt subroutine is finished, the STATUS, BSR, WREG and the FSR0L/H (if saved) must be returned to their original conditions (context restoring). The CONTEXT RESTORE command may be used for this. i.e. CONTEXT RESTORE.

The CONTEXT RESTORE command also returns the PICmicro back to the main body code where the interrupt was called from. In other words it performs a RETFIE instruction. If automatic register saving is performed, then issuing a RETFIE FAST will automatically restore the registers and exit the interrupt.

Precautions

Because a hardware interrupt may occur at any time, It cannot be fully guaranteed that a SYSTEM variable will not be disturbed while inside the interrupt handler, therefore, the safest way to use a HARDWARE interrupt is to write the code in assembler, or to implement a SOFTWARE interrupt using the ON INTERRUPT directive. This will guarantee that no system variables are being altered. Alternatively, the assembler code may be viewed in order to ascertain whether any system variables are being used. Some of these are: -

PP0, PP0H, PP1, PP1H, PP2, PP2H, PP3, PP3H, PP4, PP4H, PP5, PP5H, PP6, PP6H, PP7, PP7H, GEN, GENH, GEN2, GEN2H, GEN3, GEN3H, GEN4, GEN4H, GPR, BPF.

If an unrecognised or undeclared variable name is seen within the interrupt handler's assembler code, then it will be a SYSTEM variable, and the program may fail, either subtly or disastrously.

The code within the interrupt handler should be as quick and efficient as possible because, while it's processing the code the main program is halted.

When using assembler interrupts, care should be taken to ensure that the watchdog timer does not time-out. Placing a CLRWDT instruction at regular intervals within the code will prevent this from happening. An alternative approach would be to disable the watchdog timer altogether at programming time.

Interrupt Declare

DECLARE FSR_CONTEXT_SAVE = ON or OFF, or TRUE or FALSE, or 1, 0
When using HARDWARE interrupts, it is not always neseccary to save the FSR register. So in order to save code space and time spent within the interrupt handler, the FSR_CONTEXT_SAVE DECLARE can enable or disable the auto Context saving and restoring of the FSR register.

For 16-bit core devices, this will enable/disable FSR0 context handling. If string variables are used in the BASIC program, the FSR1L/H register pair will also be saved/restored. And FSR2L/H registers will be saved/restored if a stack is implemented.

See Also
ON_LOW_INTERRUPT
ON INTERRUPT

[Bericht gewijzigd door Henry S. op ]

De volgende keer even uitkijken wat je tussen code-tags zet, en een move /S&I

heb al meerder keren die help gelezen maar snap er nog steeds niet veel van
vandaar dat ik het hier eens vraag