HRsout, HRsout2, HRsout3, HRsout4, HRsout5
Syntax
HRsout Item {, Item... }
Overview
Transmit one or more Items from the hardware serial port on devices that contain a USART peripheral. If HRsout2, HRsout3, HRsout4, or HRsout5 are used, the device must contain that amount of USARTs.
Parameters
Item may be a constant, variable, expression, string list, or inline command. There are no operators as such, instead there are modifiers.
The modifiers are listed below: -
Modifier
Operation
At ypos,xpos Position the cursor on a serial LCD Cls Clear a serial LCD (also creates a 30ms delay)
Bin{1..32}
Send binary digits
Dec{0..10}
Send decimal digits (amount of digits after decimal point with floating point)
Hex{1..8}
Send hexadecimal digits
Sbin{1..32}
Send signed binary digits
Sdec{0..10}
Send signed decimal digits
Shex{1..8}
Send signed hexadecimal digits
Ibin{1..32}
Send binary digits with a preceding '%' identifier
Idec{0..10}
Send decimal digits with a preceding '#' identifier
Ihex{1..8}
Send hexadecimal digits with a preceding '$' identifier
ISbin{1..32}
Send signed binary digits with a preceding '%' identifier
ISdec{0..10}
Send signed decimal digits with a preceding '#' identifier
IShex{1..8}
Send signed hexadecimal digits with a preceding '$' identifier
Rep c\n
Send character c repeated n times
Str array\n
Send all or part of an array
Cstr cdata
Send string data defined in a Cdata statement.
The numbers after the Bin, Dec, and Hex modifiers are optional. If they are omitted, then the default is all the digits that make up the value will be displayed.
If a floating point variable is to be displayed, then the digits after the Dec modifier determine how many remainder digits are send. i.e. numbers after the decimal point.
Dim MyFloat as Float
MyFloat = 3.145
HRsout Dec2 MyFloat
' Send 2 digits after the decimal point
The above program will transmit the ASCII characters “3.14”
If the digit after the Dec modifier is omitted, then 3 digits will be displayed after the decimal point.
Dim MyFloat as Float
MyFloat = 3.1456
HRsout Dec MyFloat
' Send 3 digits after the decimal point
The above program will transmit the ASCII characters “3.145”
There is no need to use the Sdec modifier for signed floating point values, as the compiler's Dec modifier will automatically display a minus result: -
Dim MyFloat as Float
MyFloat = -3.1456
HRsout Dec MyFloat
' Send 3 digits after the decimal point
The above program will transmit the ASCII characters “-3.145”
Hex or Bin modifiers cannot be used with floating point values or variables.
The Xpos and Ypos values in the At modifier both start at 1. For example, to place the text "Hello World" on line 1, position 1, the code would be: -
HRsout At 1, 1, "Hello World"
Example 1
Device = 16F1829
Declare Xtal = 20 ' Tell the compiler the device will be operating at 20MHz
Declare Hserial_Baud = 9600
' Set Baud rate to 9600 for HRsin and HRsout
Dim bVar1 as Byte
Dim wWrd as Word
Dim dDwd as Dword
HRsout "Hello World"
' Display the text "Hello World"
HRsout "Var1= ", Dec bVar1 ' Display the decimal value of bVar1
HRsout "Var1= ", Hex bVar1 ' Display the hexadecimal value of bVar1
HRsout "Var1= ", Bin bVar1 ' Display the binary value of bVar1
HRsout "Dwd= ", Hex6 dDwd ' Display 6 hex characters of a Dword variable
Example 2
' Display a negative value on a serial LCD.
Symbol Negative = -200
HRsout At 1, 1, SDec Negative
Example 3
' Display a negative value on a serial LCD with a preceding identifier.
HRsout At 1, 1, ISHex -$1234
Example 3 will produce the text "$-1234" on the serial terminal.
Some microcontrollers have the ability to read and write to their own flash memory. And although writing to this memory too many times is unhealthy for the PICmicro™, reading this memory is both fast, and harmless. Which offers a unique form of data storage and retrieval, the Cdata command proves this, as it uses the mechanism of reading and storing in the device's flash memory. The Cstr modifier may be used in commands that deal with text processing i.e. Serout, HSerout, and Print etc.
The Cstr modifier is used in conjunction with the Cdata command. The Cdata command is used for initially creating the string of characters: -
String1: Cdata "Hello World", 0
The above line of case will create, in flash memory, the values that make up the ASCII text "Hello World", at address String1. Note the null terminator after the ASCII text.
null terminated means that a zero (null) is placed at the end of the string of ASCII characters to signal that the string has finished.
To display, or transmit this string of characters, the following command structure could be used:
HRsout Cstr String1
The label that declared the address where the list of Cdata values resided, now becomes the string's name. In a large program with lots of text formatting, this type of structure can save quite literally hundreds of bytes of valuable code space.
The Str modifier is used for sending a string of bytes from a Byte array variable. A string is a set of bytes sized values that are arranged or accessed in a certain order. The values 1, 2, 3 would be stored in a string with the value 1 first, followed by 2 then followed by the value 3. A Byte array is a similar concept to a string; it contains data that is arranged in a certain order. Each of the elements in an array is the same size. The string 1,2,3 would be stored in a Byte array containing three bytes (elements).
Below is an example that displays four bytes (from a Byte array): -
Dim bMyArray[10] as Byte = "Hello"
' Load the first 5 bytes of the array
HRsout Str bMyArray\5
' Display a 5-byte string.
Note that we use the optional \n argument of Str. If we didn't specify this, the PICmicro™ would try to keep sending characters until all 10 bytes of the array were transmitted. Since we do not wish all 10 bytes to be transmitted, we chose to tell it explicitly to only send the first 5 bytes.
Declares
There are several Declare directives for use with the HRsout commands. These are: -
Declare HRsout_Pin, HRsout2_Pin, HRsout3_Pin, HRsout4_Pin, or HRsout5_Pin =
Port.Pin For devices that have PPS (Peripheral Pin Select), the port and pin used for the TX lines must be given, so that the compiler can setup the PPS SFRs before the program starts. This may be any valid port on the microcontroller, but check the datasheet to see if the Port is valid for the peripheral.
Declare Hserial_Baud, Hserial2_Baud, Hserial3_Baud, Hserial4_Baud, or Hserial5_Baud
= Constant value Sets the Baud rate that will be used to transmit or receive a value serially. The Baud rate is calculated using the Xtal frequency declared in the program, and the compiler automatically sets up the required SFRs to work as close to that Baud required as possible.
Declare Hserial_Parity, Hserial2_Parity, Hserial3_Parity, Hserial4_Parity, or Hse-
rial5_Parity = Odd or Even
Enables/Disables parity on the serial port. For both HSerout and HSerin The default serial data format is 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) may be enabled using the Hserial_Parity declare.
Declare Hserial_Parity = Even
' Use if even parity desired
Declare Hserial_Parity = Odd
' Use if odd parity desired
Notes
The HRsout commands can only be used with devices that contain one, or more, hardware USARTs. See the specific device's data sheet for further information concerning the serial input pin as well as other relevant parameters.
Since the serial transmission is done in hardware, it is not possible to set the levels to an inverted state in order to eliminate an RS232 driver. Therefore a suitable driver should be used with HRsout. See HRsin for circuits.