<<Previous

Next>>

 SERIN

SERIN Pin,Mode,{Timeout,Label,}{[Qual...],}{Item...}

Receive one or more Items on Pin in standard asynchronous format using 8 data bits, no parity and one stop bit (8N1). SERIN is similar to the BS1 Serin command with the addition of a Timeout. Pin is automatically made an input. Pin may be a constant, 0 - 15, or a variable that contains a number 0 - 15 (e.g. B0) or a pin name (e.g. PORTA.0).

The Mode names (e.g. T2400) are defined in the file MODEDEFS.BAS. To use them, add the line:

Include "modedefs.bas"

to the top of the PICBASIC PRO™ program. BS1DEFS.BAS and BS2DEFS.BAS already includes MODEDEFS.BAS. Do not include it again if one of these files is already included. The Mode numbers may be used without including this file.

Mode

Mode No.

Baud Rate

State

T2400

0

2400

True

T1200

1

1200

T9600

2

9600

T300

3

300

N2400

4

2400

Inverted

N1200

5

1200

N9600

6

9600

N300

7

300

An optional Timeout and Label may be included to allow the program to continue if a character is not received within a certain amount of time. Timeout is specified in units of 1 millisecond.  If the serial input pin stays idle during the Timeout time, the program will exit the SERIN command and jump to Label.

The list of data items to be received may be preceded by one or more qualifiers enclosed within brackets. SERIN must receive these bytes in exact order before receiving the data items. If any byte received does not match the next byte in the qualifier sequence, the qualification process starts over (i.e. the next received byte is compared to the first item in the qualifier list). A Qualifier can be a constant, variable or a string constant. Each character of a string is treated as an individual qualifier.

