Computer(Stecker: Sub-D 15 und 25 male,Gameport und Parallelport) |
Sensor(von unten/hinten auf den Sensor geschautund von links nach rechts numeriert) |
| Vcc (Gameport: Pin 1) | Vcc (Pin 3) |
| GND (Gameport: Pin 4) | GND (Pin 1) |
| GND (Parallel Port: Pin 18) | GND (Pin 1) |
| Out (Parallel Port: Pin 9) * | In (Pin 2) |
| In (Parallel Port: Pin 11) ** | Out (Pin 4) |
* den Spannungsteiler (z.B. ein Poti) auf max. 3V nicht vergessen!
** je nach Stromaufnahme der Parallelen Schnittstelle, sollte man hier
sicherheitshalber einen Treiber (z.B. ein CMOS-IC) verwenden.
// Project: Test des IR-Positionssensors (SHARP GP2D02)
// File : "PSD-TEST.CPP" (main file)
// Author : Lars Ferner, Uni Kaiserslautern
// Date : 95-11-16 / 95-10-31
// Header files (prototypes of imported functions)
#include <conio.h>
#include <iostream.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <timer.h>
// Macros
#define high 128
#define low 0
// Global variables
int tmis, lpt_out, lpt_in;
// Functions (prototypes)
void Error( void );
void Calibrate();
void InitLpt();
void Single( void );
void Continous( void );
void SetVin( int Level );
int GetVout();
void Wait( long int mis );
// Main function
int main( int argc, char *argv[] )
// 1. Param.: Modus - (t)est, (s)ingle, (c)ontinous
// 2. Param.: Anzahl der Zeitschleifendurchläufe
// 3. Param.: Drucker-Port
{
clrscr();
if ( ( argc == 3 ) && ( strcmp( argv[1], "t" ) == 0 ) )
{
tmis = atoi( argv[2] );
if ( tmis < 0 )
Error();
Calibrate();
}
else if ( argc == 4 )
{
if ( strcmp( argv[1], "s" ) == 0 )
{
tmis = atoi( argv[2] );
if ( tmis < 0 )
Error();
lpt_out = atoi( argv[3] );
lpt_in = lpt_out + 1;
InitLpt();
Single();
}
else if ( strcmp( argv[1], "c" ) == 0 )
{
tmis = atoi( argv[2] );
if ( tmis < 0 )
Error();
lpt_out = atoi( argv[3] );
lpt_in = lpt_out + 1;
InitLpt();
Continous();
}
}
else
Error();
return ( EXIT_SUCCESS );
}
// Functions (definitions)
void Error( void )
{
cerr << "Bitte Modus - (t)est, (s)ingle, (c)ontinous - ," << endl;
cerr << "Anzahl der Schleifendurchlaeufe fuer 10 mikrosec. und" << endl;
cerr << "zu verwendenden Drucker-Port (0278h=632d, 0378h=888d, 03BCh=956d)
angeben." << endl << endl;
cerr << "Vergleichswerte:" << endl;
cerr << " 16 MHz 386'er SX - 2" << endl;
cerr << " 40 MHz 386'er DX - 23" << endl;
cerr << "100 MHz Pentium - 493" << endl << endl;
exit( EXIT_FAILURE );
}
void Calibrate()
{
// Local variables
Timer Stoppuhr;
Stoppuhr.reset();
Stoppuhr.start();
Wait( 1000000L );
Stoppuhr.stop();
cout << "Es sollten 10 s vergangen sein; es sind " << Stoppuhr.time() << " s
vergangen." << endl << endl;
}
void InitLpt()
{
outportb( lpt_out, 0 );
}
void Single( void )
{
// Local variables
int k = 0, i, j, Result[8], dez, min, max, ave;
do
{
cout << "Messung " << ++k << " laeuft..." << endl;
min = 256;
max = -1;
ave = 0;
for ( i = 0; i < 20; i++ )
{
SetVin( low );
Wait( 7000 );
for ( j = 0; j < 8; j++ )
{
SetVin( high );
Wait( 10 );
SetVin( low );
Wait( 5 );
Result[j] = GetVout();
Wait( 4 );
}
SetVin( high );
Wait( 200 );
dez = 0;
for ( j = 0; j < 8; j++ )
dez += ( Result[7-j] == high ? pow( 2, j ) : 0 );
if ( dez < min ) min = dez;
if ( dez > max ) max = dez;
ave += dez;
}
ave /= 20;
cout << "Minimum :" << min << " (" << ( ave - min ) << ")" << endl;
cout << "Maximum :" << max << " (" << ( max - ave ) << ")" << endl;
cout << "Mittelwert:" << ave << endl << endl;
}
while ( getch() != 'e' );
}
void Continous( void )
{
// Local variables
int j, Result[8], dez;
while ( !kbhit() )
{
SetVin( low );
Wait( 7000 );
for ( j = 0; j < 8; j++ )
{
SetVin( high );
Wait( 10 );
SetVin( low );
Wait( 5 );
Result[j] = GetVout();
Wait( 4 );
}
SetVin( high );
Wait( 200 );
dez = 0;
for ( j = 0; j < 8; j++ )
dez += ( Result[7-j] == high ? pow( 2, j ) : 0 );
cout << dez << endl;
}
getch();
}
void SetVin( int Level )
{
outportb( lpt_out, Level );
}
int GetVout()
{
return ( inportb( lpt_in ) & high );
}
void Wait( long int TenMicroSecs )
{
// Local variables
int i;
while ( TenMicroSecs-- > 0 )
{
i = tmis;
while ( i-- > 0 );
}
}
Zurück zu den Hardware-Erweiterungen