home *** CD-ROM | disk | FTP | other *** search
/ ftp.barnyard.co.uk / 2015.02.ftp.barnyard.co.uk.tar / ftp.barnyard.co.uk / cpm / walnut-creek-CDROM / CPM / MODEMS / MODEM / INTE8251.ART < prev    next >
Text File  |  2000-06-30  |  13KB  |  396 lines

  1. [ KAY*FOG RBBS | INTE8251.ART | published 05/30/85 | 379 lines  13k ]
  2.  
  3. INTEL8251:  Programming the Intel 8251A USART
  4.        by:  Ed Greenberg
  5.             Kay*Fog
  6.             CompuServe:    76703,1070
  7.             MCIMAIL:       EDG
  8.  
  9. Preface
  10. =======
  11.  
  12. This document was inspired by my collection of Intel data books.
  13. Of the three databooks, only one contains the following
  14. information.  Most microcomputer users do not have this
  15. information in their computer manuals and do not have access to
  16. the Intel manuals.  Murphy's law also states that the hobbyist
  17. will not have the required manuals at midnight on Saturday when
  18. he is busily trying to debug his communications program.
  19. [Press ENTER key for more]
  20.  
  21.  
  22. Introduction
  23. ============
  24.  
  25. In order to communicate via a serial port, a computer is equipped
  26. with a device called a Universal Asynchronous Receiver/Transmitter
  27. or UART.  This device converts a byte (written to an output port)
  28. to a series of bits sent one at a time (serially) from a
  29. communications port.  It also scans the input line on the serial
  30. port and, detecting the beginning and end of each character,
  31. assembles incoming characters and presents them on an input port.
  32.  
  33. Other names for this part are SIO's (serial input/output) or
  34. Communications adapters and USARTS (Universal Synchronous/
  35. Asynchronous Receiver/Transmitters).
  36.  
  37. This document deals with one common part - the Intel I8251a
  38. USART. That usart is used in the North Star Horizon, the Morrow
  39. Micro Decision, and other common computers.  This discussion deals
  40. with the 8251A only in asynchronous mode.  It is the mode used
  41. when communicating with a printer, a modem, a terminal and
  42. usually, another microcomputer.
  43.  
  44.  
  45. Using your USART
  46. ================
  47.  
  48. In order to use a communications port with a usart, one must
  49. first initialize it.  By doing so, one sets parameters that tells
  50. the USART how to do it's job.  One defines the parity, the number
  51. of stop and data bits, and the divisor (if any) to be applied to
  52. the incoming clock signal.
  53.  
  54. After the USART is initialized, one can read and write characters
  55. on the data port of the usart. One can also check the status of
  56. the usart on the status port.
  57.  
  58. Things to know before you start
  59. ===============================
  60.  
  61. Before you can write code for your usart, you must know the port
  62. locations on which the usart appears.  There are two ports,
  63. called the DATA port and the STATUS port.  Commands are written
  64. to the status port, and the condition of the port is read from
  65. it.  Actual characters are written to and read from the data
  66.  
  67. port.  A good place to find the port numbers is a published I/O
  68. MAP in your manual.  Another place is in a program listing of the
  69. BIOS or a communications program.  You will see something like
  70. this:
  71.  
  72. STATUS    EQU       80H
  73. DATA      EQU       81H
  74.  
  75. or the like.  Sometimes, the word MODEM is worked into the label
  76. (e.g. MODDATP for MODem DATa Port.)  Be certain that you find the
  77. correct set of ports in the case where your computer has more
  78. than one usart.
  79.  
  80. A Note About Interrupts
  81. =======================
  82.  
  83. Some computers use the facility called interrupts to signal the
  84. processor that a character is ready.  In this case, it is
  85. important for the amateur programmer to avoid messing up the
  86. interrupt structure of the machine.  You should not read or write
  87. your USART directly when it's interrupt is enabled.  Doing so may
  88. cause spurious interrupts of the processor.  The result is that
  89.  
  90. nothing will work.  Determining whether your computer uses
  91. interrupts on the communications line is beyond the scope of this
  92. document.
  93.  
  94. Initializing the usart
  95. ======================
  96.  
  97. The following code fragment is taken from the MEX overlay for the
  98. Morrow Micro Decision.  MEX is written and copyrighted by Robert
  99. Fowler.  The comments are my own.
  100.  
  101. INITMOD:  ...
  102.           ...
  103.           MVI   A,087H          ;Take the usart out of
  104.           OUT   MODCTL1         ; the condition for
  105.           OUT   MODCTL1         ;  setting the mode (*)
  106.           MVI   A,40H           ;Put it back into that
  107.           OUT   MODCTL1         ; condition.  This resets
  108.                                 ;  just about everything for
  109.                                 ;   new commands.
  110. INITMOD1: MVI   A,4EH           ;This is the MODE BYTE (*)
  111.           OUT   MODCTL1         ;Send it to the  control/status port
  112.  
  113.           MVI   A,37H           ;This is the COMMAND BYTE (*)
  114.           OUT   MODCTL1         ;Send it to the control/status port
  115.           IN    PORT            ;Clear out the DATA port
  116.  
  117.           RET                   ;Return
  118.  
  119. (*)  See the definition below
  120.  
  121. Input and Output of Characters
  122. ==============================
  123.  
  124. Below are sample routines for input and output of characters on
  125. the 8080 (or Z80) using an 8251A.
  126.  
  127. ;Input character routine.  Character is returned in A.
  128. INPUT:    IN   STATUS         ;Get the status of the usart
  129.           ANI  2              ;turn off all bits but RxR (**)
  130.           JZ   INPUT          ;go back and check again because
  131.                               ; there is no character ready
  132.           IN   DATA           ;There is a character ready,
  133.                               ; so go get it.
  134. ;OPTIONAL STEP
  135.  
  136.           ANI  7FH            ;Remove the high bit
  137.  
  138.           RET                 ;Go back to the caller
  139.  
  140. ;Output character routine.  Character is provided in C.
  141. OUTPUT:   IN   STATUS         ;Get the status of the usart
  142.           ANI  1              ;turn off all bits but TxR (**)
  143.           JZ   OUTPUT         ;Go back and check again because
  144.                               ; the USART isn't ready for another
  145.                               ;  character.
  146.           MOV  A,C            ;The USART is ready, so get the
  147.                               ; character in A for output
  148.           OUT  DATA           ;Now output it and...
  149.           RET                 ;Return to the caller.
  150.  
  151. (**)  See the definition of the Status byte below
  152.  
  153. The remainder of this document is concerned with defining the
  154. actual bytes sent to (and received from) the usart.
  155.  
  156. -----------------------------------------------------------------
  157.  
  158.  
  159. Format of the Mode Byte
  160. =======================
  161.  
  162.   7    6    5    4    3    2    1    0
  163. +----+----+----+----+----+----+----+----+
  164. |s(2)|s(1)| ep |pen |l(2)|l(1)|b(2)|b(1)|
  165. +----+----+----+----+----+----+----+----+
  166.  
  167. Content of the Mode byte
  168. ========================
  169.  
  170. s(2) and s(1) - the stop bit indicators:
  171. ----------------------------------------
  172.  
  173.         s(2)    s(1)    meaning
  174.         ----    ----    -------
  175.          0       0      Invalid
  176.          0       1      1 stop bit
  177.          1       0      1.5 stop bits
  178.          1       1      2 stop bits
  179.  
  180. ep - the parity bit
  181.  
  182. --------------------
  183.  
  184.         ep              meaning
  185.         --              -------
  186.          0              Odd parity is generated and checked
  187.          1              Even parity is generated and checked
  188.  
  189.                 Note: this bit is only active when pen is set
  190.                 to 1.
  191.  
  192. pen - the parity enable bit
  193. ---------------------------
  194.  
  195.         pen             meaning
  196.         ---             -------
  197.          0              parity disabled
  198.          1              parity enabled
  199.  
  200. l(2) and l(1) - the word length bits
  201. ------------------------------------
  202.  
  203.         l(2)    l(1)    meaning
  204.  
  205.         ----    ----    -------
  206.          0       0      5 bits
  207.          0       1      6 bits
  208.          1       0      7 bits
  209.          1       1      8 bits
  210.  
  211.                 Note:  For ASCII, we usually use only 7 or 8 bits.
  212.                 Hams and TTY/TDD (for the deaf) use 5 bits.  The only
  213.                 thing that 6 bits is used for is a colloquialism for
  214.                 seventy five cents.
  215.  
  216. b(2) and b(1) - the baud rate divisor bits
  217. ------------------------------------
  218.  
  219.         b(2)    b(1)    meaning
  220.         ----    ----    -------
  221.          0       0      synchronous
  222.          0       1      1x
  223.          1       0      16x
  224.          1       1      64x
  225.  
  226.                 Note:  This should be left alone.  Whatever
  227.  
  228.                 your system comes equipped for... that's it.
  229.  
  230. Format of the Command Byte
  231. ==========================
  232.  
  233.   7    6    5    4    3    2    1    0
  234. +----+----+----+----+----+----+----+----+
  235. |EH  |IR  |RTS |ER  |SBRK|RxE |DTR |TxEN|
  236. +----+----+----+----+----+----+----+----+
  237.  
  238. Content of the Command Byte
  239. ===========================
  240.  
  241. EH - the Enter Hunt Mode bit
  242. ----------------------------
  243.  
  244. This bit has no effect in async mode!
  245.  
  246. IR - Internal Reset
  247. -------------------
  248.  
  249.         1 Returns 8251A to Mode Instruction format.
  250.  
  251.  
  252. RTS - Request to Send
  253. ---------------------
  254.  
  255.         1 will force RTS high on the RS-232 connector.
  256.  
  257.         Note:  This assumes that the designer hooked all
  258.         the signals up on the PC board.  This is not
  259.         necessarily true.
  260.  
  261.         On some computers, where the port is wired backwards,
  262.         this will control CTS rather than RTS.
  263.  
  264. ER - Error Reset
  265. ----------------
  266.  
  267.         1 Will reset error flags in the status word
  268.         (PE OE and FE will be reset.)  See the definition
  269.         of the status word below.
  270.  
  271. SBRK - Send a break
  272. -------------------
  273.  
  274.  
  275.         1 Will send a break
  276.  
  277.         Note: Usually, one sends a command with this bit set
  278.         and then, after a delay that equals the length of a
  279.         break, one sends another command that does not have
  280.         the break bit on.
  281.  
  282. RxE - Receive Enable
  283. --------------------
  284.  
  285.         1 will enable the receiver.  If you are going to disable
  286.         the receiver for any reason, you should have a data book
  287.         in front of you and know what you're doing.  This bit
  288.         should almost always be on for any command you send.
  289.  
  290. DTR - Data Terminal Ready
  291. -------------------------
  292.  
  293.         1 will turn on Data Terminal Ready at the RS-232 connector.
  294.  
  295.         Note:  This assumes that the designer hooked all
  296.  
  297.         the signals up on the PC board.  This is not
  298.         necessarily true.
  299.  
  300.         On some computers, where the port is wired backwards,
  301.         this will control DSR rather than DTR.
  302.  
  303. TxE - Transmitter Enable
  304. ------------------------
  305.  
  306.         1 will enable the transmitter.  If you are going to disable
  307.         the transmitter for any reason, you should have a data book
  308.         in front of you and know what you're doing.  This bit should
  309.         almost always be on for any command you send.
  310.  
  311. Format of the Status Byte
  312. =========================
  313.  
  314.   7    6    5    4    3    2    1    0
  315. +----+----+----+----+----+----+----+----+
  316. |DSR |SDET|RE  |OE  |PE  |TxE |RxR |TxR |
  317. +----+----+----+----+----+----+----+----+
  318.  
  319.  
  320. Content of the Status Byte
  321. ==========================
  322.  
  323. DSR - Data Set Ready
  324. --------------------
  325.  
  326.         1 Indicates that DSR is low!  That's right, Low!
  327.  
  328.         Note that this bit may be (a) not wired at all or (b)
  329.         wired to DTR or some other pin on the RS-232 connector.
  330.         The only way to tell for sure is to look at a schematic.
  331.  
  332. SDET - SYNC Detect/ BREAK Detect
  333. --------------------------------
  334. (In the Intel documentation this is called SYNDET, not SDET.)
  335.  
  336. In async mode, this bit "will go high whenever an all zero word
  337. of the programmed length (including start bit, data bit, parity
  338. bit and *one* stop bit) is received." (***)  In other words, when
  339. the other end sends *you* a break.
  340.  
  341. This bit stays high until a reset command is issued.
  342.  
  343.  
  344. FE - Framing Error
  345. ------------------
  346.  
  347. This bit is set when "A valid stop bit is not detected at the end of
  348. every character."
  349.  
  350. This bit stays high until a reset command is issued.
  351.  
  352. OE - Overrun Error
  353. ------------------
  354.  
  355. This bit is set when a character has been pending on the USART buffer
  356. and another character comes in before the first one has been read.
  357. The first character is lost and this bit is set to alert the CPU to
  358. the problem.
  359.  
  360. This bit stays high until a reset command is issued.
  361.  
  362. PE - Parity Error
  363. -----------------
  364.  
  365.  
  366. This bit is set when a parity error is detected.
  367.  
  368. This bit stays high until a reset command is issued.
  369.  
  370. TxEMPTY - Transmit Buffer Empty
  371. -------------------------------
  372.  
  373. "When the 8251A has no characters to transmit, the TxEMPTY ...
  374. [bit will be set to 1.]  ... " (***)
  375.  
  376. RxRDY - Receiver Ready
  377. ----------------------
  378.  
  379. "This ... [bit set to 1] ... indicates that the 8251A contains
  380. a character that is ready to be input to the CPU." (***)
  381.  
  382. This is the usual bit to test to see if a character is available.
  383. Usually one sees ANI 2 (on 8080) when this bit is tested.
  384.  
  385. TxRDY - Transmitter Ready
  386. -------------------------
  387.  
  388.  
  389. "This ... [bit set to a 1] ... signals the CPU that the transmitter
  390. is ready to accept a data character."
  391.  
  392. This is the usual bit to test to see if a character may be sent.
  393. Usually one sees ANI 1 (on 8080) when this bit is tested.
  394.  
  395. ----------------------- End of INTE8251.ART Text -----------------------
  396.  ∩