I'm trying to know more about using the TIMER in Pyboard if you can help me. I want to turn on and off a led connected to pin X3 with a TIMER set at 5hz.
I wrote this code and this works ,
Code: Select all
import pyb
from pyb import Pin
led=Pin("X3", Pin.OUT)
tim = pyb.Timer(4)
tim.init(freq=5)
tim.callback(lambda t:led.value(not led.value()))
(1) because the frequency that I set in tim.init (freq = 5) is not respected. The actual frequency measured with an oscilloscope is half of that set.
(2) because if I create a function to be launched, this is not executed.
For example :
Code: Select all
import pyb
from pyb import Pin
led=Pin("X3", Pin.OUT)
def toggle_led(led):
stato_led = led.value()
if stato_led == 0:
led.on()
else:
led.off()
tim = pyb.Timer(4)
tim.init(freq=5)
tim.callback(toggle_led(led))
The TOGGLE_LED function simply turns the LED on and off but there is no cycle because the program stops immediately, as if the TIMER did not start its cycle.
Maybe because the TIMER callback can't allocate memory?
Thank you for your help and I wish you a Merry Christmas