Python pyb.Timer() Examples

The following are 25 code examples of pyb.Timer(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module pyb , or try the search function .
Example #1
Source File: mutex_test.py    From micropython-samples with MIT License 7 votes vote down vote up
def main():
    global var1, var2
    var1, var2 = 0, 0
    t2 = pyb.Timer(2, freq = 995, callback = update)
    t4 = pyb.Timer(4, freq = 1000, callback = update)
    for x in range(1000000):
        with mutex:             # critical section start
            a = var1
            pyb.udelay(200)
            b = var2
            result = a == b     # critical section end
        if not result:
            print('Fail after {} iterations'.format(x))
            break
        pyb.delay(1)
        if x % 1000 == 0:
            print(x)
    t2.deinit()
    t4.deinit()
    print(var1, var2) 
Example #2
Source File: delay_test.py    From micropython-async with MIT License 6 votes vote down vote up
def isr_test():  # Test trigger from hard ISR
    from pyb import Timer
    s = '''
Timer holds off cb for 5 secs
cb should now run
cb callback
Done
'''
    printexp(s, 6)
    def cb(v):
        print('cb', v)

    d = Delay_ms(cb, ('callback',))

    def timer_cb(_):
        d.trigger(200)
    tim = Timer(1, freq=10, callback=timer_cb)

    print('Timer holds off cb for 5 secs')
    await asyncio.sleep(5)
    tim.deinit()
    print('cb should now run')
    await asyncio.sleep(1)
    print('Done') 
Example #3
Source File: iotest.py    From micropython-samples with MIT License 5 votes vote down vote up
def __init__(self):
        self.ready = False
        self.count = 0
        tim = pyb.Timer(4)
        tim.init(freq=1)  # 1Hz - 1 simulated input line per sec.
        tim.callback(self.setready) 
Example #4
Source File: heartbeat_irq.py    From upy-examples with MIT License 5 votes vote down vote up
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) 
Example #5
Source File: heartbeat_irq.py    From upy-examples with MIT License 5 votes vote down vote up
def __init__(self):
        self.tick = 0
        self.led = pyb.LED(1)
        tim = pyb.Timer(0)
        tim.init(prescaler=128, period=37500) # 10 Hz
        tim.callback(self.heartbeat_cb) 
Example #6
Source File: Tach.py    From upy-examples with MIT License 5 votes vote down vote up
def test():
    """Test program - assumes X2 is jumpered to X1."""
    micropython.alloc_emergency_exception_buf(100)

    print("Starting tach")
    tach = Tachometer(timer_num=2, channel_num=1, pin_name='X1')

    print("Starting pulses")
    t5 = pyb.Timer(5, freq=4)
    oc_pin = pyb.Pin.board.X2
    oc = t5.channel(2, pyb.Timer.OC_TOGGLE, pin=oc_pin)

    for freq in range(0, 600, 100):
        if freq == 0:
            freq = 1
        else:
            t5.freq(freq * 4)   # x 2 for toggle, x2 for 2 pulses_per_rev
        pyb.delay(1000)
        print("RPM =",  tach.rpm(), "Freq =", freq, " as RPM =", freq * 60)

    # stop the pulses
    print("Stopping pulses")
    oc_pin.init(pyb.Pin.OUT_PP)
    # wait for 1.5 seconds
    pyb.delay(1500)
    print("RPM =",  tach.rpm())
    print("RPM =",  tach.rpm())

    print("Starting pulses again")
    # start the pulses up again
    oc = t5.channel(2, pyb.Timer.OC_TOGGLE, pin=oc_pin)
    pyb.delay(2000)
    print("RPM =",  tach.rpm())
    print("RPM =",  tach.rpm()) 
