/* FILE: MAX158E.C DESC: ACQUIRE FROM MAX158 AND PLOT NB: 0.1 SEC SAMPLING RATE */ #include #include #include #include #include #include #include void interrupt (*oldvect)(void); void interrupt myInterrupt(void); void drawAxis(void); long count = 0; #define VREF 2.5 #define pixelsPerSecond 60.0 /* 600 pixels / 10.0 sec */ #define offsetX 30.0 #define pixelsPerVolt 133.33 /* 400 pixels / 3.0 V */ #define offsetY 425.0 int main(void) { int channel, desiredChannel; float outputVoltage; int outputVoltageInPixels; int baseAddress; int AIN1, AIN2, AIN3, AIN4, AIN5, AIN6, AIN7, AIN8; int data; FILE* fp; int i = 0; long oldCount = 0, newCount = 0; float updateRateInSeconds; float timePastInSeconds; int timePastInPixels; int updateRateInTicks; fp = fopen("data.txt", "wt"); /* open for ASCII write */ baseAddress = 608; AIN1 = 0; AIN5 = 4; AIN2 = 1; AIN6 = 5; AIN3 = 2; AIN7 = 6; AIN4 = 3; AIN8 = 7; desiredChannel = 1; switch(desiredChannel) { case 1 : channel = AIN1; break; case 2 : channel = AIN2; break; case 3 : channel = AIN3; break; case 4 : channel = AIN4; break; case 5 : channel = AIN5; break; case 6 : channel = AIN6; break; case 7 : channel = AIN7; break; case 8 : channel = AIN8; break; default : channel = 999; break; } updateRateInSeconds = 0.1; /* acquire data every 0.1 sec */ /* 18.2 ticks = 1 second. 1 tick = 0.055 seconds */ updateRateInTicks = (int)(updateRateInSeconds*18.2); drawAxis(); /* draw axis and tick marks */ oldvect = getvect(0x1C); /* save old interrupt vector */ setvect(0x1C, myInterrupt); /* install my interrupt handler */ do { if(!(count % updateRateInTicks)) { /* loop entered whenever count is integer multiple of updateRateInTicks. In other words, timePastInSeconds is printed out every updateRateInSeconds */ newCount = count; timePastInSeconds = (float)(count/18.2); if(oldCount != newCount) { /* acquire ADC sample */ data = adc_convert(baseAddress, channel); outputVoltage = (float)((data/255.0)*2.5); /* plot outputVoltage */ timePastInPixels = (int)(offsetX + (timePastInSeconds * pixelsPerSecond)); outputVoltageInPixels = (int)(offsetY - (pixelsPerVolt*outputVoltage)); lineto(timePastInPixels, outputVoltageInPixels); outtextxy(timePastInPixels, outputVoltageInPixels, "*"); /* fprintf(fp, "%f\t%f\n", timePastInSeconds, outputVoltage); */ } oldCount = count; } } while( timePastInSeconds <= 10.0 ); fclose(fp); setvect(0x1c, oldvect); /* set the old interrupt handler back */ while(!kbhit()) {}; closegraph(); printf("count = %ld, timePastInSeconds = %f", count, timePastInSeconds); return 0; } /* end of main */ int adc_convert(int address, char chan_num) { int dataRead; outportb(address, chan_num); dataRead = inportb(address); return(dataRead); } /* end of adc_convert */ void interrupt myInterrupt(void) { count++; oldvect(); }; /* end of myInterrupt */ void drawAxis(void) { int gdriver, gmode, errorCode; int x, y; int deltaX = 60; /* horizontal pixels */ int deltaY = 40; /* vertical pixels */ int i = 0; float j = 3.0; char temp[10]; gdriver = DETECT; /* auto detect graphics card */ initgraph(&gdriver, &gmode, "C:\\TC"); /* directory contains *.BGI files */ errorCode = graphresult(); if(errorCode != grOk) { printf("Graphics error %s\n", grapherrormsg(errorCode)); exit(0); }; setcolor(WHITE); /* X-axis setup */ line(30,425,630,425); /* X-axis line */ outtextxy(320,450, "Time [s]"); /* Tick marks every deltaX = 60 pixels. 1 Tick mark = 1 second */ for(x=30; x<=630; x=x+deltaX) { line(x,425,x,430); sprintf(temp, "%d", i); outtextxy(x,435, temp); i = i + 1; }; /* Y-axis setup */ line(30,25,30,425); /* Y-axis line */ outtextxy(0,5, "Volts"); /* Tick marks every deltaY = 40 pixels. 1 Tick mark = 0.3 V */ for(y=25; y<=425; y=y+deltaY) { line(30,y,25,y); sprintf(temp, "%1.1f", j); outtextxy(0,y, temp); j = j - 0.3; }; putpixel(offsetX, offsetY, WHITE); } /* end of drawAxis */