Once the qualifiers are satisfied, SERIN begins storing data in the variables associated with each Item. If the variable name is used alone, the value of the received ASCII character is stored in the variable. If variable is preceded by a pound sign ( # ), SERIN converts a decimal value in ASCII and stores the result in that variable. All non-digits received prior to the first digit of the decimal value are ignored and discarded. The non-digit character which terminates the decimal value is also discarded.

SERIN assumes a 4MHz oscillator when generating its bit timing. To maintain the proper baud rate timing with other oscillator values, be sure to DEFINE the OSC setting to the new oscillator value.

While single-chip RS-232 level converters are common and inexpensive, the excellent I/O specifications of the PICmicro allow most applications to run without level converters. Rather, inverted input (N300..N9600) can be used is conjunction with a current limiting resistor.

SERIN 1,N2400,[“A”],B0	' Wait until the character “A” is received serially 
			on Pin1 and put next character into B0

 SERIN2

SERIN2 DataPin{\FlowPin},Mode,{ParityLabel,} {Timeout,Label,}[Item...]

Receive one or more Items on Pin in standard asynchronous format. SERIN2 is similar to the BS2 Serin command. DataPin is automatically made an input. The optional FlowPin is automatically made an output. DataPin and FlowPin may be a constant, 0 - 15, or a variable that contains a number 0 - 15 (e.g. B0) or a pin name (e.g. PORTA.0).

The optional flow control pin, FlowPin, may be included to help keep data from overrunning the receiver. If it is used, FlowPin is automatically set to the enabled state to allow transmission of each character. This enabled state is determined by the polarity of the data specified by Mode.

Mode is used to specify the baud rate and operating parameters of the serial transfer. The low order 13 bits select the baud rate. Bit 13 selects parity or no parity. Bit 14 selects inverted or true level. Bit 15 is not used.

The baud rate bits specify the bit time in microseconds - 20. To find the value for a given baud rate, use the equation:

(1000000 / baud) - 20

Some standard baud rates are listed in the following table.

Baud Rate

Bits 0 - 12

300

3313

600

1646

1200

813

2400

396

4800

188

9600*

84

19200*

32

* Oscillator faster than 4MHz may be required.

Bit 13 selects even parity (bit 13 = 1) or no parity (bit 13 = 0). Normally, the serial transmissions are 8N1 (8 data bits, no parity and 1 stop bit). If parity is selected, the data is received as 7E1 (7 data bits, even parity and 1 stop bit).  

To receive odd parity instead of even parity, include the following DEFINE in the program:

	DEFINE SER2_ ODD 1

Bit 14 selects the level of the data and flow control pins. If bit 14 = 0, the data is received in true form for use with RS-232 drivers. If bit14 = 1, the data is received inverted. This mode can be used to avoid installing RS-232 drivers.

Some examples of Mode are: Mode = 84 (9600 baud, no parity, true), Mode = 16780 (2400 baud, no parity, inverted), Mode = 27889 (300 baud, even parity, inverted).  Appendix A shows more Mode examples.

If ParityLabel is included, this label will be jumped to if a character with bad parity is received. It should only be used if even parity is selected (bit 13 = 1).

An optional Timeout and Label may be included to allow the program to continue if a character is not received within a certain amount of time. Timeout is specified in units of 1 millisecond.  If the serial input pin stays idle during the Timeout time, the program will exit the SERIN2 command and jump to Label.

A DEFINE allows the use of data bits other than 8 (or 7 with parity). SER2_BITS data bits may range from 4 bits to 8 (the default if no DEFINE is specified). Enabling parity uses one of the number of bits specified.

Defining SER2_BITS to 9 allows 8 bits to be read and written along with a 9th parity bit.

With parity disabled (the default):

	DEFINE SER2_BITS 4	' Set Serin2 and Serout2 data bits to 4
	DEFINE SER2_BITS 5	' Set Serin2 and Serout2 data bits to 5
	DEFINE SER2_BITS 6	' Set Serin2 and Serout2 data bits to 6
	DEFINE SER2_BITS 7	' Set Serin2 and Serout2 data bits to 7
	DEFINE SER2_BITS 8	' Set Serin2 and Serout2 data bits to 8 (default)

With parity enabled:

	DEFINE SER2_BITS 5	' Set Serin2 and Serout2 data bits to 4
	DEFINE SER2_BITS 6	' Set Serin2 and Serout2 data bits to 5
	DEFINE SER2_BITS 7	' Set Serin2 and Serout2 data bits to 6
	DEFINE SER2_BITS 8	' Set Serin2 and Serout2 data bits to 7 (default)
	DEFINE SER2_BITS 9	' Set Serin2 and Serout2 data bits to 8

SERIN2 supports many different data modifiers which may be mixed and matched freely within a single SERIN2 statement to provide various input formatting.

Modifier

Operation

BIN{1..16}

Receive binary digits

DEC{1..5}

Receive decimal digits

HEX{1..4}

Receive hexadecimal digits

SKIP n

Skip n received characters

STR ArrayVar\n{\c}

Receive string of n characters optionally ended in character c

WAIT ( )

Wait for sequence of characters

WAITSTR ArrayVar{\n}

Wait for character string

 
1)         A variable preceded by BIN will receive the ASCII representation of its binary value. For example, if BIN B0 is specified and A1000" is received, B0 will be set to 8.
2) A variable preceded by DEC will receive the ASCII representation of its decimal value. For example, if DEC B0 is specified and A123" is received, B0 will be set to 123.
3) A variable preceded by HEX will receive the ASCII representation of its hexadecimal value. For example, if HEX B0 is specified and AFE" is received, B0 will be set to 254.
4) SKIP followed by a count will skip that many characters in the input stream. For example, SKIP 4 will skip 4 characters..
5) STR followed by a byte array variable, count and optional ending character will receive a string of characters. The string length is determined by the count or when the optional character is encountered in the input. 
6) The list of data items to be received may be preceded by one or more qualifiers between parenthesis after WAIT. SERIN2 must receive these bytes in exact order before receiving the data items. If any byte received does not match the next byte in the qualifier sequence, the qualification process starts over (i.e. the next received byte is compared to the first item in the qualifier list). A Qualifier can be a constant, variable or a string constant. Each character of a string is treated as an individual qualifier.
7) WAITSTR can be used as WAIT above to force SERIN2 to wait for a string of characters of an optional length before proceeding.

