ON      (version 7.09 and later)
 
Syntax


ON TIMER msec label

    or

ON   EINT0|EINT1|EINT2    RISE|FALL|HIGH|LOW   label

Description


These statements will initialize interrupt service routines so that when the interrupt occurs the code at label will be executed.  Label must have been pre-defined and can either be a SUB (without parameters) or code beginning with a label: and ending in a RETURN. The interrupt response time is approximately 3 usec.  Other interrupts may make this time longer.

TIMER interrupts will occur every msec milliseconds. msec may be a variable or constant, expressions are not allowed.  If TIMER interrupts are used, then only 4 hardware PWM channels are available.

EINT0 and EINT2 are 2 pins that will interrupt when the defined event occurs.  RISE and FALL are the preferred method and will generate interrupts on rising or falling edges on those 2 pins. HIGH and LOW are supported, but if the pin remains in that state interrupts will be continuously generated. 

EINT1 is connected to the RTS line of the PC, and is normally high, so it can be used by a program on the PC to interrupt the ARMmite, rather than having to reset the board.  This pin is available on the wireless ARMmite, but if you intend to use it, make sure it is pulled high normally, otherwise when the board is reset it will go into the download C mode and will not run your BASIC program.  EINT1 is also available on the ARMexpress modules (pin 21), and should also be kept normally high if used.

Each time the ON statement is executed the interrupt will be initialized, so it is possible to change routines within the program.  Multiple interrupts can be used, but they are serviced in the order received, and each interrupt service routine will complete before the next one is handled (interrupts that occur while one is being serviced will be handled after the current interrupt is processed).

Interrupt routines should normally be short and simple. The state of the other user BASIC code will be restored after the interrupt, with the exception of string functions, which should NOT be done inside an interrupt. 

To disable the interrupt use the following #define

#defineVICIntEnClear  *$FFFFF014

#define TIMERoff    VICIntEnClear = $20
#define EINT0off     VICIntEnClear = $4000
#define EINT1off     VICIntEnClear = $8000
#define EINT2off     VICIntEnClear = $10000

ON added in version 7.09

Example

IO15up = 0              ' serves to declare IO15up

...
SUB IO15count
  IO15up = IO15up + 1
ENDSUB

...
main:

ON EINT2 RISE IO15count

IO15up = 0
while 1
  if IO15up <> lastIO15count then
    print IO15up
    lastIO15count = IO15up
  endif

...

loop

every20msec:
  checkIO0 = checkIO0 + (IO(0) and 1)
  IO0samples = IO0samples +1
RETURN

...
main:

ON TIMER 20 every20msec

...

PRINT "Percentage of time IO0 is HIGH =", 100*checkIO0 / IO0samples

...
 
Differences from other BASICs

  • VB ???
  • no equivalent in PBASIC
See also