PC Interfacing Lecture

Embed Size (px)

DESCRIPTION

PC Interfacing Lecture

Citation preview

  • Copyright 2010, Dr. Vo Tuong Quan _______________________________________________________________________________________________________

    1

    COMMUNICATION INTERFACING LECTURE

    CONTENTS 1. Overview 2. Parallel Communication standard 3. Programable Peripheral Interface 8255 4. Serial Communication (RS232, RS485,) 5. I2C Standard 6. SPI Standard 7. Some popular communication Card DAQ (PCLab 818L, PCI 1784, PCI

    1772,) 8. Controller Area Network (CAN) standard 9. Group projects (Similar to the Microcontroller subject) 10. Network Communication Standard (Another Subject) 11. USB Standard

    Midterm test: 20% Group project: 20% Endterm test: 80%

  • Copyright 2010, Dr. Vo Tuong Quan _______________________________________________________________________________________________________

    2

    OVERVIEW

    COMMUNICATION PURPOSES - Transmit or receive data between/ among many equipments - The communication data can be:

    + Control signals + Data signals

    COMMUNICATION TYPES - Half dupplex

    - Full dupplex

    DEFINITIONS - DTE (Data Terminal Equipment)

    The source of data generation/ the receiving data equipment. Ex: PC, MCU, PLC,

    - DCE (Data Communication Equipment) The intermediate equipment. Ex: Modem, Switch, Router,

  • Copyright 2010, Dr. Vo Tuong Quan _______________________________________________________________________________________________________

    3

    PARALLEL COMMUNICATION STANDARD

    ORIGINAL PURPOSE Use to connect the PC with the printer device to print out documents. Many data is transmit/receive at the same time. We use this function of PC in the control trend Note: Some equiptments that use the parrallel or (multi data lines) to connect together is also called the parralel standard.

    MERITS OF PARALLEL STANDARD Simple and easy to communicate or programming. The communication speed is quite high.

    DISADVANTAGES Many data line Weak in noise avoidance The transmit/receive distance is short (about < 15 meters)

    THE PARALLEL TYPE IN A PC

    SPP (Standard Paprallel Port) We just focus on this type EPP (Enhanced Paprallel Port)

  • Copyright 2010, Dr. Vo Tuong Quan _______________________________________________________________________________________________________

    4

    ECP (Extended Capability Port) EPP and ECP depend on the type of mainboard. Some mainboards support this standard and other mainboard is not. The communication speed of SPP is about: 50Kbps to 150Kbps.

    THE SPP COMMUNICATION The SPP communication mostly has the type of 25 pins female

    Pin No (D-Type 25) SPP Signal Direction In/out Register Hardware Inverted

    1 nStrobe In/Out Control Yes 2 Data 0 Out Data 3 Data 1 Out Data 4 Data 2 Out Data 5 Data 3 Out Data 6 Data 4 Out Data 7 Data 5 Out Data 8 Data 6 Out Data 9 Data 7 Out Data

    10 nAck In Status 11 Busy In Status Yes 12 Paper-Out / Paper-End In Status

  • Copyright 2010, Dr. Vo Tuong Quan _______________________________________________________________________________________________________

    5

    13 Select In Status 14 nAuto-Linefeed In/Out Control Yes 15 nError / nFault In Status 16 nInitialize In/Out Control 17 nSelect-Printer / nSelect-In In/Out Control Yes

    18 - 25 Ground Gnd

    The meaning of hardware inverted! Electric standard of SPP: TTL

    PORT ADDRESS LPT1 (base address): 378h LPT2 (base address): 278h Some other LPT has the address of 3BC seldom use

    PORT REGISTERS

    DATA REGISTER Address: base address + 0 Range: 8 bits output

    Offset Name Read/Write Bit No. Pin Properties Bit 7 9 Data 7 Bit 6 8 Data 6 Bit 5 7 Data 5 Bit 4 6 Data 4 Bit 3 5 Data 3 Bit 2 4 Data 2 Bit 1 3 Data 1

    Base + 0

    Data Register

    Write

    Bit 0 2 Data 0

    Some mainboard support the function of Bi-directional, these pins can be used as the input port.

  • Copyright 2010, Dr. Vo Tuong Quan _______________________________________________________________________________________________________

    6

    STATUS REGISTER Address: base address + 1 Range: 5 bits input

    Offset Name Read/Write Bit No. Pin Properties Bit 7 11 Busy Bit 6 10 /Ack Bit 5 12 Paper Out Bit 4 13 Select In Bit 3 15 /Error Bit 2 X /IRQ (Not) Bit 1 X Reserved

    Base + 1 Status Register

    Read Only

    Bit 0 X Reserved

    Ex: The bit 7 (busy) is the inverted bit. If we measure pin 11 and we get the voltage of 5V, this means that; the bit 7 has the logic value of zero (0).

    CONTROL REGISTER Address: base address + 2 Range: 4 bits input/output (Open collector type Can communicate in 2 directions)

    Offset Name Read/Write Bit No. Pin Properties Bit 7 X Unused Bit 6 X Unused Bit 5 X Enable Bi-Directional Port Bit 4 X Enable IRQ Via Ack Line Bit 3 17 /Select Printer Bit 2 16 /Initialize Printer (Reset) Bit 1 14 /Auto Linefeed

    Base + 2

    Control Register Read/Write

    Bit 0 1 /Strobe

    These 4 pins or 4 bits of the control register have hardware inverted functions.

  • Copyright 2010, Dr. Vo Tuong Quan _______________________________________________________________________________________________________

    7

    As mention above on the Bi-directional function of main board. If bit 5 of control register is set to 1, the pins of data registers (pin 2 to pin 9) can be used as the input port. The 4 pins or 4 bits of the control register have hardware inverted How can we do on these pins to make sure that the real data is received (Input the NOT gate at these pins)

    Special case If some main board does not support the function open collector of the control register, this means that the direction of the pins belongs to control register has only 1 direction (output), we will use the multiplexer to read 4 bits into the status register. We will read two times to get 8 bits data. Ex:

  • Copyright 2010, Dr. Vo Tuong Quan _______________________________________________________________________________________________________

    8

    Ex: outportb(controlReg, inportb(controlReg) | 0x01); a = (inportb(statusReg) & 0xF0) ; a = a >> 4 ;

    outportb(controlReg, inportb(controlReg) | 0xFE); a = (inportb(statusReg) & 0xFF) ; byte = a ^ 0x88;

    To read 8 bits data: Let bit D0 (bit strobe) of the control register equal to 1 (D0 = 1) to read 4 low bits of status register (4 low bits of input data 1A 4A). Then, right shift 4 bits. Then, let bit D0 = 0 to read 4 high bits of status register (4 high bits of input data 1B 4B). Then, we get 8 bit input data. Because of the bit Busy has the inverted hardware, so we have to let the 8 bit input data EXOR with 88h to invert bit D7 and D3 of the input signals.

    PROGRAMMING METHODS

    Visual C++, C - Read data

    + _inp : read 1 byte data + _inpw: read one word data + _inpd: read one double word data

    - Write data + _outp : write 1 byte data + _outpw: write one word data + _outpd: write one double word data

    Or we can use some other DLL support for Visual C++, C to program for LPT.

  • Copyright 2010, Dr. Vo Tuong Quan _______________________________________________________________________________________________________

    9

    Ex: #include

    #define dataReg 0x378

    #define statusReg 0x379

    int dummy;

    dummy = _outp(dataReg,0xFE); dummy = _inp(statusReg) // Consider Mask algorithm for sure of the input signals ..

    Ex: Using LPT to control LCD (2x16)

    // Register Select must be connected to Select Printer (PIN 17) // Enable must be connected to Strobe (PIN1) // DATA 0:7 Connected to DATA 0:7

    #include

    #include

    #define PORTADDRESS 0x378

    #define DATA PORTADDRESS+0

    #define STATUS PORTADDRESS+1

    #define CONTROL PORTADDRESS+2

  • Copyright 2010, Dr. Vo Tuong Quan _______________________________________________________________________________________________________

    10

    void main(void) { char string[] = {"Robot" "Fish"}; char init[10]; int count;

    int len;

    init[0] = 0x0F; // Init Display init[1] = 0x01; // Clear Display init[2] = 0x38; // Dual Line / 8 Bits

    // Reset Control Port - Make sure Forward Direction outportb(CONTROL, inportb(CONTROL) & 0xDF);

    // Set Select Printer (Register Select) */ outportb(CONTROL, inportb(CONTROL) | 0x08); for (count = 0; count

  • Copyright 2010, Dr. Vo Tuong Quan _______________________________________________________________________________________________________

    11

    Visual Basic We use some supported DLL (Inpout.dll, Port.dll,)

    - Port.dll Attribute VB_Name = "varPortDll" Option Explicit

    Declare Function OPENCOM Lib "PORT.DLL" (ByVal A$) As Integer Declare Sub CLOSECOM Lib "PORT.DLL" () Declare Sub SENDBYTE Lib "PORT.DLL" (ByVal b%) Declare Function READBYTE Lib "PORT.DLL" () As Integer Declare Sub DTR Lib "PORT.DLL" (ByVal b%) Declare Sub RTS Lib "PORT.DLL" (ByVal b%) Declare Sub TXD Lib "PORT.DLL" (ByVal b%) Declare Function CTS Lib "PORT.DLL" () As Integer Declare Function DSR Lib "PORT.DLL" () As Integer Declare Function RI Lib "PORT.DLL" () As Integer Declare Function DCD Lib "PORT.DLL" () As Integer Declare Sub DELAY Lib "PORT.DLL" (ByVal b%) Declare Sub TIMEINIT Lib "PORT.DLL" () Declare Sub TIMEINITUS Lib "PORT.DLL" () Declare Function TIMEREAD Lib "PORT.DLL" () As Long Declare Function TIMEREADUS Lib "PORT.DLL" () As Long Declare Sub DELAYUS Lib "PORT.DLL" (ByVal l As Long) Declare Sub REALTIME Lib "PORT.DLL" (ByVal i As Boolean) Declare Sub OUTPORT Lib "PORT.DLL" (ByVal A%, ByVal b%) Declare Function INPORT Lib "PORT.DLL" (ByVal p%) As Integer Declare Function JOYX Lib "PORT.DLL" () As Long Declare Function JOYY Lib "PORT.DLL" () As Long Declare Function JOYZ Lib "PORT.DLL" () As Long Declare Function JOYW Lib "PORT.DLL" () As Long Declare Function JOYBUTTON Lib "PORT.DLL" () As Integer Declare Function SOUNDSETRATE Lib "PORT.DLL" (ByVal Rate%) As Integer Declare Function SOUNDGETRATE Lib "PORT.DLL" () As Integer Declare Function SOUNDBUSY Lib "PORT.DLL" () As Boolean Declare Function SOUNDIS Lib "PORT.DLL" () As Boolean Declare Sub SOUNDIN Lib "PORT.DLL" (ByVal Puffer$, ByVal Size%) Declare Sub SOUNDOUT Lib "PORT.DLL" (ByVal Puffer$, ByVal Size%) Declare Function SOUNDGETBYTES Lib "PORT.DLL" () As Integer Declare Function SOUNDSETBYTES Lib "PORT.DLL" (ByVal b%) As Integer Declare Sub SOUNDCAPIN Lib "PORT.DLL" () Declare Sub SOUNCAPDOUT Lib "PORT.DLL" ()

    - Inpout.dll Private Sub Command1_Click() Text2.Text = Str(Inp(Val("&H" + Text1.Text))) End Sub

    Private Sub Command2_Click() Out Val("&H" + Text1.Text), Val(Text2.Text) End Sub

  • Copyright 2010, Dr. Vo Tuong Quan _______________________________________________________________________________________________________

    12

    Public Declare Function Inp Lib "inpout32.dll" _ Alias "Inp32" (ByVal PortAddress As Integer) As Integer Public Declare Sub Out Lib "inpout32.dll" _ Alias "Out32" (ByVal PortAddress As Integer, ByVal Value As Integer)

    Exercises:

    1. Control Led (Led run left direction, right direction, stop) 2. Stepmotor (motor run CW, CCW, Stop). 3. . 4. .

  • Copyright 2010, Dr. Vo Tuong Quan _______________________________________________________________________________________________________

    13

    PROGRAMMABLE PERIPHERAL INTERFACE 8255

    SPECIFICATION - PPI8255 is the programable parallel communication IC to choose the

    suitable operation mode. - This IC includes 3 input/output 8 bits parallel port (PortA, PortB, PortC)

    and these ports can be devided into two groups of 12 bits. Group1: PortA and high nibble of PortC; Group2: PortB and low nibble of PortC.

    - There operation modes: Mode0, Mode 1 and Mode 2.

  • Copyright 2010, Dr. Vo Tuong Quan _______________________________________________________________________________________________________

    14

    Function diagram

    Data Bus Buffer - Include 8 data lines, 3 status mode, 2 directions, connect directly to

    system bus. - The direction of data bus buffer is controlled by the Read/Write

    Control Logic. - Data bus buffer is the communication port between 8255 and CPU or

    MCU including data and control word.

    Read/Write Control Logic - Receive control signals and address from system to control operation

    mode of 8255.

    - 0=CS : 8255 run. - RESET: Let the 8255 to the initial state.

    - 01, AA : Choose the ports and control word of 8255.

  • Copyright 2010, Dr. Vo Tuong Quan _______________________________________________________________________________________________________

    15

    - WRRD, : input or output mode.

    - The operation mode of 8255

    Operations - PortA: Operate in Mode 0, 1, 2 - PortB: Operate in Mode 0, 1 - PortC: Operate in Mode 0 and PortC is used as the control signals for

    PortA and PortB in operation mode 1.

  • Copyright 2010, Dr. Vo Tuong Quan _______________________________________________________________________________________________________

    16

    INITIAL STATE (Reset) - When reset, three ports of 8255 are set as input port at mode 0. - To define the operation mode of 8255, we have to set the control word

    with the suitable value.

    Control Word - This control word will set the direction of ports (input/output) or

    operation mode. - Control word (8 bits) is save in the Control Word Register.

    Ex: We choose the operation mode for 8255 like this: Port A: Input, mode 1; Port B: Output, mode 0, Port C high : output, Port C low : Input.

  • Copyright 2010, Dr. Vo Tuong Quan _______________________________________________________________________________________________________

    17

    The value of CW : 10110001b

    The mode bit set/reset of PortC - Use to set or clear one specific bit of Port C. - Normally use in the control mode - The bit set reset of Port C is controlled by CW. - Specification:

    Bit 7: 0 Bit: 6, 5, 4: X Bit 3 , 2, 1: Choose bit (000 bit 0 to 111 bit 7) Bit 0: = 0: Clear

    = 1: Set The control word of this case is also output to the control word address of 8255. Note: The bit set/reset mode of port C does not effect to the normal operation mode of 8255 was set before.

    OPERATION MODE + Mode 0 (Basic Input/Output) This functional configuration provides simple input and output operations for each of the three ports. No handshaking is required, data is simply written to or read from a specific port. Mode 0 Basic Functional Definitions:

    Two 8-bit ports and two 4-bit ports

    Any Port can be input or output

    This mode does not use handshaking with the I/O.

    This mode is suitable for the data which are not normally changed. Ex, this can be used in the getting of sampling rate of data because it does not check the error of the data.

  • Copyright 2010, Dr. Vo Tuong Quan _______________________________________________________________________________________________________

    18

    Input

    Output

    Mode 1 - (Strobed Input/Output). This functional configuration provides a means for transferring I/O data to or from a specified port in conjunction with strobes or hand shaking signals. In mode 1, port A and port B use the lines on port C to generate or accept these hand shaking signals. Mode 1 Basic Function Definitions: Two Groups (Group A and Group B) Each group contains one 8-bit port and one 4-bit control/data port The 8-bit data port can be either input or output.

  • Copyright 2010, Dr. Vo Tuong Quan _______________________________________________________________________________________________________

    19

    The 4-bit port is used for control and status of the 8-bit port.

    INPUT MODE

    STB (Strobe Input) A low on this input loads data into the input latch.

    IBF (Input Buffer Full F/F) A high on this output indicates that the data has been loaded into the input latch: in essence, and acknowledgment. IBF is set by STB input being low and is reset by the rising edge of the RD input.

    INTR (Interrupt Request) A high on this output can be used to interrupt the CPU when and input device is requesting service. INTR is set by the condition: STB is a one, IBF is a one and INTE is a one. It is reset by the falling edge of RD. This procedure allows an input device to request service from the CPU by simply strobing its data into the port.

  • Copyright 2010, Dr. Vo Tuong Quan _______________________________________________________________________________________________________

    20

    INTE A Controlled by bit set/reset of PC4.

    INTE B Controlled by bit set/reset of PC2.

    OUTPUT MODE

  • Copyright 2010, Dr. Vo Tuong Quan _______________________________________________________________________________________________________

    21

    OBF (Output Buffer Full). The OBF output will go low to indicate that the CPU has written data out to be specified port. Data is guaranteed valid at the rising edge of OBF, The OBF will be set by the rising edge of the WR input and reset by ACK input being low. ACK (Acknowledge Input). A low on this input informs the 82C55 that the data from Port A or Port B is ready to be accepted. In essence, a response from the peripheral device indicating that it is ready to accept data. INTR - (Interrupt Request). A high on this output can be used to interrupt the CPU when an output device has accepted data transmitted by the CPU. INTR is set when ACK is a one, OBF is a one and INTE is a one. It is reset by the falling edge of WR.

    Mode 2 (Strobed Bi-Directional Bus I/O) Self research

  • Copyright 2010, Dr. Vo Tuong Quan _______________________________________________________________________________________________________

    22

    Ex: Using 8255 DLL // FILE: 8255.cpp #include #include // contains Visual C++'s inp and out functions // ------------------------------------------------------ // FUNC: Out8255 // DESC: uses Microsoft's Visual C++ _outp() function // to output a PortData to PortAddress // ------------------------------------------------------ short _stdcall Out8255( int PortAddress, int PortData ) { short Dummy; // Need Dummy since _outp officially returns int // short is a 16-bit integer in Win32 C++ // whereas int is 32-bit integer Win32 C++ // use (short) to force returning 16-bit integer // back to VB Dummy = (short)(_outp( PortAddress, PortData )); return(Dummy); }; // end of Out8255 // ---------------------------------------------------- // FUNC: In8255 // DESC: uses Microsoft's Visual C++ _inp() function // to read PortAddress // ---------------------------------------------------- short _stdcall In8255( int PortAddress ) { short PortData; // short is a 16-bit integer in Win32 C++ // whereas int is 32-bit integer in Win32 C++ // use (short) to force returning 16-bit integer // back to VB PortData = (short)(_inp( PortAddress )); return( PortData ); }; /* end of In8255 */

    Ex: Count from zero to 255 then light up Led

  • Copyright 2010, Dr. Vo Tuong Quan _______________________________________________________________________________________________________

    23

    Visual Basic Code Option Explicit 'Declare use of the DLL Private Declare Function Out8255 Lib "8255.dll" (ByVal PortAddress As Integer, ByVal PortData As Integer) As Integer Private Declare Function In8255 Lib "8255.dll" (ByVal PortAddress As Integer) As Integer

    'Declare variables Dim BaseAddress As Integer: ' 8255 Base Address Dim Dummy As Integer: ' Dummy variable used with DLL Dim PortA As Integer: ' 8255 Port A address Dim PortB As Integer: ' 8255 Port B address Dim PortC As Integer: ' 8255 Port C address Dim Cntrl As Integer: ' 8255 Control Address Dim Number As Integer: ' decimal number to count from 1 to 255 Dim Start As Integer: ' Start flag Dim Msg As String Dim Style As Integer Dim Response As Integer Dim PortSelected As Integer

    Private Sub Form_Load() ' Program is loaded with these values txtOutputWindow.Text = "Enter Base Address" Start = 0: 'Counting action not started Number = 0: 'Number to start with optPortA.Value = True ' Default port is A End Sub

    Private Sub cmdGo_Click() If Start = 0 Then

  • Copyright 2010, Dr. Vo Tuong Quan _______________________________________________________________________________________________________

    24

    ' user clicked GO button first time If txt8255Address.Text = "" Then ' Base address was not defined Msg = "Enter a Base Address! e.g. 608" ' Define message. Style = vbOK + vbExclamation ' Define buttons. Response = MsgBox(Msg, Style) Exit Sub End If Start = 1: ' Go button enabled; start counting cmdGo.Caption = "Pause" ' Assign values for all addresses BaseAddress = Val(txt8255Address.Text) PortA = BaseAddress PortB = BaseAddress + 1 PortC = BaseAddress + 2 Cntrl = BaseAddress + 3 ' determine which port to output to ' default is Port A If optPortA.Value = True Then PortSelected = PortA Print PortSelected End If If optPortB.Value = True Then PortSelected = PortB Print PortSelected End If If optPortC.Value = True Then PortSelected = PortC Print PortSelected End If ' configure all ports for output Dummy = Out8255(Cntrl, 128) ' initialize all Ports to 0 Dummy = Out8255(PortA, 0) Dummy = Out8255(PortB, 0) Dummy = Out8255(PortC, 0) Else Start = 0: ' user clicked GO button again cmdGo.Caption = "Go!" End If End Sub

    Private Sub cmdEnd_Click() Beep 'txtOutputWindow.Text = "Stopped" Dummy = Out8255(PortA, 0) Dummy = Out8255(PortB, 0) Dummy = Out8255(PortC, 0) ' quit program End End Sub

  • Copyright 2010, Dr. Vo Tuong Quan _______________________________________________________________________________________________________

    25

    Private Sub tmrTimer_Timer() If Start = 1 Then Number = Number + 1 Dummy = Out8255(PortSelected, Number) txtOutputWindow.Text = "Number = " + Str(Number) If Number = 255 Then Beep txtOutputWindow.Text = "Finished" Dummy = Out8255(PortSelected, 0) Start = 0 Number = 0 cmdGo.Caption = "Go!" Exit Sub End If Else Exit Sub End If End Sub

    Ex: Print out the decimal equivalent of the 8 position Dip switch

    Option Explicit 'Declare use of the DLL Private Declare Function Out8255 Lib "8255.dll" (ByVal PortAddress As Integer, ByVal PortData As Integer) As Integer Private Declare Function In8255 Lib "8255.dll" (ByVal PortAddress As Integer) As Integer 'Declare variables Dim BaseAddress As Integer: ' 8255 Base Address Dim Dummy As Integer: ' Dummy variable used with DLL Dim PortA As Integer: ' 8255 Port A address Dim PortB As Integer: ' 8255 Port B address Dim PortC As Integer: ' 8255 Port C address Dim Cntrl As Integer: ' 8255 Control Address

  • Copyright 2010, Dr. Vo Tuong Quan _______________________________________________________________________________________________________

    26

    Dim PortValue As Integer: ' decimal value read at port

    Dim PortValue As Integer: ' decimal value read at port Dim Start As Integer: ' Start flag Dim Msg As String Dim Style As Integer Dim Response As Integer Dim PortSelected As Integer

    Private Sub cmdEnd_Click() Beep 'txtOutputWindow.Text = "Stopped" ' quit program End End Sub

    Private Sub cmdGo_Click() If Start = 0 Then ' user clicked GO button first time If txt8255Address.Text = "" Then ' Base address was not defined Msg = "Enter a Base Address! e.g. 608" ' Define message. Style = vbOK + vbExclamation ' Define buttons. Response = MsgBox(Msg, Style) Exit Sub End If Start = 1: ' Go button enabled; start counting cmdGo.Caption = "Pause" ' Assign values for all addresses BaseAddress = Val(txt8255Address.Text) PortA = BaseAddress PortB = BaseAddress + 1 PortC = BaseAddress + 2 Cntrl = BaseAddress + 3 ' determine which port to output to ' default is Port A If optPortA.Value = True Then PortSelected = PortA End If If optPortB.Value = True Then PortSelected = PortB End If If optPortC.Value = True Then PortSelected = PortC End If ' configure all ports for input Dummy = Out8255(Cntrl, 155) ' initialize all Ports to 0 Else Start = 0: ' user clicked GO button again cmdGo.Caption = "Go!" End If

  • Copyright 2010, Dr. Vo Tuong Quan _______________________________________________________________________________________________________

    27

    End Sub

    Private Sub Form_Load() ' Program is loaded with these values txtOutputWindow.Text = "Enter Base Address" Start = 0: 'Counting action not started optPortA.Value = True ' Default port is A End Sub Private Sub tmrTimer_Timer() If Start = 1 Then PortValue = In8255(PortSelected) txtOutputWindow.Text = "Value = " + Str(PortValue) End If End Sub

    Ex: 8255 connects to ADC0808

  • Copyright 2010, Dr. Vo Tuong Quan _______________________________________________________________________________________________________

    28

  • Copyright 2010, Dr. Vo Tuong Quan _______________________________________________________________________________________________________

    29

    SERIAL COMMUNICATION STANDARD

    The electrical specifications of the serial port is contained in the EIA (Electronics Industry Association) RS232 standard. It states many parameters such as:

    1. A "Space" (logic 0) will be between +3 and +25 Volts. 2. A "Mark" (Logic 1) will be between -3 and -25 Volts. 3. The region between +3 and -3 volts is undefined.

    4. An open circuit voltage should never exceed 25 volts. (In Reference to GND)

    5. A short circuit current should not exceed 500mA. The driver should be able to handle this without damage.

  • Copyright 2010, Dr. Vo Tuong Quan _______________________________________________________________________________________________________

    30

    The com port in a PC has 9 female pins.

    Pin No Abbreviation Full Name Pin 3 TD Transmit Data Pin 2 RD Receive Data Pin 7 RTS Request To Send Pin 8 CTS Clear To Send Pin 6 DSR Data Set Ready Pin 5 SG Signal Ground Pin 1 CD Carrier Detect

    Pin 4 DTR Data Terminal Ready Pin 9 RI Ring Indicator

    There are two kinds of serial communication

    The speed of transmission is called Baud The width of bit also express the speed of communication. Ex, the transmission data has the width of bit is 20ms, this means that it can be transmit 1/20ms = 50 bit in 1 second. Then, it can be said that the transissin

  • Copyright 2010, Dr. Vo Tuong Quan _______________________________________________________________________________________________________

    31

    speed is 50 bit per second or the baud rate is 50bps. Baud (Baud is number of bits transmitted/sec, including start, stop, data and parity). Some typical Baud of serial communications: 300, 600, 1200, 2400, 4800, 9600, 19200,.., 56000, 115200,.

    Ex: Null Modem

    1. SYNCHRONOUS COMMUNICATION Sender and receiver must synchronize.

    Block of data can be sent.

    More efficient.

    Expensive

    Synchronous transmit/receive diagram

    Synchronous transfer does not transfer extra bits. However, it requires clock signal.

  • Copyright 2010, Dr. Vo Tuong Quan _______________________________________________________________________________________________________

    32

    Synchronous transmit/receive diagram

    2. ASYNCHRONOUS COMMUNICATION - Each byte is encoded for transmission. - No need for sender and receiver synchronization.

    Asynchronous transmit/receive diagram

    Asynchronous transfer does not require clock signal. However, it transfers extra bits (start bits and stop bits) during data communication.

  • Copyright 2010, Dr. Vo Tuong Quan _______________________________________________________________________________________________________

    33

    Asynchronous data transfer diagram

    In the RS232 standard, logic 1 is named Mark (-10V) and logic 0 is named Space (+10V).

    When not active, the transmission line is in the state of Mark.

    When starting transmission, the Start bit is transmitted first, then 8 data bit are followed (The LSB bit is transmit first then the MSB bit is transmitted in the end).

  • Copyright 2010, Dr. Vo Tuong Quan _______________________________________________________________________________________________________

    34

    3. PORT ADDRESS ON PC COM1 3F8 COM2 2F8

    COM3 3E8 COM4 2E8

    4. INTERFACE CHIP The MAX232 (Figure 4-1) includes two drivers that convert TTL or CMOS inputs to RS-232 outputs and two receivers that convert RS-232 inputs to TTL/CMOS-compatible outputs. The drivers and receivers also invert the signals.

    This chip contains two charge-pump voltage converters that act as tiny,

    unregulated power supplies that enable the chip to support loaded RS-232 outputs of 5V or greater. Four external capacitors store energy for the supplies. The recommended value for the capacitors is 1F or larger. If using polarized capacitors, take care to get the polarities correct when you put the circuit together.

  • Copyright 2010, Dr. Vo Tuong Quan _______________________________________________________________________________________________________

    35

    The voltage at pin 6 is negative, so its capacitors + terminal connects to ground. Because the outputs can be as high as 10V, be sure the capacitors are rated for a working voltage direct current (WVDC) of at least 15V.

  • Copyright 2010, Dr. Vo Tuong Quan _______________________________________________________________________________________________________

    36

    5. PROGRAMMING SERIAL COMMUNICATION USING PC The controller of the serial communication on PC is called UART (Universal Asynchronous Receiver Transmitter). Some typical UART controllers are: 8250, 8250A, 16550, 16650, 16750, In these UART controller, we just focus on some registers supply for programming PC.

    Base Address DLAB Read/Write Abr. Register Name

    =0 Write - Transmitter Holding Buffer

    =0 Read - Receiver Buffer + 0

    =1 Read/Write - Divisor Latch Low Byte

    =0 Read/Write IER Interrupt Enable Register + 1

    =1 Read/Write - Divisor Latch High Byte

    - Read IIR Interrupt Identification Register + 2

    - Write FCR FIFO Control Register

    + 3 - Read/Write LCR Line Control Register

    + 4 - Read/Write MCR Modem Control Register

    + 5 - Read LSR Line Status Register

    + 6 - Read MSR Modem Status Register

  • Copyright 2010, Dr. Vo Tuong Quan _______________________________________________________________________________________________________

    37

    + 7 - Read/Write - Scratch Register

    DLAB (Divisor Latch Access Bit) is the bit 7 of LCR 0: Programming data frame 1: Programming communication speed. User can reprogramming the communication speed by reload the value of divisor of the UART. Divisor = Clock frequency/(speed x 16)

    Ex: UART uses clock has frequency of 1.8432MHz and the desired speed is 9600 bauds. Then, the divisor is: Divisor = 1843200/(9600 x 16) = 12 + SOME TYPICLE REGISTERS

    LINE CONTROL REGISTER - LCR (+3) 1 Divisor Latch Access Bit Bit 7

    0 Access to Receiver buffer, Transmitter buffer & Interrupt Enable Register Bit 6 Set Break Enable

    Bit 5

    Bit 4

    Bit 3 Parity Select

    X X 0 No Parity 0 0 1 Odd Parity

    Bits 3, 4 And 5

    0 1 1 Even Parity Length of Stop Bit

    0 One Stop Bit Bit 2

    1 2 Stop bits for words of length 6,7 or 8 bits or 1.5 Stop Bits for Word lengths of 5 bits. Bit 1

    Bit 0 Word Length

    0 0 5 Bits 0 1 6 Bits 1 0 7 Bits

    Bits 0 And 1

    1 1 8 Bits

  • Copyright 2010, Dr. Vo Tuong Quan _______________________________________________________________________________________________________

    38

    PARITY A parity bit is a bit that is added to ensure that the number of bits with the value one in a set of bits is even or odd. Parity bits are used as the simplest form of error detecting code.

    There are two variants of parity bits: even parity bit and odd parity bit. When using even parity, the parity bit is set to 1 if the number of ones in a given set of bits (not including the parity bit) is odd, making the entire set of bits (including the parity bit) even. When using odd parity, the parity bit is set to 1 if the number of ones in a given set of bits (not including the parity bit) is even, keeping the entire set of bits (including the parity bit) odd. However, parity has the advantage that it uses only a single bit and requires only a number of XOR gates to generate. If an odd number of bits (including the parity bit) are transmitted incorrectly, the parity bit will be incorrect and thus indicates that an error occurred in transmission. The parity bit is only suitable for detecting errors; it cannot correct any errors, as there is no way to determine which particular bit is corrupted. The data must be discarded entirely, and re-transmitted from

    scratch.

    Ex: The parity bit can be computed as follows, assuming we are sending a simple 4-bit value 1001 with the parity bit following on the right, and with ^ denoting an XOR gate:

    Transmission sent using even parity: A wants to transmit: 1001

    A computes parity bit value: 1^0^0^1 = 0 A adds parity bit and sends: 10010 B receives: 10010

    B computes parity: 1^0^0^1^0 = 0

  • Copyright 2010, Dr. Vo Tuong Quan _______________________________________________________________________________________________________

    39

    B reports correct transmission after observing expected even result.

    Transmission sent using odd parity:

    A wants to transmit: 1001 A computes parity bit value: ~(1^0^0^1) = 1 A adds parity bit and sends: 10011 B receives: 10011 B computes overall parity: 1^0^0^1^1 = 1

    B reports correct transmission after observing expected odd result.

    Transmission sent using even parity: A wants to transmit: 1001 A computes parity bit value: 1^0^0^1 = 0 A adds parity bit and sends: 10010 *** ERROR CASE ***

    B receives: 11010

    B computes overall parity: 1^1^0^1^0 = 1 B reports incorrect transmission after observing unexpected odd result.

    B calculates an odd overall parity indicating the bit error. Here's the same example but now the parity bit itself gets corrupted: A wants to transmit: 1001 A computes even parity value: 1^0^0^1 = 0

    A sends: 10010 *** ERROR CASE ***

    B receives: 10011

    B computes overall parity: 1^0^0^1^1 = 1 B reports incorrect transmission after observing unexpected odd result.

  • Copyright 2010, Dr. Vo Tuong Quan _______________________________________________________________________________________________________

    40

    Special case A wants to transmit: 1001 A computes even parity value: 1^0^0^1 = 0 A sends: 10010 *** ERROR CASE*** B receives: 11011 B computes overall parity: 1^1^0^1^1 = 0 B reports correct transmission though actually incorrect. If there are two bits error, parity can not check!

    LINE STATUS REGISTER - LSR (+5) Bit Notes

    Bit 7 Error in Received FIFO Bit 6 Empty Data Holding Registers Bit 5 Empty Transmitter Holding Register Bit 4 Break Interrupt Bit 3 Framing Error Bit 2 Parity Error Bit 1 Overrun Error Bit 0 Data Ready

    Bit 0 = 1: UART receive 1 character

    INTERRUPT IDENTIFICATION REGISTER - IIR (+2) Bit Notes

    Bit 6

    Bit 7

    0 0 No FIFO 0 1 FIFO Enabled but Unusable

    Bits 6 and 7

    1 1 FIFO Enabled Bit 5 64 Byte Fifo Enabled (16750 only) Bit 4 Reserved

    0 Reserved on 8250, 16450 Bit 3 1 16550 Time-out Interrupt Pending

    Bits 1 and 2

    Bit 2

    Bit 1

  • Copyright 2010, Dr. Vo Tuong Quan _______________________________________________________________________________________________________

    41

    0 0 Modem Status Interrupt

    0 1 Transmitter Holding Register Empty Interrupt 1 0 Received Data Available Interrupt

    1 1 Receiver Line Status Interrupt 0 Interrupt Pending Bit 0 1 No Interrupt Pending

    INTERRUPT ENABLE REGISTER IER (+1) Bit Notes

    Bit 7 Reserved Bit 6 Reserved Bit 5 Enables Low Power Mode (16750) Bit 4 Enables Sleep Mode (16750) Bit 3 Enable Modem Status Interrupt Bit 2 Enable Receiver Line Status Interrupt Bit 1 Enable Transmitter Holding Register Empty Interrupt Bit 0 Enable Received Data Available Interrupt

    MODEM CONTROL REGISTER MCR (+4) Bit Notes

    Bit 7 Reserved Bit 6 Reserved Bit 5 Autoflow Control Enabled (16750 only) Bit 4 LoopBack Mode Bit 3 Aux Output 2 Bit 2 Aux Output 1 Bit 1 Force Request to Send Bit 0 Force Data Terminal Ready

  • Copyright 2010, Dr. Vo Tuong Quan _______________________________________________________________________________________________________

    42

    Ex: Transmit/Receive serial data #include #include #include #define PORT1 0x3F8 void main(void) { int C;

    int ch;

    outportb(PORT1 + 1, 0) ; // Not using interrupt on Port1 outportb(PORT1 + 3, 0x38); // Set Dlab on outportb(PORT1 + 0, 0) ; //Set baud rate divisor latch low byte // Default 0x03 = 38400 bps // 0x01 = 115200 bps // 0x02 = 56700 bps // 0x06 = 19200 bps // 0x0C= 9600 bps // 0x18 = 4800 bps // 0x30 = 2400 bps outportb(PORT1 + 1, 0x00) ; //Set baud rate divisor latch high byte outportb(PORT1 + 3, 0x03) ; //8 bit, No parity, 1 stop bit outportb(PORT1 + 2, 0xC7) ; // Enable FIFO outportb(PORT1 + 4, 0x0B) ; //Turn on DTR, RST, and OUT2

    do { c = inportb(PORT1 + 5); // Check data is received or not if (c & 1) {

  • Copyright 2010, Dr. Vo Tuong Quan _______________________________________________________________________________________________________

    43

    ch = inportb(PORT1);} if (kbhit()) { ch = getch(); outportb(PORT1, ch);} } while (ch != 27); }

  • Copyright 2010, Dr. Vo Tuong Quan _______________________________________________________________________________________________________

    44

    EXERCISEEXERCISEEXERCISEEXERCISE::::

    Using COM Port control many actuatorsUsing COM Port control many actuatorsUsing COM Port control many actuatorsUsing COM Port control many actuators

    Type 1Type 1Type 1Type 1