2018-04-11 04:20:58 +01:00
|
|
|
from pyb import ADC, Timer
|
2014-05-03 16:43:27 +01:00
|
|
|
|
2018-04-11 04:20:58 +01:00
|
|
|
adct = ADC(16) # Temperature 930 -> 20C
|
2019-12-13 05:43:17 +00:00
|
|
|
print(str(adct)[:19])
|
2018-04-11 04:20:58 +01:00
|
|
|
adcv = ADC(17) # Voltage 1500 -> 3.3V
|
|
|
|
print(adcv)
|
2014-05-03 16:43:27 +01:00
|
|
|
|
2018-04-11 04:20:58 +01:00
|
|
|
# read single sample; 2.5V-5V is pass range
|
|
|
|
val = adcv.read()
|
|
|
|
assert val > 1000 and val < 2000
|
2014-05-03 16:43:27 +01:00
|
|
|
|
2017-02-06 02:48:01 +00:00
|
|
|
# timer for read_timed
|
2018-04-11 04:20:58 +01:00
|
|
|
tim = Timer(5, freq=500)
|
2017-02-06 02:48:01 +00:00
|
|
|
|
2015-03-13 22:11:50 +00:00
|
|
|
# read into bytearray
|
2018-04-11 04:20:58 +01:00
|
|
|
buf = bytearray(b"\xff" * 50)
|
|
|
|
adcv.read_timed(buf, tim)
|
2015-03-13 22:11:50 +00:00
|
|
|
print(len(buf))
|
|
|
|
for i in buf:
|
2018-04-11 04:20:58 +01:00
|
|
|
assert i > 50 and i < 150
|
2015-03-13 22:11:50 +00:00
|
|
|
|
|
|
|
# read into arrays with different element sizes
|
|
|
|
import array
|
2020-03-23 02:26:08 +00:00
|
|
|
|
2018-04-11 04:20:58 +01:00
|
|
|
arv = array.array("h", 25 * [0x7FFF])
|
|
|
|
adcv.read_timed(arv, tim)
|
|
|
|
print(len(arv))
|
|
|
|
for i in arv:
|
|
|
|
assert i > 1000 and i < 2000
|
|
|
|
|
|
|
|
arv = array.array("i", 30 * [-1])
|
|
|
|
adcv.read_timed(arv, tim)
|
|
|
|
print(len(arv))
|
|
|
|
for i in arv:
|
|
|
|
assert i > 1000 and i < 2000
|
2018-03-04 09:07:40 +00:00
|
|
|
|
|
|
|
# Test read_timed_multi
|
|
|
|
arv = bytearray(b"\xff" * 50)
|
|
|
|
art = bytearray(b"\xff" * 50)
|
|
|
|
ADC.read_timed_multi((adcv, adct), (arv, art), tim)
|
|
|
|
for i in arv:
|
|
|
|
assert i > 60 and i < 125
|
|
|
|
# Wide range: unsure of accuracy of temp sensor.
|
|
|
|
for i in art:
|
|
|
|
assert i > 15 and i < 200
|
|
|
|
|
|
|
|
arv = array.array("i", 25 * [-1])
|
|
|
|
art = array.array("i", 25 * [-1])
|
|
|
|
ADC.read_timed_multi((adcv, adct), (arv, art), tim)
|
|
|
|
for i in arv:
|
|
|
|
assert i > 1000 and i < 2000
|
|
|
|
# Wide range: unsure of accuracy of temp sensor.
|
|
|
|
for i in art:
|
|
|
|
assert i > 50 and i < 2000
|
|
|
|
|
|
|
|
arv = array.array("h", 25 * [0x7FFF])
|
|
|
|
art = array.array("h", 25 * [0x7FFF])
|
|
|
|
ADC.read_timed_multi((adcv, adct), (arv, art), tim)
|
|
|
|
for i in arv:
|
|
|
|
assert i > 1000 and i < 2000
|
|
|
|
# Wide range: unsure of accuracy of temp sensor.
|
|
|
|
for i in art:
|
|
|
|
assert i > 50 and i < 2000
|