PIC32 Numbers & Variables

Numbers & Variables

There are three types of numbers that can be used, character, integer, double (floating point) and unsigned integer (hex). Character is 1 byte and can be specified by using single quotes such as ‘a’ or a number between 0 and 255 Integer is stored as 4 bytes (32 bits) giving a range of -2,147,483,648 to +2,147,483,647 Unsigned integer as 4 bytes giving a range of 0 to 4,294,967,295 Floating point or double stored as 8 bytes giving a range of (lots) Numbers can be specified as strings, hex, integer or float for example: 12.1, “66.3”, 10, 0x3AF

dim
vexists
dumpv
&,#,%,$
*
array
integer
character
double
string
float

Variables are created as and when they are used, they do not need to be assigned first (using for example DIM) but the name used is important or more accurately the post fix symbol determines the type of data that will be stored.

Storage space is assigned at the time of creation.

Variable Symbol Examples
Character & a& fred& ar&[3]
Float # a# fred# r#[3]
Unsigned $ a% fred% r%[3]
String $ a$ fred$ r$[3]
Integer   a fred r[3]

When an unsigned variable is used the output is printed in hex but any value can be assigned to it.

Automatic conversion takes place once a variable has been specified so for example. A$=”fred” would store “fred” to a$ and A$=a$+33 would give a string “fred33” even though the 33 is an integer.

Expression examples:

12+5+a#+”4”     string
1.2+3+’a’           float
a+(10^5)           integer
‘a’+’b’                character

Here are some examples of conversion for a variable:

Integer

a=10 \ would store 10 to a
a=a+”33” \ a now = 43
a=a+1.7 \ a now = 44 (no rounding)
a=”fred” \ produces an error
a=a+’a’ \ now a = 141

In the last example ‘a’ is a character and so its ASCII value is used which is 97.

Character

a&=’a’ \ a& now = 97
a&=10 \ a& now = 10
a&=12.3 \ a& now=12 (no rounding)
a&=”1” \ a& now = 1
a&=”x” \ produces an error

In the last example “x” cannot be converted into a number.

Double

d#=1 \ d# now = 1
d#=0xa \ d# now = 10
d#=”5.5” \ d# now = 5.5
d#=”0xa” \ d now = 0

In the last example a string of a hex number cannot be converted to a double, no error is generated.

String

Strings are stored with a terminating 0 to mark the end of the sting. This fact may be of use and hence why it is mentioned here.

a$=”fred” \ a$ now = fred
a&=97 \ charater ‘a’
a$=”fred “+a& \ a$ now = fred a
a$=12 \ a$ now = 12

In the last example the integer 12 is converted to a string. Strings occupy a fixed number of bytes and have a potential of occupying a large amount of space. The default space created for any string is 128 bytes (see DEF_STR_LEN). This can be limited, or increased, by using the following syntax:

a$[:20]

This will create a string with space for 19 characters plus the 0 terminating character. The data in any string is left justified within that space.

Character constants can also be assigned to a sting by using ‘*’

As an example: b$=”Hello”+*0x0a+”World” will place a line feed character between Hello and Word thus when printed the output would be:

Hello
World

Arrays

Arrays can be of any type and have up to 4 dimensions, i.e. a[2,2,2,2]. Each dimension will occupy its own size so the above will occupy 16 x 4 bytes.

Arrays MUST be defined prior to use using the DIM statement

Special attention is needed when specifying string arrays, this a$[3,3] for example would occupy 3x3x128 = 1,152 bytes because the default string space allocation is 128 bytes. To limit this, the same syntax is used as for single variables:

a$[3,3:10]

This specifies that 10 bytes should be allocated to the sting holder and so only 90 bytes will now be required. The size modifier applies to all elements of the array. It is not possible to have varying size elements.

dim
dim variable[=value],,,,
dim a#
dim a,b=6,a$
dim a$=”fred”,c,d

This is used to define a variable prior to its use, this is necessary when using arrays but optional for other variables. The dim statement will have an impact on scope, for more details see the section in subroutines.

A variable can be assigned a value as well as being created and multiple variables can be defined at once:

dim a$=”fred”, fl#=12.77, b=6

vexists()
a=vexists(“fred”)
b=vexists(b$)
c=vexists(“b$”)

This returns 1 if a variable exists and is mostly used for initialising global variables, see the section in subroutines under ‘keep’. If a variable named fred (dim fred=2) exists then ‘a’ will be 1. In the second example it is the string INSIDE b$ that is checked.

If you were looking for the existence of b$ then example c would be correct. This does not rule out the validity of example b as:
dim b$=”bob”
dim bob=6

Now the example at b would return true because ‘bob’ exists as a variable

dumpv
dumpv a$

This will output the contents of a variable in ASCII and hex. If b$ contained ‘fred’ then the output would be:
a0005cd5 66 72 65 64 00 00 00 00 00 00 00 00 00 00 00 00 fred............

The length of the output is taken from the size of the variable, in this case b$ has been defined as: dim b$[:16] And so this will limit the ‘dumpv’ output to 16 bytes.