CHƯƠNG 9 LẬP TRÌNH GIAO TIẾP MÁY TÍNH VÀ THIẾT BỊ - Pdf 19

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 CỔNG 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ết bị
•Mặc dùcổng RS232 phức tạp hơn cổng song song nhưng nólàcổng hữu
hiệu nhất trong truyền dữ liệu, cóthể làwireless trong một số ngành vàít
tốn chi phí.
• Liên kết cổng RS232 bằng dây vàdây này có3 chức năng chính: “truyền”,
“nhận”, “nối đấ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 được kết nối giữa 2 máy tính với nhau.
•Dữliệu được truyền theo chuỗi. Có2 chân chính làTXD vàRXD. Các

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 bị dùng các cổng nối tiếp cho giao tiếp được phân làm hai loại. Chúng gồm DCE (Data
Communications Equipment) và DTE (Data Terminal Equipment)
•DCE làcác thiết bị tương tự modem thiết bị, TA adapter, máy in chẳng hạn trong khi DTE làmáy
tính bạn hoặc các thiết bị ngoại vi.
•Một DTE làmột máy tính vàmột modem là DCE. Thông thường, mọi người hay nói về DTE tới
DCE hoặc DCE tới các bộ phát DCE.
•Bộtruyền DTE tới DCE làgiữa modem vàmáy tính của chúng ta (đôi khi được xem như làmột bộ
thiết bị ngoại vi ngoài). Điều này cho thấy truyền dự liệu rất nhanh hơn so với từ DCE đến DCE.
DCE to DCE làkết nối giữa các modem, đôi khi còn được gọi làbộkết nối các đường truyền .
16
Giao tiếp qua cổng RS232
•Hầu hết hiện nay người dùng phổ biến các modem 28.8K hoặc 33.6K. Do vậy, chúng
ta nên sử dụng các modem cótốc độ DCE đến DCE là28.8K hoặc 33.6K.
•Nếu cần loại modem cótốc độ cao, chúng ta nên dùng DTE đến DCE cótốc độ
khoảng 115,200 BPS. (Tốc độ tối đa là16550a UART) .
•Các chương trình giao tiếp chúng ta sử dụng được thiết lập cho DCE đến DTE. Tuy
nhiên, tốc độ thiết lập là9.6 KBPS, 144 KBPS, vàtheo tốc độ modem cóthể
truyền.
17
Giao tiếp qua cổng RS232
•Nếu chúng ta chuyển dữ liệu dạng file text tại 28.8K (DCE-DCE), thìmodem sẽ nén
file này lại vàtốc độ truyền thực tế là115.2 KBPS giữa các máy tình và do đó, tốc độ
truyền giữa DCE-DTE là115.2 KBPS.