Once any WAIT or WAITSTR qualifiers are satisfied, SERIN2 begins storing data in the variables associated with each Item. If the variable name is used alone, the value of the received ASCII character is stored in the variable. If variable is preceded by BIN, DEC or HEX, then SERIN2 converts a binary, decimal or hexadecimal value in ASCII and stores the result in that variable. All non-digits received prior to the first digit of the decimal value are ignored and discarded. The non-digit character which terminates the value is also discarded.

BIN, DEC and HEX may be followed by a number. Normally, these modifiers receive as many digits as are in the input. However, if a number follows the modifier, SERIN2 will always receive that number of digits, skipping additional digits as necessary.

SERIN2 assumes a 4MHz oscillator when generating its bit timing. To maintain the proper baud rate timing with other oscillator values, be sure to DEFINE the OSC setting to the new oscillator value.

While single-chip RS-232 level converters are common and inexpensive, thanks to current RS-232 implementation and the excellent I/O specifications of the PICmicro, most applications don't require level converters. Rather, inverted TTL (Mode bit 14 = 1) can be used. A current limiting resistor is suggested (RS-232 is suppose to be short-tolerant).

SERIN2 is not supported on 12-bit core PICmicro MCUs due to RAM and stack constraints.

	' Wait until the character “A” is received serially on Pin1 and put next character into B0
	SERIN2 1,16780,[wait (“A”),B0]

	' Skip 2 chars and grab a 4 digit decimal number
	SERIN2 PORTA.1,84,[skip 2,dec4 B0]

	SERIN2 PORTA.1\PORTA.0,84,100,tlabel,[wait (“x”, b0), str ar]

 SEROUT

SEROUT Pin,Mode,[Item{,Item...}]

Sends one or more items to Pin in standard asynchronous format using 8 data bits, no parity and one stop (8N1). SEROUT is similar to the BS1 Serout command. Pin is automatically made an output. Pin may be a constant, 0 - 15, or a variable that contains a number 0 - 15 (e.g. B0) or a pin name (e.g. PORTA.0).

The Mode names (e.g. T2400) are defined in the file MODEDEFS.BAS. To use them, add the line:

Include "modedefs.bas"

to the top of the PICBASIC PRO™ program. BS1DEFS.BAS and BS2DEFS.BAS already includes MODEDEFS.BAS. Do not include it again if one of these files is already included. The Mode numbers may be used without including this file.

Mode

Mode No.

Baud Rate

State

T2400

0

2400

Driven True

T1200

1

1200

T9600

2

9600

T300

3

300

N2400

4

2400

Driven Inverted

N1200

5

1200

N9600

6

9600

N300

7

300

OT2400

8

2400

Open True*

OT1200

9

1200

OT9600

10

9600

OT300

11

300

ON2400

12

2400

Open Inverted*

ON1200

13

1200

ON9600

14

9600

ON300

15

300

* Open modes not supported on 12-bit core PicMicro MCUs.

SEROUT supports three different data types which may be mixed and matched freely within a single SEROUT statement.

