HSERSETUP baud_setup, mode

Baud_setup - is a variable/constant which specifies the baud rate:

  B300_X where X =
  B600_X 4 for 4MHz
  B1200_X 8 for 8MHZ
  B2400_X 16 for 16MHz
  B4800_X 20 for 20MHZ
  B9600_X 32 for 32MHz
  B19200_X 40 for 40MHz
  B31250_X 64 for 64MHz

Mode - is a variable/constant whose bits specify special functions (not all features are supported on all chips) :

bit0 - background receive serial data to the scratchpad (not M2 parts)
bit1 - invert serial output data (0 = ‘T’, 1 = “N”)
bit 2 - invert serial input data (0 = “T”, 1 = “N”)
bit 3 - disable hserout (1 = hserout pin normal i/o)
bit 4 - disable hserin (1 = hserin pin normal i/o)


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.


When bit1 is 0, the serial output polarity is 'True' which is the 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 and most likely the appropriate setting when directly connecting to devices which are described as having a 'UART serial' connection.

When bit1 is 1, the serial output polarity is 'Inverted' which is the same as an 'Nxxx' baud rate in the 'serout' command. In this state the pin idles low and pulses high. This is the state normally used with modules such as the AXE033 and AXE133 serial LCD displays and for direct 'resistor' connection to a PC. This is usually the appropriate setting when connecting to devices which are described as having an 'RS232 serial' connection.

On some PICAXE 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.

Applies To:
08M2, 14M2, 18M2, 20M2, 28X1, 28X2, 40X1, 40X2
See Also:
Related Create:

Read EEPROM data

Transfer the values stored in EEPROM location 0 to 63 to a serial LCD at 2400 baud

Code Example:
	hsersetup B9600_4, %10	; 9600 baud, inverted TXD

main:	for b0 = 0 to 63	; start a loop
	  read b0,b1		; read value into b1
	  hserout 0,(b1)	; transmit value to serial LCD
	next b0			; next loop
Copy Code Submit an Example

Submit Your Own Code!

You must be logged in to submit code examples. Login now.


  • Login to leave a comment.