<<Previous

Next>>

 HSERIN

HSERIN {ParityLabel,}{Timeout,Label,}[Item{,...}]

Receive one or more Items from the hardware serial port on devices that support asynchronous serial communications in hardware.

HSERIN is one of several built-in asynchronous serial functions. It can only be used with devices that have a hardware USART. See the device data sheet for information on the serial input pin and other parameters. The serial parameters and baud rate are specified using DEFINEs:

	' Set receive register to receiver enabled
	DEFINE HSER_RCSTA	90h

	' Set transmit register to transmitter enabled
	DEFINE HSER_TXSTA	20h

	' Set baud rate
	DEFINE HSER_BAUD	2400

	' Set SPBRG directly (normally set by HSER_BAUD)
	DEFINE HSER_SPBRG	25

HSER_RCSTA, HSER_TXSTA and HSER_SPBRG simply set each respective PICmicro MCU register, RCSTA, TXSTA and SPBRG to the hexadecimal value DEFINEd. See the Microchip data sheet for the device for more information on each of these registers.

The TXSTA register BRGH bit (bit 2) controls the high speed mode for the baud rate generator. Certain baud rates at certain oscillator speeds require this bit to be set to operate properly. To do this, set HSER_TXSTA to 24h instead of 20h. See the Microchip data sheet for the hardware serial port baud rate tables and additional information.

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

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 1 millisecond units.  If the serial input pin stays idle during the Timeout time, the program will exit the HSERIN command and jump to Label.

The serial data format defaults to 8N1, 8 data bits, no parity bit and 1 stop bit. 7E1 (7 data bits, even parity, 1 stop bit) or 7O1 (7data bits, odd parity, 1 stop bit) can be enabled using one of the following DEFINEs:

	' Use only if even parity desired
	DEFINE HSER_EVEN	1

	' Use only if odd parity desired		
	DEFINE HSER_ODD	1

The parity setting, along with all of the other HSER DEFINEs, affect both HERIN and HSEROUT.

An optional ParityLabel may be included in the statement. The program will continue at this location if a character with a parity error is received. It should only be used if parity is enabled using one of the preceding DEFINEs.

As the hardware serial port only has a 2 byte input buffer, it can easily overflow if characters are not read from it often enough. When this happens, the USART stops accepting new characters and needs to be reset. This overflow error can be reset by toggling the CREN bit in the RCSTA register. A DEFINE can be used to automatically clear this error. However, you will not know that an error has occurred and characters may have been lost.

    DEFINE HSER_ CLROERR    1

To manually clear an overrun error:

    RCSTA. 4 = 0 
    RCSTA. 4 = 1

Since the serial reception is done in hardware, it is not possible to set the levels to an inverted state to eliminate an RS-232 driver. Therefore a suitable driver should be used with HSERIN.

On devices with 2 serial ports, HSERIN will only access the first port. The second port should be set up and read by accessing the registers directly.  Or a DEFINE may be added to tell HSERIN to use the second serial port instead of the first:

DEFINE HSER_PORT 2

HSERIN supports the same data modifiers that SERIN2 does. Refer to the section on SERIN2 for this information.

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

HSERIN [B0, dec W1]

 

 HSEROUT

HSEROUT [Item{,Item...}]

Send one or more Items to the hardware serial port on devices that support asynchronous serial communications in hardware.

HSEROUT is one of several built-in asynchronous serial functions. It can only be used with devices that have a hardware USART. See the device data sheet for information on the serial output pin and other parameters. The serial parameters and baud rate are specified using DEFINEs:

	' Set receive register to receiver enabled
	DEFINE HSER_RCSTA	90h

	' Set transmit register to transmitter enabled
	DEFINE HSER_TXSTA	20h

	' Set baud rate
	DEFINE HSER_BAUD	2400

	' Set SPBRG directly (normally set by HSER_BAUD)
	DEFINE HSER_SPBRG	25

HSER_ RCSTA, HSER_ TXSTA and HSER_ SPBRG simply set each respective PICmicro MCU register, RCSTA, TXSTA and SPBRG to the hexadecimal value DEFINEd. See the Microchip data sheet for the device for more information on each of these registers.

The TXSTA register BRGH bit (bit 2) controls the high speed mode for the baud rate generator. Certain baud rates at certain oscillator speeds require this bit to be set to operate properly. To do this, set HSER_ TXSTA to 24h instead of 20h. See the Microchip data sheet for the hardware serial port baud rate tables and additional information.

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

The serial data format defaults to 8N1, 8 data bits, no parity bit and 1 stop bit. 7E1 (7 data bits, even parity, 1 stop bit) or 7O1 (7data bits, odd parity, 1 stop bit) can be enabled using one of the following DEFINEs:

	' Use only if even parity desired
	DEFINE HSER_EVEN	1

	' Use only if odd parity desired
	DEFINE HSER_ODD	1

The parity setting, along with all of the other HSER DEFINEs, affect both HERIN and HSEROUT.

Since the serial transmission is done in hardware, it is not possible to set the levels to an inverted state to eliminate an RS-232 driver. Therefore a suitable driver should be used with HSEROUT.

On devices with 2 serial ports, HSEROUT will only access the first port. The second port should be set up and read by accessing the registers directly. Or a DEFINE may be added to tell HSEROUT to use the second serial port instead of the first:

DEFINE HSER_PORT 2

HSEROUT supports the same data modifiers that SEROUT2 does. Refer to the section on SEROUT2 for this information.

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

	' Send the decimal value of B0 followed by a linefeed out the hardware USART
	HSEROUT [dec B0,10]
<<Previous

Next>>