Beyond Logic



Interfacing the Serial / RS-232 Port

Table of Contents

Part 1 : Hardware (PC's)
Hardware Properties
Serial Pinouts (D25 and D9 connectors)
Pin Functions
Null Modems
Loopback Plugs
DTE/DCE Speeds
Flow Control
The UART (8250's and Compatibles)
Type of UARTS (For PC's)
Part 2 : Serial Ports' Registers (PC's)
Port Addresses and IRQ's
Table of Registers
DLAB ?
Interrupt Enable Register (IER)
Interrupt Identification Register (IIR)
First In / First Out Control Register (FCR)
Line Control Register (LCR)
Modem Control Register (MCR)
Line Status Register (LSR)
Modem Status Register (MSR)
Scratch Register
Part 3 : Programming (PC's)
Polling or Interrupt Driven?
Source Code - Termpoll.c (Polling Version)
Source Code - Buff1024.c (ISR Version)
Interrupt Vectors
Interrupt Service Routine
UART Configuration
Main Routine (Loop)
Determining the type of UART via Software
Part 4 : External Hardware - Interfacing Methods
RS-232 Waveforms
RS-232 Level Converters
Making use of the Serial Format
8250 and compatible UART's
CDP6402, AY-5-1015 / D36402R-9 etc UARTs
Microcontrollers


Part 3 : Programming (PC's)

    Polling or Interrupt Driven?

    When writing a communications program you have two methods available to you. You can poll the UART, to see if any new data is available or you can set up an interrupt handler to remove the data from the UART when it generates a interrupt. Polling the UART is a lot slower method, which is very CPU intensive thus can only have a maximum speed of around 34.8 KBPS before you start losing data. Some newer Pentium Pro's may be able to achieve better rates that this. The other option is using a Interrupt handler, and that's what we have used here. It will very easily support 115.2K BPS, even on low end computers.

    Termpoll.c - Simple Terminal Program using the Polling Method.

    Polling the UART should not be dismissed totally. It's a good method for diagnostics. If you have no idea of what address your card is at or what IRQ you are using you can poll the UART at several different addresses to firstly find which port your card is at and which one your modem is attached to. Once you know this information, then you can set up the Interrupt routines for the common IRQs and by enabling one IRQ at a time using the Programmable Interrupt Controller you can find out your IRQ, You don't even need a screw driver!

    Buff1024.c - Simple Terminal Program using Interrupt Requests.

    Note: The source code above is not a really good example on how to program but is rather cut down to size giving quick results, and making it easier to understand. Upon executing your communications program, it would be wise to store the status of the UART registers, so that they all can be restored before you quit the program. This is to cause the least upset to other programs which may also be trying to use the communications ports.

    The first step to using interrupts is to work out which interrupt services your serial card. Table 13 shows the base addresses and IRQ's of some standard ports. IRQ's 3 and 4 are the two most commonly used. IRQ 5 and 7 are sometimes used.

    Interrupt Vectors

    Once we know the IRQ the next step is to find it's interrupt vector or software interrupt as some people may call it. Basically any 8086 processor has a set of 256 interrupt vectors numbered 0 to 255. Each of these vectors contains a 4 byte code which is an address of the Interrupt Service Routine (ISR). Fortunately C being a high level language, takes care of the addresses for us. All we have to know is the actual interrupt vector.

    INT (Hex)
    IRQ
    Common Uses
    08
    0
    System Timer
    09
    1
    Keyboard
    0A
    2
    Redirected
    0B
    3
    Serial Comms. COM2/COM4
    0C
    4
    Serial Comms. COM1/COM3
    0D
    5
    Reserved/Sound Card
    0E
    6
    Floppy Disk Controller
    0F
    7
    Parallel Comms.
    70
    8
    Real Time Clock
    71
    9
    Reserved
    72
    10
    Reserved
    73
    11
    Reserved
    74
    12
    PS/2 Mouse
    75
    13
    Maths Co-Processor
    76
    14
    Hard Disk Drive
    77
    15
    Reserved
    Table 14 : Interrupt Vectors (Hardware Only)

    The above table shows only the interrupts which are associated with IRQ's. The other 240 are of no interest to us when programming RS-232 type communications.

    For example if we were using COM3 which has a IRQ of 4, then the interrupt vector would be 0C in hex. Using C we would set up the vector using the instruction setvect(0x0C, PORT1INT); where PORT1INT would lead us to a set of instructions which would service the interrupt.

    However before we proceed with that I should say that it is wise to record the old vectors address and then restore that address once the program is finished. This is done using oldport1isr = getvect(INTVECT); where oldport1isr is defined using void interrupt (*oldport1isr)();

    Not only should you store the old vector addresses, but also the configuration the UART was in. Why you Ask? Well it's simple, I wrote a communications program which was fully featured in the chat side of things. It had line buffering, so no body could see my spelling mistakes or how slowly I typed. It included anti-bombing routines and the list goes on. However I couldn't be bothered to program any file transfer protocols such as Zmodem etc into my communications program. Therefore I either had to run my communications program in the background of Telemate using my communications program for chat and everything else it was designed for and using Telemate to download files. Another method was to run, say Smodem as a external protocol to my communications program.

    Doing this however would mean that my communications program would override the original speed, parity etc and then when I returned to the original communications program, everything stopped. Therefore by saving the old configuration, you can revert back to it before you hand the UART back over to the other program. Makes sense? However if you don't have any of these programs you can save yourself a few lines of code. This is what we have done here.

    Interrupt Service Routine (ISR)

    Now, could we be off track just a little? Yes that's right, PORT1INT is the label to our interrupt handler called a Interrupt Service Routine (ISR). You can put just about anything in here you want. However calling some DOS routines can be a problem.

    void interrupt PORT1INT()
    {
     int c;
     do { c = inportb(PORT1 + 5);
          if (c & 1) {
                      buffer[bufferin] = inportb(PORT1);
                      bufferin++;
                      if (bufferin == 1024) bufferin = 0;
                     } 
        } while (c & 1);
    outportb(0x20,0x20);
    }
    

    From the example above we check to see if there is a character to receive and if their is we remove it from the UART and place it in a buffer contained in memory. We keep on checking the UART, in case FIFO's are enabled, so we can get all data available at the time of interrupt.

    The last line contains the instruction outportb(0x20,0x20); which tells the Programmable Interrupt Controller that the interrupt has finished. The Programmable Interrupt Controller (PIC) is what we must go into now. All of the routines above, we have assumed that everything is set up ready to go. That is all the UART's registers are set correctly and that the Programmable Interrupt Controller is set.

    The Programmable Interrupt Controller handles hardware interrupts. Most PC's will have two of them located at different addresses. One handles IRQ's 0 to 7 and the other IRQ's 8 to 15. Mainly Serial communications interrupts reside on IRQ's under 7, thus PIC1 is used, which is located at 0020 Hex.

    Bit
    Disable IRQ
    Function
    7
    IRQ7
    Parallel Port
    6
    IRQ6
    Floppy Disk Controller
    5
    IRQ5
    Reserved/Sound Card
    4
    IRQ4
    Serial Port
    3
    IRQ3
    Serial Port
    2
    IRQ2
    PIC2
    1
    IRQ1
    Keyboard
    0
    IRQ0
    System Timer
    Table 15 : PIC1 Control Word (0x21)

    Multi-Comm ports are getting quite common, thus table 16 includes data for PIC2 which is located at 0xA0. PIC2 is responsible for IRQ's 8 to 15. It operates in exactly the same way than PIC1 except that EOI's (End of Interrupt) goes to port 0xA0 while the disabling (Masking) of IRQ's are done using port 0xA1.

    Bit
    Disable IRQ
    Function
    7
    IRQ15
    Reserved
    6
    IRQ14
    Hard Disk Drive
    5
    IRQ13
    Maths Co-Processor
    4
    IRQ12
    PS/2 Mouse
    3
    IRQ11
    Reserved
    2
    IRQ10
    Reserved
    1
    IRQ9
    IRQ2
    0
    IRQ8
    Real Time Clock
    Table 16 : PIC2 Control Word (0xA1)

    Most of the PIC's initiation is done by BIOS. All we have to worry about is two instructions. The first one is outportb(0x21,(inportb(0x21) & 0xEF); which selects which interrupts we want to Disable (Mask). So if we want to enable IRQ4 we would have to take 0x10 (16) from 0xFF (255) to come up with 0xEF (239). That means we want to disable IRQ's 7,6,5,3,2,1 and 0, thus enabling IRQ 4.

    But what happens if one of these IRQs are already enabled and then we come along and disable it? Therefore we input the value of the register and using the & function output the byte back to the register with our changes using the instruction outportb(0x21,(inportb(0x21) & 0xEF);. For example if IRQ5 is already enabled before we come along, it will enable both IRQ4 and IRQ5 so we don't make any changes which may affect other programs or TSR's.

    The other instruction is outportb(0x20,0x20); which signals an end of interrupt to the PIC. You use this command at the end of your interrupt service routine, so that interrupts of a lower priority will be accepted.

    UART Configuration

    Now we get to the UART settings (Finally)

    It's a good idea to turn off the interrupt generation on the UART as the first instruction. Therefore your initialization can't get interrupted by the UART. I've then chosen to set up our interrupt vectors at this point. The next step is to set the speed at which you wish to communicate at. If you remember the process, we have to set bit 7 (The DLAB) of the LCR so we can access the Divisor Latch High and Low Bytes. We have decided to set the speed to 38,400 Bits per second which should be find for 16450's and 16550's. This requires a divisor of 3, thus our divisor latch high byte will be 0x00 and a divisor latch low byte, 0x03.

    In today's standards the divisor low latch byte is rarely used but it still pays us to write 0x00 to the register just in case the program before us just happened to set the UART at a very very low speed. BIOS will normally set UARTs at 2400 BPS when the computer is first booted up which still doesn't require the Divisor Latch Low byte.

    The next step would be to turn off the Divisor latch access bit so we can get to the Interrupt Enable Register and the receiver/transmitter buffers. What we could do is just write a 0x00 to the register clearing it all, but considering we have to set up our word length, parity as so forth in the line control register we can do this at the same time. We have decided to set up 8 bits, no parity and 1 stop bit which is normally used today. Therefore we write 0x03 to the line control register which will also turn off the DLAB for us saving one more I/O instruction.

    The next line of code turns on the FIFO buffers. We have made the trigger level at 14 bytes, thus bits 6 and 7 are on. We have also enabled the FIFO's (bit 0). It's also good practice to clear out the FIFO buffers on initialization. This will remove any rubbish which the last program may of left in the FIFO buffers. Due to the fact that these two bits are self resetting, we don't have to go any further and turn off these bits. If my arithmetic is correct all these bits add up to 0xC7 or 199 for those people which still work in decimal.

    Then DTR, RTS and OUT 2 is taken active by the instruction outportb(PORT1 + 4,0x0B);. Some cards (Both of Mine) require OUT2 active for interrupt requests thus I'm normally always take it high. All that is left now is to set up our interrupts which has be deliberately left to last as to not interrupt our initialization. Our interrupt handler is only interested in new data being available so we have only set the UART to interrupt when data is received.

    Main Routine (Loop)

    Now we are left with,

    do {
        if (bufferin != bufferout){
                                   ch = buffer[bufferout];
                                   bufferout++;
                                   if (bufferout == 1024) bufferout = 0;
                                   printf("%c",ch);
                                  }
        if (kbhit()){
                     c = getch();
                     outportb(PORT1, c);
                    }
       } while (c !=27);
    

    which keeps repeating until c = 27. This occurs when the ESC key is hit.

    The next if statement checks to see if a key has been hit. (kbhit()) If so, it gets the character using the getch() statement and outputs it to the receiver buffer. The UART then transmits the character to the modem. What we have assumed here, is that the person using the Communications Program can't type as fast as the UART can send. However if the program wishes to send something, then a check should be made to see if BIT 5 of the Line Status Register is set before attempting to send a byte to the transmitter register.

    For more information on Interrupts, try "Interfacing the PC : Using Interrupts"

    Determining the type of UART via software


    The type of UART you have installed in your system can be determined without even needing a screwdriver in most cases. As you can see from Types of UART's each UART has minor differences, all we have to do it test these.

    The first procedure we do is to set bit 0 to '1' in the FIFO control register. This tries to enable the FIFO buffers. Then we read bits 6 and 7 from the interrupt identification register. If both bits are '1' then the FIFO buffers are enabled. This would mean the UART is a 16550a. If the FIFO's were enabled but not usable then it would be a 16550. If there is no FIFO buffer enabled it is most likely to be a 16450 UART, but could be a 8250, 8250A or 8250B on very old systems.

    AT's have a fast bus speed which the 8250 series of UART can't handle to well thus it is very unlikely to be found in any AT. However if you wish to test for them as well you can follow the same test as above to distinguish 16550's or 16550A's from the rest. If no FIFOs are enabled then a possible UART is the 16450, 8250, 8250A or 8250B. Once it is established the it could be one of these four chips, try writing a byte to the scratch register and then read it back and compare the results. If the results match then you must have a scratch register, if they don't you either don't have a scratch register, or it doesn't work to well.

    From the descriptions of the UART above if you read back your byte from the scratch register then the UART must be a 16450 or 8250A. (Both have scratch registers) If you don't read back your byte then it's either a 8250 or 8250B.

    The 16750 has 64 byte FIFO's, thus the easiest way to test for it's presence is to enable the 64 byte buffer using the FIFO Control Register and then read back the status of the Interrupt Identification Register. However I have never tested this.


Part 4 : Interfacing Devices to RS-232 Ports

    RS-232 Waveforms

    So far we have introduced RS-232 Communications in relation to the PC. RS-232 communication is asynchronous. That is a clock signal is not sent with the data. Each word is synchronized using it's start bit, and an internal clock on each side, keeps tabs on the timing.

    Serial Waveforms - Logic Levels
    Figure 4 : TTL/CMOS Serial Logic Waveform

    The diagram above, shows the expected waveform from the UART when using the common 8N1 format. 8N1 signifies 8 Data bits, No Parity and 1 Stop Bit. The RS-232 line, when idle is in the Mark State (Logic 1). A transmission starts with a start bit which is (Logic 0). Then each bit is sent down the line, one at a time. The LSB (Least Significant Bit) is sent first. A Stop Bit (Logic 1) is then appended to the signal to make up the transmission.

    The diagram, shows the next bit after the Stop Bit to be Logic 0. This must mean another word is following, and this is it's Start Bit. If there is no more data coming then the receive line will stay in it's idle state(logic 1). We have encountered something called a "Break" Signal. This is when the data line is held in a Logic 0 state for a time long enough to send an entire word. Therefore if you don't put the line back into an idle state, then the receiving end will interpret this as a break signal.

    The data sent using this method, is said to be framed. That is the data is framed between a Start and Stop Bit. Should the Stop Bit be received as a Logic 0, then a framing error will occur. This is common, when both sides are communicating at different speeds.

    The above diagram is only relevant for the signal immediately at the UART. RS-232 logic levels uses +3 to +25 volts to signify a "Space" (Logic 0) and -3 to -25 volts for a "Mark" (logic 1). Any voltage in between these regions (ie between +3 and -3 Volts) is undefined. Therefore this signal is put through a "RS-232 Level Converter". This is the signal present on the RS-232 Port of your computer, shown below.

    RS-232 Waveforms
    Figure 5 : RS-232 Logic Waveform

    The above waveform applies to the Transmit and Receive lines on the RS-232 port. These lines carry serial data, hence the name Serial Port. There are other lines on the RS-232 port which, in essence are Parallel lines. These lines (RTS, CTS, DCD, DSR, DTR, RTS and RI) are also at RS-232 Logic Levels.

    RS-232 Level Converters


    Almost all digital devices which we use require either TTL or CMOS logic levels. Therefore the first step to connecting a device to the RS-232 port is to transform the RS-232 levels back into 0 and 5 Volts. As we have already covered, this is done by RS-232 Level Converters.

    Two common RS-232 Level Converters are the 1488 RS-232 Driver and the 1489 RS-232 Receiver. Each package contains 4 inverters of the one type, either Drivers or Receivers. The driver requires two supply rails, +7.5 to +15v and -7.5 to -15v. As you could imagine this may pose a problem in many instances where only a single supply of +5V is present. However the advantages of these I.C's are they are cheap.

    Pinout for MAX-232 RS-232 Level Converter
    Above: (Figure 6) Pinouts for the MAX-232,
    RS-232 Driver/Receiver.


    Right: (Figure 7) Typical MAX-232 Circuit.

    Typical MAX-232 Circuit

    Another device is the MAX-232. It includes a Charge Pump, which generates +10V and -10V from a single 5v supply. This I.C. also includes two receivers and two transmitters in the same package. This is handy in many cases when you only want to use the Transmit and Receive data Lines. You don't need to use two chips, one for the receive line and one for the transmit. However all this convenience comes at a price, but compared with the price of designing a new power supply it is very cheap.

    There are also many variations of these devices. The large value of capacitors are not only bulky, but also expensive. Therefore other devices are available which use smaller capacitors and even some with inbuilt capacitors. (Note : Some MAX-232's can use 1 micro farad Capacitors). However the MAX-232 is the most common, and thus we will use this RS-232 Level Converter in our examples.

    Making use of the Serial Format


    In order to do anything useful with our Serially transmitted data, we must convert it back to Parallel. (You could connect an LED to the serial port and watch it flash if you really want too, but it's not extremely useful). This in the past has been done with the use of UART's. However with the popularity of cheap Microcontroller's, these can be more suited to many applications. We will look into the advantages and disadvantages of each method.

    8250 and Compatible UARTs

    We have already looked at one type of UART, the 8250 and compatibles found in your PC. These devices have configuration registers accessible via the data and address buses which have to be initialized before use. This is not a problem if your device which you are building uses a Microprocessor. However if you are making a stand alone device, how are you going to initialize it?

    Most Microprocessors / Microcontrollers these days can be brought with build-in Serial Communication Interfaces (SCI). Therefore there is little need to connect a 40 pin 16550 to, for example a 68HC11 when you can buy one built in. If you are still in love with the Z-80 or 8086 then an 16550 may be option! (or if you are like myself, the higher chip count the better. After all it looks more complicated and impressive! - But a headache to debug!)

    Pin Diagrams of UARTs - 16550, 16450 & 8250
    Figure 8 : Pin Diagrams for 16550, 16450 & 8250 UARTs

    Pin No.
    Name
    Notes
    Pin 1:8
    D0:D7
    Data Bus
    Pin 9
    RCLK
    Receiver Clock Input. The frequency of this input should equal the receivers baud rate x 16
    Pin 10
    RD
    Receive Data
    Pin 11
    TD
    Transmit Data
    Pin 12
    CS0
    Chip Select 0 - Active High
    Pin 13
    CS1
    Chip Select 1 - Active High
    Pin 14
    nCS2
    Chip Select 2 - Active Low
    Pin 15
    nBAUDOUT
    Baud Output - Output from Programmable Baud Rate Generator. Frequency = (Baud Rate x 16)
    Pin 16
    XIN
    External Crystal Input - Used for Baud Rate Generator Oscillator
    Pin 17
    XOUT
    External Crystal Output
    Pin 18
    nWR
    Write Line - Inverted
    Pin 19
    WR
    Write Line - Not Inverted
    Pin 20
    VSS
    Connected to Common Ground
    Pin 21
    RD
    Read Line - Inverted
    Pin 22
    nRD
    Read Line - Not Inverted
    Pin 23
    DDIS
    Driver Disable. This pin goes low when CPU is reading from UART. Can be connected to Bus Transceiver in case of high capacity data bus.
    Pin 24
    nTXRDY
    Transmit Ready
    Pin 25
    nADS
    Address Strobe. Used if signals are not stable during read or write cycle
    Pin 26
    A2
    Address Bit 2
    Pin 27
    A1
    Address Bit 1
    Pin 28
    A0
    Address Bit 0
    Pin 29
    nRXRDY
    Receive Ready
    Pin 30
    INTR
    Interrupt Output
    Pin 31
    nOUT2
    User Output 2
    Pin 32
    nRTS
    Request to Send
    Pin 33
    nDTR
    Data Terminal Ready
    Pin 34
    nOUT1
    User Output 1
    Pin 35
    MR
    Master Reset
    Pin 36
    nCTS
    Clear To Send
    Pin 37
    nDSR
    Data Set Ready
    Pin 38
    nDCD
    Data Carrier Detect
    Pin 39
    nRI
    Ring Indicator
    Pin 40
    VDD
    + 5 Volts
    Table 17 : Pin Assignments for 16550A UART

    For more information on the 16550 and compatible UART's see The UART (8250's and Compatibles) in the first part of this tutorial.

    CDP6402, AY-5-1015 / D36402R-9 etc UARTs


    Pinout for CDP6402, AY-5-1015 / D36402R-9 UARTs
    Figure 9 : Pinout of CDP6402

    There are UARTs such as the CDP6402, AY-5-1015 / D36402R-9 and compatibles. These differ from the 8250 and compatibles, by the fact that they have separate Receive and Transmit data buses and can be configured by connecting certain pins to various logic levels. These are ideal for applications where you don't have a Microprocessor available. Such an example is if you want to connect a ADC0804 (Analog to Digital Converter) to the UART, or want to connect a LCD Display to the Serial Line. These common devices use a 8 bit parallel data bus.

    The CDP6402's Control Register is made up of Parity Inhibit (PI), Stop Bit Select (SBS), Character Length Select (CLS1 and 2) and Even Parity Enable (EPE). These inputs can be latched using the Control Register Load (CRL) or if you tie this pin high, changes made to these pins will immediately take effect.

    Pin NumberAbbr.Full NameNotes
    Pin 1
    VDD
    + 5v Supply Rail 
    Pin 2
    NC
    Not Connected 
    Pin 3
    GND
    Ground 
    Pin 4
    RRD
    Receiver Register DisableWhen driven high, outputs RBR8:RBR1 are High Impedance.
    Pin 5:12
    RBR8:
    RBR1
    Receiver Buffer RegisterReceiver's Data Bus
    Pin 13
    PE
    Parity ErrorWhen High, A Parity Error Has Occurred.
    Pin 14
    FE
    Framing ErrorWhen High, A Framing Error Has Occurred. i.e. The Stop Bit was not a Logic 1.
    Pin 15
    OE
    Overrun ErrorWhen High, Data has been received but the nData Received Reset had not yet been activated.
    Pin 16
    SFD
    Status Flag DisableWhen High, Status Flag Outputs (PE, FE, OE, DR and TBRE) are High Impedance
    Pin 17
    RRC
    Receiver Register Clockx16 Clock input for the Receiver Register.
    Pin 18
    nDRR
    Data Received ResetActive Low. When low, sets Data received Output Low (i.e. Clears DR)
    Pin 19
    DR
    Data ReceivedWhen High, Data has been received and placed on outputs RBR8:RBR1.
    Pin 20
    RRI
    Receiver Register InRXD - Serial Input. Connect to Serial Port, Via RS-232 receiver.
    Pin 21
    MR
    Master ResetResets the UART. UART should be reset after applying power.
    Pin 22
    TBRE
    Transmitter Buffer Register EmptyWhen High, indicates that Transmitter Buffer Register is Empty, thus all bits including the stop bit have been sent.
    Pin 23
    nTBRL
    Transmitter Buffer Load / StrobeActive Low. When low, data present on TBR8:TBR1 is placed in Transmitter Buffer Register. A Low to High Transition on this pin, then sends the data.
    Pin 24
    TRE
    Transmitter Register EmptyWhen High, Transmitter Register is Empty, thus can accept another byte of data to be sent.
    Pin 25
    TRO
    Transmitter Register Out (TXD)TXD - Serial Output. Connect to Serial Port, Via RS-232 Transmitter.
    Pin 26:33
    TBR8:
    TBR1
    Transmitter Buffer RegisterData Bus, for Transmitter. Place Data here which you want to send.
    Pin 34
    CRL
    Control Register LoadWhen High, Control Register (PI, SBS, CLS2, CLS1, EPE) is Loaded. Can be tied high, so changes on these pins occur instantaneously.
    Pin 35
    PI
    Parity InhibitWhen High, No Parity is Used for Both Transmit and Receive. When Low, Parity is Used.
    Pin 36
    SBS
    Stop Bit SelectA High selects 2 stop bits. (1.5 for 5 Character Word Lengths) A Low selects one stop bit.
    Pin 37:38
    CLS2:
    CLS1
    Character Length SelectSelects Word Length. 00 = 5 Bits, 01 = 6 Bits, 10 = 7 Bits and 11 = 8 Bits.
    Pin 39
    EPE
    Even Parity EnableWhen High, Even Parity is Used, When Low, Odd Parity is Used.
    Pin 40
    TRC
    Transmitter Register Clock16x Clock input for Transmitter.
    Table 18 : Pin Description for CDP6402, AY-5-1015 / D36402R-9 and compatible UART's

    However one disadvantage of these chips over the 8250's is that these UART's have no inbuilt Programmable Baud Rate Generator, and no facility to connect a crystal directly to it. While there are Baud Rate Generator Chips such as the AY-5-8116, a more cheaper (and common) alternative is the 74HC4060 14-bit Binary Counter and Oscillator.

    The 74HC4060, being a 14 bit binary counter/divider only has outputs for some of it's stages. Only Q4 to Q14 is available for use as they have external connections. This means higher Baud Rates are not obtainable from common crystals, such as the 1.8432 Mhz and 2.4576 Mhz. The UART requires a clock rate 16 times higher than the Baud Rate you will be using. eg A baud rate of 9600 BPS requires a input clock frequency of 153.6 Khz.

    Baud Rate Generator using a 74HC4060
    Figure 10 : Baud Rate Generator using a 74HC4060
    Output
    1.8432Mhz
    2.4546Mhz
    Out 2
    115.2 KBPS
    153.6 KBPS
    Q4
    7200 BPS
    9600 BPS
    Q5
    3600 BPS
    4800 BPS
    Q6
    1800 BPS
    2400 BPS
    Q7
    900 BPS
    1200 BPS
    Q8
    450 BPS
    600 BPS
    Q9
    225 BPS
    300 BPS
    Table 19 : Possible Baud Rates using a 74HC4060

    The 1.8432 Mhz crystal gives some unfamiliar Baud Rates. While many of these won't be accepted by terminal programs or some hardware, they are still acceptable if you write your own serial programs. For example the PC's baud rate divisor for 7200 BPS is 16, 3600 BPS is 32, 1800 BPS is 64 etc. If you require higher speeds, then it is possible to connect the UART to the OUT2 pin. This connection utilizes the oscillator, but has no frequency division applied. Using OUT2 with a 1.8432 Mhz crystal connected gives a baud rate of 115,200 BPS. The CMOS CDP6402 UART can handle up to 200 KBPS at 5 volts, however your MAX-232 may be limited to 120 KBPS, but is still within range.


    Microcontrollers

    It is also possible to use microcontrollers to transmit and receive Serial data. As we have already covered, some of these MCU's (Micro Controller Units) have built in UART's among other things. Take the application we have used above. We want to monitor analog voltages using a ADC and then send them serially to the PC. If the Microcontroller also has a ADC built in along with the UART or SCI, then we could simply program the device and connect a RS-232 Line Driver. This would minimize your chip count and make your PCB much smaller.

    Take the second example, displaying the serial data to a common 16 character x 2 line LCD display. A common problem with the LCD modules, is they don't accept cartridge returns, line-feeds, form-feeds etc. By using a microcontroller, not only can you emulate the UART, but you can also program it to clear the screen, should a form-feed be sent or advance to the next line should a Line-feed be sent.

    The LCD example also required some additional logic (An Inverter) to reset the data receive line on the UART, and provide a -ve edge on the enable of the LCD to display the data present on the pins. This can all be done using the Microcontroller and thus reducing the chip count and the cost of the project.

    Talking of chip count, most Microcontrollers have internal oscillators thus you don't require the 74HC4060 14 Bit Binary Counter and Oscillator. Many Microcontrollers such as the 68HC05J1A and PIC16C84 have a smaller pin count, than the 40 Pin UART. This not only makes the project smaller in size, it reduces complexity of the PCB.

    But there are also many disadvantages of the Microcontroller. The major one, is that you have to program it. For the hobbyist, you may not have a development system for a Microcontroller or a method of programming it. Then you have to learn the micro's code and work out how to tackle the problem. At least with the UART, all you did was plug it in, wire it up and it worked. You can't get much simpler that that.

    So far we have only discussed Full Duplex Transmission, that is that we can transmit and receive at the same time. If our Microcontroller doesn't have a SCI then we can Emulate a RS-232 port using a Parallel line under software control. However Emulation has it's dis-advantages. It only supports slow transmission speeds, commonly 2400, 9600 or maybe even 19,200 BPS if you are lucky. The other disadvantage is that it's really only effective in half duplex mode. That is, it can only communicate in one direction at any one given time. However in many applications this is not a problem.

    As there are many different types of Micro-Controllers all with their different instruction sets, it is very hard to give examples here which will suit everyone. Just be aware that you can use them for serial communications and hopefully at a later date, I can give a limited number of examples with one micro.





Copyright 1999-2010 Craig Peacock 25th October 2010.