Configure the hardware serial port for serial operation.
The hsersetup command is used to configure the fixed hardware serial port of the microcontroller. It configures two pins to be dedicated to hserin and hserout. Both pins are affected unless the bits 3 and 4 of 'mode' are used to disable one of the functions.
The baud rate is configured by the baud_setup value. This is a number that sets the baud rate. For convenience a number of predefined values are predefined (e.g. B9600_4 for baud rate of 9600,n,8,1 at 4MHz operation). However other baud rates can also be calculated by the formula provided later in this section.
Hardware serial input can be configured in two ways:
1) via hserin command only (mode bit0 = 0).
2) automatic in the background (mode bit0 = 1) (not M2 parts).
In automatic background mode the hardware serial input is fully automated. Serial data received by the hardware pin is saved into the scratchpad memory area as soon as it is received. Upon the hsersetup command the serial pointer (hserptr) is reset to 0. When a byte is received it is saved to this scratchpad address, the hserptr variable is incremented and the hserinflag flag is set (must be cleared by user software). Therefore the value 'hserptr -1' indicates the last byte written, and 'hserinflag = 1' indicates a byte has been received (see also the setintflags command). The scratchpad is a circular buffer that overflows without warning.
Polarity
When bit1 is 0, the serial output polarity is 'True' which is same as a 'Txxx' baud rate in the 'serout' command. In this state the pin idles high and pulses low. This is the state normally used with a MAX232 type inverter for computer connection.
When bit1 is 1, the serial output polarity is 'Inverted' which is same as a 'Nxxx' baud rate in the 'serout' command. In this state the pin idles low and pulses high. This is the state normally used with third party devices (e.g. an AXE033 serial LCD) or director 'resistor' connection to a PC.
On some parts the hardware serial input polarity is always true, it cannot be inverted (ie bit 2 serial input inversion only applies to X2 parts). This is a limitation of the internal microcontroller structure. Therefore a MAX232 type inverter is required for computer connections.
Advanced Technical Information
Users may choose to create their own 'baud_setup' setting for a specific desired baud rate. 'baud_setup' must be a word value, and can be calculated from the following equation (where 'n' is the baud_setup value):
Desired baud rate = Fosc / (4 (n + 1) )
So n = (( Fosc / baud rate ) / 4 ) - 1
Calculation example:
As an example, the ODB2 protocol requires the non-standard 10400 baud.
So if Fosc (resonator frequency) is 4MHz, and a desired baud rate of 10400
n = ((4 000 000 / 10400) / 4 ) - 1 = 95 (rounded).
Working the other way around to check the calculation, the exact actual baud rate at baud_setup value of 95 will be
Baud rate = 4000 000 / (4 (95+1)) = 10416, which is close enough for most systems!
Therefore the command uses 95 as the baud_value to set a baud rate of 10400 at 4MHz.