Development board: K210 AIRV R3 version widora
Development environment: MaixPy IDE silicon speed home
Required reference sites:
https://cn.maixpy.sipeed.com/zh/api_reference/Maix/fpioa.html
https://cn.maixpy.sipeed.com/zh/api_reference/Maix/gpio.html
https://cn.maixpy.sipeed.com/zh/api_reference/machine/timer.html
Create a new file in the same way, _4tim.py
The code uses the previous section, and only keeps the LED-related configuration
Call the timer first
from machine import Timer
See how to configure the timer
class machine.Timer(id, channel, mode=Timer.MODE_ONE_SHOT,
period=1000, unit=Timer.UNIT_MS, callback=None,
arg=None, start=True, priority=1, div=0)
Timing 300ms
tim1 = Timer(Timer.TIMER0, Timer.CHANNEL0,
mode=Timer.MODE_PERIODIC,period=300,
callback=on_timer, arg=on_timer,start=False)
id: Timer.TIMER0 #Timer 0
channel: Timer.CHANNEL0, #channel 0
mode:
MODE_ONE_SHOT
: Timer runs only once (callback once)MODE_PERIODIC
: Timer always runs (continuous callback)MODE_PWM
: The timer is not used as a callback function to generate PWM
period: timing period
unit is not filled, the default is ms milliseconds
callback callback function on_timer
arg callback function parameters
start=False I started it manually, I don’t need to start it automatically
Similarly, like the button, flash the LED light on the timer callback function
def on_timer(timer):
global i
if(i==0):
i=1
else :
i=0
print("time up:", timer)
led1.value(i)
Then start the TIM1 timer
tim1.start()
The overall code is as follows
import utime
from Maix import FPIOA
from Maix import GPIO
from machine import Timer
def on_timer(timer):
global i
if(i==0):
i=1
else :
i=0
print("time up:", timer)
led1.value(i)
fpioa = FPIOA()
fpioa.set_function(17,fpioa.GPIOHS0)
fpioa.set_function(18,fpioa.GPIOHS1)
led1 = GPIO(GPIO.GPIOHS0,GPIO.OUT)
led2 = GPIO(GPIO.GPIOHS1,GPIO.OUT)
led1.value(0)
led2.value(0)
tim1 = Timer(Timer.TIMER0, Timer.CHANNEL0,
mode=Timer.MODE_PERIODIC,period=300,
callback=on_timer, arg=on_timer,start=False)
global i
i=0
tim1.start()
After the code is typed, connect to the development board and run
Phenomenon: Just one LED light keeps flashing (300ms)