2014-05-03 16:43:27 +01:00
|
|
|
from pyb import ADC
|
|
|
|
from pyb import Pin
|
|
|
|
|
2015-03-13 22:11:50 +00:00
|
|
|
pin = Pin('X22', mode=Pin.IN, pull=Pin.PULL_DOWN)
|
2014-05-03 16:43:27 +01:00
|
|
|
adc = ADC('X22')
|
|
|
|
print(adc)
|
|
|
|
|
2015-03-13 22:11:50 +00:00
|
|
|
# read single sample
|
|
|
|
val = adc.read()
|
|
|
|
assert val < 500
|
2014-05-03 16:43:27 +01:00
|
|
|
|
2017-02-06 02:48:01 +00:00
|
|
|
# timer for read_timed
|
|
|
|
tim = pyb.Timer(5, freq=500)
|
|
|
|
|
2015-03-13 22:11:50 +00:00
|
|
|
# read into bytearray
|
|
|
|
buf = bytearray(50)
|
2017-02-06 02:48:01 +00:00
|
|
|
adc.read_timed(buf, tim)
|
2015-03-13 22:11:50 +00:00
|
|
|
print(len(buf))
|
|
|
|
for i in buf:
|
|
|
|
assert i < 500
|
|
|
|
|
|
|
|
# read into arrays with different element sizes
|
|
|
|
import array
|
|
|
|
ar = array.array('h', 25 * [0])
|
2017-02-06 02:48:01 +00:00
|
|
|
adc.read_timed(ar, tim)
|
2015-03-13 22:11:50 +00:00
|
|
|
print(len(ar))
|
|
|
|
for i in buf:
|
|
|
|
assert i < 500
|
|
|
|
ar = array.array('i', 30 * [0])
|
2017-02-06 02:48:01 +00:00
|
|
|
adc.read_timed(ar, tim)
|
2015-03-13 22:11:50 +00:00
|
|
|
print(len(ar))
|
|
|
|
for i in buf:
|
|
|
|
assert i < 500
|