7/24/2019 Bai Giang Chuong 9 c Lap Trinh Giao Tiep May Tinh Va Thiet Bi 5947 http://slidepdf.com/reader/full/bai-giang-chuong-9-c-lap-trinh-giao-tiep-may-tinh-va-thiet-bi-5947 1/20 1 1 K THU#T L#P TRÌNH CH!NG 9 L#P TRÌNH GIAO TI%P MÁY TÍNH VÀ THI %T B' 2 M"c l "c ch$%ng 9 • 9.1. Giao ti &p qua c*ng RS232 • 9.2. Giao ti &p qua c*ng song song • 9.3. Giao ti &p qua c*ng n-i ti&p • 9.4. Giao ti &p qua c*ng máy in 3 GIAO TI .P QUA C0NG RS232 CH!NG 9 4 Giao ti&p qua c*ng RS232 • C ng RS232 là c ng thông d $ng nh&t trong truy )n d , li /u gi , a máy tính và thi 0t b 1 •M2c dù c*ng RS232 ph5c t7p h%n c*ng song song nh$ng nó là c*ng h8u hi :u nh;t trong truy<n d8 li :u, có th > là wireless trong m?t s- ngành và ít t-n chi phí. • Liên k 0t c ng RS232 b3ng dây và dây này có 3 ch 5 c n7ng chính: “truy )n”, “nh8n”, “n9i :&t”, ... 5 Giao ti&p qua c*ng RS232 6 Giao ti&p qua c*ng RS232 7 Giao ti&p qua c*ng RS232 • Dây “nh@n” và “truy<n” này A$Bc k&t n-i gi8a 2 máy tính vCi nhau. •D8 li :u A$Bc truy<n theo chuDi. Có 2 chân chính là TXD và RXD. Các chân khác cFa c*ng là RTS, CTS, DSR, DTR, and RTS, RI •D8 li :u ‘1’ và ‘0’ A$Bc xác AGnh theo m5c Ai:n áp 3V to 25V và -3V to - 25V 8 Giao ti&p qua c*ng RS232 • I2c tính Ai:n áp cFa c*ng n-i ti &p theo EIA ( Electronics Industry Association) RS232C tiêu chuJn có t-c A? baud rate là 20,000bps, khá th;p so vCi tiêu chuJn t -c A? hi:n nay. • Vì lý do này, chúng ta nên chKn c*ng RS-232D vCi tiêu chuJn mCi và A$Bc áp d"ng trên các máy tính th& h: mCi hi :n nay 9 Giao ti&p qua c*ng RS232 • Baud: t 9c :; truy )n d , li /u. • Các t-c A? thi&t l@p: 300, 600, 1200, 2400, 4800, 9600, 19200,
20
Embed
Bai Giang Chuong 9 c Lap Trinh Giao Tiep May Tinh Va Thiet Bi 5947
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
7/24/2019 Bai Giang Chuong 9 c Lap Trinh Giao Tiep May Tinh Va Thiet Bi 5947
1 K THU#T L#P TRÌNHCH!NG 9L#P TRÌNH GIAO TI%P MÁY TÍNH VÀ THI%T B'
2 M"c l"c ch$%ng 9• 9.1. Giao ti&p qua c*ng RS232• 9.2. Giao ti&p qua c*ng song song• 9.3. Giao ti&p qua c*ng n-i ti&p• 9.4. Giao ti&p qua c*ng máy in
3 GIAO TI.P QUA C0NG RS232CH!NG 9
4 Giao ti&p qua c*ng RS232• C ng RS232 là c ng thông d $ng nh&t trong truy )n d , li /u gi , a máy tính
và thi 0t b 1
• M2c dù c*ng RS232 ph5c t7p h%n c*ng song song nh$ng nó là c*ng h8uhi:u nh;t trong truy<n d8 li:u, có th> là wireless trong m?t s- ngành và ítt-n chi phí.
• Liên k 0t c ng RS232 b3ng dây và dây này có 3 ch5 c n7ng chính: “truy )n”,
“nh8n”, “n9i :&t”, ...
5 Giao ti&p qua c*ng RS232
6 Giao ti&p qua c*ng RS232
7 Giao ti&p qua c*ng RS232• Dây “nh@n” và “truy<n” này A$Bc k&t n-i gi8a 2 máy tính vCi nhau.• D8 li:u A$Bc truy<n theo chuDi. Có 2 chân chính là TXD và RXD. Các
chân khác cFa c*ng là RTS, CTS, DSR, DTR, and RTS, RI• D8 li:u ‘1’ và ‘0’ A$Bc xác AGnh theo m5c Ai:n áp 3V to 25V và -3V to -
25V
8 Giao ti&p qua c*ng RS232• I2c tính Ai:n áp cFa c*ng n-i ti&p theo EIA (Electronics Industry
Association) RS232C tiêu chuJn có t-c A? baud rate là 20,000bps, kháth;p so vCi tiêu chuJn t-c A? hi:n nay.
• Vì lý do này, chúng ta nên chKn c*ng RS-232D vCi tiêu chuJn mCi vàA$Bc áp d"ng trên các máy tính th& h: mCi hi:n nay
9 Giao ti&p qua c*ng RS232• Baud: t 9c :; truy )n d , li /u.
• Kí hi /u baud t -< ng 5 ng v = i s9 bit truy )n :-> c trong 1 giây
• Có 1 bit bLt AMu và 1 bit dNng A$Bc gPi kèm theo vCi 1 byte d8 li:u
• à
n&u AGnh 9600 baud thì có t-c A? truy<n 960 byte/giây10 Giao ti&p qua c*ng RS232
• C ng RS232D hi /n : ang t ?n t @i A 2 d @ng: D lo@i 25 chân và D lo@i 9 chân,
và c B 2 d @ng này A d @ng :Cu “cái” n3m A phía sau máy tính.
• Chúng ta cMn có m?t AMu “AQc” A> k&t n-i tN máy tính này sang máy tínhkhác.
• Các chân ra cFa D-9 và D-25 A$Bc trình bày d$Ci Aây:
11 Giao ti&p qua c*ng RS232
12 Giao ti&p qua c*ng RS232
13 Giao ti&p qua c*ng RS232
14 Giao ti&p qua c*ng RS232
15 Giao ti&p qua c*ng RS232• Các thi&t bG dùng các c*ng n-i ti&p cho giao ti&p A$Bc phân làm hai lo7i. Chúng gRm DCE (Data
Communications Equipment ) và DTE (Data Terminal Equipment )• DCE là các thi&t bG t$%ng tQ modem thi&t bG, TA adapter, máy in chSng h7n trong khi DTE là máy
tính b7n ho2c các thi&t bG ngo7i vi.• M?t DTE là m?t máy tính và m?t modem là DCE. Thông th$Tng, mKi ng$Ti hay nói v< DTE tCi
DCE ho2c DCE tCi các b? phát DCE.• B? truy<n DTE tCi DCE là gi8a modem và máy tính cFa chúng ta (Aôi khi A$Bc xem nh$ là m?t b?
thi&t bG ngo7i vi ngoài). Ii<u này cho th;y truy<n dQ li:u r ;t nhanh h%n so vCi tN DCE A&n DCE.DCE to DCE là k&t n-i gi8a các modem, Aôi khi còn A$Bc gKi là b? k&t n-i các A$Tng truy<n .
16 Giao ti&p qua c*ng RS232• HMu h&t hi:n nay ng$Ti dùng ph* bi&n các modem 28.8K ho2c 33.6K. Do v@y, chúng
ta nên sP d"ng các modem có t-c A? DCE A&n DCE là 28.8K ho2c 33.6K.• N&u cMn lo7i modem có t-c A? cao, chúng ta nên dùng DTE A&n DCE có t-c A?
khoUng 115,200 BPS. (T-c A? t-i Aa là 16550a UART) .• Các ch$%ng trình giao ti&p chúng ta sP d"ng A$Bc thi&t l@p cho DCE A&n DTE. Tuy
nhiên, t-c A? thi&t l@p là 9.6 KBPS, 144 KBPS,... và theo t-c A? modem có th>truy<n.
17 Giao ti&p qua c*ng RS232• N&u chúng ta chuy>n d8 li:u d7ng file text t7i 28.8K (DCE- DCE), thì modem sV nén
file này l7i và t-c A? truy<n thQc t& là 115.2 KBPS gi8a các máy tình và do Aó, t-c A?truy<n gi8a DCE- DTE là 115.2 KBPS.
• Vì v@y, Ai<u này lý giUi t7i sao DCE- DTE nên có t-c A? k&t n-i cao h%n t-c A? k&tn-i cFa modem. Do Aó, n&u t-c A? DTE A&n DCE cFa chúng ta nhi<u lMn nhanh h%nt-c A? DCE A&n DCE, PC có th> gPi d8 li:u A&n modem cFa chúng ta W t-c A?115,200 BPS.
18 Giao ti&p qua c*ng RS232• NULL MODEM (mod em r ng):
• Null modem A$Bc dùng A> k&t n-i gi8a hai DTE vCi nhau.• Th$Tng A$Bc sP d"ng A> truy<n các file d8 li:u gi8a các máy tính, sP
d"ng các giao th5c (protocol) nh$ Zmodem, xmodem,...
19 Giao ti&p qua c*ng RS232
20 Giao ti&p qua c*ng RS232• Hình vV A$Bc A< c@p W trên cho th;y k&t n-i các dây cFa null modem. I2c tính chính A$Bc
chX ra W Aây là sP d"ng m?t máy tính A> trao A*i mKi th5 (chat) vCi modem gMn gi-ng nh$ vCi m?t máy tính khác.
• Máy tính khách (guest) và máy tính chD (host) :-> c k 0t n9i thông qua các ch9t TD, RD, và
SG.• B;t kY d8 li:u A$Bc truy<n thông qua dây TD tN máy chF A&n máy khách, A$Bc nh@n theo
dây RD. Máy tính chF phUi A$Bc thi&t l@p thông s- t$%ng tQ máy tính khách.• Tín hi:u dây n-i A;t_signal ground (SG) cFa cU hai máy tính phUi A$Bc ngLn l7i.
21 Giao ti&p qua c*ng RS232• Data Terminal Ready (DTR) A$Bc n-i kín tN b? Data Set Ready và Carrier Detect
trên cU hai máy tính.• Khi b? Data Terminal Ready A$Bc xác nh@n ho7t A?ng, thì Data Set Ready và
Carrier Detect ngay l@p t5c A$Bc ho7t A?ng.• [ Ai>m này, máy tính A$Bc xem nh$ là modem Uo Virtual Modem A$Bc k&t n-i s\n
sàng và A$Bc phát hi:n bWi các modem truy<n d8 li:u khác.
22 Giao ti&p qua c*ng RS232• All left to worry about now is the Request to Send and Clear To Send.• As both computers communicate together at the same speed, flow control is not
needed thus these two lines are also linked together on each computer.• When the computer wishes to send data, it asserts the Request to Send high and as
it is hooked together with the Clear to Send, It immediately gets a reply that it is ok tosend and does so.
23 Giao ti&p qua c*ng RS232• The Ring indicator line is only used to tell the computer that there is a
ringing signal on the phone line. As we do not have, a modem connectedto the phone line this is left disconnected
24 Giao ti&p qua c*ng RS232• To know about the RS232 ports available in your computer, Right click on "My
Computer", Goto 'Properties', Select tab 'Device Manager', go to Ports( COM & LPT), In that you will find 'Communication Port(Com1)' etc.
• If you right click on that and go to properties, you will get device status. Make surethat you have enabled the port( Use this port is selected).
• There are two popular methods of sending data to or from the serial port inTurbo C.
• One is using outportb(PORT_ID, DATA) or outport(PORT_ID,DATA)
defined in “dos.h”• Another method is using bioscom() function defined in “bios.h”
26 Giao ti&p qua c*ng RS232• Using outp ortb() :
• The function outportb () sends a data byte to the port ‘PORT_ID’. The functionoutport() sends a data word. These functions can be used for any port includingserial port, parallel ports. Similarly to receive data these are used.
• inport reads a word from a hardware port• inportb reads a byte from a hardware port• outport outputs a word to a hardware port• outportb outputs a byte to a hardware port
31 Giao ti&p qua c*ng RS232• Return Value:• # inport and inportb return the value read• # outport and outportb do not return
32 Giao ti&p qua c*ng RS232• Using b ioscom:
• The macro bioscom () and function _bios_serialcom() are used in this method in the serialcommunication using RS-232 connecter.
• First we have to set the port with the settings depending on our need and availability. In thismethod, same function is used to make the settings using control word, to send data to the portand check the status of the port.
• These actions are distinguished using the first parameter of the function. Along with that we aresending data and the port to be used to communicate.
• Here are the deatails of the Turbo C Functions for communication ports.
33 Giao ti&p qua c*ng RS232• Declaration: (Cú pháp)• bioscom(int cmd, char abyte, int port)• _bios_serialcom(int cmd ,int port, char abyte)• bioscom() and _bios_serialcom() uses the bios interrupt 0x14 to perform
various communicate the serial communication over the I/O ports givenin port.
• cmd: The I/O operation to be performed.
34 Giao ti&p qua c*ng RS232
35 Giao ti&p qua c*ng RS232• port id : port to which data is to be sent or from which data is to be read.
• 0: COM1
1: COM2
2: COM3
36 Giao ti&p qua c*ng RS232• abyte:
• When cmd =2 or 3 (_COM_SEND or _COM_RECEIVE) parameter abyteis ignored.
• When cmd = 0 (_COM_INIT), abyte is an OR combination of the followingbits (One from each group):
• the communications port is set to1200 baud (0x80 = _COM_1200)Odd parity (0x08 = _COM_ODDPARITY)1 stop bit (0x00 = _COM_STOP1)8 data bits (0x03 = _COM_CHR8)
• To initialise the port with above settings we have to write,• bios com (0, 0x8B, 0);
39 Giao ti&p qua c*ng RS232• To send a data to COM1, the format of the function will be bioscom(1,
data, 0).• Similarly bioscom(1, 0, 0 ) will read a data byte from the port.
40 Giao ti&p qua c*ng RS232
41 Giao ti&p qua c*ng RS232• When you compile and run the above program in both the computers, The characters typed in one
computer should appear on the other computer screen and vice versa.• Initially, we set the port to desired settings as defined in macro settings. Then we waited in an idle
loop until a key is pressed or a data is available on the port. If any key is pressed, then kbhit()function returns non zero value.
• So will go to getch function where we are finding out which key is pressed. Then we are sending itto the com port. Similarly, if any data is available on the port, we are receiving it from the port anddisplaying it on the screen.
42 Giao ti&p qua c*ng RS232• To check the port, If you have a single computer, you can use loop-back
connection as follows.
• This is most commonly used method for developing communicationprograms. Here, data is transmitted to that port itself. Loop-back plugconnection is as follows.
43 Giao ti&p qua c*ng RS232
44 Giao ti&p qua c*ng RS232• If you run the above program with the connection as in this diagram, the character entered
in the keyboard should be displayed on the screen.• This method is helpful in writing serial port program with single computer. Also you can
make changes in the port id if your computer has 2 rs232ports.• You can connect the com1 port to com2 of the same computer and change the port id in the
program.
• The data sent to the port com1 should come to port com2. then also whatever you type inthe keyboard should appear on the screen.
• Each printer port consists of three port addresses; data, status and controlport.
• These addresses are in sequential order. That is, if the data port is ataddress 0x0378, the corresponding status port is at 0x0379 and the controlport is at 0x037a.
47 Giao ti&p qua c*ng máy in• Printer: LPT1, LPT2, LPT3
• Data port: 0x03bc, 0x0378, 0x0278
• Status: 0x03bd, 0x0379, 0x0279
• Control: 0x03be, 0x037a, 0x027a
• To definitively identify the assignments for a particular machine, use the DOS debugprogram to display memory locations 0040:0008. For example:
48 Giao ti&p qua c*ng máy in• Note in the example that LPT1 is at 0x0378, LPT2 at 0x0278 and LPT3
and LPT4 are not assigned.• Thus, for this hypothetical machine;• Printer: LPT1, LPT2, LPT3, LPT4• Data port: 0x0378, 0x0278, none, none• Status: 0x0379, 0x0279• Control: 0x037a, 0x027a
49 Giao ti&p qua c*ng máy in• An alternate technique is to run Microsoft Diagnostics (MSD.EXE) and
review the LPT assignments.
50 Giao ti&p qua c*ng máy in• Outputs
51 Giao ti&p qua c*ng máy in
52 Giao ti&p qua c*ng máy in• Note that there are eight outputs on the Data Port (Data 7(msb) - Data 0) and four
additional outputs on the low nibble of the Control Port. /SELECT_IN, INIT, /AUTOFEED and /STROBE.• [Note that with /SELECT_IN, the "in" refers to the printer. For normal printer
operation, the PC exerts a logic zero to indicate to the printer it is selected. Theoriginal function of INIT was to initialize the printer, AUTO FEED to advance thepaper. In normal printing, STROBE is high. The character to be printed is output onthe Data Port and STROBE is momentarily brought low.]
53 Giao ti&p qua c*ng máy in• All outputs on the Data Port are true logic. That is, writing a logic one to a bit causes
the corresponding output to go high.
• However, the /SELECT_IN, /AUTOFEED and /STROBE outputs on the Control Porthave inverted logic.
• That is, outputting a logic one to a bit causes a logic zero on the correspondingoutput. This adds some complexity in using the printer port, but the fix is to simplyinvert those bits using the exclusive OR function prior to outputting.
54 Giao ti&p qua c*ng máy in• [One might ask why the designers of the printer port designed the port in this manner. Assume
you have a printer with no cable attached. An open usually is read as a logic one. Thus, if alogic one on the SELECT_IN, AUTOFEED and STROBE leads meant to take the appropriateaction, an unconnected printer would assume it was selected, go into the autofeed mode andassume there was data on the outputs associated with the Data Port.
• The printer would be going crazy when in fact it wasn't even connected. Thus, the designersused inverted logic. A zero forces the appropriate action.]
55 Giao ti&p qua c*ng máy in• #define DATA 0x03bc• #define STATUS DATA+1• #define CONTROL DATA+2• ...• int val1, val2;• ...• val1 = 0x81; /* 1000 0001 */ /* Data bits 7 and 0 at one */• outportb(DATA, val1);• val2 = 0x08; /* 0000 1000 */ outportb(CONTROL, VAL2^0x0b); /* SELECT_IN
= 1, INIT = 0, /AUTO_FEED = 0, /STROBE = 0 */
56 Giao ti&p qua c*ng máy in• Note that only the lower nibble of val2 is significant. Note that in the last
line of code, /SELECT_IN, /AUTO_FEED and /STROBE are output ininverted form by using the exclusive-or function so as to compensate forthe hardware inversion.
57 Giao ti&p qua c*ng máy in• For example; if I intended to output 1 0 0 0 on the lower nibble and did not do the
inversion, the hardware would invert bit 3, leave bit 2 as true and invert bits 1 and 0.The result, appearing on the output would then be 0 0 1 1 which is about as far f romwhat was desired as one could get. By using the exclusive-or function, 1 0 0 0 isactually sent to the port as 0 0 1 1. The hardware then inverts bits 3, 1 and 0 and the
output is then the desired 1 0 0 0.
58 Giao ti&p qua c*ng máy in• Note that in the diagram showing the Status Port there are five status leads from the printer.
(BSY, /ACK, PE (paper empty), SELECT, /ERROR).• [The original intent in the naming of most of these is intuitive. A high on SELECT indicates
the printer is on line. A high on BSY or PE indicates to the PC that the printer is busy or outof paper. A low wink on /ACK indicates the printer received something. A low on ERROR
indicates the printer is in an error condition.]• These inputs are fetched by reading the five most significant bits of the status port
59 Giao ti&p qua c*ng máy in• However, the original designers of the printer interface circuitry, inverted the bit
associated with the BSY using hardware. That is, when a zero is present on inputBSY, the bit will actually be read as a logic one. Normally, you will want to use "true"logic, and thus you will want to invert this bit.
• The following fragment illustrates the reading the five most significant bits in "true"logic.
60 Giao ti&p qua c*ng máy in• #define DATA 0x03bc
• #define STATUS DATA+1
• ...
• unsigned int in_val;
• ...• in_val = ((inportb(STATUS)^0x80) >> 3);
61 Giao ti&p qua c*ng máy in• Note that the Status Port is read and the most significant bit, corresponding to the
BSY lead is inverted using the exclusive-or function. The result is then shifted suchthat the upper five bits are in the lower five bit positions.
• 0 0 0 BUSY /ACK PE SELECT /ERROR Another input, IRQ on the StatusPort is not brought to a terminal on the DB-25 printer port connector. I have yet tofigure out how to use this bit.
62 Giao ti&p qua c*ng máy in
• At this point, you should see that, at a minimum, there are 12 outputs;eight on the Data Port and four on the lower nibble of the Control Port.There are five inputs, on the highest five bits of the Status Port. Threeoutput bits on the Control Port and one input on the Status Port areinverted by the hardware, but this is easily handled by using the exclusive-or function to selectively invert bits.
63 Giao ti&p qua c*ng máy in• Refer to the figure titled Figure #3 - Typical Application showing a normally
open push button switch being read on the BUSY input (Status Port, Bit 7)
and an LED which is controlled by Bit 0 on the Data Port. A C language
program which causes the LED to flash when the push-button is
depressed appears below. Note that an output logic zero causes the LEDto light.
64
• #include <stdio.h>
• #include <dos.h> /* required for delay function */
• in = inportb(STATUS);• if (((in^0x80)&0x80)==0) /* if BUSY bit is at 0 (sw closed) */
• {
• outportb(DATA,0x00); /* turn LED on */
• delay(100);
• outportb(DATA, 0x01); /* turn it off */
• delay(100);
• }
• else
• {
• outportb(DATA,0x01); /* if PB not depressed, turn LED off */
• }
• }
• }
65 Giao ti&p qua c*ng máy in
66 Giao ti&p qua c*ng máy in• Circuit Description: Logic 1 on output DATA 0 (Data Port - Bit 0) causes LED to beoff. Logic 0 causes LED to turn on.
• Normally open push-button causes +5V (logic 1) to appear on input BUSY (STATUSPORT - Bit 7). When depressed, push-button closes and ground (logic 0) is appliedto input Busy.
• Note external source of 5V.• Program Description: When idle, push-button is open and LED is off. On
depressing push-button, LED blinks on and off at nominally 5 pulses per second.
67 GIAO TI.P C0NG SONG SONGCH!NG 9
68 9.2. Giao ti&p qua c*ng song song• Parallel ports are easy to program and faster compared to the serial ports.• But main disadvantage is it needs more number of transmission lines.
Because of this reason parallel ports are not used in long distancecommunications.
69 9.2. Giao ti&p qua c*ng song song• Let us know the basic difference between working of parallel port and serial port.• In serial ports, there will be two data lines: One transmission and one receive line.
To send a data in serial port, it has to be sent one bit after another with some extra
bits like start bit, stop bit and parity bit to detect errors.
• But in parallel port, all the 8 bits of a byte will be sent to the port at a time and a
indication will be sent in another line.
70 9.2. Giao ti&p qua c*ng song song• In the PC there will be D-25 type of female connector having 25 pins and
in the printer, there will be a 36-pin Centronics connector.• Connecting cable will combine these connecter using following convention.
Pin structure of D-25 and Centronics connecters are explained bellow
71 9.2. Giao ti&p qua c*ng song song
72 9.2. Giao ti&p qua c*ng song song• Now let us know how communication between PC and printer takes place. Computerplaces the data in the data pins, then it makes the strobe low.
• When strobe goes low, printer understands that there is a valid data in data pins.Other pins are used to send controls to the printer and get status of the printer, youcan understand them by the names assigned to the pins.
73 9.2. Giao ti&p qua c*ng song song• To use the printer port for applications other than printing, We need to know how ports are
organized.• There are three registers associated with LPT port: Data register, Control register and
Status register.• Data register will hold the data of the data pins of the port. That means, if we store a byte of
data to the data register, that data will be sent to the data pins of the port.• Similarly control and status registers. The following table explains how these registers areassociated with ports.
74
75 9.2. Giao ti&p qua c*ng song song• * Pins with * symbol in this table are hardware inverted. Than means, If a pin has a
'low' ie. 0V, Corresponding bit in the register will have value 1.• Signals with prefix 'n' are active low. That means, Normally these pins will have low
value. When it needs to send some indication, it will become high.• For example, Normally nStrobe will be high, when the data is placed in the port,
computer makes that pin low.
76 9.2. Giao ti&p qua c*ng song song• Normally, data, control and status registers will have following addresses.
We need these addresses in programming later
77 9.2. Giao ti&p qua c*ng song song
78 9.2. Giao ti&p qua c*ng song song• Note: All the parallel ports do not have bidirectional capability. Earlier parallel ports
had only output enabled in data pins since printers only inputs data. But latter, tomake parallel port capable of communicating with other devises, bidirectional portsare introduced.
• By default, data port is output port. To enable the bidirectional property of the port,
we need to set the bit 5 of control register.
79 9.2. Giao ti&p qua c*ng song song• To know the details of parallel ports available in your computer, follow this procedure:• Right click on My Computer, go to "Properties".
• Select the tab Hardware, Click Device manager.
• You will get a tree structure of devices; In that Expand "Ports(Com1 & LPT)".
• Double Click on the ECP Printer Port(LPT1) or any other LPT port if available.
• You will get details of LPT port. Make sure that "Use this Port (enable)" is selected.
• Select tab recourses. In that you will get the address range of port.
80 9.2. Giao ti&p qua c*ng song song
• To start programming, you will need a D-25 type Male connector. Its pin structurescan be found in the connector as follows:
81 9.2. Giao ti&p qua c*ng song song• Programm ing the pr inter port in DOS:
• To start programming the port, we will use DOS.• In DOS we have commands to access the port directly. But, these programs will not
work on the systems based on Windows XP, Windows NT or higher versions.• For security reason, higher versions of the windows does not allow accessing the
port directly.• To program the parallel port in these systems, we need to write kernel mode driver.
82 9.2. Giao ti&p qua c*ng song song• When we want to find out whether particular pin of the port is high or low, we need to
input the value of corresponding register as a byte.• In that, we have to find out whether the corresponding bit is high or low using bitwise
operators.• We can't access the pins individually. So, you need to know basic bitwise operations.
83 9.2. Giao ti&p qua c*ng song song• Main bitwise operators that we need are bitwise AND '&' and bitwise OR '|'.• To make a particular bit in a byte high without affecting other bits, write a
byte with corresponding bit 1 and all other bits 0; OR it with original byte.• Similarly, to make particular bit low, write a byte with corresponding bit 0
and all other bits 1; AND it with original byte.
84 9.2. Giao ti&p qua c*ng song song• In Turbo C, there are following functions used for accessing the port:• out por tb( PORTID, data);
• data = inpo rtb( PORTID);
• out por t( PORTID, data);
• data = inpo rt( PORTID);
85 9.2. Giao ti&p qua c*ng song song• outport() function sends a word to port, inport() reads a word from the port. outportb() sends a byte
to port and inportb() reads a byte from the port. If you include DOS.H header, these functions will beconsidured as macro, otherwise as functions.
• Function inport() will return a word having lower byte as data at PORTID and higher byte as data atPORTID+2. So, we can use this function to read status and control registers together. inportb()function returns byte at PORTID. outport() writes the lower byte to PORTID and higher byte toPORTID+1.
• So this can be used to write data and control together. outportb() function write the data to PORTID.outport() and outportb() returns nothing.
86 9.2. Giao ti&p qua c*ng song song• Let us start with inputting first. Here is an example program, copy it and
88 9.2. Giao ti&p qua c*ng song song• To understand bitwise operations: you want to find data in pin 15, value of
(data & 0x08) will be 0x08 if bit 3 of register is high, 0therwise.
89 9.2. Giao ti&p qua c*ng song song• We will use the same logic throughout the article.• Now, take a D-25 male with cables connected to each pins. Short all the pins from 18
to 25, call it as ground. Now you can run above program and see the change byshorting pins 10, 11, 12, 13 and 15 to ground. I prefer using switches between eachinput pins and ground. Be careful, do not try to ground the output pins.
• To find out the availability of ports in a computer programmatically, we will use thememory location where the address of port is stored.
90 9.2. Giao ti&p qua c*ng song song
91 9.2. Giao ti&p qua c*ng song song• If you run the the following code in Turbo C or Borland C, You will get the
addresses of available ports.
92
93 9.2. Giao ti&p qua c*ng song song• Next we will go to check output pins. To check the output, we will use LED's. I have
driven LED's directly from the port.• But it is preferred to connect a buffer to prevent excessive draw of current from the
port. Connect an LED in series with a resister of 1KW or 2.2KW between any of thedata pins(2 to 9) and ground. With that, if you run the program given below, you
should see the LED blinking with app. 1 sec frequency.
94 9.2. Giao ti&p qua c*ng song song
95 GIAO TI.P QUA C0NG N]I TI.PCH!NG 9
96 9.3. Giao ti&p qua c*ng n-i ti&p
97 9.3. Giao ti&p qua c*ng n-i ti&p• Above is the standard port addresses. These should work for most P.C's.• If you just happen to be lucky enough to own a IBM P/S2 which has a
micro-channel bus, then expect a different set of addresses and IRQ's.• Just like the LPT ports, the base addresses for the COM ports can be read
from the BIOS Data Area.
98 9.3. Giao ti&p qua c*ng n-i ti&p
99 9.3. Giao ti&p qua c*ng n-i ti&p• The above table shows the address at which we can find the
Communications (COM) ports addresses in the BIOS Data Area.• Each address will take up 2 bytes.• The following sample program in C, shows how you can read these
locations to obtain the addresses of your communications ports.
100 9.3. Giao ti&p qua c*ng n-i ti&p• #include <stdio.h>
• #include <dos.h>• void main(void)• {• unsigned int far *ptraddr; /* Pointer to location of Port Addresses */• unsigned int address; /* Address of Port */• int a;• ptraddr=(unsigned int far *)0x00000400;• for (a = 0; a < 4; a++)• { address = *ptraddr;• if (address == 0) printf("No port found for COM%d \n",a+1);• else• printf("Address assigned to COM%d is %Xh\n",a+1,address);• *ptraddr++;• }• }
101 BÀI T ^P C `N B aNCH!NG 9
102 Bài 1.• L@p trình C giao ti&p l;y tín hi:u và Ai<u khi>n Aèn giao thông (xanh, Ab,
• #include<time.h>• #include<dos.h>••• #defineCOM10x03F8• #defineCOM20x02F8• #defineDATA_READY0X60B0• #define SETTINGS( _COM_9600|_COM_NOPARITY|_COM_STOP1|_COM_CHR8)•• i nt start_addr();• i nt end_addr();• void g roup _add r( );• void d isp la y_ time( );• voidtransmit_frame(int*,int*);• voidrecieve_frame(in t*) ;
104 Bài 1.• void main(void)•• {
• char send;• int abyte,in;• int a,b,c,r ,addr,s tatus;• unsigned i nt r et_value;• int count, arr[6];• int b it_num,s_addr, e_addr;• int p arity, b aud ,port,mod e;• int c om _port_no,stop _bit;• char abyte_stopbit, abyte_baud, abyte_parity,abyte_databit ;• char arr_baud[]= {0x00, 0x20, 0x40, 0x60,0x80,0xA0,0xC0,0xE0};;•• clrscr();
105 Bài 1.• //COM PORT SELECT SETTINGS//• printf("\nSelect COM Port\nCOM 1\nCOM2\n");• scanf ("%d",&com_port_no);• switch(com_port_no)• {• case 1:port = 0;• break;•• case 2:port =1;• break;• }
111 Bài 2.• L@p trình C giao ti&p l;y tín hi:u tN Card cFa hãng Advantech.
112 Bài 3.• L@p trình C giao ti&p vCi mô Aun cFa màn hình LCD
113 Bài 3.• LCD modules are available in a wide range like 8x1, 8x2, 16x1, 16x2, 20x2, 20x4,
40x4. Here we have used 16x2- that means 2 rows of 16 characters.• It is a Hitachi HD44780 compatible module, having 16 pins including 2 pins for
backlight• Following table gives pin structure of LCD module. LCD modules without backlight
will have only 14 pins. If you are using such LCDs, simply ignore 15th and 16th pins
114 Bài 3.
115 Bài 3.• Here, we are going to write on the LCD module and not reading back. So, R/W is
connected to ground directly.• We need not have to input any data through, so all output pins are used in our
application. Data pins of LCD are connected to data pins of the port.• Strobe signal (Pin 1 of D25 connector) is given to E (Pin 6 of LCD), Select printer (Pin
17 of D25) is connected to RS (pin 4 of the LCD).
116 Bài 3.
117 Bài 3.• In the above diagram, LCD module is connected to the lpt port using D25
male connector.• Pin number 3 of the LCD is for adjusting the contrast, connected in such a
way that it can be varied from 0V to 5V. Keep it to 0 initially.
{• outportb(CONTROL, inportb(CONTROL) | 0x08);if(row==2) column+=0x40;/* Add these if you are using LCD module with 4 columnsif(row==2) column+=0x14;if(row==3) column+=0x54;*/lcd_write(0x80 | column);
if you dont call this function, entry mode sets to 2 by default.mode: 0 - cursor left shift, no text shift1 - no cursor shift, text right shift2 - cursor right shift, no text shift3 - no cursor shift, text left shift*/outportb(CONTROL, inportb(CONTROL) | 0x08);lcd_write(0x04 + (mode%4));
• }
126 Bài 3.• void lcd_cursor(int cursor)
{• /*
set cursor: 0 - no cursor, no blink1 - only blink, no cursor 2 - only cursor, no blink3 - both cursor and blink*/