On Fri, 6 Jun 1997 09:30:11 -0700 Mik O Kim writes: [...] >Serial port: > Does anyone know where I can get a serial port routine that works >on >interrupt basis? I want the PIC to perform computation while the data >is >being received (sort of multitasking). I have a sample serial code >from >Microchip, but it's not "multitasking". Please send me the source; I >already know I should use port B state change interrupt, but I'm not >sure >how I would implement this. Thanks. I assume you want to use a PIC that doesn't have serial hardware. There really isn't one standard best way to build a background serial port. The port B change interrupt works in a way that makes it really only useful to wake the PIC up from sleep. The other 2 interrupts on a base model PIC16CXX, the B.0/INT interrupt and the timer0 interrupt are useful for serial reception. For slower bit rates, set the timer interrupt to interrupt at a multiple of the baud rate (at least 4, preferably 8 or 16), and use samples of the input line to drive a state machine receiver. The main program would poll a flag which is set by the ISR when data has been received. At higher bit rates, constant interrupts at a multiple of the baud rate can take too much time. Consider using the INT interrupt to interrupt on the leading edge of the start bit. The interrupt service will start the timer to interrupt again at a 1x baud rate to sample the data near the center of each bit. Or, if data comes in infrequently, the INT service could just use a software timer to sample the rest of the bits. There are a lot of different ways to go with this, it depends on your application. The PIC16C5x has no interrupts at all. However it is possible to synchronize the program to the timer (do a little processing, wait for timer to overflow, sample serial pin, do a little more processing, etc), and effectively use the multiple of bit rate "timer interrupt". The processing of the main program has to be broken up into sections that are guaranteed to take less time than it takes for the timer to overflow. > >Single Transistor RS 232 Receive > > +5 > | > \ > /10k > \ > / > |-----------TTL Sig out > | / > 10k |/ >RS232 IN---/\/\/---|\ NPN (2N2222 or 2N3904) > | \| > | > | > GND > It is a Good Idea to put a diode from the base of the transistor to ground (cathode of diode to base) so the transistor isn't damaged by large negative Vbe when the input line is negative. Most transistors are reated for only 5 to 7V reverse Vbe. An even simpler RS-232 Receiver: 100K RS232IN ----\/\/\/---- PIC input pin (other than A4/RTCC) This doesn't invert the signal so using it to generate an active-low INT on a start bit or with serial hardware that expects the input pin to idle high won't work. If an inverted signal is required, a CMOS inverter such as 74C04 or 4069 with a current-limiting resistor at the input will work exactly like the single-transistor receiver but with fewer parts and less current draw. 100K - 1M |\ RS232IN -----/\/\/\----| o----- TTL out |/ The resistor should be placed right near the CMOS input so the high impedance node doesn't have much chance to pick up noise. Another resistor to pull the RS232 line to ground is good if the input is likely to be disconnected while the receiver is running. > Now the question is, is there such a solution for other direction >(TTL >in, RS232 out)? I looked at prices for MAXIM single supply transceiver >devices and they are EXPENSIVE! Many RS-232 devices will accept 0 to 5V levels, including the single transistor receiver above and most circuits using the 1489. There are no guarantees though. About the only thing in the PC world I've had trouble doing this with is the original IBM "ASYNC CARD" which used a TI chip of some sort for a receiver. The major problem with full RS-232 compliance is getting the negative voltage. It can be obtained from an idling output line of a fully-compliant device. The same goes for the more than 5V positive voltage. This two-transistor amplifier can pull a line up to a high positive voltage and also allow it to swing below ground: + 12V |-------| \ / R (10K or so) e \| \ PNP (2N3906) |-----| c /| | / | "RS-232" Out--| \ \ c \| NPN (2N3904) RL / |---- +5V (logic VDD) \ /| | e / 10K | |---/\/\/\----- PIC output -12V When the PIC software sets the pin low, the NPN transistor will conduct, turning on the PNP transistor and pulling the output line up to near the positive voltage. When the PIC pin is high or three-state off, none of the transistors conduct so the power consumption of the circuit in standby is zero. The size of RL depends on the current capability of the + and -12V sources and the amount of output drive needed. Usually 4.7K is OK. The circuit can be expanded to include an active pull-down but I've done enough ASCII schematic for today. The 1488 or 14C88 driver chip is not very expensive if + and - voltages can be obtained for it. The C88 is much preferred for using parasitic power. Op-amp outputs can also drive RS-232. The original RS-232 specification was written with this in mind. -Mike