|
14 | 14 | The ADC chips supported by this library do not use negative numbers. If the resulting
|
15 | 15 | differential read is less than 0, then the returned integer value (and voltage value) is ``0``.
|
16 | 16 | If for some reason the voltage on a channel is greater than the reference voltage or
|
17 |
| - less than 0, then the returned integer value is ``65472`` or ``0`` respectively. |
| 17 | + less than 0, then the returned integer value is ``65535`` or ``0`` respectively. |
18 | 18 |
|
19 | 19 | """
|
20 | 20 |
|
@@ -54,15 +54,16 @@ def __init__(
|
54 | 54 |
|
55 | 55 | @property
|
56 | 56 | def value(self) -> int:
|
57 |
| - """Returns the value of an ADC pin as an integer. Due to 10-bit accuracy of the chip, the |
58 |
| - returned values range [0, 65472].""" |
59 |
| - return ( |
60 |
| - self._mcp.read(self._pin_setting, is_differential=self.is_differential) << 6 |
| 57 | + """Returns the value of an ADC pin as an integer in the range [0, 65535].""" |
| 58 | + # Initial result is only 10 bits. |
| 59 | + result = int( |
| 60 | + self._mcp.read(self._pin_setting, is_differential=self.is_differential) |
61 | 61 | )
|
| 62 | + # Stretch to 16 bits and cover full range. |
| 63 | + return (result << 6) | (result >> 4) |
62 | 64 |
|
63 | 65 | @property
|
64 | 66 | def voltage(self) -> float:
|
65 | 67 | """Returns the voltage from the ADC pin as a floating point value. Due to the 10-bit
|
66 |
| - accuracy of the chip, returned values range from 0 to (``reference_voltage`` * |
67 |
| - 65472 / 65535)""" |
68 |
| - return (self.value * self._mcp.reference_voltage) / 65535 |
| 68 | + accuracy of the chip, returned values range from 0 to ``reference_voltage``.""" |
| 69 | + return self.value * self._mcp.reference_voltage / 65535 |
0 commit comments