Tut 7 : Array

An array is a finite and arranged list of variables of the same type called elements. This type is called the base type. Each element is assigned a unique index so that different elements may have the same value. An array is declared by specifying the type of its elements (called array type), its name and the number of its elements enclosed within brackets:
array VAR BYTE[3]
Elements of an array are identified by their position. Indices go from 0 (the first element of an array) to N-1 (N is the number of elements contained in an array). The compiler must know how many memory locations to allocate when an array is declared and because of that the array size can’t be variable.

Elements of array      Contents of element
      array[0]                           5
      array[1]                  20
      array[2]                   25

In constant arrays, elements can be assigned their contents during array declaration. In the following example, an constant array named VOLTAGE is declared and each element is assigned specific number of volts:

 VOLTAGE VAR BYTE[5] = (5,20,25,16,12)

The number of assigned values must not exceed the specified array length, but can be less. In this case, the trailing ‘excess’ elements will be assigned zeroes.
PBP Program
' Example program to detect the maximum value in array

DEFINE OSC 20
@ device hs_osc 'jika guna crytal 20Mhz

Vin VAR BYTE[5]
i VAR BYTE
Vin_max VAR BYTE

Vin[0]=2
Vin[1]=150
Vin[2]=50
Vin[3]=245
Vin[4]=25

Vin_max=0

loop:
For i = 0 to 4
If Vin[i] > Vin_max THEN
Vin_max = Vin[i]
Pause 1000
End If
Next i

Goto loop
End
      


No comments:

Post a Comment