Page 95 of 229 Start Back Next End ZBasic Language Reference 85 ZBasic Microcontrollers Bit or Nibble variable to a subroutine/function by reference unless the variable is byte-aligned (see further discussion below). This implies, of course, that you may not be able to pass an array of sub-byte types as a parameter since arrays are always passed by reference. The reason for this limitation is that sub-byte variables do not necessarily begin on a byte boundary and there is no way for the called routine to know what the alignment might be. One way to work around this limitation is to define an integral-byte alias to the sub-byte type, pass the alias by reference and then define a new sub-byte alias in the called routine. This works because integral-byte aliases are required to be byte aligned. Note that when passed by value as a parameter, a sub-byte type parameter occupies an entire byte on the stack. Special type conversion functions, CBit() and CNibble() are provided to facilitate the use of these types in combination with other types. See the ZBasic System Library Reference Manual for more information on the conversion functions. In Section 3.20 Aliases, it was mentioned that there may an issue with aliases of a fundamental type overlaying sub-byte types. The issue arises because fundamental data types must be byte-aligned and a particular Bit or Nibble variable may or may not be byte-aligned. In most cases it will be simpler to define a sub-byte alias to overlay variables of fundamental type. Doing the converse may require some experimentation to achieve the required byte-alignment. The alias defined below may or may not be accepted by the compiler depending on what other sub-byte types are defined at the same scope level and the order in which the are defined. Dim ba(1 to 20) as Bit Dim bval as Byte Alias ba(3) Internally, the compiler collects together all of the sub-byte types at a given scope level (module, procedure, block) and allocates space for them collectively. The Nibble variables are allocated first in the order that they are defined followed by the Bit variables in the order that they are defined. The map file will show a special variable with a name like @BitNib00. This is the host variable that contains the individual Bit and Nibble values for a particular scope level. BasicX Compatibility Note Bit and Nibble data types are not available in BasicX compatibility mode. 3.24.1 Forcing Byte Alignment As described earlier, sub-byte type variables are normally sub-allocated within a host variable. One consequence of this space-saving strategy is that a sub-byte type variable is often not aligned on a byte boundary, a circumstance that imposes limitations on the possible uses of the variable. One solution to this problem is to instruct the compiler to align a particular variable on a byte boundary. This is done by adding the ByteAlign attribute to the variable definition as shown below. Dim ByteAlign ba(1 to 20) as Bit Each variable that is so defined will occupy an integral number of bytes of memory. For the example above, three bytes will be allocated even though only 20 of the 24 available bits will actually be used. Note that the ByteAlign attribute may be used with any variable type but it has no effect for those types that are inherently byte-aligned. Also, the ByteAlign attribute may be used in a Structure to force sub- byte types to be byte-aligned. 3.25 Structures It is often useful to define a data type that is a collection of data elements. For example, if you write a program to manipulate dates it may be useful to define a data type that contains “year”, “month” and “day”