ZBasic Language Reference
62
ZBasic Microcontrollers
An alternate type conversion method is supported for compatibility reasons but is a bit awkward to use.
For each defined enumeration, there is a special System Library conversion function just for that
enumeration whose name is To<enum> where <enum> is replaced with the enumeration name. This
special conversion function takes only one parameter, that being the value to convert.
animal = ToMammal(3)
One drawback to this conversion method is that there is no way to qualify the enumeration with the
module name in which it is defined. It is recommended that all new applications use the CType()
conversion function.
3.3 Serial Channels
All of the ZBasic devices support multiple serial channels. Channel 1 (Com1) is implemented using a
USART hardware on the microcontroller chip (if available). On ZX devices, when the program begins
running, Com1 is configured to run at 19.2K baud, 8 data bits, 1 stop bit. Your program can send and
receive data using the default configuration by utilizing the console I/O routines like Console.Write(),
Console.Read() and the related Debug.Print command. For generic target devices, Com1 is not open by
default but Option ConsoleSpeed can be used to specify that it should be initially open.
Some ZBasic devices have additional hardware-based serial channels but the remaining serial channels
are implemented in software. This strategy allows a lot of flexibility in choosing which pins are used for
transmission and reception but it also imposes a processing overhead on the system, even when
characters are not being actively sent and received. Because of this, only one additional software serial
channel, COM3, is enabled for use by default. If you wish to utilize additional software serial channels
(COM4 to COM6) you must call the System Library routine ComChannels() to specify both the number of
channels desired and the maximum baud rate that may be used. See the description of the
ComChannels() routine in the ZBasic System Library Reference manual for more details.
Since serial channels 3-6 rely on interrupts to achieve the necessary timing for serial I/O, if interrupts are
disabled for a substantial fraction of the bit time (the inverse of the baud rate) the integrity of the
transmitted or received characters may suffer. Typically, the maximum acceptable interrupt disable time
is about 25% of the bit time of the fastest channel. If the fastest channel is running at 9600 baud, the
interrupt disable time should be kept below 25µS or so. Many of the I/O routines that utilize Timer1 (e.g.
PulseOut) disable interrupts in order to achieve precise timing. Those that do disable interrupts have a
caveat to that effect in their respective descriptions in the ZBasic System Library Manual.
See the descriptions of DefineCom(), OpenCom() and CloseCom() in the ZBasic System Library Manual
for more details on setting up and using a serial channel.
3.4 Queues
A queue is a fundamental data structure that is widely used in computer programs. Its primary
distinguishing feature is that data items are extracted from the queue in the same order in which they
were inserted. This is called first-in, first-out or FIFO order.
One of the uses for a queue in ZBasic is as a temporary buffer for data going to and coming from one of
the serial channels. Another common use is as a medium through which to pass data between tasks.
The producer of the data puts data in the queue and the consumer of the data takes it from the queue.
ZBasic queues are of a fixed length, that length being determined at the time that the queue is prepared
for use using OpenQueue(). If the data producer inserts data faster than the consumer removes it, the
queue will eventually become full. Further attempts to add data to the queue will stall until enough data is
removed from the queue to make space for the new data.
Although the data in queues is nominally byte oriented, you can put data of any type into a queue. All
that is necessary is for the producer and consumer of the data to agree on the nature and meaning of the
|