Interrupt and ADC

The official pyboard running MicroPython.
This is the reference design and main target board for MicroPython.
You can buy one at the store.
Target audience: Users with a pyboard.
Post Reply
scardig
Posts: 20
Joined: Wed Jan 10, 2018 8:32 am

Interrupt and ADC

Post by scardig » 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 ?

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)

User avatar
pythoncoder
Posts: 4641
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Interrupt and ADC

Post by pythoncoder » Mon Apr 23, 2018 9:46 am

It looks OK, but it won't terminate because there's no exit from the while True loop.

You might want to look at the ADC.read_timed_multi in the latest firmware which provides a means of reading multiple ADC's in response to a timer tick. This might offer smaller code.
Peter Hinch

scardig
Posts: 20
Joined: Wed Jan 10, 2018 8:32 am

Re: Interrupt and ADC

Post by scardig » Mon Apr 23, 2018 10:06 am

pythoncoder wrote:
Mon Apr 23, 2018 9:46 am
It looks OK, but it won't terminate because there's no exit from the while True loop.

You might want to look at the ADC.read_timed_multi in the latest firmware which provides a means of reading multiple ADC's in response to a timer tick. This might offer smaller code.
Thanx. Two questions:

1. Since I read digital values, is there a ADC.read_timed_multi for such values ?
2. Is it advised to try...except inside a CB (if a digital_in_X.value() fails) ?

Bye

User avatar
pythoncoder
Posts: 4641
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Interrupt and digital reads

Post by pythoncoder » Wed Apr 25, 2018 8:18 am

From your post title I took it that you were reading ADC's. Looking again at your code you aren't - so ignore my comment about read_timed_multi.

digital_in_X.value() should never throw an exception. It returns 0 or 1. You could store the results in a byterarray instead of an array of halfwords: the latter is more appropriate for ADC readings which can be 12 bits in size.
Peter Hinch

Post Reply