![]() ZBasic System Library
276
ZBasic Microcontrollers
Type
Function returning Byte
Invocation
SerialIn(pin, baudRate)
Parameter
Method
Type
Description
pin
ByVal
Byte
The pin from which to read the data.
baudRate
ByVal
integral
The baud rate for the serial input.
Discussion
This function reads a byte, transmitted in 8-N-1 serial form (8 data bits, no parity, 1 stop bit), via a pin.
The initial state of the pin (expected to be configured as an input) is used to infer logic mode (logic 1
means non-inverted, logic zero means inverted). The function waits for a start bit and then reads eight
data bits, sampling the input at the approximate midpoint of the bit window given the specified baud rate.
If no start bit is ever detected the function will never return.
While waiting for the start bit, interrupts are not disabled but when the start bit is detected interrupts are
disabled for the remainder of the character time, typically about 9.5 bit times. Note that the logic level of
the stop bit is not verified. Because relatively precise timing is required for reliable start bit detection and
synchronization, this function is best used when few interrupts (preferably none) will occur. In some
cases, it may be best to disable interrupts before invoking the function (using, for example, an Atomic
block). This strategy, however, has its own shortcomings particularly because it is not known beforehand
how long it will be before the start bit arrives.
The theoretical maximum baud rate varies by processor frequency and is expressed as (F_CPU / 19)
while the theoretical minimum baud rate is (F_CPU / 262159). Note, particularly, that if the RTC is
enabled and the character time (i.e. 10 / baudRate) is greater than approximately 1.5 times the RTC
interrupt interval, the RTC may lose time. At 14.7MHz with a 1024Hz RTC interrupt, the minimum
standard baud rate that avoids missing RTC interrupts would be 9600.
This function is useful primarily on devices that have no hardware UARTs and/or in cases where you do
not want to dedicate a timer for the software UART channels, leaving it free for other purposes.
Example
Const pin as Byte = A.0
Dim c as Byte
' configure the pin as an input
Call PutPin(pin, zxInputTriState)
' read a character at 38.4K baud
c = SerialIn(pin, 38400)
Compatibility
This function is not available in BasicX compatibility mode nor on VM-based devices.
See Also
|