beautypg.com

5 constants conversion, 4 logical expressions – Campbell Scientific CR9000X Measurement and Control System User Manual

Page 165

background image

Section 4. CRBasic – Native Language Programming

EXAMPLE 4.2.11-3 . CRBASIC Code: Evaluation of Integers

Public X, I AS Long
BeginProg
I = 126
X = (I+3) * 3.4

‘I+3 is evaluated as an integer then converted

'to FLOAT before it is multiplied by 3.4

EndProg

4.2.11.3.5 Constants Conversion

Constants are not declared with a data type, so the CR9000X assigns the data
type as needed. If a constant (either entered as a number or declared with
CONST) can be expressed correctly as an integer, the compiler will use the type
that is most efficient in each expression. The integer version will be used if
possible, i.e., if the expression has not yet encountered a float. EXAMPLE
4.0-4 lists a program
ming case wherein a value normally considered an inte
10, is assigned by the CR9000X to be As Float.

ger,

EXAMPLE 4.0-4. CRBASIC Code: Constants to LONGs or FLOATs
Public
I AS Long

Public A1, A2

CONST ID = 10
BeginProg
A1 = A2 + ID

I = ID * 5

EndProg


In EXAMPLE 4.0-4, I is an integer. A1 and A2 are Floats. The number 5 is
loaded As Float to add efficiently with constant ID, which was compiled As
Float for the previous expression to avoid an inefficient run time conversion
from integer to float before each floating point addition.

4.2.11.4 Logical Expressions

Several different words, such as High / Low, On / Off, Yes / No, Set / Reset,
Trigger / Do Not Trigger, get used interchangeably with True / False to describe
a condition or the result of a test. However, the CR9000x understands only
True / False or -1 / 0.

The CR9000X represents “true” with “-1” because AND / OR operators are the
same for logical statements and binary bitwise comparisons. In the binary
number system internal to the CR9000X, “-1” is expressed with all bits equal to
1 (11111111). “0” has all bits equal to 0 (00000000). When -1 is ANDed with
any other number, the result is the other number. This ensures that if the other
number is non-zero (true), the result will be non-zero. The CR9000X evaluates
an expression as True if it is not equal to 0 and as False if equal to 0.

Using TRUE or FALSE conditions with logic operators such as AND and OR,
logical expressions can be encoded into a CR9000X program to perform
general logic functions, facilitating conditional processing and control
applications.

4-37