View Full Version: bparlib.c(parallel port interfacig library

booksbybibin >>Computer interfacing >>bparlib.c(parallel port interfacig library


yogi- 07-03-2006
bparlib.c(parallel port interfacig library
This tutorial is taken from the book MY EXPERIENCE IN PARALLEL PORT INTERFACING. you can download it free from the files section(file:-parallelport(3 files) of yahoo group-booksbybibin http://groups.yahoo.com/group/booksbybibin/ comments about this book on www.booksbybibin.blogspot.com bparlib.c is a program written to acess parallel port easily for any programms All source codes in this book are free to distribute, provided you should give only working code The one advantage of bparlib.c is that normal pins of control ports and status ports are inverted but when u use bparlib.c, no need to see for inversion. It is automatically inverted in the library i.e, if you apply S7=1 then your status port reads S7=0(because it is complemented) but when you use Show_StatusPort_bit(7) it will read as '1' /* Content: Parallel Port library Last modified: 15 - 04 - 2006 Copyrights: Free to use, but provide only working code Compiler: Turbo C Language : C Written By: BIBIN JOHN for e-book MY EXPERIENCE IN PARALLEL PORT INTERFACING yahoo group:booksbybibin site: www.geocities.com/njbibin */ #include<stdio.h> #include<dos.h> #define PORT1 0x378 //basic port address, check this setting in bios #define DataPort PORT1 #define ControlPort PORT1+2 #define StatusPort PORT1+1 #define _BV(a) (1<<a) int Read_ControlPort(void); void Write_ControlPort(int a); void Set_DataPort_input(void); void Write_DataPort(int a); int Read_DataPort(void); int Read_StatusPort(void); void Set_DataPort_bit(int a); void Clear_DataPort_bit(int a); void Set_ControlPort_bit(int a); void Clear_ControlPort_bit(int a); int Show_DataPort_bit(int a); int Show_ControlPort_bit(int a); int Show_StatusPort_bit(int a); /*---------------------------------------------------------------- -------------function for reading control port------------------- -----------------------------------------------------------------*/ int Read_ControlPort(void) { return(inportb(ControlPort)); } /*---------------------------------------------------------------- -------------function for writting control port------------------- -----------------------------------------------------------------*/ void Write_ControlPort(int a) { outportb(ControlPort,a); } /*---------------------------------------------------------------- -------------function for reading data port---------------------- -----------------------------------------------------------------*/ int Read_DataPort(void) { int b; Set_DataPort_input(); b=inportb(DataPort); Clear_ControlPort_bit(5); return(b); } /*---------------------------------------------------------------- -------------function for writting data port---------------------- -----------------------------------------------------------------*/ void Write_DataPort(int a) { Clear_ControlPort_bit(5); outportb(DataPort,a); } /*---------------------------------------------------------------- -------------function for reading status port--------------------- -----------------------------------------------------------------*/ int Read_StatusPort(void) { return(inportb(StatusPort)); } /*---------------------------------------------------------------- -------------function for setting a bit in Data Port-------------- -----------------------------------------------------------------*/ void Set_DataPort_bit(int a) { if(a<8) Write_DataPort(Read_DataPort()|_BV(a)); } /*---------------------------------------------------------------- -------------function for clearing a bit in Data Port------------- -----------------------------------------------------------------*/ void Clear_DataPort_bit(int a) { if(a<8) Write_DataPort(Read_DataPort()&~_BV(a)); } /*---------------------------------------------------------------- -------------function for setting a bit in ControlPort------------ -----------------------------------------------------------------*/ void Set_ControlPort_bit(int a) { if(a<8) { if(a==0||a==1||a==3) Write_ControlPort(Read_ControlPort()&~_BV(a)); else Write_ControlPort(Read_ControlPort()|_BV(a)); } } /*---------------------------------------------------------------- ----------function for clearing a bit in Control Port------------- -----------------------------------------------------------------*/ void Clear_ControlPort_bit(int a) { if(a<8) { if(a==0||a==1||a==3) Write_ControlPort(Read_ControlPort()|_BV(a)); else Write_ControlPort(Read_ControlPort()&~_BV(a)); } } /*---------------------------------------------------------------- -----function for making data port for input (C5=1)--------------- -----------------------------------------------------------------*/ void Set_DataPort_input(void) { Write_ControlPort(Read_ControlPort()|_BV(5)); } /*---------------------------------------------------------------- -----function for showing Data Port bit ------------------------- -----returns (2) if the a is invalid -- ------------------------- -----------------------------------------------------------------*/ int Show_DataPort_bit(int a) { if(a<8) return((Read_DataPort()&_BV(a))>>a); else return (2); } /*---------------------------------------------------------------- -----function for showing Control Port bit ------------------------- -----returns (2) if the a is invalid -- ------------------------- -----------------------------------------------------------------*/ int Show_ControlPort_bit(int a) { if(a<8) return((Read_ControlPort()&_BV(a))>>a); else return (2); } /*---------------------------------------------------------------- -----function for showing Status Port bit ----------------------- -----returns (2) if the a is invalid -- ------------------------- -----------------------------------------------------------------*/ int Show_StatusPort_bit(int a) { int i; if(a<8) { i=(Read_StatusPort()&_BV(a))>>a; if(a==7) return(!i); else return(i); } else return (2); }


Forumer™ is Voted #1 Free Forum Hosting provider
Build your own community today with the largest message board hosting company.