pref home next
day4: write test software
Let's hope the principle works, then I can really begin building a device.
The program I will write should be able to recieve the status, and should know which FF is ON and which one is OFF (the hardest part of this program)
After that I should be able to toggle the FF of my choice.
when that's done, The program should be able to see that.
The design will be easy:
1, read data from parallel port
2, decode data and print which FF's are ON and which are OFF
3, ask which FF should be toggled
4, send data to parallel port
5, pause and go to 1 (endless loop)
the code:
// prototype_test.c - e.boelen - 15 august 2002
// I have connected 4 flip flops to the parallel port, their outputs will be read
// after that you can toggle the flip flops
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h> /* needed for ioperm() */
#include <asm/io.h> /* for outb() and inb() */
#define PORT 0x378
int main(int argc, char *argv[])
{
int readport,value,getdata;
int toggleport;
readport=PORT+1;
for(;;){
//// read data from parallel port
if (ioperm(readport,1,1)) {
printf("Sorry, you were not able to gain access to the ports\n");
printf("You must be root to control ports\n");
getdata=0;
}
else {
getdata=inb(readport);
getdata=getdata-15;// assume inputs 5,6,7,8 (binair status 15) are always ON
}
printf("readport gave: %d \n",getdata);
// decode the binair code
if ( (getdata-128)>=0) {
printf(" - input 1 - OFF \n");// input 1 is inverted
getdata=getdata-128;}
else printf(" - input 1 - ON\n");
if ( (getdata-64)>=0){
printf(" - input 2 - ON \n");
getdata=getdata-64;}
else printf(" - input 2 - OFF \n");
if ( (getdata-32)>=0){
printf(" - input 3 - ON \n");
getdata=getdata-32;}
else printf(" - input 3 - OFF \n");
if ( (getdata-16)>=0){
printf(" - input 4 - ON \n \n");}
else printf(" - input 4 - OFF \n");
// figure out what to send
printf ("enter a output (1-4) you want to toggle: \n");
scanf ("%d",&toggleport);
while (toggleport<=0||toggleport>=5){
printf("enter a number between 1 and 4 for the port you want to toggle: \n");
scanf ("%d",&toggleport);
}
printf ("sending to port: %d...\n",toggleport);
if (toggleport==1) value=1;
if (toggleport==2) value=2;
if (toggleport==3) value=4;
if (toggleport==4) value=8;
//send the data to PORT
ioperm(PORT,1,1);
outb(value,PORT);
sleep(1);
outb(0,PORT);
//done
printf("done\n");
sleep(1);
}
return(0);
}
pref home next
day4: write test software