home *** CD-ROM | disk | FTP | other *** search
- program onepercent; {Copyright 1989 Norm Bernstein}
- uses crt;
-
- { Program 1% converts a real argument to the closest value in the standard
- one percent resistor series. It is accessible from the DOS prompt, or the
- underlying routines could be extracted from this source code and recompiled
- for other applications.
-
-
- Usage:
-
- >1% <value>(cr)
-
- where [value] is in the range of 10 ohms to 10 Megohms
-
-
-
- Donations to:
-
- Norm Bernstein
- 24 Foxfire Drive
- Sharon Ma 02067
-
- Yeah, I know. . . . this program is trivial and inconsequential and easy
- to write and hardly worth a shareware fee. On the other hand, I you are
- a circuit hacker and find yourself constantly rummaging through your desk
- for the tattered photocopy of the article from a 1965 issue of EDN con-
- taining the 1% series sequence, you might actually find this program useful.
- If you do, a buck or two might ease your conscience about using this
- program, and could actually prompt me to write and publish something
- with genuine merit. If you don't happen to have a conscience, then I don't
- want your money anyway!
-
-
- }
-
-
- var
- closest_r:real;
- param_value:real;
- ercode:integer;
-
-
- {------------------------------------------------------------------}
-
-
- function exp10(value:real):real;
-
- {Returns the int of the base 10 exponent of the argument, converted
- to a real }
-
- begin
- exp10:=int(ln(value)/ln(10));
- end;
-
- {--------------------------------------------------------}
-
- function r_string(value:real):string;
-
- { converts a resistor value to a string, using engineering scaling
- and units. For example, converts the real value 17400.0 to a string
- consisting of '17.4 Kohms'. Constrained to 3 significant digits }
-
- var
- fracs:integer;
- decade:real;
- units,vstring:string;
-
- begin
- decade:=int(exp10(value));
- if decade<3 then units:=' '+chr(234);
- if ((decade>2) and (decade<6)) then units:=' K'+chr(234);
- if decade>=6 then units:= ' M'+chr(234);
-
- fracs:=0;
- if ((decade=1) or (decade=4) or (decade=7)) then fracs:=1;
- if ((decade=3) or (decade=6)) then fracs:=2;
-
- if ((decade>2) and (decade<6)) then value:=value/1000;
- if (decade>5) then value:=value/1000000;
-
- str(value:3:fracs,vstring);
- r_string:=vstring+units;
- end;
-
-
-
- {------------------------------------------------------}
-
-
- function closest_onepercent(r_target:real):real;
-
-
-
- var
- n:integer;
- raw_onepercent:array[1..100] of real;
- decade,divisor:real;
-
-
- begin
-
-
- if ((r_target>10) and (r_target<1e7)) then
- begin
- decade:=exp10(r_target);
- divisor:=round(exp(ln(10.0)*decade));
- r_target:=r_target/divisor;
-
-
- for n:=1 to 97 do
- begin
- raw_onepercent[n]:=exp( (ln(10))* ((n-1)/96) );
- raw_onepercent[n]:=(round(raw_onepercent[n]*100))/100;
- end;
-
- n:= 0;
- repeat
- n:=n+1;
- until ((r_target>=raw_onepercent[n]) and (r_target<=raw_onepercent[n+1]));
- if r_target>(raw_onepercent[n]+raw_onepercent[n+1])/2 then
- n:=n+1;
- closest_onepercent:=raw_onepercent[n]*round(exp( ln(10)*decade));
- end else closest_onepercent:=0;
-
-
- end;
-
-
-
-
- {-------------------------------------------------------------------}
-
- begin {main}
- val(paramstr(1),param_value,ercode);
- if ((param_value>=10) and (param_value<=1e7) and (ercode=0)) then
- begin
- closest_r:=closest_onepercent(param_value);
- writeln(r_string(closest_r));
- end else
- begin
- clrscr;
- writeln('1% -- finds the nearest 1% resistor value.');
- writeln;
- writeln('Usage: 1% <value>(cr)');
- writeln('where <value> is in the range of 10 ohms to 10 Mohms.');
- writeln;
- writeln('Copyright 1989 N. Bernstein ');
- end;
- end.