Skip to content

Instantly share code, notes, and snippets.

@U-1F992
Created March 24, 2024 00:30
Show Gist options
  • Save U-1F992/e1f2c59b018ddd139ea4d9bc0e02875f to your computer and use it in GitHub Desktop.
Save U-1F992/e1f2c59b018ddd139ea4d9bc0e02875f to your computer and use it in GitHub Desktop.
import argparse
def figure_6_v_o(n: int, d: int, d_3: int) -> float:
"""
Voltage divider calculation based on the approach shown in Figure 6.
Args:
n (int): The resolution of the RDACs in bits.
d (int): The data bits of `U_1` and `U_2`.
d_3 (int): The data bits of `U_3`.
Returns:
float: The calculated output voltage ratio. Note that `V_A` is factored out.
"""
assert 0 < n
assert d < 2**n
assert d_3 < 2**n
coarse = (d / 2**n) * ((2**n + 1) / (2**n + 2))
fine = (d_3 / 2**n) * (1 / (2**n + 2))
return coarse + fine
def main(v_a: float, r_ab: float, n: int):
r_p = r_ab / 2**n
result = sorted(
[
{"d": d, "d_3": d_3, "v_o": v_a * figure_6_v_o(n, d, d_3)}
for d in range(256)
for d_3 in range(256)
],
key=lambda d: d["v_o"],
)
print(f"r_p: {r_p}")
print(
"["
+ ",\n ".join([str(d) for d in result[:10]])
+ "\n ...\n "
+ ",\n ".join([str(d) for d in result[-10:]])
+ "]"
)
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Voltage divider calculation based on the approach shown in Figure 6.\nhttps://www.analog.com/media/en/technical-documentation/application-notes/an-582.pdf",
formatter_class=argparse.RawDescriptionHelpFormatter,
)
parser.add_argument("--v_a", type=float, required=True, help="The supply voltage.")
parser.add_argument(
"--r_ab", type=float, required=True, help="The resistance of the RDACs in ohms."
)
parser.add_argument(
"--n", type=int, required=True, help="The resolution of the RDACs in bits."
)
args = parser.parse_args()
main(args.v_a, args.r_ab, args.n)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment