HARDWARE INTERFACING USING C. Prepared By: ... computers, or between
computers and their terminals, printers or other peripheral devices. The data ...
HARDWARE INTERFACING USING C
Prepared By: Chirag Gohel
E-mail:
[email protected]
-1-
HARDWARE INTERFACING USING C Data communication is the process of communicating information in binary from between two or more points. Data communication is sometimes called computer communications because most of the information interchanged today is between computers, or between computers and their terminals, printers or other peripheral devices. The data might be as elementary as the binary symbols 1 and 0, or as complex as the characters represented by the keys on a typewriter keyboard. In this case, the characters or symbols represent information.
General Data Communication System The following diagram show the general data communication system, which consist of following three component. 1. The Transmitter (SOURCE) 2. The Receiver (DESTINATION) 3. The Transmission Path (Channel) SOURCE
SINK
SINK
SOURCE
General Data Communication System
Universal Seven Part Data Circuit The following diagram show the Universal Seven Part Data Circuit, which consist of following seven component. 1. 2. 3. 4. 5. 6. 7.
DTE at Point A DCE at Point A DTE at Point B DCE at Point B DTE-DCE Interface at Point A DTE-DCE Interface at Point B Communication Channel
DCE
DTE
Point A
DCE
DTE
Point B Universal Seven Part Data Circuit
Prepared By: Chirag Gohel
E-mail:
[email protected]
-2-
HARDWARE INTERFACING USING C DTE : Data Terminal Equipment DCE : Data Carrier Equipment DTE-DCE Interface : A set of rule which specify the data movement across the interface between DTE-DCE. It is also called RS-232 Standard or V.24.
Serial and Parallel Data Transmission If the bits move one by one over a single line it is called SERIAL data transmission. If a groups of bits move over several lines at the same time it is called PARALLE data transmission.
Synchronous/Asynchronous Serial Transmission Synchronous 1. Speed of transmitter and receiver is different. 2. Data are send in a GROUP called blocks, with special synchronization characters placed at start and end. 3. Error checking is performed automatically on entire block. (if any error entire block is retransmitted)
Asynchronous Speed of transmitter and receiver is almost same. Data are send in bit by bit, many special characters are sent along with data. Manually Error checking is done.
Modes of Communication Simplex Data in a simplex channel is always one way. Simplex channels are not often used because it is not possible to send back error or control signals to the transmit end.
It's like a one way street. An example of simplex is Television, or Radio.
Prepared By: Chirag Gohel
E-mail:
[email protected]
-3-
HARDWARE INTERFACING USING C Half Duplex A half-duplex channel can send and receive, but not at the same time. It's like a one-lane bridge where two way traffic must give way in order to cross. Only one end transmits at a time, the other end receives. In addition, it is possible to perform error detection and request the sender to retransmit information that arrived corrupted. In some aspects, you can think of Internet surfing as being half-duplex, as a user issues a request for a web document, then that document is downloaded and displayed before the user issues another request.
Another example of half-duplex is talk-back radio, and CB Radio (Citizens Band). You might have seen movies where truckies (drivers of very big trucks) communicate to each other, and when they want the other person to speak they say "over". This is because only one person can talk at a time.
Full Duplex Data can travel in both directions simultaneously. There is no need to switch from transmit to receive mode like in half duplex. Its like a two lane bridge on a two-lane highway. Have you ever watched these television talk shows where the host has a number of people on the show, and they all try to talk at once. Well, that's full duplex!
Prepared By: Chirag Gohel
E-mail:
[email protected]
-4-
HARDWARE INTERFACING USING C
Prepared By: Chirag Gohel
E-mail:
[email protected]
-5-
HARDWARE INTERFACING USING C Parallel port is a simple and inexpensive tool for building computer controlled devices and projects. The simplicity and ease of programming makes parallel port popular in electronics hobbyist world. The parallel port is often used in Computer controlled robots, Atmel/PIC programmers, home automation, ...etc... Here a simple tutorial on parallel port interfacing and programming with some examples. Everybody knows what is parallel port, where it can be found, and for what it is being used. the primary use of parallel port is to connect printers to computer and is specifically designed for this purpose. Thus it is often called as printer Port or Centronics port (this name came from a popular printer manufacturing company 'Centronics' who devised some standards for parallel port). You can see the parallel port connector in the rear panel of your PC. On almost all the PCs only one parallel port is present, but you can add more by buying and inserting ISA/PCI parallel port cards. The port is composed of 4 control lines, 5 status lines and 8 data lines. It's found commonly on the back of your PC as a D-Type 25 Pin female connector. There may also be a D-Type 25 pin male connector.
Parallel port modes The IEEE 1284 Standard which has been published in 1994 defines five modes of data transfer for parallel port. They are, 1) Compatibility Mode 2) Nibble Mode 3) Byte Mode 4) EPP 5) ECP The aim was to design new drivers and devices which were compatible with each other and also backwards compatible with the Standard Parallel Port (SPP). Compatibility, Nibble & Byte modes use just the standard hardware available on the original Parallel Port cards while EPP & ECP modes require additional hardware which can run at faster speeds, while still being downwards compatible with the Standard Parallel Port. Compatibility mode or "Centronics Mode" as it is commonly known, can only send data in the forward direction at a typical speed of 50 kbytes per second but can be as high as 150+ kbytes a second. In order to receive data, you must change the mode to either Nibble or Byte mode. Nibble mode can input a nibble (4 bits) in the reverse direction. E.g. from device to computer. Byte mode uses the Parallel's bi-directional feature (found only on some cards) to input a byte (8 bits) of data in the reverse direction.
Prepared By: Chirag Gohel
E-mail:
[email protected]
-6-
HARDWARE INTERFACING USING C
Hardware The pin outs of DB25 connector is shown in the picture below
Pin No (DB25) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18-25
Signal name
Direction
nStrobe Data0 Data1 Data2 Data3 Data4 Data5 Data6 Data7 nAck Busy Paper-Out Select Linefeed nError nInitialize nSelect-Printer Ground
Out In/Out In/Out In/Out In/Out In/Out In/Out In/Out In/Out In In In In Out In Out Out -
Register bit Control-0 Data-0 Data-1 Data-2 Data-3 Data-4 Data-5 Data-6 Data-7 Status-6 Status-7 Status-5 Status-4 Control-1 Status-3 Control-2 Control-3 -
Inverted Yes No No No No No No No No No Yes No No Yes No No Yes -
LPT1(Parallel Port) is normally assigned base address 378h, while LPT2 is assigned 278h. However this may not always be the case as explained later. 378h & 278h have always been commonly used for Parallel Ports. The lower case h denotes that it is in hexadecimal. These addresses may change from machine to machine.
Prepared By: Chirag Gohel
E-mail:
[email protected]
-7-
HARDWARE INTERFACING USING C Address 3BCh - 3BFh
378h - 37Fh 278h - 27Fh
Notes: Used for Parallel Ports which were incorporated on to Video Cards - Doesn't support ECP addresses Usual Address For LPT 1 Usual Address For LPT 2 Port Addresses
When the computer is first turned on, BIOS (Basic Input/Output System) will determine the number of ports you have and assign device labels LPT1, LPT2 & LPT3 to them. BIOS first looks at address 3BCh. If a Parallel Port is found here, it is assigned as LPT1, then it searches at location 378h. If a Parallel card is found there, it is assigned the next free device label. This would be LPT1 if a card wasn't found at 3BCh or LPT2 if a card was found at 3BCh. The last port of call, is 278h and follows the same procedure than the other two ports. Therefore it is possible to have a LPT2 which is at 378h and not at the expected address 278h. What can make this even confusing, is that some manufacturers of Parallel Port Cards, have jumpers which allow you to set your Port to LPT1, LPT2, LPT3. Now what address is LPT1? - On the majority of cards LPT1 is 378h, and LPT2, 278h, but some will use 3BCh as LPT1, 378h as LPT1 and 278h as LPT2. Life wasn't meant to be easy. The lines in DB25 connector are divided in to three groups, they are 1) Data lines (data bus) 2) Control lines 3) Status lines As the name refers , data is transferred over data lines , Control lines are used to control the peripheral and of course , the peripheral returns status signals back computer through Status lines. These lines are connected to Data, Control And Status registers internally . The details of parallel port signal lines are given below.
Parallel port registers As you know, the Data, Control and status lines are connected to there corresponding registers inside the computer. So by manipulating these registers in program , one can easily read or write to parallel port with programming languages like 'C' and BASIC. The registers found in standard parallel port are , 1) data register 2) Status register 3) Control register
Prepared By: Chirag Gohel
E-mail:
[email protected]
-8-
HARDWARE INTERFACING USING C As there names specifies, Data register is connected to Data lines, Control register is connected to controll lines and Status lregistyer is connected to Status lines. (Here the word connection does not mean that there is some physical connection between data/control/status lines. The registers are virtually connected to the corresponding lines.). So what ever you write to these registers , will appear in corresponding lines as voltages, Of course, you can measure it with a multimeter. And What ever you give to Parallel port as voltages can be read from these registers(with some restrictions). For example , if we write '1' to Data register , the line Data0 will be driven to +5v. Just like this ,we can programmatically turn on and off any of the data lines and Control lines.
Where these registers are ? In an IBM PC, these registers are IO mapped and will have unique address. We have to find these addresses to to work with parallel port. For a typical PC , the base address of LPT1 is 0x378 and of LPT2 is 0x278. The data register resides at this base address , status register at baseaddress + 1 and the control register is at baseaddress + 2. So once we have the base address , we can calculate the address of each registers in this manner. The table below shows the register addresses of LPT1 and LPT2.
Register data registar(baseaddress + 0) status register (baseaddress + 1) control register (baseaddress + 2)
LPT1 0x378 0x379 0x37a
LPT2 0x278 0x279 0x27a
Software Registers - Standard Parallel Port (SPP) Offset Name Read/Write Bit No. Base + 0 Data Write Bit 7 Port (Note-1) Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0 Data Port
Properties Data 7 Data 6 Data 5 Data 4 Data 3 Data 2 Data 1 Data 0
Note 1 : If the Port is Bi-Directional then Read and Write Operations can be performed on the Data Register. The base address, usually called the Data Port or Data Register is simply used for outputting data on the Parallel Port's data lines (Pins 2-9). This register is normally a write only port. If you read from the port, you should get the last byte sent. However if your port is bi-directional, you can receive data on this address. See Bi-directional Ports for more detail. Prepared By: Chirag Gohel
E-mail:
[email protected]
-9-
HARDWARE INTERFACING USING C Offset Name Read/Write Bit No. Base Status Read Only Bit 7 +1 Port Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0 Status Port
Properties Busy Ack Paper Out Select In Error IRQ (Not) Reserved Reserved
The Status Port (base address + 1) is a read only port. Any data written to this port will be ignored. The Status Port is made up of 5 input lines (Pins 10,11,12,13 & 15), a IRQ status register and two reserved bits. Please note that Bit 7 (Busy) is a active low input. E.g. If bit 7 happens to show a logic 0, this means that there is +5v at pin 11. Likewise with Bit 2. (nIRQ) If this bit shows a '1' then an interrupt has not occurred. Offset Name Read/Write Bit No. Base + 2 Control Read/Write Bit 7 Port Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0 Control Port
Properties Unused Unused Enable BiDirectional Port Enable IRQ Via Ack Line Select Printer Initialize Printer (Reset) Auto Linefeed Strobe
PORT ACCESS The C language can be used to transfer data to and from the contents of the various registers and controllers associated with the IBM-PC. These registers and control devices are port mapped, and are accessed using special IN and OUT instructions. Most C language support library's include functions to do this. The following is a brief description of how this may be done.
#include outp( Port_Address, value); value = inp( Port_address);
/* turboC uses outportb() */ /* and inportb() */
Example : outp (0x378,10);
/*send the decimal value (10) to the port (0x378)(lpt1)(Parallel Port)*/ value = inp(0x378); /*recive the value from the port (0x378)(lpt1)(Parallel Port)*/ Prepared By: Chirag Gohel
E-mail:
[email protected]
- 10 -
HARDWARE INTERFACING USING C The various devices, and their port values, are shown below, Port Range 00 - 0f 20 - 21 40 - 43 60 - 63 80 - 83 200 - 20f 278 - 27f 2f8 - 2ff 378 - 37f 3b0 - 3bf 3d0 - 3df 3f0 - 3f7 3f8 - 3ff
Device DMA Chip 8737 8259 PIC Timer Chip 8253 PPI 8255 (cassette, sound) DMA Page registers Game I/O Adapter Reserved COM2 Parallel Printer Monochrome Display Color Display Diskette COM1
Example of Interfacing Parallel Port : 1. Demonstration of control of light Bulb through parallel port :
Prepared By: Chirag Gohel
E-mail:
[email protected]
- 11 -
HARDWARE INTERFACING USING C
The Software Controlling the above Circuit : 1. C program to control ON/OFF Through keyboard #include void main(void) { outportb(0x378,0x01); getch(); outportb(0x378,0x00); getch(); }
//turn on the bulb //wait for any key to be pressed //turn off the bulb // wait for a key to be Pressed
2. C program to have the bulb blink for five times for (int i=1; ico-ordinate in cx register and Y->co-ordinate in dx register.printf statements prints x and y co-ordinates as long as the loop continues.Maximum screen resolution for mouse in text mode is 640x200 and in graphics mode is 640x480.
Prepared By: Chirag Gohel
E-mail:
[email protected]
- 39 -
HARDWARE INTERFACING USING C 5) Program to print which mouse button is pressed. Knowing which button is pressed is very important task.The program below prints which button is pressed as soon as we press any button. #include Void main() { union REGS i,o; int button; clrscr(); i.x.ax=0; int86(0x33,&i,&o); if(o.x.ax==0) { printf("No mouse available...."); exit(); } i.x.ax=1; int86(0x33,&i,&o); gotoxy(24,23); printf("Press any key to exit...."); while(!kbhit()) { i.x.ax=3; int86(0x33,&i,&o); button=o.x.bx&7; gotoxy(23,11); switch(button) { case 1: printf("Left button pressed"); break; case 2: printf("Right button pressed”); break; case 4: printf("Middle button pressed”); break;
Prepared By: Chirag Gohel
E-mail:
[email protected]
- 40 -
HARDWARE INTERFACING USING C
case 3: printf("Both buttons pressed”); break; case 5: printf("Left and Middle buttons pressed”); break; case 6: printf("Right and Middle buttons pressed"); break; case 7: printf("All the three buttons pressed”); break; default: printf("No button pressed...."); } } i.x.ax=2; int86(0x33,&i,&o); } The above program is same as the previous program except we have little extra in while loop.In while we used the same sub-function 3 and invoked mouse interrupt.This subfunction even returns button press information in bx register.Entire button press information is stored in the first 3 bits of the bx register.So we ANDED bx with 7 to separate the first 3 bits and stored them in button variable. If the first bit's value is 1 then the left button is pressed,if the value is 0 then it is not pressed.If the second bit's value is 1 then the right button is pressed,if value is 0 then it is not pressed.If the last bit's value is 1 then the middle button is pressed,if value is 0 then it is not pressed.
Prepared By: Chirag Gohel
E-mail:
[email protected]
- 41 -
HARDWARE INTERFACING USING C 6) Program to set the position of the mouse pointer on the screen. Sometimes we need to set the position of the mouse pointer, just as we set the position of the keyboard's cursor using gotoxy().The following program sets the pointer to (x=150,y=100) position on the screen. #include Void main() { union REGS i,o; clrscr(); i.x.ax=0; int86(0x33,&i,&o); if(o.x.ax==0) { printf("No mouse available"); exit(); } i.x.ax=1; int86(0x33,&i,&o); i.x.ax=3; int86(0x33,&i,&o); gotoxy(1,1); printf("Current Position:x=%d y=%d”,o.x.cx,o.x.dx); gotoxy(15,23); printf("Press any key to set the mouse pointer to (150,100)..."); getch(); i.x.ax=4; i.x.cx=150; i.x.dx=100; int86(0x33,&i,&o); gotoxy(15,23); printf("Cursor is set ... press a key to exit”); getch(); } In the above program,we use sub-function 4 to set the pointer's position. We set the X->co-ordinate by placing a value in the cx register and Y->co-ordinate by placing a value in the dx register. Prepared By: Chirag Gohel
E-mail:
[email protected]
- 42 -
HARDWARE INTERFACING USING C 7) Program to switch to graphics mode. The program below switches text mode to graphcs mode.After executing this program,observe the mouse pointer.Now,it's in an arrow shape. Try to execute program-4 in graphics mode and observe the maximum screen resolution for mouse is increased to 640x480. #include #include Void main() { int gd=DETECT,gm; union REGS i,o; initgraph(&gd,&gm,"c:\\tc\\bgi"); i.x.ax=0; int86(0x33,&i,&o); if(o.x.ax==0) { printf("No Mouse Avaialable.."); restorecrtmode(); exit(); } i.x.ax=1; int86(0x33,&i,&o); outtextxy(100,400,"Mouse Pointer in graphics mode!!Press any key to exit"); getch(); i.x.ax=2; int86(0x33,&i,&o); restorecrtmode(); } In the above program,we used standard library function initgraph() to initialize graphics system.This function takes 3 arguments;graphics driver, graphics mode,path to the driver.By using DETECT,we tell the function to select a suitable driver by itselt.When DETECT is used,no need to assign anything to graphics mode.Path is null since the driver files are located in the current directory.This function initializes graphics system and when the program terminates we come to text mode by using restorecrtmode() function. Prepared By: Chirag Gohel
E-mail:
[email protected]
- 43 -
HARDWARE INTERFACING USING C 8) Program to restrict the mouse pointer within a boundary. Sometimes,in our program,we need to restrict the mouse pointer with in a screen boundary.In order to do that,we need to specify the top,left co-ordinates as well bottom,right co-ordinates.Sub-functions 7,8 are used to limit the pointer within a boundary.Look at the following program. #include #include main() { union REGS i,o; int gd=DETECT,gm; initgraph(&gd,&gm,"c:\\tc\\bgi"); i.x.ax=0; int86(0x33,&i,&o); if(o.x.ax==0) { restorecrtmode(); printf("No Mouse Available....."); exit(); } rectangle(99,49,501,151); i.x.ax=1; int86(0x33,&i,&o); i.x.ax=7; i.x.cx=100; i.x.dx=500; int86(0x33,&i,&o); i.x.ax=8; i.x.cx=50; i.x.dx=150; int86(0x33,&i,&o); while(!kbhit()) ; i.x.ax=2; int86(0x33,&i,&o); restorecrtmode(); }
Prepared By: Chirag Gohel
E-mail:
[email protected]
- 44 -
HARDWARE INTERFACING USING C In the above program,sub-fuction 7 is used to specify two x->co-ordinates and subfunction 8 is used to specify two y->co-ordinates and these co-ordinates form a rectangular boundary within which mouse is restricted.
Prepared By: Chirag Gohel
E-mail:
[email protected]
- 45 -
HARDWARE INTERFACING USING C 9) Free-hand drawing. If you ever wonder,how pencil tool in paint works.Then,the following program shows how it can be written in C.The following program makes use of some of the subfunction,which we already disussed above,and shows how they can be used to write useful programs like free-hand drawing.Just,go through the following program. #include #include union REGS i,o; main() { int gd=DETECT,gm,button,x1,y1,x2,y2; initgraph(&gd,&gm,""); i.x.ax=0; int86(0x33,&i,&o); if(o.x.ax==0) { printf("No Mouse is available.."); exit(); restorecrtmode(); } outtextxy(230,400,"Press any key to exit...."); while(!kbhit()) { show_mouse(); get_mouse_pos(&x1,&y1,&button); x2=x1; y2=y1; while(button==1) { hide_mouse(); line(x1,y1,x2,y2); x1=x2; y1=y2; get_mouse_pos(&x2,&y2,&button); } Prepared By: Chirag Gohel
E-mail:
[email protected]
- 46 -
HARDWARE INTERFACING USING C } restorecrtmode(); } show_mouse() { i.x.ax=1; int86(0x33,&i,&o); } hide_mouse() { i.x.ax=2; int86(0x33,&i,&o); } get_mouse_pos(int *x,int *y,int *button) { i.x.ax=3; int86(0x33,&i,&o); *x=o.x.cx; *y=o.x.dx; *button=o.x.bx&1; } There is nothing in this program to explain.Since you have gone through the program,you must have understood the logic.
Prepared By: Chirag Gohel
E-mail:
[email protected]
- 47 -
HARDWARE INTERFACING USING C 10) Line drawing using mouse. Following program shows how to draw a line interactively using mouse. If you know how to draw a line,there is no big deal in developing a program that draws a square.So,carefully observe and understand the following program. #include #include #include union REGS i,o; char far *p; void main() { Int gd=DETECT,gm,button; Int x1,y1,x2,y2,prevx2,prevy2,x,y; initgraph(&gd,&gm,"c:\\tc\\bgi"); i.x.ax=0; int86(0x33,&i,&o); if(o.x.ax==0) { printf("No Mouse is available.."); exit(); restorecrtmode(); } while(!kbhit()) { show_mouse(); get_mouse_pos(&x1,&y1,&button); if(button==1) { hide_mouse(); x2=x1; y2=y1; save(x1,y1,x2,y2); line(x1,y1,x2,y2); prevx2=x2; prevy2=y2; get_mouse_pos(&x2,&y2,&button); Prepared By: Chirag Gohel
E-mail:
[email protected]
- 48 -
HARDWARE INTERFACING USING C
while(button==1) { if(x2!=prevx2 || y2!=prevy2) { setcolor(BLACK); line(x1,y1,prevx2,prevy2); x=x1