Example #7
Source File: Tach.py    From upy-examples with MIT License 5 votes vote down vote up
def __init__(self, timer_num, channel_num, pin_name, pulses_per_rev=2):
        self.timestamp = [0] * Tachometer.NUM_SAMPLES
        self.num_samples = -1
        self.delta_time = 0
        self.idx = 0
        self.pulses_per_rev = pulses_per_rev
        self.pulse_detected = False

        # Setup the timer to run at 1MHz
        # We assume that we're running on a 32-bit timer).
        if timer_num != 2 and timer_num != 5:
            raise ValueError("Tachometer needs a 32-bit timer")
        self.timer = pyb.Timer(timer_num)
        print("Initializing timer")
        self.timer.init(prescaler=(int(self.timer.source_freq() / 1000000) - 1),
                        period=0x1fffffff)

        self.pin = pyb.Pin(pin_name)

        print("Initializing channel")
        self.channel = self.timer.channel(channel_num, pyb.Timer.IC,
                                          pin=self.pin,
                                          polarity=pyb.Timer.RISING)
        self.channel.callback(self.channel_callback)
        print("self.channel =", self.channel)
        print("Initialization done") 
Example #8
Source File: heartbeat_fade_irq.py    From upy-examples with MIT License 5 votes vote down vote up
def __init__(self):
        self.tick = 0
        self.led = pyb.LED(4)
        tim = pyb.Timer(4)
        tim.init(freq=100)
        tim.callback(self.heartbeat_cb) 
Example #9
Source File: ultrasonic.py    From MicroPython-Examples with MIT License 5 votes vote down vote up
def distance_in_cm(self):
        start = 0
        end = 0

        # Create a microseconds counter.
        micros = pyb.Timer(2, prescaler=83, period=0x3fffffff)
        micros.counter(0)

        # Send a 10us pulse.
        self.trigger.high()
        pyb.udelay(10)
        self.trigger.low()

        # Wait 'till whe pulse starts.
        while self.echo.value() == 0:
            start = micros.counter()

        # Wait 'till the pulse is gone.
        while self.echo.value() == 1:
            end = micros.counter()

        # Deinit the microseconds counter
        micros.deinit()

        # Calc the duration of the recieved pulse, divide the result by
        # 2 (round-trip) and divide it by 29 (the speed of sound is
        # 340 m/s and that is 29 us/cm).
        dist_in_cm = ((end - start) / 2) / 29

        return dist_in_cm 
Example #10
Source File: pin_cb_test.py    From micropython-async with MIT License 5 votes vote down vote up
def test(fast_io=True, latency=False):
    loop = asyncio.get_event_loop(ioq_len=6 if fast_io else 0)
    pinin = pyb.Pin(pyb.Pin.board.X2, pyb.Pin.IN)
    pyb.Timer(4, freq = 2.1, callback = toggle)
    for _ in range(5):
        loop.create_task(dummy())
    if latency:
        pin_cb = PinCall(pinin, cb_rise = cbl, cbr_args = (pinin,))
    else:
        pincall = PinCall(pinin, cb_rise = cb, cbr_args = (pinin, 'rise'), cb_fall = cb, cbf_args = (pinin, 'fall'))
    loop.run_until_complete(killer()) 
Example #11
Source File: iorw_can.py    From micropython-async with MIT License 5 votes vote down vote up
def __init__(self, read=False, write=False):
        self.ready_rd = False  # Read and write not ready
        self.rbuf = b'ready\n'  # Read buffer
        self.ridx = 0
        pyb.Timer(4, freq = 5, callback = self.do_input)
        self.wch = b''
        self.wbuf = bytearray(100)  # Write buffer
        self.wprint_len = 0
        self.widx = 0
        pyb.Timer(5, freq = 10, callback = self.do_output)

    # Read callback: emulate asynchronous input from hardware.
    # Typically would put bytes into a ring buffer and set .ready_rd. 
