ADC Callback Exception

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
nelfata
Posts: 74
Joined: Wed Apr 30, 2014 10:50 pm

ADC Callback Exception

Post by nelfata » Tue Aug 05, 2014 1:11 pm

I am not sure why the following causes and exception with MemoryError when a divide by 3 is added:

adcY11 = pyb.ADC( pyb.Pin.board.Y11 )
adcTimer = pyb.Timer(7, freq=2)

# THIS WORKS FINE
adcTimer.callback(lambda t: print(adcY11.read() ) )
# THIS GIVES MemoryError exception
adcTimer.callback(lambda t: print(adcY11.read()/3 ) )

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: ADC Callback Exception

Post by dhylands » Tue Aug 05, 2014 2:15 pm

Dividing by 3 needs to create a new float object, which needs to do a memory allocation.

If you used // 3 then it would create an int which doesn't need to do a memory allocation.

IRQs aren't allowed to do memory allocations.

nelfata
Posts: 74
Joined: Wed Apr 30, 2014 10:50 pm

Re: ADC Callback Exception

Post by nelfata » Tue Aug 05, 2014 2:59 pm

Ok that makes sense. Thank you.

Post Reply