-- elecfn.e - Common electronics formulae -- Michael J. Sabal -- Most equations from ARRL Handbook 2006 global constant PI = 3.1415926535897932 global constant TWO_PI = 2*PI constant inf=+1e310 ------------------------------------------------------------------------------- -- Basic functions ------------------------------------------------------------------------------- global function abs(object x) return (((x<0)*-x)+((x>0)*x)) end function ------------------------------------------------------------------------------- global function sum(object x) object s s=0 if atom(x) then return x end if for i = 1 to length(x) do s = s + x[i] end for return s end function ------------------------------------------------------------------------------- global function product(object x) object s s=0 if atom(x) then return x end if for i = 1 to length(x) do s = s * x[i] end for return s end function ------------------------------------------------------------------------------- -- Ohm's Law functions ------------------------------------------------------------------------------- ---- All Ohm's Law functions use base units of volts, amperes, ohms, and watts global function voltage(atom i, atom r) -- Ohm's Law: E=IR return i*r end function ------------------------------------------------------------------------------- global function resistance(atom e, atom i) -- Ohm's Law: R=E/I if i!=0 then return e/i else return inf end if end function ------------------------------------------------------------------------------- global function current(atom e, atom r) -- Ohm's Law: I=E/R if r!=0 then return e/r else return inf end if end function ------------------------------------------------------------------------------- global function watts_ir(atom i, atom r) -- Ohm's Law: P=(I^2)R -- Can't use function power for this name b/c it already exists return i*i*r end function ------------------------------------------------------------------------------- global function watts_er(atom e, atom r) -- Ohm's Law: P=(I^2)R --> (e/r)*(e/r)*r --> ((e*r)/r)*(e/r) --> (e*e)/r return (e*e)/r end function ------------------------------------------------------------------------------- global function watts_ei(atom e, atom i) -- Ohm's Law: P=(I^2)R --> (i*i)*(e/i) --> i*e return i*e end function ------------------------------------------------------------------------------- -- Frequency & Wavelength functions ------------------------------------------------------------------------------- global function freq2wavelen(atom f) -- Freq in MHz, wavelen in meters if f!=0 then return 300/f else return inf end if end function ------------------------------------------------------------------------------- global function wavelen2freq(atom w) -- Freq in MHz, wavelen in meters if w!=0 then return 300/w else return inf end if end function ------------------------------------------------------------------------------- -- Series & parallel functions ------------------------------------------------------------------------------- global function series_r(object r) -- All values of resistance must be in ohms return sum(r) end function ------------------------------------------------------------------------------- global function series_c(object c) -- All values of capacitance must be in the same power of Farads atom s s = 0 if atom(c) then return c end if for ctr = 1 to length(c) do if c[ctr]!=0 then s=s+(1/c[ctr]) else return 0 end if end for if s!=0 then return 1/s else return inf end if end function ------------------------------------------------------------------------------- global function series_l(object l) -- All values of inductance must be in the same power of Henries return sum(l) end function ------------------------------------------------------------------------------- global function series_r2e(atom e, object r) -- Kirschhoff's Second Law (Voltage Law): Current remains constant across -- resistors in series -- Returns a sequence of voltages the same length as r -- e is volts, r is ohms, i is amperes -- Note: Since we usually know the total voltage, but not the total current, -- we are accepting total voltage as input atom ti sequence edrop if atom(r) then return {e} end if ti = current(e,sum(r)) edrop = repeat(0,length(r)) for ctr = 1 to length(r) do edrop[ctr] = voltage(ti,r[ctr]) end for return edrop end function ------------------------------------------------------------------------------- global function parallel_r(object r) -- All values of resistance must be in ohms atom s s = 0 if atom(r) then return r end if for ctr = 1 to length(r) do if r[ctr]!=0 then s=s+(1/r[ctr]) else return 0 end if end for if s!=0 then return 1/s else return inf end if end function ------------------------------------------------------------------------------- global function parallel_c(object c) -- All values of capacitance must be in the same power of Farads return sum(c) end function ------------------------------------------------------------------------------- global function parallel_l(object l) -- All values of inductance must be in the same power of Henries atom s s = 0 if atom(l) then return l end if for ctr = 1 to length(l) do if l[ctr]!=0 then s=s+(1/l[ctr]) else return 0 end if end for if s!=0 then return 1/s else return inf end if end function ------------------------------------------------------------------------------- global function parallel_r2i(atom e, object r) -- Kirschhoff's First Law (Current Law): Voltage remains constant across -- resistors in parallel -- Returns a sequence of currents the same length as r -- e is volts, r is ohms, i is amperes sequence i if atom(r) then return {current(e,r)} end if i = repeat(0,length(r)) for ctr = 1 to length(r) do if r[ctr]!=0 then i[ctr] = e/r[ctr] else i[ctr]=inf end if end for return i end function ------------------------------------------------------------------------------- -- Reactance functions ------------------------------------------------------------------------------- global function XofC(atom freq, atom C) -- Returns a reactance in ohms of capacitance in microFarads at -- a frequency in Megahertz. if freq=0 or C=0 then return inf else return 1/(TWO_PI*freq*C) end if end function ------------------------------------------------------------------------------- global function XofL(atom freq, atom L) -- Returns a reactance in ohms of inductance in microHenries at -- a frequency in Megahertz. return TWO_PI*freq*L end function ------------------------------------------------------------------------------- global function Xsum(object Xc, object Xl) -- summing unlike reactances requires a different formula. All values in ohms. -- positive results are inductive, negative results are capacitive. return sum(Xl)-sum(Xc) end function ------------------------------------------------------------------------------- global function series_x(object Xc, object Xl) return Xsum(Xc,Xl) end function ------------------------------------------------------------------------------- global function parallel_x(object Xc, object Xl) -- positive results are inductive, negative results are capacitive. return (-parallel_r(Xl)*parallel_r(Xc))/(parallel_r(Xl)-parallel_r(Xc)) end function ------------------------------------------------------------------------------- global function series_lc_freq(object L, object C) -- resonant frequency (Hertz) of a series circuit: L in Henries, C in Farads return 1/(TWO_PI*sqrt(series_l(L)+series_c(C))) end function ------------------------------------------------------------------------------- -- RLC time functions ------------------------------------------------------------------------------- global function period(atom freq) if freq!=0 then return 1/freq else return inf end if end function ------------------------------------------------------------------------------- global function rc_tau(atom R, atom C) -- resistance in ohm * capacitance in Farads = tau in seconds. -- 1 tau = 63.2% charge, 2 tau = 86.5% charge, 3 tau = 95% charge, 5 tau = 99.24% (full) charge -- discharge works at the same percentages return R*C end function ------------------------------------------------------------------------------- global function tau_rc(atom pct, atom tim) -- returns tau (R*C) given a percentage of Vcc (i.e., 0.667) and trigger -- time in seconds if pct >= 1 then return 0 else return -tim/log(1-pct) end if end function ------------------------------------------------------------------------------- global function rl_tau(atom R, atom L) -- inductance in Henries / resistance in ohms = tau in seconds. -- 1 tau = 63.2% current buildup, 2 tau = 86.5%, 3 tau = 95%, 5 tau = 99.24% (full) buildup -- current release is instantaneous w/o additional networks if R!=0 then return L/R else return inf end if end function ------------------------------------------------------------------------------- -- AC voltage/current functions -------------------------------------------------------------------------------