Example #12
Source File: iorw.py    From micropython-async with MIT License 5 votes vote down vote up
def __init__(self, read=False, write=False):
        self.ready_rd = False  # Read and write not ready
        self.rbuf = b'ready\n'  # Read buffer
        self.ridx = 0
        pyb.Timer(4, freq = 5, callback = self.do_input)
        self.wch = b''
        self.wbuf = bytearray(100)  # Write buffer
        self.wprint_len = 0
        self.widx = 0
        pyb.Timer(5, freq = 10, callback = self.do_output)

    # Read callback: emulate asynchronous input from hardware.
    # Typically would put bytes into a ring buffer and set .ready_rd. 
Example #13
Source File: iorw.py    From micropython-async with MIT License 5 votes vote down vote up
def __init__(self, read=False, write=False):
        self.ready_rd = False  # Read and write not ready
        self.rbuf = b'ready\n'  # Read buffer
        self.ridx = 0
        pyb.Timer(4, freq = 5, callback = self.do_input)
        self.wch = b''
        self.wbuf = bytearray(100)  # Write buffer
        self.wprint_len = 0
        self.widx = 0
        pyb.Timer(5, freq = 10, callback = self.do_output)

    # Read callback: emulate asynchronous input from hardware.
    # Typically would put bytes into a ring buffer and set .ready_rd. 
Example #14
Source File: tft.py    From micropython-tft-gui with MIT License 5 votes vote down vote up
def backlight(self, percent):
# deferred init of LED PIN
        if self.pin_led is None:
# special treat for BG LED
            self.pin_led = pyb.Pin("Y3", pyb.Pin.OUT_PP)
            self.led_tim = pyb.Timer(4, freq=500)
            self.led_ch = self.led_tim.channel(3, pyb.Timer.PWM, pin=self.pin_led)
        percent = max(0, min(percent, 100))
        self.led_ch.pulse_width_percent(percent)  # set LED
#
# switch power on/off
# 
Example #15
Source File: Main_Timers.py    From uPyIDE with GNU General Public License v3.0 5 votes vote down vote up
def callb(timer):
	print("Timer tick")
	print(timer) 
Example #16
Source File: iotest5.py    From micropython-samples with MIT License 5 votes vote down vote up
def __init__(self, read=False, write=False):
        self.ready_rd = False  # Read and write not ready
        self.rbuf = b'ready\n'  # Read buffer
        self.ridx = 0
        pyb.Timer(4, freq = 5, callback = self.do_input)
        self.wch = b''
        self.wbuf = bytearray(100)  # Write buffer
        self.wprint_len = 0
        self.widx = 0
        pyb.Timer(5, freq = 10, callback = self.do_output)

    # Read callback: emulate asynchronous input from hardware.
    # Typically would put bytes into a ring buffer and set .ready_rd. 
Example #17
Source File: iotest1.py    From micropython-samples with MIT License 5 votes vote down vote up
def __init__(self):
        self.ready_rd = False
        self.ready_wr = False
        self.wbuf = bytearray(100)  # Write buffer
        self.wprint_len = 0
        self.widx = 0
        self.wch = b''
        self.rbuf = b'ready\n'  # Read buffer
        pyb.Timer(4, freq = 1, callback = self.do_input)
        pyb.Timer(5, freq = 10, callback = self.do_output)

    # Read callback: emulate asynchronous input from hardware.
    # Typically would put bytes into a ring buffer and set .ready_rd. 
Example #18
Source File: iotest7.py    From micropython-samples with MIT License 5 votes vote down vote up
def __init__(self, read=False, write=False):
        self.read_count = 0
        self.dummy_count = 0
        self.ready_rd = False
        pyb.Timer(4, freq = 100, callback = self.do_input)

    # Read callback: emulate asynchronous input from hardware. 
Example #19
Source File: iotest6.py    From micropython-samples with MIT License 5 votes vote down vote up
def __init__(self, read=False, write=False):
        self.read_count = 0
        self.dummy_count = 0
        self.ready_rd = False
        pyb.Timer(4, freq = 100, callback = self.do_input)

    # Read callback: emulate asynchronous input from hardware. 
