1 Star 1 Fork 0

donjion/dcdc_resistance_calculate

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
dcdc_calculate.py 3.07 KB
一键复制 编辑 原始数据 按行查看 历史
donjion 提交于 2024-10-14 11:46 . temp
import itertools
# 电阻备选值
resistor_values = [100, 220, 470, 1000, 2000, 4700, 5100, 6800, 10000, 12000, 15000, 20000, 22000, 47000, 51000, 100000]
# 计算输出电压Vout
def calculate_vout(vfb, r1, r2):
return vfb * (1 + r1 / r2)
# 计算误差百分比
def calculate_error(expected_vout, actual_vout):
return ((actual_vout - expected_vout) / expected_vout) * 100
# 串联电阻可能性(最多2个电阻串联)
def possible_resistors(values):
return [(r,) for r in values] + [(r1, r2) for r1, r2 in itertools.combinations(values, 2)]
# 将电阻值格式化为字符串
def format_resistor_value(value):
if value >= 1000:
return f"{value / 1000:.1f}kΩ"
return f"{value}Ω"
# 计算电阻组合
def find_best_resistors(vfb, vout, tolerance=1.0, max_results=3):
best_combinations = []
# 获取所有可能的电阻组合
r1_combinations = possible_resistors(resistor_values)
r2_combinations = possible_resistors(resistor_values)
for r1_combo in r1_combinations:
for r2_combo in r2_combinations:
# 计算组合电阻值
r1_value = sum(r1_combo)
r2_value = sum(r2_combo)
# 计算实际输出电压
actual_vout = calculate_vout(vfb, r1_value, r2_value)
# 计算误差
error = calculate_error(vout, actual_vout)
# 检查误差是否在容差范围内
if abs(error) <= tolerance:
best_combinations.append({
'R1': r1_combo,
'R2': r2_combo,
'Actual_Vout': actual_vout,
'Error_Percentage': error
})
# 按误差绝对值排序,并返回最多max_results个组合
best_combinations = sorted(best_combinations, key=lambda x: abs(x['Error_Percentage']))[:max_results]
return best_combinations
# 命令行交互
def main():
print("DCDC反馈电压分压电阻计算器")
vfb = float(input("请输入反馈电压 Vfb: "))
vout = float(input("请输入期望输出电压 Vout: "))
tolerance = float(input("请输入误差容差百分比 (默认1%): ") or 1.0)
max_results = int(input("请输入返回的最佳结果数量 (默认3组): ") or 3)
# 计算最佳电阻值
best_combinations = find_best_resistors(vfb, vout, tolerance, max_results)
if not best_combinations:
print("没有找到符合要求的电阻组合,请尝试增加容差范围。")
else:
print(f"{'组合':<5} {'R1':<30} {'R2':<30} {'输出电压 Vout':<20} {'误差':<10}")
print("=" * 100)
for i, combo in enumerate(best_combinations):
r1 = " + ".join([format_resistor_value(r) for r in combo['R1']])
r2 = " + ".join([format_resistor_value(r) for r in combo['R2']])
actual_vout = combo['Actual_Vout']
error = combo['Error_Percentage']
print(f"{i+1:<5} {r1:<30} {r2:<30} {actual_vout:<20.4f} {error:<10.2f}%")
input("按Enter键退出...")
if __name__ == "__main__":
main()
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/donjion/dcdc_resistance_calculate.git
git@gitee.com:donjion/dcdc_resistance_calculate.git
donjion
dcdc_resistance_calculate
dcdc_resistance_calculate
master

搜索帮助