TheAlgorithms-Python

Форк
0
75 строк · 2.6 Кб
1
from __future__ import annotations
2

3
"""
4
    Calculate the frequency and/or duty cycle of an astable 555 timer.
5
    * https://en.wikipedia.org/wiki/555_timer_IC#Astable
6

7
    These functions take in the value of the external resistances (in ohms)
8
    and capacitance (in Microfarad), and calculates the following:
9

10
    -------------------------------------
11
    | Freq = 1.44 /[( R1+ 2 x R2) x C1] |               ... in Hz
12
    -------------------------------------
13
    where Freq is the frequency,
14
          R1 is the first resistance in ohms,
15
          R2 is the second resistance in ohms,
16
          C1 is the capacitance in Microfarads.
17

18
    ------------------------------------------------
19
    | Duty Cycle = (R1 + R2) / (R1 + 2 x R2) x 100 |    ... in %
20
    ------------------------------------------------
21
    where R1 is the first resistance in ohms,
22
          R2 is the second resistance in ohms.
23
"""
24

25

26
def astable_frequency(
27
    resistance_1: float, resistance_2: float, capacitance: float
28
) -> float:
29
    """
30
    Usage examples:
31
    >>> astable_frequency(resistance_1=45, resistance_2=45, capacitance=7)
32
    1523.8095238095239
33
    >>> astable_frequency(resistance_1=356, resistance_2=234, capacitance=976)
34
    1.7905459175553078
35
    >>> astable_frequency(resistance_1=2, resistance_2=-1, capacitance=2)
36
    Traceback (most recent call last):
37
        ...
38
    ValueError: All values must be positive
39
    >>> astable_frequency(resistance_1=45, resistance_2=45, capacitance=0)
40
    Traceback (most recent call last):
41
        ...
42
    ValueError: All values must be positive
43
    """
44

45
    if resistance_1 <= 0 or resistance_2 <= 0 or capacitance <= 0:
46
        raise ValueError("All values must be positive")
47
    return (1.44 / ((resistance_1 + 2 * resistance_2) * capacitance)) * 10**6
48

49

50
def astable_duty_cycle(resistance_1: float, resistance_2: float) -> float:
51
    """
52
    Usage examples:
53
    >>> astable_duty_cycle(resistance_1=45, resistance_2=45)
54
    66.66666666666666
55
    >>> astable_duty_cycle(resistance_1=356, resistance_2=234)
56
    71.60194174757282
57
    >>> astable_duty_cycle(resistance_1=2, resistance_2=-1)
58
    Traceback (most recent call last):
59
        ...
60
    ValueError: All values must be positive
61
    >>> astable_duty_cycle(resistance_1=0, resistance_2=0)
62
    Traceback (most recent call last):
63
        ...
64
    ValueError: All values must be positive
65
    """
66

67
    if resistance_1 <= 0 or resistance_2 <= 0:
68
        raise ValueError("All values must be positive")
69
    return (resistance_1 + resistance_2) / (resistance_1 + 2 * resistance_2) * 100
70

71

72
if __name__ == "__main__":
73
    import doctest
74

75
    doctest.testmod()
76

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.