Sub-EndSub
Source: Positron16 Compiler User Manual, PDF page 455
Syntax
Sub Label Name() BASIC commands inside the Subroutine
EndSub
Overview
Create a ‘standard’ subroutine but with a start directive (Sub) and an end directive (EndSub).
Parameters
Label Name is the name of the subroutine.
Example
' Create a subroutine to flash an LED 10 times
Device = 24FJ64GA002
Declare Xtal = 16
Declare Hserial_Baud = 9600 ' UART1 Baud rate
Declare Hrsout1_Pin = PORTB.14
' Select the pin for TX with USART1
Dim FlashAmount as Byte
' Create a variable for the amount of LED flashes
Symbol LED = PORTB.0
' Create a name for the LED’s Port and Pin
Do
' Create a loop
FlashLED()
' Call the subroutine
DelayMs 1000
' Delay for 1 second
Loop
' Loop forever
' Create a subroutine that will flash an LED
'
Sub FlashLED()
For FlashAmount = 1 to 10
' A loop for the amount of flashes
PinHigh LED
' Illuminate the LED
DelayMs 500
' Wait for half a second
PinLow LED
' Extinguish the LED
DelayMs 500
' Wait for half a second
Next
' Close the loop
EndSub
' End the subroutine and return from it
The EndSub directive will produce a Return command and exit the subroutine as normal. There is also an ExitSub command that will create a Return command and return from the subroutine.
' Create a subroutine that will flash an LED and exit when required
'
Sub FlashLED()
For FlashAmount = 1 to 100
' A loop for the amount of flashes
PinHigh LED
' Illuminate the LED
DelayMs 500
' Wait for half a second
PinLow LED
' Extinguish the LED
DelayMs 500
' Wait for half a second
If FlashAmount >= 10 Then ExitSub ' Exit the subroutine after 10 flashes
Next
' Close the loop
EndSub
' End the subroutine and return from it
Calling a sub only requires the name, and a pair of parenthesis after it. This makes a program easier to read:
MySub()
' Call the subroutine named MySub