/* This program demonstrates reading a character from the data port of the parallel port... compile this program using gcc -o nibble nibble.c */ #include #include #include #include #include /* Mem address of P port */ #define DATA 0x378 #define STATUS DATA+1 #define CONTROL DATA+2 main (int argc, char *argv[]) { unsigned char val, reg; unsigned int d; int fd, ack; // open /dev/port for reading and writing fd = open("/dev/port", O_RDWR); if(!fd) { printf("Error: unable to open /dev/port\n"); exit(-1); } //start of process //set HostBusy low to indicate ready for nibble lseek(fd, CONTROL, SEEK_SET); val = 0x02; write(fd, &val, 1); printf("HostBusy set low\n"); //we need to read in a loop until ack is low ack = 1; while (ack != 0) { // this is how you read the five input lines on the status port lseek(fd, STATUS, SEEK_SET); read(fd, &val, 1); // this converts the status lines to positive logic and shifts // them to the low 5 bits of val. val = ( val^0x80) >> 3 ; ack = val & 0x08; } //put high 4 bits into register reg = val & 0x07; reg = reg | ((val & 0x10) >> 1); reg = reg << 4; printf("Ack went low%X\n",reg); //set HostBusy high to indicate nibble received lseek(fd, CONTROL, SEEK_SET); val = 0x00; write(fd, &val, 1); printf("HostBusy high again\n"); //loop and read till ack goes high while (ack == 0) { lseek(fd, STATUS, SEEK_SET); read(fd, &val, 1); val = ( val^0x80) >> 3 ; ack = val & 0x08; } printf("Ack went high\n"); //we want to set HostBusy to low again lseek(fd, CONTROL, SEEK_SET); val = 0x02; write(fd, &val, 1); printf("HostBusy set low again\n"); //wait till ack is low again ack = 1; while(ack != 0) { lseek(fd, STATUS, SEEK_SET); read(fd, &val, 1); val = ( val^0x80) >> 3 ; ack = val & 0x08; } printf("ack went low again %X\n", (int)val); //put second 4 bits into register reg = reg | (val & 0x07); reg = reg | ((val & 0x10) >> 1); //set HostBusy high to indicate second nibble received lseek(fd, CONTROL, SEEK_SET); val = 0x00; write(fd, &val, 1); //end of process close(fd); //display final value printf("Received %d\n", (int)reg); }