Easy-Python  1.00.00Change to: 中文
Timer_pwm

Functions

 Timer (id, mode)
 Make new Timer object. More...
 
 init (mode)
 Initialise the timer. More...
 
 deinit ()
 Deinitialises the timer. Stops the timer, and disables the timer peripheral. More...
 
 channel (ch, freq, period, duty_cycle)
 Initialises the timer/PWM channel. More...
 
 freq (hz)
 Get or set the timer channel frequency (in Hz). More...
 
 period (period)
 Get or set the timer channel period (in microseconds). More...
 
 duty_cycle (cycle)
 Get or set the duty cycle of the PWM signal. More...
 
 irq (trigger, handler)
 Set timer channel irq function. More...
 

Detailed Description

The EASYPY provides eight sets of timers. Each counter/timer is designed to count cycles of the peripheral clock (PCLK) or an external clock, and can optionally generate interrupts or perform other actions at specified timer values based on four match registers. Each counter/timer also includes one capture input to trap the timer value during input signal transitions, optionally generating an interrupt.

In PWM mode, up to two matching registers can be used to provide a single-edge controlled PWM output on match output pins. It is recommended to use the match registers that are not pinned out to control the PWM cycle length.

Features of the timers include:

LED Example of Micropython
  1. timer_led ( Click here )
  2. timer_print ( Click here )
  3. pwm ( Click here )
Example Code of usage:

Function Documentation

Timer ( id  ,
mode   
)

Make new Timer object.

Construct a new timer object of the given id and mode:

Parameters
[in]idis the Timer number, range 0~7
[in]modecan be one of :
  • Timer.ONE_SHOT : The timer runs once until the configured period of the channel expires
  • Timer.PERIODIC : The timer runs periodically at the configured frequency of the channel.
  • Timer.PWM : Output a PWM signal on a pin.
Returns
(no)
Example of usage:
from machine import Timer
tim = Timer(0, mode=Timer.ONE_SHOT) # one shot 32-bit timer
tim = Timer(1, mode=Timer.PERIODIC) # periodic 32-bit timer
tim = Timer(2, mode=Timer.PWM) # 32-bit PWM
init ( mode  )

Initialise the timer.

Initial timer object of the given mode:

Parameters
[in]modecan be one of :
  • Timer.ONE_SHOT : The timer runs once until the configured period of the channel expires
  • Timer.PERIODIC : The timer runs periodically at the configured frequency of the channel.
  • Timer.PWM : Output a PWM signal on a pin.
Returns
(no)
Example of usage:
from machine import Timer
tim = Timer(0, mode=Timer.ONE_SHOT) # one shot 32-bit timer
tim.init(mode=Timer.PERIODIC) # periodic 32-bit timer
tim.init(mode=Timer.PWM) # 32-bit PWM
deinit ( )

Deinitialises the timer. Stops the timer, and disables the timer peripheral.

Deinitialises timer object:

Parameters
no
Returns
(no)
Example of usage:
from machine import Timer
tim = Timer(0, mode=Timer.ONE_SHOT) # one shot 32-bit timer
tim.init(mode=Timer.PERIODIC) # periodic 32-bit timer
tim.init(mode=Timer.PWM) # 32-bit PWM
channel ( ch  ,
freq  ,
period  ,
duty_cycle   
)

Initialises the timer/PWM channel.

Parameters
[in]channelcan be one of :
  • In timer mode :
    • Timer.TIMER_MATCHER_0 : Set timer channel 0.
    • Timer.TIMER_MATCHER_1 : Set timer channel 1.
    • Timer.TIMER_MATCHER_2 : Set timer channel 2.
    • Timer.TIMER_MATCHER_3 : Set timer channel 3.
  • In PWM mode :
    • Timer.TIMER_PWM_0 : Set PWM channel 0.
    • Timer.TIMER_PWM_1 : Set PWM channel 1.
    • Timer.TIMER_PWM_2 : Set PWM channel 2.
[in]freqSets the frequency in Hz.
[in]periodsets the period in microseconds.
[in]duty_cycleOnly applicable to PWM. It’s a percentage (0.00-100.00). Since the MicroPython doesn’t support floating point numbers the duty cycle must be specified in the range 0-10000, where 10000 would represent 100.00, 5050 represents 50.50, and so on.
Returns
(no)
Note
  • Either freq or period must be given, never both.
  • When the channel is in PWM mode, the corresponding pin is assigned automatically, therefore there’s no need to assign the Pin class. The pins which support PWM functionality are the following table:
    PWM pin define table
    Timer Channel PWM pin
    0 0
    1
    2
    1 0 P12
    1 P10
    2 P4
    2 0 P9
    1 P8
    2 P15
    3 0 P14
    1 P13
    2 P16
    4 0
    1
    2
    5 0
    1
    2
    6 0 P3
    1 P2
    2 P1
    7 0 P0
    1 P7
    2 P6
Example of usage:
from machine import Timer
tim = Timer(0, mode=Timer.ONE_SHOT) # one shot 32-bit timer0
tim.channel(Timer.A,freq=1)
tim2 = Timer(1, mode=Timer.PWM) # 32-bit PWM
pwm = tim2.channel(Timer.PWM_0, , freq=1, duty_cycle=5055) # 32-bit pwm timer1 channel 0
freq ( hz  )

Get or set the timer channel frequency (in Hz).

Parameters
[in]hzset the timer channel frequency (in Hz).
Returns
(no)
Example of usage:
from machine import *
t=Timer(1,mode=Timer.PWM)
p=t.channel(Timer.PWM_0,freq=1,duty_cycle=5055)
p.freq()
p.freq(2)
period ( period  )

Get or set the timer channel period (in microseconds).

Parameters
[in]periodset the timer channel period (in microseconds).
Returns
(no)
Example of usage:
from machine import *
t=Timer(1,mode=Timer.PWM)
p=t.channel(Timer.PWM_0,freq=1,duty_cycle=5055)
p.period()
p.period(400000)
duty_cycle ( cycle  )

Get or set the duty cycle of the PWM signal.

Parameters
[in]cycleIt’s a percentage (0.00-100.00). Since the WiPy doesn’t support floating point numbers the duty cycle must be specified in the range 0-10000, where 10000 would represent 100.00, 5050 represents 50.50, and so on.
Returns
(no)
Example of usage:
from machine import *
t=Timer(1,mode=Timer.PWM)
p=t.channel(Timer.PWM_0,freq=1,duty_cycle=5055)
p.duty_cycle(6000)
irq ( trigger  ,
handler   
)

Set timer channel irq function.

Parameters
[in]triggercan be one of:
  • In timer mode :
    • Timer.POSITIVE : Set irq positive trigger mode.
    • Timer.NEGATIVE : Set irq negative trigger mode.
    • Timer.TIMEOUT : Set irq timout trigger mode.
    • Timer.MATCH : Set irq match trigger mode.
[in]handleris an optional function to be called when the interrupt is triggered.
Returns
(no)
Example of usage:
from machine import Timer
tim = Timer(0, mode=Timer.ONE_SHOT) # one shot 32-bit timer
tim.init(mode=Timer.PERIODIC) # periodic 32-bit timer
tim.init(mode=Timer.PWM) # 32-bit PWM