programming
programming the parallel port interface in C is very easy.the example programs work fine for me on a x86 architecture machine with linux. other architectures and operating systems are untested, but may work, too.
the following example shows how to set and read bits from the interface. actually a byte is read and written and - of course - must be encoded/decoded with a binary encoding to obtain the bits.
--- io-example.c ---
/*
* Compile with `gcc -O2 [...]'.
*/
#include <stdio.h>
#include <unistd.h>
#include <asm/io.h>
#define BASEPORT 0x278 /* lp2 */
/*#define BASEPORT 0x378 /* lp1 */
int main()
{
/* Get access to the ports */
if (ioperm(BASEPORT,3,1)) {perror("ioperm");exit(1);}
/* Set the data signals (D0-7) of the port to all low (0) */
outb(0,BASEPORT);
/* Sleep for a while (1000 ms) */
usleep(1000000);
/* Read from the status port (BASE+1) and display the result */
printf("status: %d\n",inb(BASEPORT+1));
/* We don't need the ports anymore */
if (ioperm(BASEPORT,3,0)) {perror("ioperm");exit(1);}
exit(0);
}
/* end of io-example.c */
the following command line tool was designed for interactive and non-interactive operation. e.g. a bash script could use
echo "246x" |facoto toggle the outputs 2, 4 and 6.
--- faco.c ---
/*
* Compile with `gcc -O2 [...]'.
* faco - Face Control Program
* (c) 1998, Ben Fuhrmannek <ben@fuhrmannek.de>
* released under GPL (GNU General Public License)
*/
#include <stdio.h>
#include <unistd.h>
#include <asm/io.h>
#define BASEPORT 0x278 /* lp2 */
/*#define BASEPORT 0x378 /* lp1 */
int li[7];
int in[4];
ok(char x) {printf("%c: OK!\n",x);}
led(int l) /* l = 1..8 */
{
int i, setz = 0;
if (li[l-1] == 0) li[l-1] = 1; else li[l-1] = 0;
setz = (1*li[0]) + (2*li[1]) + (4*li[2]) + (8*li[3]) +
(16*li[4])+(32*li[5])+(64*li[6])+(128*li[7]);
outb(setz,BASEPORT);
}
getIN()
{
int i, wert;
for (i=0; i <= 4; ++i) in[i] = 0;
wert=inb(BASEPORT+1)-7;
if (wert >= 128) {wert = wert - 128; in[0] = 1;}
wert = 127 - wert;
if (wert > 64) {wert = wert - 64; in[1] = 1;}
if (wert > 32) {wert = wert - 32; in[2] = 1;}
if (wert > 16) {wert = wert - 16; in[3] = 1;}
if (wert > 8) in[4] = 1;
}
main()
{
int i, j, ex = 1;
char c;
for (i = 0; i <= 7; ++i) li[i] = 0;
if (ioperm(BASEPORT,3,1)) {perror("ioperm");exit(1);}
outb(0,BASEPORT);
while (ex > 0)
{
/* printf("--> "); */
c = getchar();
switch(c)
{
case '0':
for (i = 0; i <= 7; ++i) li[i] = 0;
outb(0,BASEPORT);
break;
case '1': led(1); break;
case '2': led(2); break;
case '3': led(3); break;
case '4': led(4); break;
case '5': led(5); break;
case '6': led(6); break;
case '7': led(7); break;
case '8': led(8); break;
case '9':
for (i = 0; i <= 7; ++i) li[i] = 1;
outb(255,BASEPORT);
break;
case 's':
printf("LED");
for (i = 0; i <= 7; ++i) printf(" : %d",li[i]);
printf("\n");
printf("status: %d OK!\n",inb(BASEPORT+1));
getIN();
printf("IN");
for (i = 0; i <= 4; ++i) printf(" : %d",in[i]);
printf("\n");
break;
case 'd':
usleep(1000000);
break;
case 'x': ex = 0; break;
default: /* printf("Falsche Eingabe!"); */ break;
}
/* ok(c); */
}
if (ioperm(BASEPORT,3,0)) {perror("ioperm");exit(1);}
}