Es folgt das erste Listing von Michael Müller. Hiermit betreibt er die Karte gemäß Abschnitt 4.1 als Thermometer.
/*
* ad.c: very simple example of port I/O
*
* This code reads the 16 inputs of a connected 12-bit io-card
* Compile with `gcc -O2 -lm -o adwandler adwandler.daemon.c`
*/
#include <stdio.h>
#include <unistd.h>
#include <asm/io.h>
#include <math.h>
#include <time.h>
#define datei "/var/log/temperatur"
#define refvoltage 2407.0 /* entspricht 5V bei 12 bit bis 8,5V */
#define vara 158 /* empirisch ermittelt-NTC abhaengig */
#define varb 3567.221 /* empirisch ermittelt-NTC abhaengig */
#define BASEPORT 0x290 /* ad1 */
#define AD_CHANNEL (BASEPORT + 0)
#define AD_LOWBYTE (BASEPORT + 1)
#define AD_HIGHBYTE (BASEPORT + 2)
#define AD_SETZERO (BASEPORT + 3)
#define AD_readf (BASEPORT + 4)
#define AD_reads (BASEPORT + 5)
#define DA_LOWBYTE (BASEPORT + 6)
#define DA_HIGHBYTE (BASEPORT + 7)
#define LOOPS 1 /* Anzahl der Messungen pro Output */
#define OFFSET 0 /* 0 Volt Fusspunkt */
int main()
{
double temperatur;
double temper;
float resistor;
float resist;
int highbyte;
int lowbyte;
int channel;
int i;
int a;
int k;
FILE *protokoll;
time_t sekunden;
/* get access to the ports */
if (ioperm(BASEPORT,8,1)) {perror("ioperm");exit(1);}
protokoll = fopen(datei, "a");
while ( 5 > 3 ) /* dumme Bedingung, ich weiss */
{
sekunden = time((time_t *) 0);
fprintf(protokoll, "%lu", sekunden);
/* Auslesen der 16 Kanaele */
for (channel = 0; channel < 16; channel++)
{
outb(0,AD_SETZERO); /* noch ungeklaert aber notwendig */
outb(channel,AD_CHANNEL); /* Anwahl des Kanals */
a = 0;
/* for (k = 0; k < LOOPS; k++) */
{
outb(channel,AD_CHANNEL);
/* outb(0,AD_CHANNEL); */
for (i = 0; i < 7; i++) /* 7bits digitalisieren low */
{
a = inb(AD_readf);
usleep(1000); /* verzoeg. fuer den Wandler */
}
for (i = 0; i < 7; i++) /* 7bits digitalisieren high sind
zwar nur 4 muss aber sein */
{
a = inb(AD_reads);
usleep(1000); /* verzoeg. fuer den Wandler */
}
highbyte = (inb(AD_HIGHBYTE) & 0x0f);
lowbyte = inb(AD_LOWBYTE);
}
a = highbyte * 256 + lowbyte + OFFSET;
resist = (refvoltage / a) - 1;
resistor = 1000 / resist; /* bestimmen des NTC Widerstands */
temperatur = varb / log(resistor * vara) - 273;
fprintf(protokoll, "%7.2f", temperatur);
usleep(1000);
}
fprintf(protokoll, "\n");
fflush(protokoll);
sleep(55); /* ergibt ca 1 minute pro Messreihe */
}
/* Alles ab hier ist eigentlich quatsch, da es nie ausgefuehrt wird, da oben
eine endlosschleife ist. */
/* We don't need the ports anymore */
if (ioperm(BASEPORT,3,0)) {perror("ioperm");exit(1);}
exit(0);
}
/* end of adwandler.daemon.c */