CSN200 Introduction to Telecommunications, Winter 2000 Lecture_16 UART8250UART: UNIVERSAL ASYNCHRONOUSRECEIVERTRANSMITTER Serial communications is sufficiently complex that special chips have been designed to do the work of forming and timing the strings of bits that comprise serial data. Such a chip is called a universal asynchronousreceivertransmitter or UART. Without UARTs, communications programming would be very complicated. The IBM family of microcomputers use the Intel 8250 UART. The Intel 8250 is a programmable UniversalAsynchronous Receiver/Transmitter packaged in a 40- pin integrated circuit. • The UART is used to convert parallel data to serial format on the transmit side (data going to a modem for example) and to convert serial data to a parallel format on the receive side (coming from a modem). • The serial format, in order of transmission and reception, is a start bit, followed by five to eight data bits, a parity bit (if programmed) and one or two stop bits. The maximum recommended data rate is 54K bits per second. • It can transmit characters at the same time as it is receiving characters (full duplex). • The transmission speed of the UART is also programmable. • Newer UARTs are now used in serial ports in PCs : 16450 and 16550 capable of speeds up to 115,000 bps. They are more efficient than the 8250 but are compatible with it from a programming point of view. Internal registers enable the user to program various types of interrupts, modem controls, and character formats. The user can read the status of the UART at any time, monitoring transmission conditions, interrupts and modem status. FEATURES * Automatically generates and strips the Serial Async Control Bits (start, stop, parity) * Full double buffering allows precise synchronization. * Independently controlled Transmit, Receive, Line Status, and data set Interrupts. * Modem interface capabilities. * Fully programmable Serial-Interface characteristics: - 5-, 6-, 7-, or 8-Bit Characters - Even, Odd, or No-parity Bit Generation and Detection - 1-, 1 1/2-, or 2-Stop Bit Generation - Baud Rate Generation (DC to 56K Baud) * False Start Bit detector * Complete status reporting capabilities. * Internal Diagnostic capabilities - Loopback Controls for Communications Link Fault Isolation - Break, Parity, Overrun, and Framing Error Simulation Lectur16.doc Page 1 (9) CSN200 Introduction to Telecommunications, Winter 2000 Lecture_16 UART DOS supports at least two communications ports, and hence requires at least two UARTS. Their base addresses are kept at 0040:0000 for COM1 and 0040:0002 for COM2. A base address is the lowest two-byte port address of the group of port addresses by which the UART is accessed. COM1 starts at 3F8H and COM2 at 2F8H. For convenience, the discussion here refers to the registers numbered 3FxH, but the same specifications apply to the registers at 2FxH. The 8250 has ten programmable one-byte registers by which to control and monitor the serial port. Most are devoted to initializing the port, a process that can be rather complicated. The ten registers are accessed through seven port addresses, numbers 3F8H-3FEH (or 2F8H-2FEH). Detailed knowledge of these registers is necessary for programming the UART at the register level. We will use a simpler method called BIOS INT14. UART Internal Registers of Importance: Transmitter Holding Register holds the byte of data about to be sent to the modem. Receiver Data Register keeps the most recent byte of data received from the modem. Line Control Register is used to initialize the UART for a particular mode of operation. Line Status Register monitors the status of the serial line indicating when characters have been received, or fully sent and when errors occur. The Modem Control and Modem Status Registers are used only for modem communications to control and monitor the control lines in the RS-232 circuits. • Polling Mode: Simple communications routines constantly monitor the line status register, waiting for an incoming character, or waiting until the register indicates that it is all right to transmit another byte of data. Serial data transmission rates are very slow compared to a microprocessors speed. A program using polling mode spends most of its time waiting for characters to be transmitted, bit by bit, or received bit by bit. • Interrupt Mode: Because the CPU operates very quickly relative to the 1200 or 2400 bit-per- second rate at which serial data typically moves, this method can be wasteful of CPU time that might otherwise be devoted to processing the incoming/outgoing data. • For this reason the 8250 may be set up to bring about an interrupt whenever a character arrives, an error occurs, etc. • The interrupt momentarily brings into action a procedure in your program that would, say, output the next character from a communications buffer. We will not use Interrupt Mode but will rather Poll the status of the UART before sending or receiving characters. INITIALIZING THE SERIAL PORT When a communications port is initialized ("opened"), all of the parameters by which it operates are set. These parameters include the word length, the number of stop bits, the parity setting, and the baud rate. The word length is the number of bits that form the basic data unit. While we are accustomed to working in eight bits, seven bits are adequate for standard ASCII files (where all characters are below ASCII 128). Lectur16.doc Page 2 (9) CSN200 Introduction to Telecommunications, Winter 2000 Lecture_16 UART Initialization of the 8250 involves supplying the following information: Baud rate (speed) 75, 100, 150, 300, 600, 1200, 4800, 2400, 4800, or 9600 bits per second. Parity given as a one-character code: O for ODD parity; E for EVEN parity (the default); N for NONE (no parity); S for SPACE, where the parity bit is always 0; and M for MARK, where the parity bit is always 1. If eight data bits are used, specify N; if four bits are used, do not use N. Data bits 4, 5, 6, 7, or 8 Stop bits 1 or 2. PROGRAMMING THE SERIAL PORT Two techniques for programming the serial port on a PC are: 1. BIOS INT 14H SERVICES a) There are 4 "services" available to control the serial port. b) This method is not suitable for transmission speeds in excess of 1200 baud. 2. DIRECT PORT ACCESS The 8250 registers must be programmed directly for high performance applications. It usually requires the use of interrupt handlers. We will not access the serial port addresses directly in this course. USING BIOS INT 14H FOR SERIAL PORT ACCESS: Service Number 0 (INT 14H) - Initialize the Serial Port This service sets up the communications parameters for the serial port: baud rate (speed), parity, stop bits and character length. An 8-bit number is loaded into the microprocessor’s AL Register according to the table below: Bits 7 6 5 4 3 2 1 0 Character Length 1 0 = 7 bits 11 = 8 bits Stop Bits 0 = 1 stop bit 1 = 2 stop bits Parity 01 = Odd 11 = Even 00 = None 10 = None Baud Rate 010 = 300 011 = 600 100 = 1200 101 = 2400 110 = 4800 111 = 9600 Fig. 1 Initializing The Serial Port Lectur16.doc Page 3 (9) CSN200 Introduction to Telecommunications, Winter 2000 Lecture_16 UART Sample Assembly Language Program: Initialization of COM1 for 1200 baud, odd parity, 7 bits, 1 stop bit (Note: BIOS COM1 is port number 0; COM2 is port number 1) MOV AL,8AH ;SET UP BYTE 10001010 binary MOV AH,0 ;REQUEST SERVICE NUMBER 0 MOV DX,0 ;COMM PORT NUMBER 0 (COM1) INT 14H ; BIOS SERVICE Sample C Program: A C Program to initialize the UART to the same value could use the int86 instruction to execute BIOS int14. See attached listing of a sample program appended at the end. Service Number 1 (INT 14H) - Transmit a Character This function is used to transmit a character through the communications line. The character is simply loaded into the microprocessor’s AL register. The DX register, as in all BIOS INT 14H services, is loaded with the number of the communications port and the AH register with the service number (01 in this case). Example: Send a '$' sign through the communications line. MOV AL,'$' ;Char to send MOV AH,1 ;Request service no. 1 MOV DX,0 ; Comm port no. 0 (COM1) INT 14H Upon return to the caller, bit 7 of the AH is set if the routine was unable to transmit a byte of data, indicating some error condition. Service Number 2 (INT 14H) - Receive a Character This function receives one character from the communications port. The character received is returned in the AL register. Bit 7 of the AH register is set if the operation failed because of the data set ready signal, indicating an error; for example, there was no data there to be received yet. Service Number 3 (INT 14H) - Read the Serial Port Status This service returns the status of the serial port. The AH register reports the status of the communications line and the AL register contains the modem status. Meaning of bits set by FUNCTION 3, INT 14H AH REGISTER (COMM. LINE) AL REGISTER (MODEM) Bit 7= Time-out error Received line signal detect Bit 6= Trans shift register empty Ring Indicator Bit 5= Trans Holding Register Empty Data Set Ready Bit 4= Break Detect Clear to Send Bit 3= Framing Error Delta Rx Line signal detect Bit 2= Parity error Trailing edge Ring detector Bit 1= Overrun error Delta Data Set Ready Bit 0= Data Ready Delta Clear to send Lectur16.doc Page 4 (9) CSN200 Introduction to Telecommunications, Winter 2000 Lecture_16 UART The (THRE) Transmit Holding Register Empty Bit 0 ==> a character is still in the process of being sent. Transmitter is still Busy so a new character cannot be sent until this bit becomes a 1. 1 ==> Transmit Holding Register is empty so a new character can now be transmitted. Data Ready Bit 0 ==> Data has not been received yet. 1 ==> A character has arrived in the Data Received Register and must be read in as soon as possible. Lectur16.doc Page 5 (9) CSN200 Introduction to Telecommunications, Winter 2000 Lecture_16 UART Simple Program to Endlessly Transmit a character using a UART: UART Transmit Program Initialize the UART Transmit Character Has Last Char been sent yet? (Transmit Register EmptyYet ?) Read the UART Status Register Yes No Lectur16.doc Page 6 (9) CSN200 Introduction to Telecommunications, Winter 2000 Lecture_16 UART The C program to transmit character 'a' continuously: /* view_a.c , C Program for transmitting the character 'a' continuously. */ /* A single character is transmitted repeatedly on COM1 for viewing on an oscilloscope */ #include <dos.h> /* Essential for int86 function */ #include <stdio.h> #include <conio.h> union REGS inregs, outregs; /* Provides Global access to microprocessors internal registers. The REGS union is declared in dos.h header file*/ /* int14( ) function prototype */ unsigned int int14(unsigned char, unsigned char, unsigned int); /* int14(initializer, service number, comm port) is a user written function which simulates the BIOS INT14 operation */ int main(void) { unsigned int return_val; int ready = 0; /* status flag indicating NOT ready when 0*/ char letter = 'a'; /* character to be transmitted */ unsigned char initializer; /* 8-bit value used to initialize the UART */ char from_kb; int keypress; /* To Initialize the COM1 port for 2400 bps, 7 bit data, even parity, 1 stop bit */ initializer = 0xBA; /* binary 1011 1010 or, 101 11 0 10 */ /* 101 = 2400 baud rate 11 = Even parity 0 = One stop bit 10 = 7 bits data */ /* Initialize the port */ int14(initializer, 0, 0); while(1) /* Endless loop: ESC key is pressed to stop transmission */ { keypress = kbhit(); if (keypress) if(( from_kb = getch()) == 0x1B) /* ESC key pressed?*/ break; /* Transmit a character */ int14(letter, 1, 0); do{ /* Wait Loop */ /* Wait for Character to be fully transmitted */ return_val =int14(0, 3, 0); /* before transmitting the next one */ Lectur16.doc Page 7 (9) CSN200 Introduction to Telecommunications, Winter 2000 Lecture_16 UART ready = (return_val >> 13) & 1; /* Read UART Status */ /* Test Transmit Holding Register Empty Bit in the Line Status Reg */ }while(!ready); } /* end_while */ return (0); } /* ***************INT14 (equivalent) function ******************/ /* To Use BIOS INT 14h for COM1 control: Put initializer to be used in AL register Put INT14 service # in AH register Put 0 in DX register for COM1 (or, 1 for COM2) */ unsigned int int14( unsigned char initializer, unsigned char service_number, unsigned int com_port) { inregs.h.al = initializer; /* value passed for initialization or sending a char */ inregs.h.ah = service_number; /* Service # to be executed for BIOS INT14 */ inregs.x.dx = com_port; /* Serial Port to be used: COM1 =0, COM2=1 */ int86(0x14, &inregs, &outregs); /* int86 instr'n enables us to execute BIOS software interrupts from C */ return (outregs.x.ax); /* return the value in the microprocessor's AX */ } /* register (16 bits) */ Special Note: The Intel microprocessors AX register is a 16 bit register which can also be accessed as two separate 8- bit registers AL (lower 8 bits) and as AH (upper 8 bits). Int86 instruction returns the 16 bit value in AX. For example: AH (8 bits) = FFh AL (8 bits) = 72h AX (16 bits) = FF72h Lectur16.doc Page 8 (9) CSN200 Introduction to Telecommunications, Winter 2000 Lecture_16 UARTUART or Universal AsynchronousReceiver Transmitter: UART or Universal AsynchronousReceiverTransmitter is an integrated circuit used in PC serial ports to send and receive data serially. It must perform parallel to serial conversion of the data so data can be sent to the UART from the microprocessor in parallel on the system bus and then transmitted out the serial port serially. Common UART chips in serial ports are known as 8250, 16450 and 16550. Example Transmission of an ASCII character on a UART’s Transmit Data pin: Sending ASCII character ‘a’ at a speed of 1200 bits per second, using 7 data bits, Odd parity and 1 Stop bit. ‘a’ === Hexadecimal 61 or 110 0001 as a 7-bit number Note: The Output of a UART is in a “1” state (also called the MARK state) before and after transmission of a character. 1 bit ===> +5 volts (TTL logic) Mark state 0 bit ===> 0 volts Space state Bit time = 1 / (bit rate) = 1/2400 = 0.417 m sec = 417 μs mark (1) +5v start 0 LSB 1 odd parity 0 stop 1 space (0) 0v 0 0 0 0 1 1 0 0 0 0 1 11 0 0 0 0 1 11 +5v +12 -12 0v RS232 Signaling TTL Logic Lectur16.doc Page 9 (9) . Telecommunications, Winter 2000 Lecture_ 16 UART UART or Universal Asynchronous Receiver Transmitter: UART or Universal Asynchronous Receiver Transmitter is an integrated. Introduction to Telecommunications, Winter 2000 Lecture_ 16 UART 8250 UART : UNIVERSAL ASYNCHRONOUS RECEIVER TRANSMITTER Serial communications is sufficiently