SERIN Pin, BaudMode, ByteVar {, Timeout, ToLabel}

Receive asynchronous serial byte (e.g., RS-232).

Quick Facts
  FREQ = 4 MHz FREQ = 20 MHz FREQ = 50 MHz
 Maximum Baud Rate † 38400 115200 > 230400
 Baud Modes T (true), N (inverted), OT (open, true), ON (open, inverted)
 Units in Timeout 1 millisecond

† When used without Timeout parameter.

Receive asynchronous serial byte at the selected baud rate and mode using no-parity, 8-data bits, and 1-stop bit. If Timeout is specified, jump to ToLabel if serial byte does not arrive within Timeout milliseconds.

One of the most popular forms of communication between electronic devices is serial communication. There are two major types of serial communication: asynchronous and synchronous. The SERIN and SEROUT commands are used to receive and send asynchronous serial data. See the SHIFTIN and SHIFTOUT command for information on the synchronous method.

The term asynchronous means "no clock." More specifically, "asynchronous serial communication" means data is transmitted and received without the use of a separate "clock" wire. Data can be sent using as little as two wires: one for data and one for ground.

This simple demo shows how to receive a single byte through RA.0 at 2400 baud, 8N1, inverted:

    SERIN RA.0, N2400, sData                    ' wait for byte
    LEDs = sData                                ' put byte value on LEDs

Here, SERIN will wait for and receive a single byte of data through pin RA.0 and store it in the variable sData. If the SX were connected to a PC running a terminal program (set to the same baud rate) and the user pressed the "A" key on the keyboard, after the SERIN command executed, the variable sData would contain 65, the ASCII code for the letter "A".

When used without a Timeout parameter, SERIN block program operation until a byte arrives. Where this condition will have an adverse affect on program operation a Timeout parameter and label can be specified. In this update to the example above, the program will wait for two seconds (2000 milliseconds) for an incoming byte; if no byte arrives the program will be redirected to No_Char and the LEDs will be blanked.

    SERIN RA.0, N2400, sData, 2000, No_Char     ' wait for byte
    LEDs = sData                                ' put byte value on LEDs

  LEDs = %00000000                              ' clear LEDs
  GOTO Main

Note: Interrupts will interfere with the proper operation of SERIN and, in most cases, should be disabled before the SERIN instruction is used. If the interrupt is short and designed to run the same number of cycles under any condition, the EffectiveHz  parameter of FREQ may be used.

Related instruction: SEROUT
Related projects: Serial LCD and RFID Reader Interface