/*************************************************************************** * C SAMPLE #2 SAMPLE2.C * * * * This sample will generate 3 different wave forms, sine, triangle and * * saw tooth. The user has the choice of base address, DAC number and the* * number of points per cycle. * * * * * The following setup of the board is expected: * * -- Base address should be set to the address entered during * * program execution. * * * ***************************************************************************/ #include #include #include #include #define PI 3.1415927 unsigned counts; // number of points per cycle unsigned Address; // board base address unsigned dacnum; // DAC used for output unsigned progstruct[20000]; // buffer to hold points void sendtoport( void); void setparms( void); void sinecurve( void); void sawcurve( void); unsigned AskForBaseAddress(unsigned int OldOne) { char msg[7]; int NewOne = 0, Success = 0, Dummy; int AddrInputPosX, AddrInputPosY; puts("\nPlease enter the Base Address for your card (in hex)"); printf("or press ENTER for %X.\n>", OldOne); AddrInputPosX = wherex(); AddrInputPosY = wherey(); do { gotoxy(AddrInputPosX, AddrInputPosY); clreol(); msg[0] = 5; msg[1] = 0; cgets(msg); sscanf(msg + 2, "%x", &NewOne); Success = 1; Dummy = NewOne; if (msg[1] == 0) { gotoxy(AddrInputPosX, AddrInputPosY); printf("%X", OldOne); Success = 1; Dummy = OldOne; } } while(!Success); return (Dummy); } // end of AskForBaseAddress void setparms() { char ch; clrscr(); Address=AskForBaseAddress(0x300); //clear output voltage restrict - allow ref to reach non-zero volts outportb(Address+0x10,0x40); do { gotoxy (1,7); clreol(); printf("Enter the DAC number you wish to output to (0 - 7 only): "); ch=getche(); dacnum=ch-'0'; // using ascii code to get dac_number } while (((int)dacnum < 0) || ((int)dacnum > 7)); printf("\n\n\nEnter the number of points you wish to calculate per cycle,\n"); printf("(20000 maximum, program will use modulus if needed) :"); scanf("%u",&counts); counts%=20001; } // end setparams void sendtoport() { int i,temp; unsigned char lowbyte,hibyte; do { for( i = 0;i < counts;i++) { outport(Address+(dacnum*2),progstruct[i]); } } while (!kbhit()); outport(Address+(dacnum*2),0); // set DAC to 0 output } // end sendtoport void sinecurve() { int i; double rads,sine; if (counts == 0) return; // no point -- no curve clrscr(); printf("Calculating sine wave points....."); rads = (double) 2 * PI / (counts - 1); // rad per count for(i = 0;i < counts;i++) { sine = (sin(rads * i) + 1.0) * 2047; progstruct[i] = (unsigned) sine; } clrscr(); printf("Generating sine wave, press any key to stop...."); sendtoport(); } // end sinecurve void trianglecurve(void) { int i; double slope,temp; if (counts == 0) return; // no counts -- no curve clrscr(); printf("Calculating triangle wave points....."); slope = 4095.0 / counts * 2.0; // wave form slope for(i=0;i < counts/2;i++) { temp = slope * i; progstruct[i] = (int)temp; temp = 4095 - temp; progstruct[i+counts/2+1] = (int)temp; } clrscr(); printf("Generating triangle wave, press any key to stop...."); sendtoport(); } // end trianglecurve void sawcurve() { int i; double slope,temp; if (counts == 0) return; clrscr(); printf("Calculating saw tooth wave points....."); slope = 4095.0 / counts; /* saw tooth slope */ for(i = 0;i < counts;i++) { temp = slope * i; progstruct[i] = (int) temp; progstruct[i] %= 4096; } clrscr(); printf("Generating saw tooth wave, press any key to stop...."); sendtoport(); } // end sawcurve void menulist(void) { clrscr(); printf("\n\n\n"); printf("Your menu selections are:\n"); printf("1. Input Board Data (do this first.)\n"); printf("2. Sine curve\n"); printf("3. Triangle curve\n"); printf("4. Saw curve\n"); printf("5. End program, return to DOS\n"); printf("Input Choice: "); } // end menulist /******************/ /* main function */ /******************/ void main(void) { char menuchoice; clrscr(); printf( " C SAMPLE #2: SAMPLE2.C \n" " \n" " This sample will generate 3 different wave forms, sine, triangle and \n" " saw tooth. The user has the choice of base address, DAC number and the\n" " number of points per cycle. \n" " \n\n" " Board Configuration: \n\n" " -- The Base Address should be set to the address which will be \n" " entered during program execution\n" "\n\n\n\nPress any key to continue"); getch(); clrscr(); do { memset(progstruct,0,sizeof(int)); // clear buffer menulist(); // display the menu menuchoice=getch(); // fetch the menu choice switch(menuchoice) // execute the menu selection { case '1': setparms(); // fetch the system params outport(Address+0x04,0); // set DAC 0 to 0V output outport(Address+0x06,0); // set DAC 1 to 0V output outport(Address+0x08,0); // set DAC 2 to 0V output outport(Address+0x0a,0); // set DAC 3 to 0V output break; case '2': sinecurve(); // generate a sine wave break; case '3': trianglecurve(); // generate a triangle wave break; case '4': sawcurve(); // generate a saw tooth wave break; case '5': ; // exit to operating system }; } while(menuchoice !='5'); } // end main