PIC to RS-232 starts up bad, finishes good

I have a PIC16F88 and a MAX232 connected to my machine's serial port. (The MAX232 is connected as configured in Maxim's data sheet, with five

1uF caps in place.) The program is a test program for the 16F628's UART that I modified slightly for the 16F88 (corrected the pin numbers, set the internal oscillator correctly) and its own UART.

The program does two things. First, it outputs a greeting message to the port. Then, it echoes everything it receives. As it's set up now, it performs the second part flawlessly, but the greeting message never comes out correctly...in fact, it comes out differently each time; there's no correspondence of the wrong characters to the right ones.

It seems as if there's just some sort of startup issue that might need to be handled in hardware, but I'm not sure what. So, before I started on any software-based workaround I wanted to ask if anyone had any ideas regarding fixing this The Right Way.

Thanks PSM

Reply to
Peter S. May
Loading thread data ...

"Peter S. May" schreef in bericht news:3q2dnWDHHamTX1PbnZ2dnUVZ snipped-for-privacy@comcast.com...

Read it several times, not clear what does what. So assuming the PIC sends the welcome message to the PC, most likely the reception on the PC side causes the trouble. What program runs on the PC side?

petrus bitbyter

Reply to
petrus bitbyter

Try a delay after you initialise everything, but before you send anything out the uart. The caps in on the max232 will need some time to charge up and stabilise etc. Have a look in the data sheet or just put in a looooong delay (a second or so) to test the idea out. If you have one, have a look with a CRO at the line side outgoing signal and you may see what is going on.

Reply to
Robbo

Seems perfectly obvious now that I think about it. I was so caught up in trying to figure out what was going wrong with the PIC itself that this didn't hit me. Duh... Anyway, I'm gonna try this as soon as I get home.

I still wish I had an oscilloscope. :-/ A couple more months and it may work its way into the budget.

Thanks PSM

Reply to
Peter S. May

Sounds like you are crashing the transmit buffer maybe. Be sure the first character of your welcome message is in the TX shift register before loading the next byte into the transmit holding register. Incoming messages would automatically pace themselves that's why your echo test works but your initial welcome doesn't (possibly.......)

-Jeff

Reply to
wiz

On my boards, I put in a one-byte delay right after reset to make sure that the next edge the serial port sees is interpreted as a start bit. Somtimes, the reset itself throws garbage out the serial port lines.

Reply to
DJ Delorie

That was my guess. Make sure the TRMT bit of the TXSTA register is set (1) before you throw the next character at it. When doing the echo thing, the bytes are coming in at a rate that allows the buffer to empty before it gets filled again.

Jim

Reply to
James Beck

Hi, Peter.

It can be because the receiver isn't sync'ing on the first start bit and takes a few bytes or a pause in the bitstream for it to find which start-like transition is the real start bit.

I've got a MAX232 wired to an AVR Mega128 and I've found that a 3ms delay after configuring the USART (before enabling its interrupts) causes the first character to be properly recognized. I haven't chased it further to see how tight this timing can get, or whether it's an issue with the MAX232 or the USART in the AVR.

I suspect it's less about whether the components are fully initialized, and more about the receiver being able to clearly distinguish the first start bit from any noise that may have been on the line as a result of power-up. I'd speculate that at 9600bps, 1ms (about 1 byte time) should be more than enough delay after power-on.

As FYI, you can have a similar anomaly if you repeat the same character back-to-back (e.g., as a heartbeat indicator) - if the receiver starts listening mid-stream, it doesn't know which transition is the start. Adding an occasional pause in the data stream fixes this.

Cheers, Richard

Reply to
Richard H.

During start up and lot of things happen. What I would do is use one of the outputs to kill the Serial out going to the PC, hold it in break for example at least one frame time. then release it. At which point the serial should be ready to send.

--
"I\'m never wrong, once i thought i was, but was mistaken"
Real Programmers Do things like this.
http://webpages.charter.net/jamie_5
Reply to
Jamie

Could it be that the PC is set to 2 stop bits? If your pic is sending flat out with 1 stop bit, that would be a problem...

Reply to
TT_Man

I've been caught this way several times before, the last time was this week.........

Reply to
Robbo

This program seems identical to the sample one supplied by Microchip for their PIC18F242, and I modified it for my purposes. I did not have the same problems you are experiencing, but one thing you might try is a trigger of some sort to perform the greeting output on command (a control character or a pin logic change). Also see if it echoes back a full string of characters correctly. I run my PIC at 57.6 kbps and no hardware control, and I rarely miss a character even with a continuous stream. The code for the 18F242 might be a little different, as it uses prioritiized interrupts, but it might be worth a try to look at it.

Paul

Reply to
Paul E. Schoen

On Fri, 24 Aug 2007 07:33:31 -0400 in sci.electronics.design, "Peter S. May" wrote,

How long does it take after power on, till the MAX232 has charged up its capacitors and is ready to drive RS232 voltages?

?
Reply to
David Harmon

So, the sample program I was handed apparently did have a delay in it already, but it was far too short...if I did the math correctly, it was on the order of 100ns. Bumping it up to even 1ms made it work consistently. Of course, due to my own paranoia and the non-time-critical nature of this beast, I'll probably bump it up to just shy of human-perceptible...which I'll have to determine experimentally, but from certain things I've read I would peg at somewhere around 20ms.

Thanks for the lightning flash that helped me realize the obvious... :-D PSM

Reply to
Peter S. May

With .1uF caps, about 125uS or so. With 1uF caps, maybe a little less than

1mS.
Reply to
Anthony Fremont

I reckon the only ones who don't get caught out a few times with this one are liars! Good to hear you got it working.

Reply to
Robbo

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.