On Wed, 24 Dec 1997 04:45:04 +0100 ZyLog writes: >First, i'v tryed to connect with a serie resistor straight to >Tx(tested: >82k,200k,382k) and a common Ground; noisy results. The results should not be "noisy". A resistor of 220K to 1M is suitable. The resistor should be connected near the PIC pin, without extra circuitry loading down the node between the resistor and PIC. It could be that the timing in your program is imprecise, this is causing errors which you attribute to "noise". >Now, i'v wired it with a MAX232; noisy results too. > > >I wanna do this in ISR (interrupt), like that I'm free of this in MAIN >program. > >I thought about few methods to do this : > >- I could set TMR0 to 0xFF, triggered by RA4/T0CLK input to make=20 > a T0IF(timer overflow), etc... > * Not good for my purpose, cause i need TMR0 for a base time. I think it would be possible to run the TIMR0 constantly at 1200 Hz, and use a RB0 interrupt from the start bit to synchronize (restart it in phase) with the start bit, so subsequent overflows are at the proper times to sample the data bits. After the last bit, it would be restarted again so that the next interrupt is in the proper phase to continue as it was before the start bit. Thus the phase of the base time would vary during reception, but the overall frequency would remain without error. Depending on what else the base time is used for this may be acceptable. > >- I could set interrupt on PORTB change. > * Not recommended.(I heared this, near here) Don't do this. The RB on change interrupt is only suitable for restarting the PIC from sleep. It should not be used while the PIC is awake. > >- I could sample the Rx pin. > * Good, but wasted time to sample each bit, as many it was >super-sampled.(3,4 times ?) It appears you want to set the timer for 4800 Hz anyway, thus it is well-suited to this method. >- And i could set RB0/INT for interrupt on transition, and calculate > time between each of them. > * This is my choice ... > > >I want to implement a classic 8,N,1 @ 1200 >(1bit start, 8bit data lsb/msb, no parity, 1bit stop, at 1200 baud.) > >May be i'm in wrong, for this case it's, > > start stop (hope ASCII drawing good.) >1______ _______________ _____ > |_|_|_|_|_|_|_|_|_|_| >0 1 2 3 4 5 6 7 8 This is wrong. The stop bit is a logic 1. The signal drawn above (except for the stop bit) is what would be received after an inverting line receiver (MAX232, etc). If you connect the RS-232 line directly (via resistor) to the PIC, then start bits are high, zero data bits are high, and 1 data bits are low. >(1) set first for falling edge interrupt(INTEDG=3D0) > >(2) interrupt(INTF) > - next INTF on rising edge(INTEDG=3D1) > - at least(+/- 1.2% ?), 10*(1/1200)/second later must come a >rising >edge.(STOP bit) > No, the routine must have a timeout. If, for example, the data sent is 0xFF, the start bit is the only pulse: ____ _______________ _ IIIIS01234567KIIIIII I = line idle, S = Start bit, 0-7 = data bit, K = stop bit. If it is some other pattern of the form 11111000, etc. the result is similar, the start bit and the zero data bits form a longer single pulse. Although the general principle described of interrupting on transistions and computing the number and polarity of bits received based on the time is sound, there needs to be a little time-based processing to decide when the byte is complete if it doesn't have a final transition (MSB is 1). Or if some error occurs. Note that triggering on transitions is rather sensitive to noise. If the transitions don't occur cleanly and singly, multiple interrupts could occur which could require a lot of processing. At 1200 baud, it's usually not impractical to set the timer interrupt at a multiple of the baud rate and use an oversampling type of receiver as part of the timer ISR... >The wanted result(?) is 4 times the baud rate, ~4800 Hz > >4194304/4 /1 /218=3D 4809 int/s, then Reload value =3D (256-218) =3D >38=20 If possible, use a crystal that works out exactly, this prevents needing to reload the timer at all. Not reloading saves processing time and the possible (though predictable) error from the prescaler clearing and the timer stopping briefly after each reload. For example, 4.9152 MHz and prescale of 1 (no prescale) gives 4800 overflows per second.