This is largely guess work but it may serve to point you in the right direction.
Most of you functions start with the following code:Though it isnt' always on the same pins.
That's a problematic way of doing things:
Some suggestions:
Most of you functions start with the following code:
Code:
GSCLK = PWM(Pin(8)) # 设定GPIO 20为PWM输出方式 GSCLK.freq(100000) # 设定频率60HZ GSCLK.duty_u16(32768) # 设定占空比,取值范围0~65535 SCK_PIN = 10 MOSI_PIN = 11 mosi_pin = Pin(MOSI_PIN, Pin.OUT, value=0) sck_pin = Pin(SCK_PIN, Pin.OUT, value=0) def mosi_high(): mosi_pin.value(1) def mosi_low(): mosi_pin.value(0) def sck_high(): sck_pin.value(1) def sck_low(): sck_pin.value(0) SCK_PIN = 10 MOSI_PIN = 11 mosi_pin = Pin(MOSI_PIN, Pin.OUT, value=0) sck_pin = Pin(SCK_PIN, Pin.OUT, value=0)
That's a problematic way of doing things:
- It's inefficient
- It creates a lot of garbage that will need to be collected. Variables, functions, and other object created within a function are discarded when the function reaches its end and recreated when it next starts. When the garbage collector can't keep up you run out of RAM.
- Micropython's garbage collection is not aggressive. It runs much less frequently that you might expect.
- Unlike C/C++, micropython programs are copied to RAM and then run. Bigger programs means less RAM for variables and the heap.
Some suggestions:
- Define the variables and functions once at the start of your program. Do the same with your pin setup. Reference the global object from inside your functions.
- Use classes instead of functions. Define the sub functions as class methods. Use instance variables for pin numbers and objects. Do that in your class' __init__() method.
- Force a garbage collection at the start and end of each function.
Statistics: Posted by thagrol — Fri Feb 09, 2024 12:41 am