Skip to content

Commit

Permalink
ads1x15.py: changed the order of channel rate, with optional channel2…
Browse files Browse the repository at this point in the history
… arg.

readme.md: Adapted the documents
  • Loading branch information
robert-hh committed Mar 26, 2017
1 parent ee2b547 commit 0b68c2e
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 145 deletions.
52 changes: 22 additions & 30 deletions ads1x15.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,12 @@
_PGA_0_512V, # 8x
_PGA_0_256V # 16x
)
_CHANNELS = (_MUX_SINGLE_0, _MUX_SINGLE_1, _MUX_SINGLE_2, _MUX_SINGLE_3)
_DIFFS = {

_CHANNELS = {
(0, None): _MUX_SINGLE_0,
(1, None): _MUX_SINGLE_1,
(2, None): _MUX_SINGLE_2,
(3, None): _MUX_SINGLE_3,
(0, 1): _MUX_DIFF_0_1,
(0, 3): _MUX_DIFF_0_3,
(1, 3): _MUX_DIFF_1_3,
Expand All @@ -112,7 +116,7 @@


class ADS1115:
def __init__(self, i2c, address=0x49, gain=0):
def __init__(self, i2c, address=0x48, gain=1):
self.i2c = i2c
self.address = address
self.gain = gain #
Expand All @@ -132,17 +136,17 @@ def _read_register(self, register):
self.i2c.readfrom_into(self.address, self.temp2)
return (self.temp2[0] << 8) | self.temp2[1]

def set_conv(self, channel, rate):
def set_conv(self, rate, channel1, channel2 = None):
"""Read voltage between a channel and GND. Time depends on conversion rate."""
self.mode = (_CQUE_NONE | _CLAT_NONLAT |
_CPOL_ACTVLOW | _CMODE_TRAD | _RATES[rate] | _MODE_SINGLE |
_OS_SINGLE | _GAINS[self.gain] | _CHANNELS[channel])

def read(self, channel, rate):
_OS_SINGLE | _GAINS[self.gain] | _CHANNELS[(channel1, channel2)])
def read(self, rate, channel1, channel2 = None):
"""Read voltage between a channel and GND. Time depends on conversion rate."""
self._write_register(_REGISTER_CONFIG, (_CQUE_NONE | _CLAT_NONLAT |
self.mode = (_CQUE_NONE | _CLAT_NONLAT |
_CPOL_ACTVLOW | _CMODE_TRAD | _RATES[rate] | _MODE_SINGLE |
_OS_SINGLE | _GAINS[self.gain] | _CHANNELS[channel]))
_OS_SINGLE | _GAINS[self.gain] | _CHANNELS[(channel1, channel2)])
while not self._read_register(_REGISTER_CONFIG) & _OS_NOTBUSY:
time.sleep_ms(1)
return self._read_register(_REGISTER_CONVERT)
Expand All @@ -153,29 +157,20 @@ def read_rev(self):
self._write_register(_REGISTER_CONFIG, self.mode)
return res

def diff(self, channel1, channel2, rate):
"""Read voltage between two channels. Takes 1ms/10ms."""
self._write_register(_REGISTER_CONFIG, _CQUE_NONE | _CLAT_NONLAT |
_CPOL_ACTVLOW | _CMODE_TRAD | _RATES[rate] | _MODE_SINGLE |
_OS_SINGLE | _GAINS[self.gain] | _DIFFS[(channel1, channel2)])
while not self._read_register(_REGISTER_CONFIG) & _OS_NOTBUSY:
time.sleep_ms(1)
return self._read_register(_REGISTER_CONVERT)

def alert_start(self, channel, rate, threshold_high = 0x4000):
def alert_start(self, rate, channe1l, channel2 = None, threshold_high = 0x4000):
"""Start continuous measurement, set ALERT pin on threshold."""
self._write_register(_REGISTER_HITHRESH, threshold_high)
self._write_register(_REGISTER_CONFIG, _CQUE_1CONV | _CLAT_LATCH |
_CPOL_ACTVLOW | _CMODE_TRAD | _RATES[rate] |
_MODE_CONTIN | _GAINS[self.gain] | _CHANNELS[channel])
_MODE_CONTIN | _GAINS[self.gain] | _CHANNELS[(channel1, channel2)])

def conversion_start(self, channel, rate):
def conversion_start(self, rate, channel1, channel2 = None):
"""Start continuous measurement, trigegr on ALERT/RDY pin."""
self._write_register(_REGISTER_LOWTHRESH, 0)
self._write_register(_REGISTER_HITHRESH, 0x8000)
self._write_register(_REGISTER_CONFIG, _CQUE_1CONV | _CLAT_NONLAT |
_CPOL_ACTVLOW | _CMODE_TRAD | _RATES[rate] |
_MODE_CONTIN | _GAINS[self.gain] | _CHANNELS[channel])
_CPOL_ACTVLOW | _CMODE_TRAD | _RATES[rate] |
_MODE_CONTIN | _GAINS[self.gain] | _CHANNELS[(channel1, channel2)])

def alert_read(self):
"""Get the last reading from the continuous measurement."""
Expand All @@ -185,14 +180,11 @@ class ADS1015(ADS1115):
def __init__(self, i2c, address=0x48):
return super().__init__(i2c, address)

