#include <stdio.h>
#include <NIDAQmx.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
void wait ( int seconds )
{
clock_t endwait;
endwait = clock () + seconds * CLOCKS_PER_SEC ;
while (clock() < endwait) {}
}
int main(void)
{
time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
time_t seconds;
int flag=0;
double temp=0;
int i=0;
uInt8 s=0;
uInt8 Dati[8]={0,0,0,0,0,0,0,0};
uInt8 Porta1[2]={0,0};
uInt8 LED[8]={1,0,0,0,0,0,0,0};
TaskHandle tk1, tk2, tk3, tk4, data, led, t, t2; ;
DAQmxCreateTask("tk1",&tk1);
DAQmxCreateTask("tk2",&tk2);
DAQmxCreateTask("tk3",&tk3);
DAQmxCreateTask("tk4",&tk4);
DAQmxCreateTask("Data",&data);
DAQmxCreateTask("led",&led);
DAQmxCreateTask("task1",&t);
//Creo File
FILE *fp = fopen("C:\\Users\\Studenti_Simone\\Desktop\\CorsoSimone\\Risorse\\RisultatoTemp.txt","w");
if(!fp)
{
printf("\n file di input non trovato \n");
system("pause");
return 0;
};
for(flag = 0; flag <= 1000; flag++)
{
//Output channel
DAQmxCreateDOChan(tk1, "Dev1/port1/line0", "Output1", DAQmx_Val_ChanForAllLines);
DAQmxStartTask(tk1);
DAQmxCreateDOChan(tk2, "Dev1/port1/line7", "Output2", DAQmx_Val_ChanForAllLines);
DAQmxStartTask(tk2);
DAQmxCreateDOChan(tk3, "Dev1/port1/line6", "Output3", DAQmx_Val_ChanForAllLines);
DAQmxStartTask(tk3);
DAQmxCreateDOChan(led, "Dev1/port0/line0:7", "Output4", DAQmx_Val_ChanForAllLines);
DAQmxCreateDOChan(t, "Dev1/port1/line0:1", "input", DAQmx_Val_ChanForAllLines);
//Input Channel
DAQmxCreateDIChan(tk4, "Dev1/port2/line1", "input1", DAQmx_Val_ChanForAllLines);
DAQmxStartTask(tk4);
DAQmxCreateDIChan(data, "Dev1/port0/line0:7", "InputData", DAQmx_Val_ChanForAllLines);
DAQmxStartTask(data);
//Otput Reset linea 1.0
DAQmxWriteDigitalLines(tk1,1,0,10,DAQmx_Val_GroupByChannel,&s,NULL,NULL);
s=1;
DAQmxWriteDigitalLines(tk1,1,0,10,DAQmx_Val_GroupByChannel,&s,NULL,NULL);
s=0;
DAQmxWriteDigitalLines(tk1,1,0,10,DAQmx_Val_GroupByChannel,&s,NULL,NULL);
//Output Start Conversion linea 1.7
DAQmxWriteDigitalLines(tk2,1,0,10,DAQmx_Val_GroupByChannel,&s,NULL,NULL);
s=1;
DAQmxWriteDigitalLines(tk2,1,0,10,DAQmx_Val_GroupByChannel,&s,NULL,NULL);
s=0;
DAQmxWriteDigitalLines(tk2,1,0,10,DAQmx_Val_GroupByChannel,&s,NULL,NULL);
//printf(" Waiting for data ready on linea 2.1\n");
i = 0;
while(s==1){
//printf("Step %d:\nlinea2.1 %d \n", i,s);
DAQmxReadDigitalLines(tk4,1,10,DAQmx_Val_GroupByChannel,&s,1,NULL,NULL,NULL);
i++;
};
s=0;
//Output Data Enable 1.6
DAQmxWriteDigitalLines(tk3,1,0,10,DAQmx_Val_GroupByChannel,&s,NULL,NULL);
s=1;
DAQmxWriteDigitalLines(tk3,1,0,10,DAQmx_Val_GroupByChannel,&s,NULL,NULL);
//printf(" linea 1.6 abilitata\n");
//Fase di lettura porta 8 Bit
//printf("Fase di Lettura : Inizio lettura porta0 a 8 bit\n");
//operazione di lettura
DAQmxReadDigitalLines(data,1,1,DAQmx_Val_GroupByChannel,Dati,8,NULL,NULL,NULL);
// FIne Lettura
i=0;
while(i!=8){
printf("%d", Dati[i]);
fprintf(fp,"%d" ,Dati[i]);
temp=(Dati[i]*pow(2.,i))/2+temp;
i++;
}
fprintf(fp,"\t" );
seconds = time (NULL);
timeinfo = localtime ( &rawtime );
printf("-> %.1f C %ld sec %s", temp, seconds,asctime (timeinfo));
fprintf(fp,"%f\t%ld\t%s\n" ,temp,seconds,asctime (timeinfo));
temp=0;
printf(" \n");
//Output Data Disable 1.6
s=0;
DAQmxWriteDigitalLines(tk3,1,0,10,DAQmx_Val_GroupByChannel,&s,NULL,NULL);
DAQmxStopTask(tk1);
DAQmxStopTask(tk2);
DAQmxStopTask(tk3);
DAQmxStopTask(tk4);
DAQmxStopTask(data);
//segnali di inizializzazione
DAQmxStartTask(t);
DAQmxWriteDigitalLines(t,1,0,1,DAQmx_Val_GroupByChannel,Porta1,NULL,NULL);
Porta1[0]=1;
DAQmxWriteDigitalLines(t,1,0,1,DAQmx_Val_GroupByChannel,Porta1,NULL,NULL);
Porta1[0]=0;
DAQmxWriteDigitalLines(t,1,0,1,DAQmx_Val_GroupByChannel,Porta1,NULL,NULL);
printf("\a");
DAQmxStartTask(led);
//visualizzo sui led
DAQmxWriteDigitalLines(led,1,0,1,DAQmx_Val_GroupByChannel,Dati,NULL,NULL);
//Segnale di scrittura per rendere effettivo lo spegnimento
Porta1[1]=0;
DAQmxWriteDigitalLines(t,1,0,1,DAQmx_Val_GroupByChannel,Porta1,NULL,NULL);
Porta1[1]=1;
DAQmxWriteDigitalLines(t,1,0,1,DAQmx_Val_GroupByChannel,Porta1,NULL,NULL);
Porta1[1]=0;
DAQmxWriteDigitalLines(t,1,0,1,DAQmx_Val_GroupByChannel,Porta1,NULL,NULL);
wait(2);
DAQmxStopTask(t);
DAQmxStopTask(led);
}
fclose(fp);
system("pause");
}
Nessun commento:
Posta un commento