Mauricio Culibrk wrote: > In one of my applications, I need a serial RX/TX routine capable > to 'recognize' the speed of the serial line, so it can adjust > itself to that speed (eg. I have a '84 based 'host' to which I > want to attach few 'terminals' working at 1200, 2400, 4800, 9600 > baud). I meant to reply earlier and got busy. Sorry. There are both synchronous and asynchronous serial communications. With sync serial, a separate wire provides a clock transition in the middle of each data bit. No such thing as baud rate detecting. I'll assume you mean _async_ serial comm. With asynchronous serial, the data is sandwiched in between a start bit and a stop bit. Normally, there are 8 data bits between as in the following diagram: 0 marking !---:''':''':''':''':''':''':''':''': ! : : : : : : : : : 1 spacing ---! :...:...:...:...:...:...:...:...:---: Start 0 1 2 3 4 5 6 7 Stop RS232 specified the physical properties of the interface. This includes voltages. At the transmitter, logic 0 or "spacing" is +5 to +25V while logic 1 or "marking" is -5 to -25V. Marking and spacing are terms that date back to Teletype days. RS232 receiver must accept -25 to -3 or +3 to +25. Range between -3 and +3V is supposed to be used for hysteresis to reject noise. Lots of receiver chips do NOT handle the -3V to +3V range correctly. Their rejection band is about 1/2 a volt somewhere around 0V. Note that the transmitter side minimum output is specified as a higher voltage than the minimum specified at the receiver to allow resistive drop in the wire. Also, it's a 1-wire voltage based interface, so you need a ground wire. And be carefull of current flows between the equipment on the ground wire -- particularly if the two ends are located some distance from each other. Normally an async RS232 interface is idle and marking (-5 to -25V). When a character is transmitted, the start bit guarantees a low to high transition (from marking to spacing). The stop bit is used to guarantee that (irrespective of the data) in a continuous stream of characters, the line will be driven back down to the marking state before the next start bit is sent. When you see the leading edge of a start bit, you should wait 1/2 bit time and then sample again. If the line is back at marking, then you ignore it as noise. This helps reject noise pulses of up to 1/2 bit time. The "baud rate" is the number of bits per second when the line is running continuously. The difference between bits per second and baud rate is that baud rate may vary from 0 bps to maximum. Thus the baud rate is the correct term for an async line while BPS (bits per second) is correct for a sync line. [Misuse of this term is a pet peeve of mine.] So for 1200 baud, each bit is 1/1200 second wide. The data bits are sent LEAST significant bit first. Originally, Teletypes used 7 data bits(*). The 8th bit (data bit 7 above) was used for a parity bit. With inclusion of the parity bit, if you counted up all the 1's data bits, you got an odd number (for odd parity) or an even number (even parity). Now the 8 data bits are all used for sending 1 octet (aka byte) from the computer. For example, an ASCII J (capital J, dec 74, octal 112, hex 4A) is: 0 marking !---+---! !---! !---+---! !---! ! ! ! ! ! ! ! ! 1 spacing ---! !---! !---! !---! !--- Start Stop ASCII J (4A hex) = 0 1 0 1 0 0 1 0 While, an ASCII A (capital A, dec 65, octal 101, hex 41) is: 0 marking !---! !---+---+---+---+---! !---! ! ! ! ! ! ! 1 spacing ---! !---! !---! !--- Start Stop ASCII A (41 hex) = 1 0 0 0 0 0 1 0 Now a more direct reply to your question. I assumed you wanted to sense baud rate because you weren't in control of both sides of the communications link. And at power up, the other end might have changed and be at an unknown rate. And I assume that you are NOT using a built-in USART (like in the PIC 16c65 or 16C74). So I'm assuming you have a 1 bit input watching the received async data line. You can look at the start bit width to determine baud rate -- ASSUMING the first data character cooperates. Note that the example of A above allows this, the J does not (result would be off by a factor of 2). As a cross-check, you use the measured bit time to check for a stop bit at the expected location. If the stop bit is not at a marking state, then you have a framing error or data overrun. Maybe it was noise or whatever, but you have to try again. So you can auto-sense baud rate if the low order bit of the first character is a 1 (50% probability). Luckily, common characters that people use to "wake up" a system are all odd (i.e. have a low order bit of 1) like carriage return (13 dec, 0D hex), escape (27 dec, 1B hex), or control-C (03 hex). This also shows why, without knowing ahead of time what the first character is, you cannot auto-detect baud rate without the possibility of loosing a character. Hope this long discussion wasn't too boring and is helpfull. Lee Jones (*) Way, way back the Teletypes used 5 data bits, but I'll ignore that for this discussion. Early Teletypes at 110 baud also used 2 stop bits -- but everybody uses 1 stop bit now. ------------------------------------------------------------------- Jones Computer Communications lee@frumble.claremont.edu 509 Black Hills Dr, Claremont, CA 91711 voice: 909-621-9008 -------------------------------------------------------------------