/* SURP summer '06 * Department of Physics, Pomona College * Program modified by Yavor Kostov * and Ragib Morshed (till 06/02/06) */ #include #include #include #include #include #include //#include #define keyb 0x0060 // Global variables int i, running, run; int ntot, kcheck; long int count; float circum; char cho, filename[20], buffer[80],dum[80]; double startt, TT[2000], tt[2000],vv[2000],on[2000], off[2000], onn, fac, cpu; int ntot, s1; double fac, cpu, fr, frc; unsigned long tl1, th1, ontl1, onth1, ptest; unsigned long tl2, th2, ontl2, onth2; unsigned long tl3, th3, ontl3, onth3; unsigned long dum1, dum2; FILE *stream, *out,*inf; void menu(); void setpoints(); void takedat(); void testgate(); void writeout(); int main() { ntot=4; iopl(3); circum=30.37; fac=4294967296.0; // Get the cpu frequency char filename[]="/proc/cpuinfo"; inf=fopen(filename,"r"); while(fr<100.0) { fscanf(inf,"%s", dum); fr=atof(dum); if(fr>10) frc=fr; // printf("%s %f\n",dum, fr); } cpu=frc*1000000.0; printf("The cpu clock frequency is %f Hz\n\n",cpu); menu(); running=1; do { { printf("input an option: \n"); scanf("%c", &cho); switch(cho) { case 'n' : setpoints(); //set points menu(); break; case 'd' : takedat(); //take data break; case 't' : testgate(); //test gate break; case 'm' : menu(); //menu display break; case 'w' : writeout(); //write to file break; case 'Q' : running = 2; default : menu(); break; } } }while (running==1); return (0); } // writes out the data taken in a text file void writeout() { printf("Input the filename for saving the file: \n"); scanf("%s",filename); out=fopen(filename,"w"); fprintf(out, "time velocity absolute time \n"); for (i=2; i<=ntot; i++) { fprintf(out, "%0.10lf %0.10lf %0.10lf \n", tt[i],vv[i],TT[i]); } fclose(out); printf("The file has been written \n"); } // displays the menu options void menu() { printf(" ntot = %d \n", ntot); printf(" press n to set the number of data points\n"); printf(" press d to take data\n"); printf(" press t to test the photogate\n"); printf(" press m to see the menu\n"); printf(" press w to write out\n"); printf(" press Q to exit the program\n\n\n"); } // to test the photodiode void testgate() { for (i=0; i<40;i++) { ptest=inb(0x379); printf("%u", ptest); if (inb(0x379)==63) printf("Un-blocked\n"); if (inb(0x379)==127) printf("Blocked\n"); usleep(100000); } printf(" \n The test is over\n"); printf("Press t to test again or m to menu\n\n"); } // sets the number of data points to be stored // minimum: 4 // maximum: as set by user void setpoints() { printf("Input the # of data points to be stored in file\n"); scanf("%d",&s1); ntot = s1; if (s1<4) ntot=4; if (s1>100) ntot=s1; } // takes data real time and displays in a table void takedat() { asm volatile("rdtsc":"=a"(dum1),"=d"(dum2)); run=1; while (run==1) { if (inb(0x379)==127) run=2; } asm volatile("rdtsc":"=a"(dum1),"=d"(dum2)); tl1=dum1; th1=dum2; run=1; while (run==1) { if (inb(0x379)==63) run=2; } asm volatile("rdtsc":"=a"(dum1),"=d"(dum2)); ontl1=dum1; onth1=dum2; run=1; while (run==1) { if (inb(0x379)==127) run=2; } asm volatile("rdtsc":"=a"(dum1),"=d"(dum2)); tl2=dum1; th2=dum2; run=1; while (run==1) { if (inb(0x379)==63) run=2; } asm volatile("rdtsc":"=a"(dum1),"=d"(dum2)); ontl2=dum1; onth2=dum2; i=0; kcheck=1; startt=0.0; // loop to take in data real time // and display in tabular form while(kcheck==1) { i=i+1; run=1; while (run==1) { if (inb(0x379)==127) run=2; } asm volatile("rdtsc":"=a"(dum1),"=d"(dum2)); tl3=dum1; th3=dum2; usleep(200); run=1; while (run==1) { if (inb(0x379)==63) run=2; } asm volatile("rdtsc":"=a"(dum1),"=d"(dum2)); ontl3=dum1; onth3=dum2; usleep(200); while (run==2) { if (inb(0x379)==127) run=1; } asm volatile("rdtsc":"=a"(dum1),"=d"(dum2)); tl2=dum1; th2=dum2; usleep(200); run=2; while (run==2) { if (inb(0x379)==63) run=1; } asm volatile("rdtsc":"=a"(dum1),"=d"(dum2)); ontl2=dum1; onth2=dum2; onn=(1.0*ontl2-1.0*tl2)+fac*(onth2-th2); vv[i]=cpu*1.0/onn; TT[i]=0; if (i==2) startt=(1.0*ontl2+1.0*tl2+fac*(onth2+th2)); if (i>2) TT[i]=(TT[i]+1.0*ontl2+1.0*tl2+fac*(onth2+th2)-startt)/(cpu*2.0); onn=(ontl3*1.0 - 1.0*ontl1 + tl3*1.0 - 1.0*tl1)/2 + fac*((1.0*onth3 - 1.0*onth1 + 1.0*th3 - 1.0*th1)/2); tt[i]=onn/cpu*1.0; printf("%d %.10f %.10f %.10f \n", i, tt[i], vv[i], TT[i]); tl1=tl3; th1=th3; ontl1=ontl3; onth1=onth3; if(inb(keyb)==144) kcheck=2; usleep(200); ntot=i; } off[0]=1; on[0]=1; printf("\nThe last %d points can be stored\n", ntot); printf("Press d to take data again or m to see menu\n\n"); }