def read(self, channel, rate):
return super().read(channel) >> 4

def diff(self, channel1, channel2, rate):
return super().diff(channel1, channel2) >> 4
def read(self, rate, channel1, channel2 = None):
return super().read(rate, channel1, channel2) >> 4

def alert_start(self, channel, threshold):
return super().alert_start(channel, threshold << 4)
def alert_start(self, rate, channel1, channel2 = None, threshold = 0x400):
return super().alert_start(rate, channel1, channel2, threshold << 4)

def alert_read(self):
return super().alert_read() >> 4
Expand Down
34 changes: 10 additions & 24 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ table. It defines the full range of the ADC. Acceptable values are:

### adc.read()
```
value = adc.read(channel, rate)
value = adc.read(rate, channel1[, channel2])
```
Start a conversion on channel at speed rate and return the value.
Channel is the single input channel (0 .. 3), rate is the conversion rate. Suitable values are (ADS1015 / ADS1115):
Channel1 is the single input channel (0 .. 3). If channel2 is supplied, the difference between channel1 and channel2 is taken. Rate is the conversion rate. Suitable values are (ADS1015 / ADS1115):
```
0 : 128/8 samples per second
1 : 250/16 samples per second
Expand All @@ -61,29 +61,15 @@ The first value applies to the ADS1015, the second to the ADS1115. The time
required for a single conversion is 1/samples\_per\_second plus the time needed for communication with the ADC, which is about 1 ms on an esp8266 at 80 MHz. Slower conversion yiled in a less noisy result. The data sheet figures of the ads1x15
are given for the slowest sample rate.

### adc.diff()

```
value = adc.diff(channel1, channel2, rate)
```
Start a conversion for the difference between channel 1 and channel 2 at sampling
speed rate. Suitable values for channel 1 and channel 2 are:
```
0, 1 Difference between channel 0 and 1
0, 3 Difference between channel 0 and 3
1, 3 Difference between channel 1 and 3
2, 3 Difference between channel 2 and 3
```

### adc.set_conv and adc.read_rev()

Pair of methods for a time optimized sequential reading triggered by a time. For using, you would first set the conersion parameters with set_conv() and then get
the values in a timer callback function with read_rev().
```
adc.set_con(channel, rate)
adc.set_con(rate, channel1[, channel2])
value = adc.read_rev()
```
The definition of channel and rate are the same as with adc.read(). The methods
The definition of channel1, channel2 and rate are the same as with adc.read(). The methods
read_rev() reads first the last conversion value back, and the starts a new
conversion. Care has to be taken, that the time needed for conversion and
communication is shorter than the timer period plus the time needed to process the data.
Expand All @@ -96,10 +82,10 @@ it's own issues.
Pair of methods to start a contious sampling on a single channel and trigger
an alert once a certain threshold is reached,
```
adc.alert_start(channel, rate, threshold)
adc.alert_start(rate, channel1[, channel2][, threshold])
value = adc.alert_read()
```
The values of channel and rate are the same as for adc.read(). Threshold tells
The values of channel1, channel2 and rate are the same as for adc.read(). Threshold tells
trigger value anshould be within the range of the adc, 0..32767 for ADC1115 and
0..2047 for ADS1015. Rate should be chosen according to the input signal
change rate and the precision needed.
Expand All @@ -110,10 +96,10 @@ change rate and the precision needed.
Pair of methods to start a contious sampling on a single channel and trigger
an alert at every sample. This functions pair to be used in an irq-based set-up.
```
adc.conversion_start(channel, rate)
adc.conversion_start(rate, channel1 [, channel2])
value = adc.alert_read()
```
The values of channel and rate are the same as for adc.read(). The timing jitter
The values of channel1, channel2 and rate are the same as for adc.read(). The timing jitter
observed is a few µs. However the ADC's timer is not very precise. In
applications where this is of importance some control and calibration of
the returned timing pattern has to be done.
Expand Down Expand Up @@ -182,7 +168,7 @@ i2c = I2C(scl=Pin(5), sda=Pin(4), freq=400000)
ads = ads1x15.ADS1115(i2c, addr, gain)
# set the conversion rate tp 860 SPS = 1.16 ms; that leaves about
# 3 ms time for processing the data with a 5 ms timer
ads.set_conv(0, 7) # start the first conversion
ads.set_conv(7, 0) # start the first conversion
ads.read_rev()
sleep_ms(ADC_RATE)
tim = Timer(-1)
Expand Down Expand Up @@ -227,7 +213,7 @@ index_put = 0
i2c = I2C(scl=Pin(5), sda=Pin(4), freq=400000)
irq_pin = Pin(13, Pin.IN, Pin.PULL_UP)
ads = ads1x15.ADS1115(i2c, addr, gain)
ads.conversion_start(0, 5)
ads.conversion_start(5, 0)
irq_pin.irq(trigger=Pin.IRQ_FALLING, handler=sample_auto)
Expand Down
38 changes: 0 additions & 38 deletions test.py

This file was deleted.

53 changes: 0 additions & 53 deletions test2.py

This file was deleted.

0 comments on commit 0b68c2e

Please sign in to comment.