There has been a lot of confusion on how the RX or TX pins should be set with reguard to TRIS on the PICs with hardware USARTs especially with reguard to makeing them work in interrupt mode.
First, keep in mind that MPLAB does not (correctly or otherwise) simulate the USART Hardware.
Second, read the datasheets.
From DS30292B-page 33 (the '873 datasheet section 3.3 PORTC and TRISC Registers):
When enabling peripheral functions, care should be taken in defining TRIS bits for each PORTC pin. Some peripherals override the TRIS bit to make a pin an output, while other peripherals override the TRIS bit to make a pin an input. Since the TRIS bit override is in effect while the peripheral is enabled, read-modify-write instructions (BSF, BCF, XORWF) with TRISC as destination should be avoided. The user should refer to the corresponding peripheral section for the correct TRIS bit settings.
From DS30292B-page 95 (the '873 datasheet section 10.0n on USART):
Bit SPEN (RCSTA<7>) and bits TRISC<7:6> have to be set in order to configure pins RC6/TX/CK and RC7/RX/DT as the Universal Synchronous Asynchro-nous Receiver Transmitter.
In Microchip lingo "set" means make the bit a 1.
The bottom line, don't do read-modify-write operations on a TRIS register that is controlling pins with another active peripheral, such as the TRISC register when using the UART or CCP.
Finally, PICUART.ZIP from http://redrival.com/mcgahee/ or http://mcgahee.freeservers.com is known to work and explains most of the common problems.
Samples:
rs232@ TTL to/from RS232@ TI Calc@ PIC C IO@
See also:
"Serial PIC'n : PIC Microcontroller Serial Communications" by David Benson |
Comments:
Code:
In the clock, UART, etc. VP's, the assumption is that the processor is running at 50 Mhz. The constants that are used are: int_period = 163 ;period between interrupts ; tick_lo = 80 ;50000 = msec instruction count tick_hi = 195 ; for 50MHz, turbo, prescaler=1 To convert these for use at 4 Mhz, change the constants to: int_period = 104 ;period between interrupts ; tick_lo = 160 ;4000 = msec instruction count tick_hi = 15 ; for 4MHz, turbo, prescaler=1 That's all you need for the clock/timer VP's. For the UART VP, here are the settings for 2400 and 1200 BPS. (at 4Mhz you can't go higher than 2400, and I didn't bother to figure out anything lower than 1200) ; *** 2400 baud (at 50MHZ) ;baud_bit = 7 ;for 2400 baud ;start_delay = 128+64+1 ; " " " ;int_period = 163 ; " " " ; ; *** 2400 baud (at 4MHZ) baud_bit = 4 ;for 2400 baud start_delay = 16+8+1 ; ' ' ' int_period = 104 ; ' ' ' ; ; *** 1200 baud (at 4MHZ) ;baud_bit = 5 ;for 1200 baud ;start_delay = 32+16+1 ; ' ' ' ;int_period = 104 ; ' ' ' I have tested the 2400 baud settings and it works. I've not tested the 1200 baud settings but the math follows and they should work. Note that the time allowed in the ISR is significantly shorter with these settings. If you need more time in the ISR you could use 204 for int_period and adjust everything else accordingly. William Phelps