pilotaggio adc seriale con porta parallela pc.doc



#include <stdio.h>
#include <conio.h>
#define LPTOUT 0x378
#define LPTIN 0x379
#define D0 0x1 //ioclock ADC
#define D1 0x2 //cs ADC
#define SELIN 0x10 //dataout ADC#
void ritardo(int rit)
{
asm{
mov cx,rit
}
lrit:
asm{
loop lrit
}
}
int Convers()
{
int v;
// Ciclo conversione
asm {
mov dx,LPTOUT // CS=0, abilito ADC
in al,dx
and al,not D1
out dx,al
mov bx,0 //bl contiene il dato prelevato dall'ADC
}
ritardo(100);
asm{
mov cx,8 //contatore
}
cicloconv:
asm {
mov dx,LPTIN
in al,dx
and al,SELIN //prelevo il bit dall'ADC
rcr ax,5
rcl bx,1
mov dx,LPTOUT // impulso di CLK su ADC
in al,dx
or al,D0
out dx,al
push cx
}
ritardo(1000);
asm{
pop cx
and al,not D0
out dx,al
push cx
}
ritardo(1000);
asm{
pop cx
loop cicloconv
mov v,bx
mov dx,LPTOUT // CS=0, abilito ADC
in al,dx
or al,D1
out dx,al
}
return v;
}
void InitAdc()
{
asm{
mov dx,LPTOUT //inizializzazione
mov al,D1 //CS=1 disabilito ADC
out dx,al
}
}
void main()
{
int ADCValue;
InitAdc();
ADCValue=0;
do
{
ADCValue=Convers();
printf("%5d",ADCValue);
}
while (!kbhit());
}