/*************************************************************************** * 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 #include "commdrv.h" #define PI 3.1415927 unsigned counts; /* number of points per cycle */ unsigned Address; /* board base address */ unsigned dacnum; /* DAC used for output */ unsigned maxbuf; //highest write in dac buffer unsigned int buffer[25000]; 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 */ /*************************************************************************** * FUNCTION: setparams() - local routine * * PURPOSE: Prompts the user for DAC number, base address and the number * * of points per cycle. * ***************************************************************************/ void setparms(void) { char ch; clrscr(); Address=AskForBaseAddress(0x300); do { gotoxy (1,7); clreol(); printf("Enter the DAC number (0 through 7 only): "); ch=getche(); dacnum=ch-'0'; /* using ascii code to get dacnum*/ }while ((dacnum < 0) || (dacnum > 7)); puts(""); puts("The dac buffer is stored between 3000 hex and 7800 hex and divided "); puts("into 900-hex sections. For example, DAC 0 starts at 3000 hex and DAC 1 "); puts("starts at 3901 hex."); puts(""); do{ puts("Enter the maximum address in the buffer that you would like to "); printf("write to (hex), keeping in mind the starting address for that dac number.\n"); scanf("%x",&maxbuf); }while(maxbuf >= 0x77ff); 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 */ /*************************************************************************** * FUNCTION: writebuffer() - local routine * * PURPOSE: Fills the dac buffer with values to generate a wave form. * ***************************************************************************/ void writebuffer(void) //write to buffer an,iiii=xxx0 command { char temp[255]; char MSG[255]; unsigned int i = 0, x = 0; unsigned int startbuf; startbuf = 0;//0x0000 + (dacnum*0x900); //maxbuf = maxbuf + (dacnum*0x900); for (i = startbuf; i <= maxbuf; i++){ //maxbuf needs to account for dacnum for(x = 0; x < counts; x++){ sprintf(temp, "a%x,%04x=%03x0", dacnum,i,buffer[x]); //fill buffer printf("a%x,%04x=%03x0",dacnum,i,buffer[x]);//onscreen updates writePod(Address, temp); i++; if(i == maxbuf) //cheap exit from loop x=counts; delay(1); readPod(Address,MSG); } // readPod(Address,MSG); } } // end writebuffer void dacgo(void) { char temp[255]; char msg[255]; // return; //ac command should be issued on its own, but its not //FIX //ac command sets up divisor, number of times to run, startup state, gain //and polarity, and highest address to read in buffer sprintf(temp, "ac%x=0000,00,00,00,%04x",dacnum,maxbuf); writePod(Address, temp); delay(1); readPod(Address,msg); delay(1); sprintf(temp, "a%x=gogogo",dacnum); //start dac writePod(Address, temp); delay(1); readPod(Address,msg); } void writezero(void) { char temp[255]; char MSG[255]; unsigned int i = 0, x = 0; unsigned int startbuf; startbuf = 0x3000 + (dacnum*0x900); for (i = startbuf; i <= maxbuf; i++){ for(x = 0; x < counts; x++){ sprintf(temp, "a%x,%04x=0000", dacnum,i); //fill buffer writePod(Address, temp); i++; readPod(Address,MSG); } // readPod(Address,MSG); } } /*************************************************************************** * FUNCTION: sinecurve() - local routine * * PURPOSE: Calculate the points for creating a sine wave. . * ***************************************************************************/ void sinecurve(void) { int i; double rads,sine; unsigned int value; 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; value = (unsigned)sine; //value <<= 4; //shift data into upper nybble buffer[i] = value; } writebuffer(); clrscr(); printf("Generating sine wave, press any key to stop...."); //wave actually generated in dacgo() } /* end sinecurve */ /*************************************************************************** * FUNCTION: trianglecurve() - local routine * * PURPOSE: Calculate the points for creating a triangle wave. * ***************************************************************************/ void trianglecurve(void) { int i; double slope,temp; unsigned int buffer[25000]; 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; buffer[i] = (int)temp; temp = 4095 - temp; //temp <<= 4; //shift into upper nybble--fix later maybe buffer[i+counts/2+1] = (int)temp; } //for(i = 0; i < counts; i++) writebuffer(); printf("Generating triangle wave, press any key to stop...."); //wave actually generated in dacgo() } /* 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; buffer[i] = (int) temp; buffer[i] %= 4096; //buffer[i] <<= 4; //shift into upper nybble--fix later maybe } //for(i = 0; i < counts; i++) writebuffer(); printf("Generating saw tooth wave, press any key to stop...."); //wave actually generated in dacgo() } /* 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("0. DEBUG--Fill buffer with zeros\n"); fix later-not working 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: main() - local routine * ***************************************************************************/ void main(void) { char menuchoice; int DONE = 0; char temp[255]; int i = 0; 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, buffer\n" " index,and the number of points per cycle. \n" "\n\n\n" " Board Configuration: \n\n" " -- Base address should be set to the address which will be entered \n" " during program execution. \n" " -- All remaining jumper settings are irrelevant \n" "\n\n" "Press any key to continue"); getch(); //check on board configurations clrscr(); do { menulist(); /* display the menu */ menuchoice=getch(); /* fetch the menu choise */ switch(menuchoice) /* execute the menu selection */ { // case '0': writezero(); //debug // dacgo();break; case '1': setparms(); /* fetch the system params */ break; case '2': sinecurve(); /* generate a sine wave */ dacgo(); break; case '3': trianglecurve(); /* generate a triangle wave */ dacgo(); break; case '4': sawcurve(); /* generate a saw tooth wave */ dacgo(); break; case '5': DONE=1;break; /* exit to operating system */ }; }while(!DONE); //turn off all dacs?? // for (i = 0; i < 8; i++){ // sprintf(temp, "a%x=stop",i); //turn off all dacs before exiting // writePod(Address, temp); // } } /* end main */