2018-04-10 21:20:58 -06:00
|
|
|
from pyb import ADC, Timer
|
2014-05-03 09:43:27 -06:00
|
|
|
|
2018-04-10 21:20:58 -06:00
|
|
|
adct = ADC(16) # Temperature 930 -> 20C
|
|
|
|
print(adct)
|
|
|
|
adcv = ADC(17) # Voltage 1500 -> 3.3V
|
|
|
|
print(adcv)
|
2014-05-03 09:43:27 -06:00
|
|
|
|
2018-04-10 21:20:58 -06:00
|
|
|
# read single sample; 2.5V-5V is pass range
|
|
|
|
val = adcv.read()
|
|
|
|
assert val > 1000 and val < 2000
|
2014-05-03 09:43:27 -06:00
|
|
|
|
2017-02-05 19:48:01 -07:00
|
|
|
# timer for read_timed
|
2018-04-10 21:20:58 -06:00
|
|
|
tim = Timer(5, freq=500)
|
2017-02-05 19:48:01 -07:00
|
|
|
|
2015-03-13 16:11:50 -06:00
|
|
|
# read into bytearray
|
2018-04-10 21:20:58 -06:00
|
|
|
buf = bytearray(b'\xff' * 50)
|
|
|
|
adcv.read_timed(buf, tim)
|
2015-03-13 16:11:50 -06:00
|
|
|
print(len(buf))
|
|
|
|
for i in buf:
|
2018-04-10 21:20:58 -06:00
|
|
|
assert i > 50 and i < 150
|
2015-03-13 16:11:50 -06:00
|
|
|
|
|
|
|
# read into arrays with different element sizes
|
|
|
|
import array
|
2018-04-10 21:20:58 -06: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 02:07:40 -07: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
|