1)      A string constant is output as a literal string of characters.
2) A numeric value (either a variable or a constant) will send the corresponding ASCII character. Most notably, 13 is carriage return and 10 is line feed.
3) A numeric value preceded by a pound sign ( # ) will send the ASCII representation of its decimal value. For example, if W0 = 123, then #W0 (or #123) will send '1', '2', '3'.

SEROUT assumes a 4MHz oscillator when generating its bit timing. To maintain the proper baud rate timing with other oscillator values, be sure to DEFINE the OSC setting to the new oscillator value.

In some cases, the transmission rates of SEROUT instructions may present characters too quickly to the receiving device. A DEFINE adds character pacing to the serial output transmissions. This allows additional time between the characters as they are transmitted. The character pacing DEFINE allows a delay of 1 to 65,535 microseconds (.001 to 65.535 milliseconds) between each character transmitted.

For example, to pause 1 millisecond between the transmission of each character:

DEFINE CHAR_PACING 1000

While single-chip RS-232 level converters are common and inexpensive, thanks to current RS-232 implementation and the excellent I/O specifications of the PICmicro, most applications don't require level converters. Rather, inverted TTL (N300..N9600) can be used. A current limiting resistor is suggested (RS-232 is suppose to be short-tolerant).

	SEROUT 0,N2400,[#B0,10]	' Send the ASCII value of B0 followed by 
					a linefeed out Pin0 serially

 SEROUT2

SEROUT2 DataPin{\FlowPin},Mode,{Pace,} {Timeout,Label,}[Item...]

Send one or more Items to DataPin in standard asynchronous format. SEROUT2 is similar to the BS2 Serout command. DataPin is automatically made an output. The optional FlowPin is automatically made an input. DataPin and FlowPin may be a constant, 0 - 15, or a variable that contains a number 0 - 15 (e.g. B0) or a pin name (e.g. PORTA.0).

The optional flow control pin, FlowPin, may be included to help keep data from overrunning the receiver. If it is used, the serial data will not be sent until FlowPin is in the proper state. This state is determined by the polarity of the data specified by Mode.

An optional Timeout and Label may be included to allow the program to continue if the FlowPin does not change to the enabled state within a certain amount of time. Timeout is specified in units of 1 millisecond.  If FlowPin stays disabled during the Timeout time, the program will exit the SEROUT2 command and jump to Label.

In some cases, the transmission rates of SEROUT2 instructions may present characters too quickly to the receiving device. It may not be desirable to use an extra pin for flow control. An optional Pace can be used to add character pacing to the serial output transmissions. This allows additional time between the characters as they are transmitted. The character pacing allows a delay of 1 to 65,535 milliseconds between each character transmitted.

Mode is used to specify the baud rate and operating parameters of the serial transfer. The low order 13 bits select the baud rate. Bit 13 selects parity or no parity. Bit 14 selects inverted or true level. Bit 15 selects whether it is driven or open.

The baud rate bits specify the bit time in microseconds - 20. To find the value for a given baud rate, use the equation:

(1000000 / baud) - 20

Some standard baud rates are listed in the following table.

Baud Rate

Bits 0 - 12

300

3313

600

1646

1200

813

2400

396

4800

188

9600*

84

19200*

32

* Oscillator faster than 4MHz may be required.

Bit 13 selects even parity (bit 13 = 1) or no parity (bit 13 = 0). Normally, the serial transmissions are 8N1 (8 data bits, no parity and 1 stop bit). If parity is selected, the data is sent as 7E1 (7 data bits, even parity and 1 stop bit).  To transmit odd parity instead of even parity, include the following DEFINE in the program:

DEFINE SER2_ ODD 1

Bit 14 selects the level of the data and flow control pins. If bit 14 = 0, the data is sent in true form for use with RS-232 drivers. If bit14 = 1, the data is sent inverted. This mode can be used to avoid installing RS-232 drivers.

Bit 15 selects whether the data pin is always driven (bit 15 = 0), or is open in one of the states (bit 15 = 1). The open mode can be used to chain several devices together on the same serial bus.

Some examples of Mode are: Mode = 84 (9600 baud, no parity, true, always driven), Mode = 16780 (2400 baud, no parity, inverted, driven), Mode = 60657 (300 baud, even parity, inverted, open).   Appendix A shows more Mode examples.

A DEFINE allows the use of data bits other than 8 (or 7 with parity). SER2_BITS data bits may range from 4 bits to 8 (the default if no DEFINE is specified). Enabling parity uses one of the number of bits specified.

Defining SER2_BITS to 9 allows 8 bits to be read and written along with a 9th parity bit.

With parity disabled (the default):

	DEFINE SER2_BITS 4	' Set Serin2 and Serout2 data bits to 4
	DEFINE SER2_BITS 5	' Set Serin2 and Serout2 data bits to 5
	DEFINE SER2_BITS 6	' Set Serin2 and Serout2 data bits to 6
	DEFINE SER2_BITS 7	' Set Serin2 and Serout2 data bits to 7
	DEFINE SER2_BITS 8	' Set Serin2 and Serout2 data bits to 8 (default)

With parity enabled:

	DEFINE SER2_BITS 5	' Set Serin2 and Serout2 data bits to 4
	DEFINE SER2_BITS 6	' Set Serin2 and Serout2 data bits to 5
	DEFINE SER2_BITS 7	' Set Serin2 and Serout2 data bits to 6
	DEFINE SER2_BITS 8	' Set Serin2 and Serout2 data bits to 7 (default)
	DEFINE SER2_BITS 9	' Set Serin2 and Serout2 data bits to 8

SEROUT2 supports many different data modifiers which may be mixed and matched freely within a single SEROUT2 statement to provide various output formatting.

Modifier

Operation

{I}{S}BIN{1..16}

Send binary digits

{I}{S}DEC{1..5}

Send decimal digits

{I}{S}HEX{1..4}

Send hexadecimal digits

REP c\n

Send character c repeated n times

STR ArrayVar{\n}

Send string of n characters

 

1)       A string constant is output as a literal string of characters.
2) A numeric value (either a variable or a constant) will send the corresponding ASCII character. Most notably, 13 is carriage return and 10 is line feed.
3) A numeric value preceded by BIN will send the ASCII representation of its binary value. For example, if B0 = 8, then BIN B0 (or BIN 8) will send "1000".
4) A numeric value preceded by DEC will send the ASCII representation of its decimal value. For example, if B0 = 123, then DEC B0 (or DEC 123) will send "123".
5) A numeric value preceded by HEX will send the ASCII representation of its hexadecimal value. For example, if B0 = 254, then HEX B0 (or HEX 254) will send "FE".
6) REP followed by a character and count will repeat the character, count time. For example, REP A0"\4 will send "0000".
7) STR followed by a byte array variable and optional count will send a string of characters. The string length is determined by the count or when a 0 character is encountered in the string.

