/*************************************************************************** * 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 #include #include "kbinp.h" #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[3]; unsigned int newone; unsigned int temp = 0; int index = 0; int letter; puts("Please enter the Base Address for your card (in hex)"); printf("or press ENTER for %X.\n>", OldOne); while(temp==0) { newone=0; index=0; while((letter = getchar())!='\n') msg[index++] = letter; sscanf(msg, "%X", &newone); if(newone==0){ temp=OldOne; } else if((newone>=0x100)&&(newone<=0x3f0)){ temp=newone; } else { printf("\nEnter the Base Address or press ENTER for %x > ", OldOne); for(index=0; index<=3; index++) msg[index]=0; } } return(temp); } /* end of AskForBaseAddress */ void IOPermission(unsigned abase) { if(ioperm(abase,16,1)<0) { fprintf(stderr, "NO IO PERMISSION\n"); } } void setparms() { char ch; system("clear"); Address=AskForBaseAddress(0x340); system("clear"); IOPermission(Address); do { system("tput cup 0 0"); puts("Enter the DAC number you wish to output to (0 or 1 only): "); ch=getch(); dacnum=ch-'0'; /* using ascii code to get dac_number */ } while (((int)dacnum < 0) || ((int)dacnum > 1)); 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() { unsigned int i,temp; unsigned char lowbyte,hibyte; /* each point is broken into the hi byte and low byte, and then written to the DAC in two seperate bytes. */ do { for( i = 0;i < counts;i++) { temp = progstruct[i] % 256; lowbyte = (unsigned char)temp; temp = progstruct[i] / 256; hibyte = (unsigned char)temp; outb(lowbyte,Address+0x08+(dacnum*2)); outb(hibyte,Address+0x08+(dacnum*2+1)); } } while(kbhit()!=1); outb(0,Address+0x08+(dacnum*2)); /* set DAC to 0 output */ outb(0,Address+0x08+(dacnum*2+1)); } /* end sendtoport */ void sinecurve() { unsigned int i; double rads,sine; if (counts == 0) return; /* no point -- no curve */ system("clear"); printf("Calculating sine wave points.....\n"); rads = (double) 2 * PI / (counts - 1); /* rad per count */ for(i = 0;i < counts;i++) { sine=sin(rads); // sine = (sin(rads * i) + 1.0) * 2047; progstruct[i] = (unsigned) sine; } printf("Generating sine wave, press any key to stop....\n"); sendtoport(); } /* end sinecurve */ void trianglecurve(void) { unsigned int i; double slope,temp; if (counts == 0) return; /* no counts -- no curve */ system("clear"); printf("Calculating triangle wave points.....\n"); 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; } printf("Generating triangle wave, press any key to stop....\n"); sendtoport(); } /* end trianglecurve */ void sawcurve() { unsigned int i; double slope,temp; if (counts == 0) return; system("clear"); printf("Calculating saw tooth wave points.....\n"); slope = 4095.0 / counts; /* saw tooth slope */ for(i = 0;i < counts;i++) { temp = slope * i; progstruct[i] = (int) temp; progstruct[i] %= 4096; } printf("Generating saw tooth wave, press any key to stop....\n"); sendtoport(); } /* end sawcurve */ void menulist(void) { system("clear"); 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: \n"); } /* end menulist */ /******************/ /* main function */ /******************/ int main(void) { char menuchoice; system("clear"); 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" " -- The 2's complement jumper should be removed.\n" "\n\n\n\nPress enter to continue\n"); while(kbhit()==0); system("clear"); 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 */ outb(0,Address+0x08); /* set DAC to 0 output */ outb(0,Address+0x09); outb(0,Address+0x0a); /* set DAC to 0 output */ outb(0,Address+0x0b); 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': return 0; /* exit to operating system */ }; } while(1 == 1); } /* end main */