the Hardware Serial Port with PicBasic Pro
Introduction: This application note will illustrate how to utilize the hardware serial port available on the PicStic 5.
PicStic 5 utilizes a pair of communication line drivers to allow several
types of serial communication. RS-232A, RS-422, and RS-485 protocols can
be used to communicate to the PicStic 5. With an 8MHz-system clock the
PicStic 5 has the ability to communicate at baud rates up to 250Kbps.
Background: The PicStic 5 utilizes a pair of communication line drivers to allow several types of serial communication. RS-232A, RS-422, and RS-485 protocols can be used to communicate to the PicStic 5. With an 8MHz-system clock the PicStic 5 has the ability to communicate at baud rates up to 250Kbps.
How it works: Upon power-up, the PicStic 5 is not capable of using the serial port. Before serial communication can take place, the transmitter needs to be enabled. Setting bit 1 of Port C will enable the transmitter of the serial drivers. There needs to be a brief pause of 1µSec before any serial communication can transpire.
PicBasic Pro provides serial commands that read from (HSERIN) and write to (HSEROUT) the hardware serial port. These commands have several parameters that can be modified to create the required data format. These parameters default to the following data format; 8N1@2400bps (8 data bits, no parity, and 1 stop bit). To change the data format simply change the parameter by changing one or more of the following definitions (NOTE: When changing the definitions listed below note that each definition is fully capitalized. A compiler error will be generated if the definitions are not capitalized.);
DEFINE HSER_RCSTA 90H ‘ Set receive status and control register*
DEFINE HSER_TXSTA 20H ‘ Set transmit status and control register*
DEFINE HSER_BAUD 2400 ‘ It is only necessary to set one of these parameters.
DEFINE HSER_SPBRG 25 ‘ When one is set the other is automatically set.
DEFINE HSER_EVEN 1 ‘ Only odd or even should be defined at a time
DEFINE HSER_ODD 1 ‘ Only odd or even should be defined at a time
For 8N1 communications between 1200bps and 9600bps, there is no need to change the RCSTA* or TXSTA* registers. If baud rates above 9600 are required then the TXSTA* register will need to be defined as 23H. By setting the TXSTA* register to 24H baud rates from 9600bps to 250Kbps will be available (except 115.2K).
PicBasic Pro supports several data modifiers, which may be mixed and matched freely within a single HSERIN OR HSEROUT command to provide for various input/output formatting. For a complete list of the modifiers available with the HSERIN and HSEROUT commands please refer to the PicBasic Pro programmers reference manual.
*For detailed descriptions of the RCSTA and TXSTA registers please refer to the Microchip PIC16F87XÔ Data Sheet or contact Micromint Inc..
following program illustrates how to use the HSERIN command with the
hardware serial port. Examples of the HEX, BIN, and DEC modifiers are also
HSEROUT ["ENTER A DECIMAL NUMBER BETWEEN 0 – 65535"
HSERIN [DEC5 INPUT]
HSEROUT [“HERE IS THE HEX VALUE OF THE NUMBER ENTERED”, HEX
HSEROUT ["ENTER A HEX NUMBER BETWEEN 0 – FFFFH" ,10,13]
HSERIN [HEX4 INPUT]
HSEROUT [“HERE IS THE BINARY VALUE OF THE NUMBER ENTERED”, BIN