Python pyb.udelay() Examples

The following are 7 code examples of pyb.udelay(). 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: printmidiin.py    From micropython-stm-lib with MIT License 5 votes vote down vote up
def loop(midiin):
    while True:
        midiin.poll()
        pyb.udelay(50) 
Example #3
Source File: hd44780.py    From micropython-stm-lib with MIT License 5 votes vote down vote up
def _usleep(self, us):
        """Delay by (sleep) us microseconds."""
        # Wrapped as a method for portability
        pyb.udelay(us) 
Example #4
Source File: mutex_test.py    From micropython-samples with MIT License 5 votes vote down vote up
def update(t):                  # Interrupt handler may not be able to acquire the lock
    global var1, var2           # if main loop has it
    if mutex.test():            # critical section start
        var1 += 1
        pyb.udelay(200)
        var2 += 1
        mutex.release()         # critical section end 
Example #5
Source File: upcd8544.py    From Smart-IoT-Planting-System with MIT License 5 votes vote down vote up
def sleep_us(self, useconds):
        try:
            time.sleep_us(useconds)
        except AttributeError:
            machine.udelay(useconds) 
Example #6
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 #7
Source File: flash.py    From micropython-epaper with Apache License 2.0 5 votes vote down vote up
def info(self):
        self.pinCS.low()
        pyb.udelay(1000)                        # FLASH wake up delay
        self.spi.send(FLASH_RDID)
        manufacturer = self.spi.send_recv(FLASH_NOP)[0]
        id_high = self.spi.send_recv(FLASH_NOP)[0]
        id_low = self.spi.send_recv(FLASH_NOP)[0]
        self.pinCS.high()
        return manufacturer, id_high << 8 | id_low