beautypg.com

4 declarations, 1 variables, 2 variable arrays – Campbell Scientific CR9000X Measurement and Control System User Manual

Page 139

background image

Section 4. CRBasic – Native Language Programming

4.2.4 Declarations

Pre-defined constants, Public variables, Dim variables, Aliases, Units, Data
Tables, and Subroutines are all declared at the beginning of a CRBASIC
program. All variables/constants used in a CRBasic program must be declared.
See Table 4.2.7-1 Rules for Names for nomenclature rules.

4.2.4.1 Variables

A variable is a packet of memory, given an alphanumeric name, through which
pass measurements and processing results during program execution. Variables
are declared either as Public or Dim at the discretion of the programmer.
Variables declared using the Public instruction can be viewed through the
CR1000KD or software numeric monitors. Variables declared using the Dim
instruction cannot be monitored in real time unless they are stored to an Output
table.

4.2.4.2 Variable Arrays

When a variable is declared, several variables of the same root name can also be
declared. This is done by placing a suffix of “(x)” on the alphanumeric name,
which creates an array of x number of variables that differ only by the
incrementing number in the suffix. For example, rather than declaring four
similar variables as follows,

Public TempC1
Public TempC2
Public TempC3

simply declare a variable array as shown below:

Public TempC(3),

This creates in memory the four variables TempC(1), TempC(2), and
TempC(3). References to the array with empty brackets is the same as
referencing the first element of the array; i.e: TempC() and TempC(1) can be
used interchangeably. Unique names can be given to these array elements using
the Alias instruction.

A variable array is useful in program operations that affect many variables in
the same way. EXAMPLE 4.2.4-1 shows program code using a variable array
to reduce the amount of code required to convert four temperatures from
Celsius degrees to Fahrenheit degrees.

EXAMPLE 4.2.4-1. CRBASIC Code: Using a variable array in calculations.

Public TRef, TempC(4), TempF(4)
Alias TempF(1) = Radiator_In

: Alias

TempF(2) = Radiator_Out

Alias TempF(3) = Air_Intake

: Alias

TempF(2) = Exhaust

Dim T
BeginProg

Scan (1,Sec,0,0)

ModuleTemp (TRef,1,4,40)

TCDiff (TempC(),4,mV50C,4,1,TypeT,TRef,True ,30,100,1.0,0)

For T = 1 To 4

TempF(T) = TempC(T) * 1.8 + 32

Next

NextScan

EndProg

4-11