BIN, DEC and HEX may be preceded or followed by several optional parameters. If any of them are preceded by an I (for indicated), the output will be preceded by either a A%@, A#@ or A$@ to indicate the following value is binary, decimal or hexadecimal.

If any are preceded by an S (for signed), the output will be sent preceded by a A-A if the high order bit of the data is set. This allows the transmission of negative numbers. Keep in mind that all of the math and comparisons in PBP are unsigned. However, unsigned math can yield signed results. For example, take the case of B0 = 9 - 10. The result of DEC B0 would be A255". Sending SDEC B0 would give A-1" since the high order bit is sent. So with a little trickery, the unsigned math of PBP can yield signed results.

BIN, DEC and HEX may also be followed by a number. Normally, these modifiers display exactly as many digits as are necessary, zero blanked (leading zeros are not sent). However, if a number follows the modifier, SEROUT2 will always send that number of digits, adding leading zeros as necessary. It will also trim of any extra high order digits. For example, BIN6 8 would send A001000" and BIN2 8 would send A00".

Any or all of the modifier combinations may be used at once. For example, ISDEC4 B0.

SEROUT2 assumes a 4MHz oscillator when generating its bit timing. To maintain the proper baud rate timing with other oscillator values, be sure to DEFINE the OSC setting to the new oscillator value.  An oscillator speed faster than 4MHz may be required for reliable operation at 9600 baud and above.

While single-chip RS-232 level converters are common and inexpensive, thanks to current RS-232 implementation and the excellent I/O specifications of the PICmicro, most applications don't require level converters. Rather, inverted TTL (Mode bit 14 = 1) can be used. A current limiting resistor is suggested (RS-232 is suppose to be short-tolerant).

SEROUT2 is not supported on 12-bit core PICmicro MCUs due to RAM and stack constraints.

	' Send the ASCII value of B0 followed by a linefeed out Pin0 serially at 2400 baud 
	SEROUT2 0,16780,[dec B0,10]

	' Send “B0 =” followed by the binary value of B0 out PORTA pin 1 serially at 9600 baud 
	SEROUT2 PORTA.1,84,[“B0=”, ihex4 B0]
<<Previous

Next>>