• Ở điểm này, máy tính được xem như làmodem ảo Virtual Modem được kết nối sẵn
sàng và được phát hiện bởi các modem truyền dữ 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 toSend high and as
it is hooked together with the Clear to Send, It immediately gets a reply that it is ok to
send 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 connected
to 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 toPorts( 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 sure
that you have enabled the port( Use this port is selected).
25
Giao tiếp qua cổng RS232
[email protected]
4
•There are two popular methods of sending data to or from the serial port in
Turbo C.
•One is using outportb(PORT_ID, DATA) or outport(PORT_ID,DATA)
defined in “dos.h”

• Inport-port that inport and inportb read from;
• Outport-port that outport and outportb write to
• value:
• Word that outport writes to portid;
• Byte-that outportb writes to portid.
30
Giao tiếp qua cổng RS232
•If you call inportb or outportb when dos.h has been included, they are
treated as macros that expand to inline code.
•If you don't include dos.h, or if you do include dos.h and #undef the
macro(s), you get the function(s) of the same name.
[email protected]
5
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 bioscom:
•The macro bioscom () and function _bios_serialcom() are used inthis method inthe serial
communication using RS-232 connecter.
•First we have to set the port with the settings depending on ourneed and availability. In this
method, same function is used to make the settings using controlword, to send data to the port
and check the status of the port.
•These actions are distinguished using the first parameter of thefunction. Along with that we are
sending data and the port to be used to communicate.
•Here are the deatails of the Turbo C Functions for communicationports.
33

• abyte = 0x8B = (0x80 | 0x08 | 0x00 | 0x03) = (_COM_1200 | _COM_ODDPARITY |
_COM_STOP1 | _COM_CHR8)
• the communications port is set to
1200 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,
• bioscom(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 akey 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 it
to the com port. Similarly, if any data is available on the port, we are receiving it from the port and
displaying 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.

• 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 debug
program to display memory locations 0040:0008. For example:
• >debug
•-d 0040:0008 L8 0040:0008 78 03 78 02 00 00 00 00
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, /AUTO

•#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 in
inverted form by using the exclusive-or function so as to compensate for
the 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 nibbleand 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 from
what was desired as one could get. By using the exclusive-or function, 1 0 0 0 is
actually 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 out
of paper. A low wink on /ACK indicates the printer received something. A low on ERROR
[email protected]

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. Three
output bits on the Control Port and one input on the Status Portare
inverted 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 LED
to light.
64
• #include <stdio.h>
• #include <dos.h> /* required for delay function */
• #define DATA 0x03bc
• #define STATUS DATA+1
[email protected]
10
• #define CONTROL DATA+2
• void main(void)
• {
• int in;
• while(1)
• {
• in = inportb(STATUS);
• if (((in^0x80)&0x80)==0) /* if BUSY bit is at 0 (sw closed) */
• {
• outportb(DATA,0x00); /* turn LED on */

communications.
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.
[email protected]
11
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. Computer
places 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, you
can 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, Weneed to know how ports are
organized.

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 portif available.
[email protected]
12
• 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 structures
can be found in the connector as follows:
81
9.2. Giao tiếp qua cổng song song
• Programming the printer 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.

run in Turbo C or Borland C without anything connected to parallel port.
• Then you should see data available in status register and pin numbers 10,
11, 12, 13 and 15 of the parallel port.
• Pin 11 (active low) is 0 and all other pins are 1 means it is OK.
87
9.2. Giao tiếp qua cổng song song
• #include"stdio.h"
#include"conio.h"
#include"dos.h"
#define PORT 0x378
void main()
{
int data;
clrscr();
while(!kbhit())
{
data=inportb(PORT+1);
gotoxy(3,10);
printf("Data available in status register: %3d (decimal), %3X (hex)\n", data, data);
printf("\n Pin 15: %d",(data & 0x08)/0x08);
printf("\n Pin 13: %d",(data & 0x10)/0x10);
printf("\n Pin 12: %d",(data & 0x20)/0x20);
printf("\n Pin 11: %d",(data & 0x80)/0x80);
printf("\n Pin 10: %d",(data & 0x40)/0x40);
delay(10);
}
}
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

GIAO TIẾP QUA CỔNG NỐI TIẾP
CHƯƠ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 mostP.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;


• intstart_addr();
• intend_addr();
• voidgroup_addr();
• voiddisplay_time();
• voidtransmit_frame(int*,int*);
• voidrecieve_frame(int*);
104
Bài 1.
• voidmain(void)

• {
• charsend;
• intabyte,in;
• inta,b,c,r,addr,status;
• unsignedintret_value;
• intcount,arr[6];
• intbit_num,s_addr,e_addr;
• intparity,baud,port,mode;
• intcom_port_no,stop_bit;
• charabyte_stopbit,abyte_baud,abyte_parity,abyte_databit;
• chararr_baud[]={0x00,0x20,0x40,0x60,0x80,0xA0,0xC0,0xE0};;

• clrscr();
105
Bài 1.
•//COMPORTSELECTSETTINGS//
• printf("\nSelectCOMPort\nCOM1\nCOM2\n");
• scanf("%d",&com_port_no);
• switch(com_port_no)

• voidtransmit_frame(int*addr,int*port)
• {

• unsignedintin;
• inttrans,i;
• chararr[]={0x01};



• if(kbhit())
• {
• if(getch()=='\x1B')
• {exit(0);
• }
• }
108
Bài 1.
•else
• {
• trans=arr[0];
• printf("%x",trans);
• _bios_serialcom(_COM_SEND,*port,trans);
• delay(100);
• }

• }
109
Bài 1.
• voidrecieve_frame(int*port)
• {intv;

Bài 3.
•Lập trình C giao tiếp với mô đun của 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, connectedin such a
way that it can be varied from 0V to 5V. Keep it to 0 initially.
118
Bài 3.

lcd_goto(1,0);
lcd_puts("electroSofts.com");
while(!kbhit() ) //wait until a key is pressed
{}
•}
void lcd_init()
{
•outportb(CONTROL, inportb(CONTROL) & 0xDF);
//config data pins as output
outportb(CONTROL, inportb(CONTROL) | 0x08);
//RS is made high: control (register select)
122
Bài 3.
•lcd_write(0x0f);
delay(20);
lcd_write( 0x01);
delay(20);
lcd_write( 0x38);
delay(20);
•}
void lcd_write(char char2write)
{
•outportb(DATA, char2write);
outportb(CONTROL,inportb(CONTROL) | 0x01); /* Set Strobe */
delay(2);
outportb(CONTROL,inportb(CONTROL) & 0xFE); /* Reset Strobe */
delay(2);
•}
123
Bài 3.

{
• outportb(CONTROL, inportb(CONTROL) | 0x08);
lcd_write(0x01);
• }
• void lcd_home()
{
• outportb(CONTROL, inportb(CONTROL) | 0x08);
lcd_write(0x02);
• }
• void lcd_entry_mode(int mode)
{
• /*
if you dont call this function, entry mode sets to 2 by default.
mode: 0 -cursor left shift, no text shift
1 -no cursor shift, text right shift
2 -cursor right shift, no text shift
3 -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 blink
1 -only blink, no cursor
2 -only cursor, no blink
3 -both cursor and blink

Bài 3.
• while(!kbhit())
{
• gettime(&t);
getdate(&d);
lcd_goto(0,4);
sprintf(strtime,"%02d:%02d:%02d", t.ti_hour%12, t.ti_min, t.ti_sec);
lcd_puts(strtime);
gotoxy(12,4);
cputs(strtime);
lcd_goto(1,3);
sprintf(strtime,"%02d:%02d:%4d", d.da_day, d.da_mon, d.da_year);
lcd_puts(strtime);
gotoxy(11,5);
cputs(strtime);
delay(200);
• }
textbackground(0);
textcolor(7);
• }
131
KẾT THÚC
CHƯƠNG 9
[email protected]


Nhờ tải bản gốc

Tài liệu, ebook tham khảo khác

Music ♫

Copyright: Tài liệu đại học © DMCA.com Protection Status