Problems interfacing PIC UART to PC Serial Port

Started by zerang shah March 10, 2004
Hello,

I am working on a design that requires interfacing a PIC
microcontroller to the rs232 serial port of a PC. I am using the UART
of the PIC16F628 but I am having problems: I can not get my PC to
recognize the serial data sent by my PIC.

For testing purposes I hooked up a palm pilot to my PC serial port and
tried to echo characters using Hyper Terminal - it worked fine.
However I can not get Hyper Terminal to echo the characters my PIC
sends no matter how hard I try.

I have been trying to debug this for several days, even to the point
of hooking up a logic analyzer to the PIC. However I still do not
understand why I can not communicate with my PC serial port out of the
box: RS232 is RS232 is RS232 right?

I'm sure I have all the connections correct, TX on the pic to RX on
the serial port with common ground. I can even get two PICs to
comunicate with each other with the UART!!

Someone please help, I'm losing my mind here trying to debug this.

-- ZS
ninjak@gmx.de (zerang shah) wrote in 
news:4d6c559c.0403101702.5fe63fc7@posting.google.com:
> I am working on a design that requires interfacing a PIC > microcontroller to the rs232 serial port of a PC. I am using the UART > of the PIC16F628 but I am having problems: I can not get my PC to > recognize the serial data sent by my PIC.
do you use level converters on the pic side? rs232 is not using TTL levels...
> For testing purposes I hooked up a palm pilot to my PC serial port and > tried to echo characters using Hyper Terminal - it worked fine.
i usualy test the terminal program by shortcutting pins 2 and 3 :-)
> However I can not get Hyper Terminal to echo the characters my PIC > sends no matter how hard I try.
but i have made bad experiences with hyper terminal, i'll never use it again
> I have been trying to debug this for several days, even to the point > of hooking up a logic analyzer to the PIC. However I still do not > understand why I can not communicate with my PC serial port out of the > box: RS232 is RS232 is RS232 right?
almost... there can be subtle differences, like output voltages can vary from +/- 3V to 25V, desktop PCs often with +/- 12V, PDAs often lower, e.g 6V, ...
> I'm sure I have all the connections correct, TX on the pic to RX on > the serial port with common ground. I can even get two PICs to > comunicate with each other with the UART!!
you dont tell much about the circuit, but i'd guess you dont use a level converter for RS232. there are simple circuits that do not produce valid RS232 levels but most PC can decode 0/3V signals on the input too. e.g. http://chaokhun.kmitl.ac.th/~kswichit/ap275/ap275.htm however, idle on the data lines, RS232 side, is _low_, while the CPU usualy is idle high. so you need at least an inverter or invert the output pin (most uarts cant, but if you write a software uart it possible, i use a 1k series resitor and a Timer_A uart on a MSP430 to talk to the PC :-) chris -- GCC for MSP430: http://mspgcc.sf.net Chris <cliechti@gmx.net>
zerang shah wrote:
> Hello, > > I am working on a design that requires interfacing a PIC > microcontroller to the rs232 serial port of a PC. I am using the UART > of the PIC16F628 but I am having problems: I can not get my PC to > recognize the serial data sent by my PIC. > > For testing purposes I hooked up a palm pilot to my PC serial port and > tried to echo characters using Hyper Terminal - it worked fine. > However I can not get Hyper Terminal to echo the characters my PIC > sends no matter how hard I try. > > I have been trying to debug this for several days, even to the point > of hooking up a logic analyzer to the PIC. However I still do not > understand why I can not communicate with my PC serial port out of the > box: RS232 is RS232 is RS232 right? > > I'm sure I have all the connections correct, TX on the pic to RX on > the serial port with common ground. I can even get two PICs to > comunicate with each other with the UART!! > > Someone please help, I'm losing my mind here trying to debug this. > > -- ZS
Assuming you have matched the baud rate, character length, parity, stop bits, RS-232 voltage levels (they're not necessarily just 0 and 5 volts, although you can often get away with it, but they are inverted relative to the outputs that the PIC is producing) and Tx/Rx connections there's just one other possibility, which is the control signals, CTS, DSR and DCD. The PC (which is always acting as DTE) will expect these to be set correctly. You can fake it by feeding back the PC's own DTR and RTS signals. The following article shows how. You'll need the 3-wire interface as shown in the upper right corner of Fig. 1, where your PIC acts as the DCE side. http://www.elektor-electronics.co.uk/miniproj/download/020370uk.pdf -- Joe Legris
"zerang shah" <ninjak@gmx.de> wrote in message
news:4d6c559c.0403101702.5fe63fc7@posting.google.com...
> I'm sure I have all the connections correct, TX on the pic to RX on > the serial port with common ground. I can even get two PICs to > comunicate with each other with the UART!!
Where's the RS-232 driver chip? It brings the voltage levels up to the RS-232 levels AND it inverts the signal. Meindert
"Joe Legris" <jalegris@xympatico.ca> wrote in message
news:404FC8C1.6020503@xympatico.ca...
> zerang shah wrote: > > Hello, > > > > I am working on a design that requires interfacing a PIC > > microcontroller to the rs232 serial port of a PC. I am using the UART > > of the PIC16F628 but I am having problems: I can not get my PC to > > recognize the serial data sent by my PIC. > > > > For testing purposes I hooked up a palm pilot to my PC serial port and > > tried to echo characters using Hyper Terminal - it worked fine. > > However I can not get Hyper Terminal to echo the characters my PIC > > sends no matter how hard I try. > > > > I have been trying to debug this for several days, even to the point > > of hooking up a logic analyzer to the PIC. However I still do not > > understand why I can not communicate with my PC serial port out of the > > box: RS232 is RS232 is RS232 right? > > > > I'm sure I have all the connections correct, TX on the pic to RX on > > the serial port with common ground. I can even get two PICs to > > comunicate with each other with the UART!! > > > > Someone please help, I'm losing my mind here trying to debug this. > > > > -- ZS > > Assuming you have matched the baud rate, character length, parity, stop > bits, RS-232 voltage levels (they're not necessarily just 0 and 5 volts, > although you can often get away with it, but they are inverted relative > to the outputs that the PIC is producing) and Tx/Rx connections there's
That's definitely the first thing to check here.
> just one other possibility, which is the control signals, CTS, DSR and > DCD. The PC (which is always acting as DTE) will expect these to be set > correctly. You can fake it by feeding back the PC's own DTR and RTS
signals.
>
These signals are only needed if you use a brain-dead PC program like HyperTerminal (or are intentionally using hardware handshaking for some other reason). Get a real terminal program (google for "Tera Term Pro" - 10 minutes downloading could easily save you several more days debugging).
> The following article shows how. You'll need the 3-wire interface as > shown in the upper right corner of Fig. 1, where your PIC acts as the > DCE side. > > http://www.elektor-electronics.co.uk/miniproj/download/020370uk.pdf > > -- > Joe Legris >
The other replies about voltage levels are probably spot on.

Once you get these bits right then I have just wrote these routines for a
16f628

My init code is as follows.

  bsf STATUS,RP0
  movlw b'00100100'
  movwf TXSTA & 7fh
  movlw 129
  movwf SPBRG & 7fh
  bcf STATUS,RP0
  movlw b'10010000'
  movwf RCSTA

This is for 9600,n,8,1

My receive code is

        btfss PIR1,RCIF
         goto NoReceive
         movfw RCREG

HTH

Mike.


On 10 Mar 2004 17:02:12 -0800, zerang shah wrote:

>I'm sure I have all the connections correct, TX on the pic to RX on >the serial port with common ground. I can even get two PICs to >comunicate with each other with the UART!!
Simply, a PIC doesn't do serial I/O at RS-232 signal levels. It uses logic levels (0V = low, Vdd = high). You use a level converter (such as a Maxim MAX232) to convert between logic and RS-232 levels. http://www.maxim-ic.com/quick_view2.cfm?qv_pk=1798&ln= A serial line idles in a "marking" condition, which is logic high, or -10V on the RS-232 line. A logic low corresponds to +10V on the RS-232 line. Note that the +/-10V levels are what is called for by the RS-232 spec, but they're often not very precise, and levels of +/-5V are not uncommon. Obviously, a PIC can talk directly to another PIC at logic levels, without using RS-232 level converters at all. -- Max
On Thu, 11 Mar 2004 09:12:32 +0100, David Brown wrote:

>> just one other possibility, which is the control signals, CTS, DSR and >> DCD. The PC (which is always acting as DTE) will expect these to be set >> correctly. You can fake it by feeding back the PC's own DTR and RTS >signals. >> > >These signals are only needed if you use a brain-dead PC program like >HyperTerminal (or are intentionally using hardware handshaking for some >other reason).
HyperTerminal doesn't need to use hardware handshaking. It can use XON/OFF codes or no flow-control at all, if that's what you want.
>Get a real terminal program (google for "Tera Term Pro" - 10 >minutes downloading could easily save you several more days debugging).
Why, if all that's required is a simple terminal emulator? HyperTerminal is perfectly adequate for everyday use, and there's no apparent need for anything exotic in this case. TeraTerm Pro isn't much better featured than HyperTerminal anyway. -- Max
"Max" <mtj2@btopenworld.com> wrote in message
news:bgq050t6qkmfaithdpac20fu68rqn61kkj@4ax.com...
> On Thu, 11 Mar 2004 09:12:32 +0100, David Brown wrote: > > >> just one other possibility, which is the control signals, CTS, DSR and > >> DCD. The PC (which is always acting as DTE) will expect these to be set > >> correctly. You can fake it by feeding back the PC's own DTR and RTS > >signals. > >> > > > >These signals are only needed if you use a brain-dead PC program like > >HyperTerminal (or are intentionally using hardware handshaking for some > >other reason). > > HyperTerminal doesn't need to use hardware handshaking. It can use > XON/OFF codes or no flow-control at all, if that's what you want. >
It can, sometimes - but I (and others) have had occasions when it just won't communicate at all, even with no flow control. Whether it is because of handshake issues or other problems, I don't know - it is simply not worth bothering about.
> >Get a real terminal program (google for "Tera Term Pro" - 10 > >minutes downloading could easily save you several more days debugging). > > Why, if all that's required is a simple terminal emulator? > HyperTerminal is perfectly adequate for everyday use, and there's no > apparent need for anything exotic in this case. > > TeraTerm Pro isn't much better featured than HyperTerminal anyway. >
Tera Term Pro is hardly exotic. It has features that HyperTerminal does not have (such as macros), and HyperTerminal has functionality that TTP does not (for easy access to bulliten boards and such like), but in practice TTP is faster and easier to use - it has all the functionality you need in most cases, without having to mess around trying to persuade HyperTerminal that you are not using a modem, and don't need an area code because you are not dialing, etc. I'm sure HyperTerminal is great for getting a modem connection to dinosaur servers, but when all you really want is a simple terminal connection on a direct serial link, then you are better with TTP (or many other programs - TTP is free and simple to use). When you want something with more features, at the cost of usability, then there are again many other choices that are far better than HyperTerminal - RealTerm is one option I've used.
> > HyperTerminal doesn't need to use hardware handshaking. It can use > > XON/OFF codes or no flow-control at all, if that's what you want.
Correct. But if you don't know that, it can take you all day before you find out that the default setting is 'hardware handshake on' and you wonder why Hyperterminal refuses to send characters to your device over three wires.....
> It can, sometimes - but I (and others) have had occasions when it just
won't
> communicate at all, even with no flow control. Whether it is because of > handshake issues or other problems, I don't know - it is simply not worth > bothering about.
Hyperterminal sometimes refuses to open a port. This happens when data is already coming on that port. When opened by hyperterminal, is sees overrun errors or so and erronously says "unable to open port"
> > TeraTerm Pro isn't much better featured than HyperTerminal anyway.
But a helluvalot more robust. The only drawback is that it only supports ports up to COM4 Meindert