Professional Documents
Culture Documents
Vo Tuong Quan
_______________________________________________________________________________________________________
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.
DISADVANTAGES
Many data line
Weak in noise avoidance
The transmit/receive distance is short (about < 15 meters)
SPP Signal
Direction
In/out
Register
Hardware
Inverted
nStrobe
In/Out
Control
Yes
Data 0
Out
Data
Data 1
Out
Data
Data 2
Out
Data
Data 3
Out
Data
Data 4
Out
Data
Data 5
Out
Data
Data 6
Out
Data
Data 7
Out
Data
10
nAck
In
Status
11
Busy
In
Status
12
Paper-Out / Paper-End
In
Status
Yes
13
Select
In
Status
14
nAuto-Linefeed
In/Out
Control
15
nError / nFault
In
Status
16
nInitialize
In/Out
Control
17
nSelect-Printer / nSelect-In
In/Out
Control
18 - 25
Ground
Gnd
Yes
Yes
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
Base +
0
Data
Register
Write
Bit 7
Data 7
Bit 6
Data 6
Bit 5
Data 5
Bit 4
Data 4
Bit 3
Data 3
Bit 2
Data 2
Bit 1
Data 1
Bit 0
Data 0
STATUS REGISTER
Address: base address + 1
Range: 5 bits input
Offset
Base + 1
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
/IRQ (Not)
Bit 1
Reserved
Bit 0
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
Unused
Bit 6
Unused
Bit 5
Bit 4
Bit 3
17
/Select Printer
Bit 2
16
Bit 1
14
/Auto Linefeed
Bit 0
/Strobe
These 4 pins or 4 bits of the control register have hardware inverted functions.
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;
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
..
//
//
//
#include <dos.h>
#include <string.h>
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
10
Visual Basic
We use some supported DLL (Inpout.dll, Port.dll,)
- Port.dll
Attribute VB_Name = "varPortDll"
Option Explicit
Declare
Declare
Declare
Declare
Declare
Declare
Declare
Declare
Declare
Declare
Declare
Declare
Declare
Declare
Declare
Declare
Declare
Declare
Declare
Declare
Declare
Declare
Declare
Declare
Declare
Declare
Declare
Declare
Declare
Declare
Declare
Declare
Declare
Declare
Declare
- 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
Exercises:
1. Control Led (Led run left direction, right direction, stop)
2. Stepmotor (motor run CW, CCW, Stop).
3. .
4. .
12
13
Function diagram
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
17
Input
Output
The 4-bit port is used for control and status of the 8-bit port.
INPUT MODE
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
24
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
Dim
Dim
Dim
Dim
Dim
Dim
End Sub
27
28
29
Pin No Abbreviation
Pin 3
TD
Pin 2
RD
Pin 7
RTS
Full Name
Transmit Data
Receive Data
Request To Send
Pin 8
CTS
Clear To Send
Pin 6
Pin 5
Pin 1
DSR
Pin 4
DTR
Data Terminal
Ready
Pin 9
RI
Ring Indicator
SG
CD
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
31
2. ASYNCHRONOUS COMMUNICATION
- Each byte is encoded for transmission.
- No need for sender and receiver synchronization.
33
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.
34
35
+0
Register Name
=0
Write
=0
Read
Receiver Buffer
=1
Read/Write
=0
=1
Read/Write
Read
Write
+1
-
+2
+3
+4
+5
Read
+6
Read
+7
Read/Write
Scratch Register
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
X
0
Bit 2
X
0
0
1
No Parity
Odd Parity
0
1
1 Even Parity
Length of Stop Bit
0
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
10010
B computes parity:
1^0^0^1^0 = 0
38
1001
10011
1^0^0^1^1 = 1
1001
11010
1^1^0^1^0 = 1
1001
1^0^0^1 = 0
A sends:
10010
10011
1^0^0^1^1 = 1
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
Notes
Error in Received FIFO
Empty Data Holding Registers
Bit 5
Bit 4
Bit 3
Bit 2
Bit 1
Parity Error
Overrun Error
Bit 0
Data Ready
Notes
0
1
Bit 0
1
0
No Interrupt Pending
Notes
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
Notes
Reserved
Reserved
Autoflow Control Enabled (16750 only)
Bit 4
LoopBack Mode
41
do {
c = inportb(PORT1 + 5); // Check data is received or not
if (c & 1) {
42
ch = inportb(PORT1);}
if (kbhit()) {
ch = getch();
outportb(PORT1, ch);}
}
while (ch != 27);
}
43
EXERCISE:
EXERCISE:
Using COM Port control many actuators
Type 1
44