Salted Fish Micropython—pyb related functions

Salted Fish Micropython — pyb related functions


The pyb module contains functions related to pyboard.

Time function
  • pyb.delay(ms)
    Delay in milliseconds.

  • pyb.udelay(us)
    Delay in microseconds.

  • pyb.millis()
    Returns the elapsed time (milliseconds) after startup.
    The return value is of type micropython smallint (31-bit signed integer), so it will become negative after 2 ^ 30 milliseconds (approximately 12.4 days).
    Note that calling pyb.stop () will stop the hardware counter, so it will count during "sleep". It also affects pyb.elapsed_millis ().

  • pyb.micros()
    Returns the microsecond after reset.
    The return value is of type micropython smallint (31-bit signed integer), so it will become negative after 2 ^ 30 microseconds (about 17.8 minutes).

  • pyb.elapsed_millis(start)
    Returns the time (in milliseconds) since the start time.
    This function takes into account the counter wraparound, so the return value is always positive. Therefore it can be used to measure up to 12.4 days.

example:

start = pyb.millis()
while pyb.elapsed_millis(start) < 1000:
  • pyb.elapsed_micros(start)
    Returns the time (microseconds) from the moment of start to the present.
    This function takes into account the counter wraparound, so the return value is always positive. Therefore it can be used to measure up to 17.8 minutes.

example:

start = pyb.micros()
while pyb.elapsed_micros(start) < 1000:
   pass
Reset function
  • pyb.hard_reset()
    Reset has the same effect as pressing the reset button.

  • pyb.bootloader()
    Go directly to the bootloader.

Interrupt function
  • pyb.disable_irq()
    Interrupts are prohibited. Return to the previous interrupt enable state. The return value can be used to restore the interrupt enable state in the enable_irq function.

  • pyb.enable_irq(state=True)
    When state is True (default), interrupts are allowed, and when False, interrupts are prohibited. Commonly used to resume interruption when exiting a critical time zone.

Power function
  • pyb.freq(sysclk, hclk, pclk1, pclk2)
    When there is no parameter, the current clock frequency is returned, including: (sysclk, hclk, pclk1, pclk2). It corresponds to:

  • sysclk: CPU clock frequency

  • hclk: AHB, memory and DMA bus frequency

  • pclk1: APB1 bus frequency

  • pclk2: APB2 bus frequency

If parameters are specified, the CPU frequency will be set. The frequency unit is Hz. For example, freq (120000000) sets sysclk (CPU frequency) to 120MHz. Note that not all parameters can be used, the supported clock frequencies are (MHz): 8, 16, 24, 30, 32, 36, 40, 42, 48, 54, 56, 60, 64, 72, 84, 96, 108 , 120, 144, 168 etc. The maximum hclk is 168MHz, pclk1 is 42MHz, pclk2 is 84MHz. Do not set the frequency beyond this range.

The frequency of hclk, pclk1 and pclk2 is divided by the system clock. The frequency division ratio supported by hclk is: 1, 2, 4, 8, 16, 64, 128, 256, 512. The distribution ratio of pclk1 and pclk2 is: 1, 2, 4, 8.

sysclk directly uses HSE (external oscillator) at 8MHz, and directly uses HSI (internal oscillator) at 16MHz. Above this frequency, HSE is used to drive the PLL (phase-locked loop) output.

Note that when changing the clock frequency, connecting to a computer via USB will make the USB unusable. Therefore, it is best to change the clock in boot.py, at this time the USB
peripherals have not been enabled. In addition, when the system clock is lower than 36MHz, the USB function cannot be used.

  • pyb.wfi()
    Wait for internal or external interrupt. Execute the wfi instruction to reduce power consumption until any interrupt (internal or external) occurs, and then continue to run. Note that the system-tick interrupt occurs every millisecond (1000Hz), so it blocks up to 1ms.

  • pyb.stop()
    Entering the "sleeping" state can reduce power consumption to 500 uA. Waking up from sleep mode requires an external interrupt or real-time clock event, and continues to run from the sleep position after waking up.
    See rtc.wakeup () to configure the real-time clock wake-up event.

  • pyb.standby()
    Enter the "deep sleep" state, the power consumption will be less than 50 uA. Waking up from deep sleep mode requires a real-time clock event, or external interrupt on the X1 pin (PA0 = WKUP), or X18 (PC13 = TAMP1). A reset will be performed after waking up.
    See rtc.wakeup () to configure the real-time clock wake-up event.

Other functions
  • pyb.have_cdc()
    Returns True if the USB is connected and acts as a serial device, otherwise it returns False.

Note that this function is obsolete, please use pyb.USB_VCP (). Isconnected () in the future.

  • pyb.hid((buttons, x, y, z))
    Get the 4-parameter tuple (or list) and send it to the USB host (PC) to drive the HID mouse.

Note that this function is obsolete, please use pyb.USB_HID (). Send (…).

  • pyb.info()
    Print development board information.

  • pyb.main(filename)
    Set the file name of the main script that starts after boot.py runs. If this function is not called, the default file main.py will be executed.
    Only calling this function in boot.py is valid.

  • pyb.mount(device, mountpoint, *, readonly=False, mkfs=False)

Mount the device as part of the file system. The device must provide the following protocol: readblocks (self, blocknum, buf)
writeblocks (self, blocknum, buf) (optional) count (self) sync (self)
(optional) readblocks and writeblocks need to copy data between buf and the device. buf is a
bytearray in multiples of 512 . If writeblocks are not defined, the device will be loaded in read-only mode and the return values ​​of the two functions are ignored. count returns
the number of device blocks, sync, and sync data to the device. Mountpoint must start with a right slash under the root of the file system. If readonly is
True, the device will be loaded in read-only mode, otherwise it will be loaded in read-write mode. If mkfs is True, a new file system will be created. To unmount the device, use None
as the device name parameter and the mount point as the mountpoint.

  • pyb.repl_uart(uart)
    Get the serial port where the REPL is set.

  • pyb.rng()
    Returns a 30-bit random number, which is generated by RNG hardware. (Note that if there is no RNG module, this function will not be available)

  • pyb.sync()
    Synchronize all file systems.

  • pyb.unique_id()
    Returns a 12-byte (96-bit) unique ID number.

Classes

class Accel-acceleration sensor
class ADC-analog to digital conversion
class CAN-CAN bus communication
class DAC-digital to analog conversion
class ExtInt-external interrupt
class I2C-two-wire communication protocol
class LCD-pyskin LCD control LCD
class LED-LED object
class Pin-Control I / O
class PinAF-Pin alternate function
class RTC-Real-time clock
class Servo-3-wire servo motor drive
class SPI-Main SPI drive
class Switch-Button object
class Timer-Internal timer
class TimerChannel-Timer channel control
class UART-serial communication
class USB_VCP-USB virtual serial port

Published 166 original articles · 22 praises · 10,000+ views

Guess you like

Origin blog.csdn.net/weixin_45020839/article/details/102389065