At 08:51 AM 09/02/1997 -0500, you wrote: >I hav been trying to get reliable serial communications on a 16C57 for >a while now, an almost have it working. The problem is that it doesnt >seem to get the first 3 bytes ok, they are always the same relative to >the data but wrong. > >Here is what I should receive: > > 36h 32h 30h 32h 30h 38h > The rest is ok. > This is what I get: > > 4dh 4ch 4ch 99h 98h 38h > Sounds like a jitter problem. Once you detect your start bit edge, you need to wait until you are approximately in the middle of the next bit so a total delay of 1.5 bit time is needed. Actually not quite 1.5 bit time but a little less. How much? Depends on when the start bit occurred and when you spotted it. For example: 2400 baud is about 416.6666 uSec per bit. Assuming you detect the start bit within 1 uSec of it occuring you need to delay 208 uSec, then read start bit again. If not there any more than you have false triggering so go back to waiting for a start bit. If start bit is still there then delay 416 uSec into the next bit position read first data bit. Then delay 416 again for each bit until done. This way you are always in the middle of a data bit. Now if, worst case you miss the start bit by 50 uSec then subtract half that from your initial delay. ie: delay about 175 uSec. from start bit edge and after that back to 416 uSec. Note that you lose .666666666 uSec each loop so over one char you'll be out by 6 uSec. If your data has no inter character delay where you can resync on a new start bit this could accumulate and become a problem but not usually at 1200 baud. That is how the UARTs do it. Start bit edge begins the sequence. 8 clocks of the 16x baud clock are counted and the start bit is verified. After that 16 clocks are counted. You can do the whole thing under interrupts on the PIC by choosing a Baud rate crystal. ie: one that divides nicely by 16 (or 8) into the baud clock. For example. If you produce an interrupt every 52 uSec (8x bit rate of 2400 baud) and check your serial line you can see a start bit within 1/8th of a bit time. Set a counter to ignore 4 of those interrupts and then on the fifth interrupt check it again (or is it fourth???? whatever...). If start bit still there then set the counter to ignore the next 8 interrupts and when the 8th arrives shift in your first bit. Wait 8 interrupts again until you've accumulated your last bit. Then wait 12 to get past stop bit. If input line is still active after 12 interrupts you are into another start bit or you have an over run error. You could disable these interrupts when you don't want to receive characters. Just an idea. Regards, John Pioneers are the ones, face down in the mud, with arrows in their backs. Automation Artisans Inc. Ph. 1-250-544-4950 PO Box 20002 Fax 1-250-544-4954 Sidney, BC CANADA V8L 5C9