PC Robot starter OS
Robot os. The infinite loop.
While true%
' set or reset values
' input sensor values
' Other jobs
' Do what is needed based on input values.
wend
Part I. Set or reset values.
Part II Get sensor values.
Using basic, the command to read from ports in qbasic is INP. The address you need to read from is the address of the parallel port (usually 378h) + 1; so the usual address is 379h. As i mentioned before, the pins used for input are 10-13 and 15. Reading the different pins is little harder than writing to the ports, as you have to mask out the pins your not interested in. When you read the port, the first 3 bits returned are not used. For example, the qbasic code below would read pin 12 (out of paper). When this port is high, "Out of paper / pin 12 toggled" will be displayed:
data = inp(&h379) IF (data and 32) = 32 then print "Out of paper / pin 12 high"
The table below list the pin input pin number, its normal purpose, and the number required to read it (eg 32 was used in the above example):
Pin Number | Normal Purpose | Number to read |
10 | Ackowledge | 64 |
11 | High when not Busy | 128 |
12 | High when out of paper | 32 |
13 | High when printer online | 16 |
15 | High when no error | 8 |
If you understand binary, you should immediately see where these numbers are coming from, and why they are used to mask out all of the other pins we aren't trying to read. The simplest thing you can use to test this is to just connect a switch between an input pin and a ground pin (18-25). For example, if you connect a switch between pins 15 and 20, you could use the following code to monitor when the switch is pushed:
start: res = INP(&H379) CLS IF (res AND 8) = 8 THEN PRINT "Button pushed" ELSE PRINT "Button NOT pushed" FOR delay = 1 TO 500: NEXT delay GOTO start
I know this code is a little messy, and will flicker, but it's written for clarity. The same code can be used to monitor the other four input lines by changing to the '8' to another number from the table.
Part III Other jobs.
Deliver reports to the outside world. etc etc etc
Part IV Do what is needed based on input values.
We can use Qbasic or freebasic to set values at the parallel port for such jobs as controlling the power train (aka turning or moving forward/backward). Moving other attachements. See also:
http://www.instructables.com/id/Mini-parallel-port-break-out-cable/
http://www.instructables.com/id/No-solder-parallel-port-break-out/
Control the individual output lines on the parallel port.
parcon.c
#include <stdio.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/io.h>
char *binprint( unsigned char x, char *buf )
{
int i;
for( i=0; i<8; i++ )
buf[7-i]=(x&(1<<i))?'1':'0';
buf[8]=0;
return buf;
}
int main( int argc, char *argv[] )
{
char c;
unsigned char val;
char buf[9];
int x;
if( argc<2 )
{
printf(" example usage: parcon 1l 2l 3h 5h 8l\n");
return 2;
}
if( ioperm(888,1,1) )
{
printf("Couldn't get port 888\n");
return 1;
}
val = inb(888);
printf("old = %s\n",binprint(val,buf));
for( x=1; x<argc; x++ )
if( argv[x][1]!='h' )
val &= ~(1<<(argv[x][0]-'1'));
else
val |= 1<<(argv[x][0]-'1');
printf("new = %s\n",binprint(val,buf));
outb(val,888);
return 0;
}
Comments
Post a Comment