Positron Compiler Documentation

Global Variables

Source: Positron16 Compiler User Manual, PDF page 29

Global variables are standard variables, but can be seen by all procedures as well as the main program, when created within a procedure. Global variables are also created using the Dim directive, but the text ‘Global’ must precede it.

The format for creating a Global variable is as follows: -

Global Dim Name as Type

Name is any valid identifier, (excluding keywords). Type is Pin, Byte, Word, Dword, SByte, SWord, Sdword, Float, Double, String, or an Array. Some examples of creating Global variables within a procedure are: -

Proc TestProc()
  Global Dim bDog as Byte
                                   ' Create an 8-bit unsigned Global variable
  Global Dim MyPin as Pin
                                   ' Create a Global Port.Pin mask variable
  Global Dim wRat as Word
                                   ' Create a 16-bit unsigned Global variable
  Global Dim dRat as Dword
                                   ' Create a 32-bit unsigned Global variable
  Global Dim fBoat as Float
                                   ' Create a 32-bit Floating Point Global variable
  Global Dim bDog[10] as Byte
                                     ' Create an 8-bit unsigned Global array
  Global Dim wRatAr[10] as Word
                                     ' Create a 16-bit unsigned Global array
  Global Dim dRatAr[10] as Dword   ' Create a 32-bit unsigned Global array
  Global Dim fBoatAr[10] as Float  ' Create a 32-bit Floating Point Global array
   Global Dim MyString as String * 10 ' Create a Global String variable
  Global Dim sbDog as SByte
                                   ' Create an 8-bit signed Global variable
  Global Dim swRat as SWord
                                   ' Create a 16-bit signed Global variable
  Global Dim sdLrg_Rat as SDword ' Create a 32-bit signed Global variable
EndProc

If the above Global Dim directives are placed preceding a variable within a procedure, it is not created as a local type variable that can only be seen within the procedure itself, but it can be seen by all of the program. This is the meaning of the word 'Global'. For example:

Device = 24FJ64GA002
                                ' Select the device to compile for
Declare Xtal = 16 ' Tell the compiler the device will be operating at 16MHz
Declare Hserial_Baud = 9600 ' Set the Baud rate for HRsout
Declare Hrsout1_Pin = PORTB.14  ' Select the pin for TX with USART1
RPOR7 = 3
                              ' Make PPS Pin RP14 U1TX
  MyByte = 0
                                ' Clear the Global variable MyByte
  Do
                                ' Create a loop
     TestProc()
                                ' Call the procedure so it will increment MyByte
     HRsoutLn Dec MyByte
                                ' Transmit the result to a serial terminal
     DelayMs 512
                                ' A delay so the terminal is not flooded with values
  Loop
                                ' Do it forever
Proc TestProc()
Global Dim MyByte as Byte
                                ' Create an 8-bit unsigned Global variable
  MyByte = MyByte + 1
                                ' Increment the Global variable MyByte
EndProc