← index #6773Issue #732
Related · high · value 3.240
QUERY · ISSUE

uncaught exception in Timer(1) interrupt handler MemoryError:

openby Irvingaoopened 2021-01-15updated 2026-03-24
proposed-close

Hello, here is my problem:

import pyb
import ps2
import encoder
from pyb import LED, Timer

from machine import I2C,Pin
from oled import SSD1306_I2C

def mainTimer_cb(cb):
    '''
    #函数功能:用于总体程序的逻辑定时功能
    '''
    global count
    LED(3).toggle()
    count += 1
    print(count)
    if (count%8) == 0: 
        LED(2).toggle() 
    elif (count%20) == 0:
        count = 0
    elif (count%5):
        oled_draw(count)

def oled_draw(text):
    oled.text(str(text),20,0)

count = 0

mainTimer = Timer(1, freq=2, callback=mainTimer_cb) 

i2c = I2C(scl = Pin("X9"),sda = Pin("X10"),freq = 10000) 
oled = SSD1306_I2C(128, 64, i2c) #创建oled对象

oled.text("count:",0,0)
oled.text("i love u!",20,40)
oled.show()

while True:
    i = 0

when i ran this code in pyb, it always got the memory error. So i wanna ask how can i write my main code in callback function? Because i can do this by C in STM32, just so confused to the error.

CANDIDATE · ISSUE

Exceptions inside interrupt handlers are always reported as MemoryError

closedby dhylandsopened 2014-06-29updated 2014-07-02

I coded up this:

import pyb

class Heartbeat(object):

    def __init__(self):
        self.tick = 0
        self.led = pyb.LED(4) # 4 = Blue
        tim = pyb.Timer(4)
        tim.init(freq=10)
        tim.callback(self.heartbeat_cb)

    def heartbeat_cb(self, tim):
        if self.tick <= 3:
            led.toggle()
        self.tick = (self.tick + 1) % 10

Heartbeat()

and when I run it on my ppyboard, I get the following output:

>>> import heartbeat_irq 
Uncaught exception in Timer(4) interrupt handler
MemoryError: 

when it should be reporting a NameError (due to using led.toggle instead of self.led.toggle)

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied