You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
104 lines
2.7 KiB
104 lines
2.7 KiB
#!/usr/bin/env python |
|
# coding: utf8 |
|
|
|
NOMINALS = [ |
|
1000000.0, |
|
750000.0, |
|
470000.0, |
|
220000.0, |
|
75000.0, |
|
47000.0, |
|
22000.0, |
|
10000.0, |
|
6800.0, |
|
2200.0, |
|
1000.0, |
|
750.0, |
|
470.0, |
|
220.0, |
|
100.0, |
|
47.0, |
|
22.0, |
|
10.0 |
|
] |
|
|
|
|
|
def format_value(value, suffix=''): |
|
order_suffix = '' |
|
divider = 1.0 |
|
|
|
orders = [ |
|
[1000000000.0, 'G'], |
|
[1000000.0, 'M'], |
|
[1000.0, 'k'], |
|
[1.0, ''], |
|
[0.001, 'm'], |
|
[0.000001, u'\u00b5'], |
|
[0.000000001, u'n'], |
|
[0.000000000001, u'p'], |
|
] |
|
|
|
for suggested_divider, suggested_order_suffix in orders: |
|
if value >= suggested_divider: |
|
divider = suggested_divider |
|
order_suffix = suggested_order_suffix |
|
break |
|
|
|
mag_value = value / divider |
|
mag_value = int(mag_value * 1000) |
|
mod = mag_value % 1000 |
|
if mod == 0: |
|
return "%i%s%s" % (mag_value / 1000.0, order_suffix, suffix) |
|
elif mod % 100 == 0: |
|
return "%.1f%s%s" % (mag_value / 1000.0, order_suffix, suffix) |
|
elif mod % 10 == 0: |
|
return "%.2f%s%s" % (mag_value / 1000.0, order_suffix, suffix) |
|
return "%.3f%s%s" % (mag_value / 1000.0, order_suffix, suffix) |
|
|
|
def choose_resistors(v_in, v_out, limits='both'): |
|
if v_in <= v_out: |
|
raise Exception("Vin should be greater than Vout") |
|
v_in = float(v_in) |
|
v_out = float(v_out) |
|
best_r1 = None |
|
best_r2 = None |
|
best_v_out = None |
|
|
|
for r1 in NOMINALS: |
|
for r2 in NOMINALS: |
|
tmp_v_out = float(v_in * r2) / (r1 + r2) |
|
choose_this_pair = best_v_out is None |
|
|
|
if limits == 'both': |
|
choose_this_pair = choose_this_pair or abs(tmp_v_out - v_out) < abs(best_v_out - v_out) |
|
elif limits == 'upper': |
|
choose_this_pair = choose_this_pair or v_out <= tmp_v_out < best_v_out or best_v_out < v_out |
|
elif limits == 'lower': |
|
choose_this_pair = choose_this_pair or best_v_out < tmp_v_out <= v_out or best_v_out > v_out |
|
|
|
if choose_this_pair: |
|
best_r1 = r1 |
|
best_r2 = r2 |
|
best_v_out = tmp_v_out |
|
|
|
print u"R1 %s\nR2 %s\nVout %s\nDissipation %s\nFactor %f" % ( |
|
format_value(best_r1, u'Ω'), |
|
format_value(best_r2, u'Ω'), |
|
format_value(best_v_out, 'V'), |
|
format_value(v_in * v_in / best_r1, 'W'), |
|
best_r2 / (best_r1 + best_r2) |
|
) |
|
|
|
|
|
def main(): |
|
print "\nPower supply UVLO divider" |
|
choose_resistors(30, 1.3, 'upper') |
|
|
|
print "\nPower supply feedback divider" |
|
choose_resistors(5, 1.225, 'lower') |
|
|
|
print "\nUnidirectional sensor" |
|
choose_resistors(5.1, 3.3, 'lower') |
|
|
|
if __name__ == '__main__': |
|
main()
|
|
|