Modules

A module imports declaration enables you to import pre-tested subroutines and functions into your main program body. Modules may also contain other public declarations which your program can use, such as constants and variables. To import a module into your program, use the imports keyword, followed by the modules filename. For example,

Imports USART
Imports Math
Imports OW

Note that you do not need to include the modules *.bas extension or surround the filename in quotes. However, if your filename has spaces, then you will need to enclose the filename in quotes, like this,

Imports "My USART.bas"
Imports "My OW.bas"

Once imported, you can start using any of the module’s public declarations in your main program block. For example,

Imports USART
Imports Math

Sub Main()
   Write(CStr(Sin(10)), 13, 10)
End Sub

The subroutine Write() are located in the USART library and the function Sin() is located in the math library. Some modules may use the same naming convention for identifiers. For example, the USART library and LCD library both have an output subroutine called Write(). You can easily use both from within your program through redirection. This is achieved by prefixing the identifier by the modules name and by using the dot notation. For example,

Imports USART
Imports LCD
Sub Main()
   USART.SetBaudrate(br19200)
   USART.Write("USART Write", 13, 10) ' redirect to USART module
   LCD.Write("LCD Write")             ' redirect to LCD module
End Sub

If you did not use redirection, then all output would be sent via the USART module. This is because the USART library has been included before the LCD library. If you switched them around, then all output would be sent via the LCD module. Redirection is a really good way to document your programs, even if it is not actually needed. For example,

USART.Write("Hello World", 13, 10)

Module imports are followed by your program declarations such as constants, structures, variables, subroutines and functions. Generally, the order in which you declare items is a matter of personal style. As a guide, it is usually good practice to import modules and declare items in the following order,

  • imports
  • constants
  • structures
  • variables
  • aliases and modifiers
  • subroutines and functions
  • main sub

The final part of a program block is the statements group. These are placed in sub main(). That is, the code you write to get the microcontroller to start doing something.