Hserout, Hserout2, Hserout3, Hserout4
Syntax
Hserout [Item {, Item... }]
Overview
Transmit one or more Items from the USART on devices that support asynchronous serial communications in hardware.
Parameters
Item may be a constant, variable, expression, string list, or inline command. There are no operands as such, instead there are modifiers. For example, if an at sign'@' precedes an Item, the ASCII representation for each digit is transmitted.
The modifiers are listed below: -
| Modifier | Operation |
| Bin{1..32} | Send binary digits |
| Dec{1..10} | Send decimal digits |
| Hex{1..8} | Send hexadecimal digits |
| Sbin{1..32} | Send signed binary digits |
| Sdec{1..10} | Send signed decimal digits |
| Shex{1..8} | Send signed hexadecimal digits |
| Ibin{1..32} | Send binary digits with a preceding '%' identifier |
| Idec{1..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{1..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 Label | Send string data defined in code memory. |
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
Hserout [Dec2 MyFloat] ' Send 2 values after the decimal point
The above program will send 3.14
If the digit after the Dec modifier is omitted, then 3 values will be displayed after the decimal point.
Dim MyFloat as Float
MyFloat = 3.1456
Hserout [Dec MyFloat] ' Send 3 values after the decimal point
The above program will send 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
Hserout [Dec MyFloat] ' Send 3 values after the decimal point
The above program will transmit the ASCII representation of -3.145
Example
Device = 24FJ64GA002 ' Select the device to compile for
Declare Xtal = 16
Declare Hserial_Baud = 9600 ' USART1 Baud rate
Declare Hrsout1_Pin = PORTB.14 ' Select the pin for TX with USART1
Dim MyByte as Byte
Dim MyWord as Word
Dim MyDword as Dword
RPOR7 = 3 ' Make PPS Pin RP14 U1TX
Hserout ["Hello World"] ' Display the text "Hello World"
Hserout ["Var1= ", Dec MyByte] ' Display the decimal value of MyByte
Hserout ["Var1= ", Hex MyByte] ' Display the hexadecimal value of MyByte
Hserout ["Var1= ", Bin MyByte] ' Display the binary value of MyByte
' Display 6 hex characters of a Dword type variable
Hserout ["MyDword= ", Hex6 MyDword]
The Cstr modifier is used in conjunction with flash memory strings. The Dim as Flash8 directive is used for initially creating the string of characters: -
Dim String1 as Flash8 = "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:
Hserout [Cstr String1]
The label that declared the address where the list of code memory 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.
Try both these small programs, and you'll see that using Cstr saves a few bytes of code: -
First the standard way of displaying text: -
Device = 24FJ64GA002 ' Select the device to compile for
Declare Xtal = 16
Declare Hserial_Baud = 9600 ' USART1 Baud rate
Declare Hrsout1_Pin = PORTB.14 ' Select the pin for TX with USART1
RPOR7 = 3 ' Make PPS Pin RP14 U1TX
Hserout ["Hello World\r"]
Hserout ["How are you?\r"]
Hserout ["I am fine!\r"]
Now using the Cstr modifier: -
Dim Text1 as Flash8 = "Hello World", 0
Dim Text2 as Flash8 = "How are you?\r", 0
Dim Text3 as Flash8 = "I am fine!\r", 0
Hserout [Cstr Text1]
Hserout [Cstr Text2]
Hserout [Cstr Text3]
Again, note the null terminators after the ASCII text in the code memory strings. Without these, the device will continue to transmit data until a value 0 is reached.
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 MyArray[10] as Byte ' Create a 10 element byte array.
MyArray [0] = "H" ' Load the first 5 bytes of the array
MyArray [1] = "E" ' With the data to send
MyArray [2] = "L"
MyArray [3] = "L"
MyArray [4] = "O"
Hserout [Str MyArray\5] ' Display a 5-byte string.
Note that we use the optional \n argument of Str. If we didn't specify this, the microcontroller 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.
The above example may also be written as: -
Dim MyArray [10] as Byte ' Create a 10 element byte array.
Str MyArray = "Hello" ' Load the first 5 bytes of the array
Hserout [Str MyArray\5] ' Send 5-byte string.
The above example, has exactly the same function as the previous one. The only difference is that the string is now constructed using Str as a command instead of a modifier.
Declares
There are several Declare directives for use with the Hserout commands. These are the same declares as used by the HRsout commands.
Notes.
Hserout can only be used with devices that contain a hardware USART. See the specific device's data sheet for further information concerning the serial input pin as well as other relevant parameters.
See also : Declare, Rsin, Rsout, Serin, Serout, Hserin, Hserin.



