Interrupt and ADC
Posted: Mon Apr 23, 2018 8:33 am
Hello,
here is a code snippet to achieve the following:
1. Read 10 digital values on X1,X2,X3,X4,X5,X6 at 5 Hz
2. Perform a "calculation" on such values
3. goto 1
Do you find it correct ?
here is a code snippet to achieve the following:
1. Read 10 digital values on X1,X2,X3,X4,X5,X6 at 5 Hz
2. Perform a "calculation" on such values
3. goto 1
Do you find it correct ?
Code: Select all
import array
import utime, time
import micropython
micropython.alloc_emergency_exception_buf(100)
NSAMPLES = 10
FREQ = 5
digital_in_1 = pyb.Pin(pyb.Pin.board.X1, pyb.Pin.IN)
digital_in_2 = pyb.Pin(pyb.Pin.board.X2, pyb.Pin.IN)
digital_in_3 = pyb.Pin(pyb.Pin.board.X3, pyb.Pin.IN)
digital_in_4 = pyb.Pin(pyb.Pin.board.X4, pyb.Pin.IN)
digital_in_5 = pyb.Pin(pyb.Pin.board.X5, pyb.Pin.IN)
digital_in_6 = pyb.Pin(pyb.Pin.board.X6, pyb.Pin.IN)
tim = pyb.Timer(4)
buf1 = array.array("H", (0 for _ in range(NSAMPLES)))
buf2 = array.array("H", (0 for _ in range(NSAMPLES)))
buf3 = array.array("H", (0 for _ in range(NSAMPLES)))
buf4 = array.array("H", (0 for _ in range(NSAMPLES)))
buf5 = array.array("H", (0 for _ in range(NSAMPLES)))
buf6 = array.array("H", (0 for _ in range(NSAMPLES)))
def cb(timer):
global i
buf1[i] = digital_in_1.value()
buf2[i] = digital_in_2.value()
buf3[i] = digital_in_3.value()
buf4[i] = digital_in_4.value()
buf5[i] = digital_in_5.value()
buf6[i] = digital_in_6.value()
i += 1
if i >= NSAMPLES:
timer.deinit()
while True:
i = 0
tim.init(freq = FREQ, callback = cb)
start = utime.ticks_us()
while i < NSAMPLES:
pass
print("Read time: %s" % str(utime.ticks_us() - start))
for buf in [buf1, buf2, buf3, buf4, buf5, buf6]:
for val in buf:
print(str(val))
#pyb.delay(1000)