home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PROGRAMS / UTILS / HAMRADIO / 1_.ZIP / 1%.PAS < prev   
Encoding:
Pascal/Delphi Source File  |  1989-05-23  |  3.8 KB  |  151 lines

  1. program onepercent;  {Copyright 1989 Norm Bernstein}
  2. uses crt;
  3.  
  4. { Program 1% converts a real argument to the closest value in the standard
  5. one percent resistor series. It is accessible from the DOS prompt, or the
  6. underlying routines could be extracted from this source code and recompiled
  7. for other applications.
  8.  
  9.  
  10.     Usage:
  11.  
  12.               >1% <value>(cr)
  13.  
  14.     where [value] is in the range of 10 ohms to 10 Megohms
  15.  
  16.  
  17.  
  18. Donations to:
  19.  
  20.                 Norm Bernstein
  21.                 24 Foxfire Drive
  22.                 Sharon Ma 02067
  23.  
  24. Yeah, I know. . . . this program is trivial and inconsequential and easy
  25. to write and hardly worth a shareware fee. On the other hand, I you are
  26. a circuit hacker and find yourself constantly rummaging through your desk
  27. for the tattered photocopy of the article from a 1965 issue of EDN con-
  28. taining the 1% series sequence, you might actually find this program useful.
  29. If you do, a buck or two might ease your conscience about using this
  30. program, and could actually prompt me to write and publish something
  31. with genuine merit. If you don't happen to have a conscience, then I don't
  32. want your money anyway!
  33.  
  34.  
  35. }
  36.  
  37.  
  38. var
  39.    closest_r:real;
  40.    param_value:real;
  41.    ercode:integer;
  42.  
  43.  
  44. {------------------------------------------------------------------}
  45.  
  46.  
  47. function exp10(value:real):real;
  48.  
  49. {Returns the int of the base 10 exponent of the argument, converted
  50. to a real }
  51.  
  52.     begin
  53.        exp10:=int(ln(value)/ln(10));
  54.     end;
  55.  
  56. {--------------------------------------------------------}
  57.  
  58. function r_string(value:real):string;
  59.  
  60. {  converts a resistor value to a string, using engineering scaling
  61. and units. For example, converts the real value 17400.0 to a string
  62. consisting of '17.4 Kohms'. Constrained to 3 significant digits }
  63.  
  64. var
  65.     fracs:integer;
  66.     decade:real;
  67.     units,vstring:string;
  68.  
  69. begin
  70.     decade:=int(exp10(value));
  71.     if decade<3 then units:=' '+chr(234);
  72.     if ((decade>2) and (decade<6)) then units:=' K'+chr(234);
  73.     if decade>=6 then units:= ' M'+chr(234);
  74.  
  75.     fracs:=0;
  76.     if ((decade=1) or (decade=4) or (decade=7)) then fracs:=1;
  77.     if ((decade=3) or (decade=6)) then fracs:=2;
  78.  
  79.     if ((decade>2) and (decade<6)) then value:=value/1000;
  80.     if (decade>5) then value:=value/1000000;
  81.  
  82.     str(value:3:fracs,vstring);
  83.     r_string:=vstring+units;
  84. end;
  85.  
  86.  
  87.  
  88. {------------------------------------------------------}
  89.  
  90.  
  91. function closest_onepercent(r_target:real):real;
  92.  
  93.  
  94.  
  95. var
  96.     n:integer;
  97.     raw_onepercent:array[1..100] of real;
  98.     decade,divisor:real;
  99.  
  100.  
  101. begin
  102.  
  103.  
  104. if ((r_target>10) and (r_target<1e7)) then
  105.     begin
  106.     decade:=exp10(r_target);
  107.     divisor:=round(exp(ln(10.0)*decade));
  108.     r_target:=r_target/divisor;
  109.  
  110.  
  111.     for n:=1 to 97 do
  112.        begin
  113.           raw_onepercent[n]:=exp(  (ln(10))*   ((n-1)/96)   );
  114.           raw_onepercent[n]:=(round(raw_onepercent[n]*100))/100;
  115.        end;
  116.  
  117.        n:= 0;
  118.        repeat
  119.          n:=n+1;
  120.        until ((r_target>=raw_onepercent[n]) and (r_target<=raw_onepercent[n+1]));
  121.        if r_target>(raw_onepercent[n]+raw_onepercent[n+1])/2 then
  122.           n:=n+1;
  123.        closest_onepercent:=raw_onepercent[n]*round(exp( ln(10)*decade));
  124.     end else closest_onepercent:=0;
  125.  
  126.  
  127. end;
  128.  
  129.  
  130.  
  131.  
  132. {-------------------------------------------------------------------}
  133.  
  134. begin {main}
  135.     val(paramstr(1),param_value,ercode);
  136.     if ((param_value>=10) and (param_value<=1e7) and (ercode=0)) then
  137.         begin
  138.            closest_r:=closest_onepercent(param_value);
  139.            writeln(r_string(closest_r));
  140.         end else
  141.           begin
  142.             clrscr;
  143.             writeln('1% -- finds the nearest 1% resistor value.');
  144.             writeln;
  145.             writeln('Usage:   1% <value>(cr)');
  146.             writeln('where <value> is in the range of 10 ohms to 10 Mohms.');
  147.             writeln;
  148.             writeln('Copyright 1989 N. Bernstein ');
  149.           end;
  150. end.
  151.