|
AN207 |
PicStic 1-4
(2K Version)
|
|
Using
the Hardware Serial Port with PicBasic Pro |
December 13,
2001
|
|
|
|
|
How it works: In
the configurations shown in Figures A and B the transmit enable is
connected to PortB.0. By using the port pin to control the transmit enable
the PicStic is capable of RS232A, RS485 and RS422 communications. Before
any serial communication can take place, the transmitter needs to be
enabled by setting PortB.0 high. PortB.0 is tied to the transmit enable
pin of the 485 driver to allow for the wide communication protocols. There
needs to be a brief pause of 1µSec to allow the transmitter time to
enable 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 300bps and 2400bps, there is no need
to change the RCSTA* or TXSTA* registers. Baud rates from 1200Kbps to
19.2Kbps can be reached by defining the TXSTA* register as 24H. The HSER_BAUD also needs to be defined as one of the
following: 1202 for
1200Kbps 2403 for
2400Kbps 9615 for
9600Kbps 19231 for 19.2Kbps
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 PIC16F62XÔ Data Sheet or contact Micromint Inc..
Figure A
Figure B |
|
Program
Listing: The
following program illustrates how to use the HSERIN command with the
hardware serial port. Examples of the HEX, BIN, and DEC modifiers are also
shown. Define
HSER_TXSTA 24H
‘ Set TXSTA to allow for higher baud rate Define
HSER_BAUD
56000
‘ Set baud rate to 56Kbps INPUT
VAR
WORD
‘
Define INPUT as a word sized variable START:
HIGH PORTB.0
PAUSE 1
HSEROUT ["ENTER A DECIMAL NUMBER BETWEEN 0 – 65535"
,10,13]
HSERIN [DEC5 INPUT]
HSEROUT [“HERE IS THE HEX VALUE OF THE NUMBER ENTERED”, HEX
INPUT,10,13]
HSEROUT ["ENTER A HEX NUMBER BETWEEN 0 – FFFFH" ,10,13]
HSERIN [HEX4 INPUT]
HSEROUT [“HERE IS THE BINARY VALUE OF THE NUMBER ENTERED”, BIN
INPUT,10,13]
PAUSE 1000
GOTO START
END |