home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PROGRAMS / UTILS / HAMRADIO / COILS.ZIP / COILS.C next >
Encoding:
C/C++ Source or Header  |  1989-12-30  |  6.5 KB  |  177 lines

  1. /******************************************************************************
  2. **                                                                           **
  3. **                                  COILS                                    **
  4. **                            C Language Version                             **
  5. **                                                                           **
  6. **       Translated and modified by Alan E. Carman, original BASIC program   **
  7. **          by David E. Powell. Published in the November 1988 issue of      **
  8. **                        Radio-Electronics magazine.                        **
  9. **                                                                           **
  10. ******************************************************************************/
  11.  
  12. /****              Version written on an Amiga-500 using 
  13.                    the Lattice AmigaDOS C compiler V4.0                 ****/
  14.  
  15. /*  modified for DOS and corrected 12-19-89  (using Mix C compiler)        */
  16. /*                      by                                                 */
  17. /*                 Mendel Cooper                                           */
  18. /*                 Baltimore, MD                                           */
  19.  
  20.  
  21.  
  22. #include <stdio.h>
  23. #include <stdlib.h>      /* may change for your compiler */
  24.  
  25. #define K 1473061.855    /* conductance of copper wire...mhos per sq in area */
  26. #define MAXTURNS 10000   /* maximum allowed turns */
  27. #define PI 3.1415927
  28.  
  29. /* The following three #define statements are the only system specific     
  30.                 definitions I know of in this program                 
  31.               change them as necessary for your printer                
  32.                          and computer */
  33.                            
  34. #define PRINTER  "prn"      /* file name for fprintf() to parallel printer */
  35. #define SCREEN   "con"       /* file name for fprintf() to screen */
  36. #define RESET    "\x1b\x40"  /* escape code for reset on MX-80 printer */
  37.                          
  38. void main()
  39. {
  40. register float Ltry;
  41. float  a, atemp, btemp, c, ctemp, r, wire_area, diff1, diff2;
  42. float  max_length=0, wire_size=0, form_dia=0, L=0, b=0, Lfinal=0, layers;
  43. double log(), pow();
  44. int    min_n=1, max_n=MAXTURNS, n=0;
  45. int    ntemp, laytemp;
  46. long int wire_len;
  47. char   ch;
  48. FILE   *fp;
  49.  
  50.   clrscrn();   /*clears screen - may be different on your compiler*/
  51.  
  52.   printf("             ****************************************************\n");
  53.   printf("             **           Inductor Design Calculator           **\n");
  54.   printf("             **           [Circular  air-core coils]           **\n");
  55.   printf("             ****************************************************\n");
  56.  
  57.   printf("\n\nEnter desired inductance in microhenries........ ");
  58.   scanf("%f",&L);
  59.  
  60.   printf("\nEnter gauge, or diameter of wire in inches...... ");
  61.   scanf("%f",&wire_size);
  62.  
  63.   printf("\nEnter diameter of coil form in inches........... ");
  64.   scanf("%f",&form_dia);
  65.  
  66.   printf("\nEnter maximum length of coil in inches..........  ");
  67.   scanf("%f",&max_length);
  68.  
  69.   if (wire_size >= 1)
  70.     wire_size = .46/(pow(1.1229283027,wire_size + 3));
  71.  
  72.   do  /*************** start of loop to calculate number of turns **********/
  73.    {
  74.    ntemp = (max_n - min_n) / 2 + min_n;
  75.    laytemp = 1;
  76.         
  77.    while((btemp = ntemp * wire_size / laytemp) > max_length)
  78.      laytemp++; /* calculates laytemp for current ntemp */ 
  79.                                               
  80.    ctemp = laytemp * wire_size;
  81.    atemp = ctemp + form_dia;
  82.    
  83.    Ltry =(0.0175*atemp*atemp * ntemp*ntemp) / (3*atemp + 9*btemp + 10*ctemp);
  84.           /* WAS ERRONEOUSLY 0.2*atemp*atemp... in old program */
  85.  
  86.    if ( Ltry < L )
  87.       min_n = ntemp;
  88.    if ( Ltry > L )
  89.        max_n = ntemp; 
  90.      
  91.    diff1 = Ltry - L;                        /******************/
  92.    diff2 = Lfinal - L;                  
  93.      if( (fabs (diff1) ) < (abs (diff2) ) ) /*    this finds  */
  94.      {                   
  95.         Lfinal = Ltry;                      /*    the value   */ 
  96.         layers = laytemp;
  97.         a = atemp;                          /*    of Ltemp    */
  98.         b = btemp;
  99.         c = ctemp;                          /*  closest to L  */
  100.         n = ntemp;
  101.                                             /******************/ 
  102.      }
  103.          
  104.    if(n >= (MAXTURNS - 1))
  105.       {
  106.       printf("\n\nERROR....This value will exceed %d turns.\n",MAXTURNS);
  107.       printf("Try a longer or larger coil form diameter.\n");
  108.       exit(1);
  109.       }
  110.    }
  111.  while((max_n - min_n) > 1); /********* end of number of turns loop ********/
  112.  
  113.  if((n <= 1) && (Lfinal < .000001) )
  114.     {
  115.        printf("\n\nERROR...Less than one turn needed for this value.\n");
  116.        printf("Try a smaller coil form diameter.\n");
  117.        exit(1);
  118.     }  
  119.  
  120.   wire_len = n * a * PI;
  121.   wire_area = pow((wire_size / 2),2.0) * PI;
  122.   r = 1 / (wire_area * K) * wire_len;
  123.  
  124.   printf("\nDo you want a hardcopy (Y/N) ? ");
  125.   
  126.   ch = getchar();
  127.   if ((ch = getchar()) == 'y')
  128.      {
  129.      fp = fopen(PRINTER,"w");
  130.      fprintf(fp,RESET);
  131.      }
  132.   else
  133.      fp = fopen(SCREEN,"w"); 
  134.      
  135.   if(fp == NULL)
  136.   {
  137.     printf("Error opening file");   
  138.     exit(1);
  139.   }       
  140.   
  141.   /**************** outputs ******************/
  142.   
  143.   clrscrn();
  144.  
  145.   fprintf(fp,"Desired inductance............   %-10.6f uH\n",L);
  146.   fprintf(fp,"Wire diameter................. %10.6f in.",wire_size);
  147.   fprintf(fp," or %2.0f gauge.\n",
  148.   fabs( (log(wire_size/.46))/(log(1.1229283027)) +3) ); 
  149.   fprintf(fp,"Coil form diameter............ %10.6f in.\n",form_dia);
  150.   fprintf(fp,"Maximum coil length........... %10.6f in.\n",max_length);
  151.      
  152.   fprintf(fp,"\nOverall coil diameter......... %10.6f in.",wire_size * layers *
  153.   2 + form_dia);
  154.   fprintf(fp,"\nAverage coil diameter......... %10.6f in.",a);
  155.   fprintf(fp,"\nRadial depth of coil.......... %10.6f in.",layers * wire_size);
  156.   fprintf(fp,"\nLength of coil................ %10.6f in.",b);
  157.   fprintf(fp,"\nCoil DC resistance............   %-3.2f ohms",r);
  158.   fprintf(fp,"\nActual Inductance.............   %-10.6f uH",Lfinal);  
  159.   fprintf(fp,"\nApproximate length of wire....   %ld ft. %ld in.",
  160.   wire_len /12,wire_len % 12);
  161.   fprintf(fp,"\nNumber of layers..............   %1.0f",layers);
  162.   fprintf(fp,"\nTotal number of turns.........   %d",n);
  163.   fprintf(fp,"\nTurns per layer...............   %1.2f\n\n\n\n",n/layers);
  164.  
  165.   
  166.   if (ch == 'y')
  167.   {
  168.      fprintf(fp,"\n\n\n");
  169.      fprintf(fp,"\n");
  170.      fprintf(fp,RESET);
  171.   }   
  172.   
  173.   fclose(fp);
  174.   
  175.  
  176. }
  177.