PC Interfacing Lecture
PC Interfacing Lecture
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,
2
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) EPP (Enhanced Paprallel Port)
3
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
SPP Signal nStrobe Data 0 Data 1 Data 2 Data 3 Data 4 Data 5 Data 6 Data 7 nAck Busy Paper-Out / Paper-End 4
Direction In/out In/Out Out Out Out Out Out Out Out Out In In In
Register Control Data Data Data Data Data Data Data Data Status Status Status
Yes
13 14 15 16 17 18 - 25
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 Base + 0 Name Data Register Read/Write Write Bit No. Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0 Pin Properties 9 8 7 6 5 4 3 2 Data 7 Data 6 Data 5 Data 4 Data 3 Data 2 Data 1 Data 0
Some mainboard support the function of Bi-directional, these pins can be used as the input port.
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 directions)
Offset Name Read/Write Bit No. Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0 Pin X X X X 17 16 14 1 Properties Unused Unused Enable Bi-Directional Port Enable IRQ Via Ack Line /Select Printer /Initialize Printer (Reset) /Auto Linefeed /Strobe Base + Control Register Read/Write 2
Can communicate in 2
These 4 pins or 4 bits of the control register have hardware inverted functions.
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 can we do on these pins to make sure that the real data is received the NOT gate at these pins) How (Input
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:
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.
8
Ex:
#include <conio.h> #define dataReg 0x378 #define statusReg 0x379 int dummy; dummy = _outp(dataReg,0xFE); dummy = _inp(statusReg) // Consider Mask algorithm for sure of the input signals ..
// // //
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
#define PORTADDRESS 0x378 #define DATA PORTADDRESS+0 #define STATUS PORTADDRESS+1 #define CONTROL PORTADDRESS+2
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 <= 2; count++) { outportb(DATA, init[count]); outportb(CONTROL,inportb(CONTROL) | 0x01); // Set Strobe (Enable) delay(20); // Larger Delay for INIT
outportb(CONTROL,inportb(CONTROL) & 0xFE); // Reset Strobe (Enable) delay(20); } // Larger Delay for INIT
//Reset Select Printer (Register Select) outportb(CONTROL, inportb(CONTROL) & 0xF7); len = strlen(string);
for (count = 0; count < len; count++) { outportb(DATA, string[count]); outportb(CONTROL,inportb(CONTROL) | 0x01); /* Set Strobe */ delay(2); outportb(CONTROL,inportb(CONTROL) & 0xFE); /* Reset Strobe */ delay(2); } }
10
- 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
11
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. .
12
13
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. - CS = 0 : 8255 run. - RESET: Let the 8255 to the initial state. -
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.
15
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.
16
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.
17
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.
18
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.
19
INTE A Controlled by bit set/reset of PC4. INTE B Controlled by bit set/reset of PC2.
OUTPUT MODE
20
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.
22
' 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
24
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 25
PortValue As Integer: ' decimal value read at port Start As Integer: ' Start flag Msg As String Style As Integer Response As Integer 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 26
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
27
28
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.
29
Pin No Abbreviation Pin 3 TD Pin 2 RD Pin 7 RTS Pin 8 Pin 6 Pin 5 Pin 1 Pin 4 Pin 9 CTS DSR SG CD DTR RI
Full Name Transmit Data Receive Data Request To Send Clear To Send Data Set Ready Signal Ground Carrier Detect Data Terminal Ready 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
30
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.
31
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.
32
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).
33
3. PORT ADDRESS ON PC COM1 COM2 COM3 COM4 3F8 2F8 3E8 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.
34
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.
35
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. =0 +0 =0 =1 =0 +1 =1 +2 +3 +4 +5 +6 Write FCR FIFO Control Register Read/Write LCR Line Control Register Read/Write MCR Modem Control Register Read Read LSR Line Status Register MSR Modem Status Register
36
Register Name Transmitter Holding Buffer Receiver Buffer Divisor Latch Low Byte
Read/Write IER Interrupt Enable Register Read/Write Read Divisor Latch High Byte
+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)
Bit 7 1 0 Divisor Latch Access Bit Access to Receiver buffer, Transmitter buffer & Interrupt Enable Register Bit 6 Set Break Enable Bits 3, 4 And Bit Bit Bit Parity Select 5 5 4 3 X 0 Bit 2 X 0 0 1 No Parity Odd Parity
One Stop Bit 2 Stop bits for words of length 6,7 or 8 bits or 1.5 Stop 1 Bits for Word lengths of 5 bits. Bits 0 And 1 Bit Bit Word Length 1 0 0 0 1 1 0 1 0 1 5 Bits 6 Bits 7 Bits 8 Bits
37
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: B computes parity: 10010 1^0^0^1^0 = 0
38
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: B computes overall parity: 10011 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: B computes overall parity: 11010 1^1^0^1^0 = 1
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: A computes even parity value: A sends: *** ERROR CASE *** B receives: B computes overall parity: 10011 1^0^0^1^1 = 1 1001 1^0^0^1 = 0 10010
39
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 Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0 Notes Error in Received FIFO Empty Data Holding Registers Empty Transmitter Holding Register Break Interrupt Framing Error Parity Error Overrun Error Data Ready
0 0 1 Bit 0 1 0 1
0 1 0
Modem Status Interrupt Transmitter Holding Register Empty Interrupt Received Data Available Interrupt
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
Bit 3 Aux Output 2 Bit 2 Aux Output 1 Bit 1 Force Request to Send Bit 0 Force Data Terminal Ready
41
Ex: Transmit/Receive serial data #include <dos.h> #include <stdio.h> #include <conio.h> #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
43
44