Example #20
Source File: iotest3.py    From micropython-samples with MIT License 5 votes vote down vote up
def __init__(self):
        self.wbuf = bytearray(20)  # Buffer for printing
        self.wprint_len = 0
        self.widx = 0
        self.wch = b''
        wtim = pyb.Timer(5, freq = 10, callback = self.do_output)

    # Write timer callback. Emulate hardware: if there's data in the buffer
    # write some or all of it 
Example #21
Source File: iotest3.py    From micropython-samples with MIT License 5 votes vote down vote up
def __init__(self):
        self.ready_rd = False
        self.rbuf = b'ready\n'  # Read buffer
        pyb.Timer(4, freq = 1, callback = self.do_input)

    # Read callback: emulate asynchronous input from hardware.
    # Typically would put bytes into a ring buffer and set .ready_rd. 
Example #22
Source File: iotest4.py    From micropython-samples with MIT License 5 votes vote down vote up
def __init__(self, read=False, write=False):
        self.ready_rd = False  # Read and write not ready
        self.wch = b''
        if read:
            self.rbuf = b'ready\n'  # Read buffer
            pyb.Timer(4, freq = 1, callback = self.do_input)
        if write:
            self.wbuf = bytearray(100)  # Write buffer
            self.wprint_len = 0
            self.widx = 0
            pyb.Timer(5, freq = 10, callback = self.do_output)

    # Read callback: emulate asynchronous input from hardware.
    # Typically would put bytes into a ring buffer and set .ready_rd. 
Example #23
Source File: test_fast_scheduling.py    From micropython-samples with MIT License 5 votes vote down vote up
def __init__(self):
        self.read_count = 0
        self.dummy_count = 0
        self.ready_rd = False
        pyb.Timer(4, freq = 100, callback = self.do_input)

    # Read callback: emulate asynchronous input from hardware. 
Example #24
Source File: dftclass.py    From micropython-fourier with MIT License 5 votes vote down vote up
def __init__(self, length, adcpin, winfunc=None, timer=6):
        super().__init__(length, winfunc = winfunc)
        self.buff = array.array('i', (0 for x in range(self._length)))
        if isinstance(adcpin, pyb.ADC):
            self.adc = adcpin
        else:
            self.adc = pyb.ADC(adcpin)
        if isinstance(timer, pyb.Timer):
            self.timer = timer
        else:
            self.timer = pyb.Timer(timer)
        self.dboffset = PYBOARD_DBOFFSET # Value for Pyboard ADC 
Example #25
Source File: DHT22.py    From uPython-DHT22 with MIT License 5 votes vote down vote up
def init(timer_id = 2, nc_pin = 'Y3', gnd_pin = 'Y4', vcc_pin = 'Y1', data_pin = 'Y2'):
    global nc
    global gnd
    global vcc
    global data
    global micros
    global timer
    # Leave the pin unconnected
    if nc_pin is not None:
        nc = Pin(nc_pin)
        nc.init(Pin.OUT_OD)
        nc.high()
    # Make the pin work as GND
    if gnd_pin is not None:
        gnd = Pin(gnd_pin)
        gnd.init(Pin.OUT_PP)
        gnd.low()
    # Make the pin work as power supply
    if vcc_pin is not None:
        vcc = Pin(vcc_pin)
        vcc.init(Pin.OUT_PP)
        vcc.high()
    # Configure the pid for data communication
    data = Pin(data_pin)
    # Save the ID of the timer we are going to use
    timer = timer_id
    # setup the 1uS timer
    micros = pyb.Timer(timer, prescaler=83, period=0x3fffffff) # 1MHz ~ 1uS
    # Prepare interrupt handler
    ExtInt(data, ExtInt.IRQ_FALLING, Pin.PULL_UP, None)
    ExtInt(data, ExtInt.IRQ_FALLING, Pin.PULL_UP, edge)

# Start signal