InbuiltRoutines

Most of the time you will be using subroutines and functions that you have created, or you may use routines from a supplied library. However, the compiler provides a number of inbuilt subroutines and functions, many of which have been specially optimized for use with a PICŪ microcontroller.

AddressOf

 function addressof(byref variable) as ushort
 function addressof(byref sub | function) as ushort | uinteger

There are times when you may want to access the address of a variable, subroutine or function. For example,

Public Function Len(ByVal value As String) As Byte
   addr2 = AddressOf(value)
   Len = 0
   While *(addr2+) <> 0
      Len += 1
   End While
End Function

BitOf

 function bitof(byref variable, optional masked as boolean = true) as byte

The bitof function returns the bit number of a bit variable. By default, bitof returns a masked value. To return a non-masked value, call with the masked parameter set to false. For example,

Sub DisplayBitOf(ByRef pBit As Bit)
   Console.Write("Mask  :", CStr(BitOf(pBit)), 13, 10)
   Console.Write("NoMask:", CStr(BitOf(pBit,False)), 13, 10)
End Sub

will output

 Mask  : 128
 NoMask: 7

UBound

 function ubound(byref Array()) as ushort

The ubound function returns the highest addressable index for a given array. For example,

Sub Init(ByRef array() As Byte)
   For index As Byte = 0 To UBound(array)
      array(index) = &HFF
   Next
End Sub

Sub Main()
   Dim arrayA(20) As Byte
   Dim arrayB(32) As Byte
   Init(arrayA)
   Init(arrayB)  
End Sub

Erase

 sub erase(byref variable)

The erase subroutine fills a variable with zeros. For example,

' declare a structure...
Structure Struct
   Dim a As Byte
   Dim b As Byte
   Dim value As UShort
End Structure

' create some variables...   
Dim array(10) As Byte
Dim myStruct As Struct
Dim value As Byte

Sub Main()
   Erase(array)
   Erase(myStruct)
   Erase(value)
End Sub

DelayMS

 sub delayms(expression as ushort)

The delayms subroutine suspends program execution for up to 65535 milliseconds (ms). For example,

DelayMS(100)       ' delay 100 ms
DelayMS(value)     ' delay Value ms
DelayMS(value * 2) ' delay Value * 2 ms

DelayUS

 sub delayus(expression as ushort)

The delayus subroutine suspends program execution for up to 65535 microseconds (μs). For example,

DelayUS(100)       ' delay 100 µs
DelayUS(value)     ' delay Value µs
DelayUS(value * 2) ' delay Value * 2 µs

High

 sub high(byref portpin as bit) 

The high subroutine sets a port pin to a high state. The port pin is automatically set to output. For example,

' turn on LED...
Sub Main()
   Dim LED As PORTD.7
   High(LED)
End Sub

Input

 sub input(byref portpin as bit) 

The input subroutine makes the specified port pin an input. For example,

Input(PORTD.7)  

Low

 sub low(byref portpin as bit) 

The low subroutine sets a port pin to a low state. The port pin is automatically set to output. For example,

' turn off LED...
Sub Main()
   Dim LED As PORTD.7
   Low(LED)
End Sub

Output

 sub output(byref portpin as bit) 

The output subroutine makes the specified port pin an output. For example,

Output(PORTD.7) 

Toggle

 sub toggle(byref portpin as bit) 

The toggle subroutine switches the input or output state of a port pin. That is, a high state becomes low and vice versa. The port pin is automatically set to output. For example,

Sub Main()
   Dim LED As PORTD.7
   Low(LED)       ' LED is off
   DelayMS(1000)  ' wait one second
   Toggle(LED)    ' switch on
End Sub

SizeOf

 function sizeof(identifier) as uinteger

The sizeof function returns the storage size (in bytes) of a variable and can be useful in identifying how much space data is taking. For example,

Sub Main()
   Dim myByte As Byte
   Dim myStr As String 
   Dim myArray(100) As Byte

   ' display values...
   Console.Write("myByte = ", CStr(SizeOf(myByte)), 13, 10)
   Console.Write("myStr = ", CStr(SizeOf(myStr)), 13, 10)
   Console.Write("myArray = ", CStr(SizeOf(myArray)), 13, 10)
End Sub

Will output,

 MyByte = 1
 MyStr = 24
 MyArray = 100