#include #include /* getadc(n, chan) returns integer value proportional to * average (from n measurements) voltage in the range of * -32000 to 32000 ( respectively from -10.0 V to +10.0 V ) * n - number of measurements (no more than 32000) * chan - channel number (0 - 7) */ getadc(n,chan) int n,chan; { int i,curdata,result; long int sum; for(i=1,sum=0;i<=n;++i) { utpokew(START_ADC,chan); while(( utpeekw(ADC) & 040000) == 0); curdata = (utpeekw(ADC) & 0107777); if(curdata & 0100000) curdata = -(curdata & 07777); sum += curdata; } result = sum*8/n; return(result); } /* set_freq(timer_addr, chan, infreq, outfreq) * sets output freqency of a timer. * const timer_addr - TIMER_1 or TIMER_2; * chan - channel of selected timer (0, 1 or 2); * double infreq - input freqency, Hz. * outfreq - output freqency, Hz. */ set_freq(timer_addr, chan, infreq, outfreq) unsigned timer_addr; int chan; double infreq, outfreq; { unsigned int ctrl_addr, data_addr, mode, div_coeff; div_coeff = infreq/outfreq + 0.5; ctrl_addr = timer_addr + 6; data_addr = timer_addr + chan*2; mode = 066 + 0100*chan; utpokew( ctrl_addr, mode); utpokew( data_addr, utlobyte(div_coeff)); utpokew( data_addr, uthibyte(div_coeff)); } /* set_dac( dac_addr, voltage) sets output voltage of digital-analogue * converter ( DAC ). * const dac_addr - DAC_1, DAC_2, DAC_3 ( and DAC_4 * if installed); * float voltage - voltage in Volts. */ set_dac( dac_addr, voltage) unsigned int dac_addr; float voltage; { int code; code = (int)(voltage * 400.0); if( code < 0) code = (-code) | 0100000; utpokew( dac_addr, ~code); /* DAC channels require */ /* inverted code */ } /* init_serialport() Initializes Serial port RS-232 * addr - RS_STATUS * */ com_init() { set_freq( TIMER_1, 0, CLOCKFREQ, 76800.0); timdelay(100); utpokew( RS_STATUS, 0100); timdelay(10); utpokew( RS_STATUS, 0116); timdelay(10); utpokew( RS_STATUS, 023); timdelay(10); } com_send( ch ) int ch; { int s; utpokew( RS_STATUS, 027); timdelay(10); utpokew( RS_DATA, ch); timdelay(10); while((s & 01)==0) s = utpeekw(RS_STATUS); return(1); } char com_recieve() { char ch; utpokew( RS_STATUS, 027); timdelay(10); while(( utpeekw(RS_STATUS) & 02 ) == 0); return( utpeekw(RS_DATA)); } init_clock() { unsigned t; set_freq(TIMER_1, 0, CLOCKFREQ, 100.0); set_freq(TIMER_1, 1, 100.0, 1.0); utpokew(TIMER_1 + 4, 0260); utpokew(TIMER_1 + 4, utlobyte (MAXSECS)); utpokew(TIMER_1 + 4, uthibyte (MAXSECS)); /* while ( (t = secs()) != 0);*/ } unsigned int secs() { unsigned int lobyte; utpokew (TIMER_1 + 6, 0100); lobyte = utpeekw(TIMER_1 + 4); return ( utbyword ( utpeekw(TIMER_1 + 4), lobyte) - MAXSECS); } /* Places message to row, col. * mode - CLRSCR, CLRLINE, MSGONLY */ message( mess, row, col, mode) int row, col, mode; char *mess; { switch( mode ) { case CLRSCR: CLS; cursor(row, col); break; case CLRLINE: clearline(row, col); break; case MSGONLY: cursor(row, col); break; default: break; } printf("%s",mess); } clearline( row, col) /* Clears line (row) from position (col) */ int row, col; { cursor( row, col); printf("\033K"); } cursor(row, col) /* Places cursor to row, col */ int row, col; { printf("\033Y%c%c",31+row,31+col); } timdelay(cycles) int cycles; { int i; for(i=0;i