Some PIC microprocessors have a built-in serial interface module while others do not. The latter group includes the 16C54, ’55, ’56, ’70, ’71, ’72, and others. Adding a serial output to these processors takes but a few lines of code (Listing 1).
All of these PIC microprocessors execute one instruction cycle for every four clock cycles. Most assembly instructions complete in one instruction cycle, although a few (such as GOTO) take two cycles. The first step is to write the RS-232 output instructions. Then calculate their execution time, and finally, find the delay count needed to give the correct bit time based on clock frequency and baud rate.
In Listing 1, the first “block” of code (between START and GOTO DY1) sets up the 8-bit data length and generates one start bit. The delay count loaded into TEMP sets the bit duration. The second code block (between LOOP and GOTO LOOP) transmits the number in VALUE one bit at a time, least-significant bit first. Again, the delay count in TEMP sets the bit duration. The final code block generates one stop bit.
Clock frequency divided by baud rate should be at least 400 to get adequate baud-rate accuracy. As an example, the clock frequency should be 480 kHz minimum for 1200 baud. This gives an instruction cycle time of 8.33 ms. At 1200 baud, one bit time is 833.33 ms. Therefore, the first code block should use that amount of time to send the start bit. The first five instructions take 41.65 ms; 833.33 ms minus 41.65 ms equals 791.66 ms. Thus, the DY1 to GOTO DY1 loop must be repeated enough times to use up this interval.
The DECFSZ instruction uses one instruction cycle, the GOTO uses two; N = 791.66/3(8.33) = 31.68. This rounds up to 32 . If needed, a NOP will add a one instruction cycle delay. As long as the bit duration is within 2 or 3%, it’s close enough.
The Listing 1 routine was written to drive an inverting RS-232 driver. If you want to use this code without the inversion, then the BCF PORT_B,Tx instructions (clear Tx bit in register PORT_B) would be changed to BSF (to set the Tx bit) and vice versa. The port and Tx bit can be changed to any available I/O pin by consulting the appropriate PIC data sheet. This code can be embedded in a higher-language program such as “C” or Pascal. Merely define COUNT, TEMP, and VALUE as 8-bit integers and remove the memory variable definitions.
Listing 2 shows a simple MS-DOS BASIC program to test the serial output. It read 20 values from the serial data stream and displays them on the monitor screen. The RS-232 transmit routine operates in “broadcast” mode, that is, handshaking with the receiver isn’t used. A two-wire connection, signal, and common is all that’s required. However, you may have to connect DTR to DSR and RTS to CTS at the receiving end to get your PC serial port to respond.