/*************************************************************************** * 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. * ***************************************************************************/ #include #include #include #include #include #define PI 3.1415927 unsigned base; /* card base address */ unsigned counts; /* number of points per cycle */ unsigned base; /* board base address */ unsigned dacnum; /* DAC used for output */ unsigned progstruct[20000]; /* buffer to hold points */ /*************************************************************************** * FUNCTION: setparams() - local routine * * PURPOSE: Prompts the user for DAC number, base address and the number * * of points per cycle. * ***************************************************************************/ void setparms(void) { clrscr(); printf("Enter the DAC number you wish to output to (0 through 1):"); scanf("%u",&dacnum); dacnum %= 2; printf("Enter 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 */ /*************************************************************************** * FUNCTION: sendtoport() - local routine * * PURPOSE: Writes the point buffer to the DAC until a key is pressed. * ***************************************************************************/ void sendtoport(void) { int i,j,temp; unsigned counts_out; /* each point is written to the DAC */ do { for( i = 0;i < counts;i++) { outport(base+(dacnum*2), progstruct[i]); for ( j = 0; j < 10; ++j ) inportb(0x61); } } while (!kbhit()); outport(base+(dacnum*2),0); /* set DAC to 0 output */ } /* end sendtoport */ /*************************************************************************** * FUNCTION: sinecurve() - local routine * * PURPOSE: Calculate the points for creating a sine wave. . * ***************************************************************************/ void sinecurve(void) { 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; /* 2047 is 12-bit scaling factor */ progstruct[i] = (unsigned) sine; } clrscr(); printf("Generating sine wave, press any key to stop...."); sendtoport(); } /* end sinecurve */ /*************************************************************************** * FUNCTION: trianglecurve() - local routine * * PURPOSE: Calculate the points for creating a triangle wave. * ***************************************************************************/ 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.0 - temp; progstruct[i+counts/2+1] = (int)temp; } clrscr(); printf("Generating triangle wave, press any key to stop...."); sendtoport(); } /* end trianglecurve */ /*************************************************************************** * FUNCTION: sawcurve() - local routine * * PURPOSE: Calculate the points for creating a saw tooth wave. * ***************************************************************************/ void sawcurve(void) { 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] %= 4095; } clrscr(); printf("Generating saw tooth wave, press any key to stop...."); sendtoport(); } /* end sawcurve */ /*************************************************************************** * FUNCTION: menulist() - local routine * * PURPOSE: Display the menu choise on the screen. * ***************************************************************************/ 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 */ /*************************************************************************** * FUNCTION: AskForBaseAddress * * PURPOSE: Prompt user to enter base address for their I/O card. * **************************************************************************/ unsigned AskForBaseAddress(unsigned int OldOne) { char msg[7]; int NewOne = 0, Success = 0, Dummy; int AddrInputPosX, AddrInputPosY; puts("\nPlease enter the Base Address OF THE DAC 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 */ /*************************************************************************** * FUNCTION: intro * * PURPOSE: Present user with introductory screen. Get base address. * **************************************************************************/ void intro(void) { 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" ); puts("Please press any key to set base address."); getch(); clrscr(); base = AskForBaseAddress(0x30C); clrscr(); } /* end of intro */ /*************************************************************************** * FUNCTION: main() - local routine * ***************************************************************************/ void main(void) { char menuchoice; int Ch; int done; intro(); /* Set all 16 buffers to 0 */ for (Ch = 0; Ch < 2; Ch++) outport(base + (Ch*2), 0x800); clrscr(); done = 0; do { memset(progstruct,0,sizeof(int)); /* clear buffer */ menulist(); /* display the menu */ menuchoice=getch(); /* fetch the menu choise */ switch(menuchoice) /* execute the menu selection */ { case '1': setparms(); /* fetch the system params */ 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 sample program. */ done = 1; }; /* end switch */ } while( !done ); /* Set all 16 buffers to 0 */ for (Ch = 0; Ch < 2; Ch++) outport(base + (Ch*2), 0x800); puts("DAC waveform output complete."); } /* end main */