I am using a PIC18F452, with a MAX232 and the HSEROUT function to communicate with my PC over a 3m long serial cable. My code is based on the famous post on this forum (many thanks!):
DEFINE OSC 4
TRISC = %10000000 'PORTC.7 is RX input PORTC.6 is TX output. Using internal USART and MAX232 to interface to PC
DEFINE HSER_RCSTA 90h 'Enable serial port. Enable continuous receive
define HSER_TXSTA 24h 'Enable transmit. BRGH=1
define HSER_SPBRG 12 '103 for baudrate of 2400 @ 4MHz (25 for 9600 @ 4MHz; 12 for 19200 @ 4MHz)
DEFINE HSER_CLOERR 1 'Automatic clear overrun error
'Alias definitions
RCIF VAR PIR1.5 'Receive interrupt flag (1=full , 0=empty)
TXIF VAR PIR1.4 'Transmit interrupt flag (1=empty, 0=full)
SerialData var byte 'Variable definition
pause 100 'Start-up delay
Main:
if RCIF then 'Incoming data?
hserin [Serialdata] 'Take it
hserout [serialdata] 'Send it
endif
goto main
This provides perfect 2-way comunications at 4MHz with any of the following settings:
'TXTA 24h with SPBRG 103 (gives 2400 baud)
'TXTA 24h with SPBRG 25 (gives 9600 baud)
'TXTA 24h with SPBRG 12 (gives 19200 baud)
.... but when I change the clock to 20MHz and alter the SPBRG value according to the datasheet, there is no comms at all. For example, although 19200 baud requires SPBRG to be 64, this produces nothing.
I'm desperate! Any ideas folks?
Bookmarks