.. _PYB_quickref: PYB快速入门 =============================== 鉴于有很多小伙伴对由官网翻译过来的Pyboard快速入门文档的组织架构十分不爽,我们特此针对我们店推出的几款核心板单独整理个PYB快速入门文档,方便小伙伴快速找到自己想要的资料。 源地工作室提供的可用uPython的pyboard版本 --------------------- 官方标准版复刻版PYBV1.1: .. only:: not latex .. image:: https://img.alicdn.com/imgextra/i2/87224073/O1CN013NaKfu1fxThlFhReH_!!87224073.png :alt: PYBv1.0 pinout :width: 500px 加强版PYBV1.1PLUS: .. only:: not latex .. image:: https://img.alicdn.com/imgextra/i4/87224073/O1CN01ofmQk51fxThlDADVY_!!87224073.png :alt: PYBv1.0 pinout :width: 500px mini版PYBV1.1mini: .. only:: not latex .. image:: https://img.alicdn.com/imgextra/i4/87224073/O1CN01u0ddkO1fxThgVwok5_!!87224073.png :alt: PYBv1.0 pinout :width: 500px 各个版本淘宝购买链接 --------------------- `upython PYBV1.1标准版本 `_ `upython PYBV1.1PLUS加强版本 `_ `upython PYBV1.1mini版本 `_ 各个版本功能引脚示意图 --------------------- .. only:: not latex .. image:: https://img.alicdn.com/imgextra/i3/87224073/O1CN01kU8SiB1fxThk86jBc_!!87224073.jpg :alt: PYBv1.1 pinout :width: 700px .. only:: not latex .. image:: https://img.alicdn.com/imgextra/i3/87224073/O1CN01bsjeN31fxThmwtqmn_!!87224073.jpg :alt: PYBv1.1 pinout :width: 700px .. only:: not latex .. image:: https://img.alicdn.com/imgextra/i2/87224073/O1CN01zzASpu1fxThoexszA_!!87224073.png :alt: PYBv1.1 pinout :width: 700px python编程工具介绍 --------------------- python是一种解释性语言,如果将板子内部植入了Mircropython系统,也就意味着将python语言的解释器,植入到了STM32芯片内,这样用户程序就不像C语言那样,在芯片外编译,链接了,这就意味着,Pyboard使用的python程序,不需要像C语言那样需要复杂的IDE了,理论上讲,windows自带的记事本软件就可以了,不过记事本这样的软件,无法提供关键词高亮,提示,格式的对齐也是挺不直观的,显的十分LOW,我推荐用户使用专业的程序编辑软件,如果你有高级语言使用基础,大型的高级语言开发工具一般都支持Python语言,继续用就行,如果用户没有编程基础或者只是想入门编程,我推荐使用 .. only:: not latex .. image:: https://img.alicdn.com/imgextra/i1/87224073/O1CN01Xeol7U1fxThnHZtT3_!!87224073.png :alt: PYBv1.0 pinout :width: 750px `Sublime Text 下载 `_ pyb程序REPL交互调试使用的串口调试工具 --------------------- 编写完程序点击保存就相当于下载烧写程序,注意LED1在点击保存时会亮起,熄灭表示保存完毕,一定等LED1熄灭后再复位启动程序。 一般启动程序有两种方法,一种是硬启动,一种是软启动。硬启动是按复位键,或者重新上电。软启动是通过生成的虚拟串口打开串口调试软件,进行命令交互发送ctrl+D指令启动,强烈建议使用软启动,因为你不能保证你写的程序没有BUG,如果进行软启动,程序如果bug会在串口调试程序窗口交互中(REPL)提示哪一行,出现什么问题,对应修改就行。 .. only:: not latex .. image:: https://img.alicdn.com/imgextra/i2/87224073/O1CN012OmKDw1fxTiRJZryp_!!87224073.png :alt: PYBv1.0 pinout :width: 750px `SSCOM串口调试程序用于串口调试 `_ .. _hardware_index: .. include:: hardware/index.rst 实验例程 --------------------- 实验例程讲述各种应用实验,在实验中学Python编程,电子电路知识。 .. toctree:: :maxdepth: 1 ex-factory/index.rst 基本功能测试示例 --------------------- 详情参考 :mod:`pyb`. :: import pyb pyb.repl_uart(pyb.UART(1, 9600)) # duplicate REPL on UART(1) pyb.wfi() # pause CPU, waiting for interrupt pyb.freq() # get CPU and bus frequencies pyb.freq(60000000) # set CPU freq to 60MHz pyb.stop() # stop CPU, waiting for external interrupt 延时和定时 ---------------- 使用 :mod:`time ` 模块:: import time time.sleep(1) # sleep for 1 second time.sleep_ms(500) # sleep for 500 milliseconds time.sleep_us(10) # sleep for 10 microseconds start = time.ticks_ms() # get value of millisecond counter delta = time.ticks_diff(time.ticks_ms(), start) # compute time difference 板载LED控制 ------------- 参考 :ref:`pyb.LED `. :: from pyb import LED led = LED(1) # 1=red, 2=green, 3=yellow, 4=blue led.toggle() led.on() led.off() # LEDs 3 and 4 support PWM intensity (0-255) LED(4).intensity() # get intensity LED(4).intensity(128) # set intensity to half 板子用户按键 --------------- 参考 :ref:`pyb.Switch `. :: from pyb import Switch sw = Switch() sw.value() # returns True or False sw.callback(lambda: pyb.LED(1).toggle()) 引脚和通用输入输出引脚控制 ------------- 参考 :ref:`pyb.Pin `. :: from pyb import Pin p_out = Pin('X1', Pin.OUT_PP) p_out.high() p_out.low() p_in = Pin('X2', Pin.IN, Pin.PULL_UP) p_in.value() # get value, 0 or 1 舵机控制 ------------- 参考 :ref:`pyb.Servo `. :: from pyb import Servo s1 = Servo(1) # servo on position 1 (X1, VIN, GND) s1.angle(45) # move to 45 degrees s1.angle(-60, 1500) # move to -60 degrees in 1500ms s1.speed(50) # for continuous rotation servos 外部中断 ------------------- 参考 :ref:`pyb.ExtInt `. :: from pyb import Pin, ExtInt callback = lambda e: print("intr") ext = ExtInt(Pin('Y1'), ExtInt.IRQ_RISING, Pin.PULL_NONE, callback) 定时器 ------ 参考 :ref:`pyb.Timer `. :: from pyb import Timer tim = Timer(1, freq=1000) tim.counter() # get counter value tim.freq(0.5) # 0.5 Hz tim.callback(lambda t: pyb.LED(1).toggle()) RTC (实时时钟) --------------------- 参考 :ref:`pyb.RTC ` :: from pyb import RTC rtc = RTC() rtc.datetime((2017, 8, 23, 1, 12, 48, 0, 0)) # set a specific date and time rtc.datetime() # get date and time PWM (脉宽调制输出) ---------------------------- 参考 :ref:`pyb.Pin ` & :ref:`pyb.Timer `. :: from pyb import Pin, Timer p = Pin('X1') # X1 has TIM2, CH1 tim = Timer(2, freq=1000) ch = tim.channel(1, Timer.PWM, pin=p) ch.pulse_width_percent(50) ADC (模数转换) ---------------------------------- 参考 :ref:`pyb.Pin ` & :ref:`pyb.ADC `. :: from pyb import Pin, ADC adc = ADC(Pin('X19')) adc.read() # read value, 0-4095 DAC (数模转换) ---------------------------------- 参考 :ref:`pyb.Pin ` & :ref:`pyb.DAC `. :: from pyb import Pin, DAC dac = DAC(Pin('X5')) dac.write(120) # output between 0 and 255 UART (串口) ----------------- 参考 :ref:`pyb.UART `. :: from pyb import UART uart = UART(1, 9600) uart.write('hello') uart.read(5) # read up to 5 bytes SPI 总线 ------- 参考 :ref:`pyb.SPI `. :: from pyb import SPI spi = SPI(1, SPI.MASTER, baudrate=200000, polarity=1, phase=0) spi.send('hello') spi.recv(5) # receive 5 bytes on the bus spi.send_recv('hello') # send and receive 5 bytes I2C 总线 ------- 参考 :ref:`pyb.I2C `. :: from pyb import I2C i2c = I2C(1, I2C.MASTER, baudrate=100000) i2c.scan() # returns list of slave addresses i2c.send('hello', 0x42) # send 5 bytes to slave with address 0x42 i2c.recv(5, 0x42) # receive 5 bytes from slave i2c.mem_read(2, 0x42, 0x10) # read 2 bytes from slave 0x42, slave memory 0x10 i2c.mem_write('xy', 0x42, 0x10) # write 2 bytes to slave 0x42, slave memory 0x10 CAN 总线 --------------------------------- 参考 :ref:`pyb.CAN `. :: from pyb import CAN can = CAN(1, CAN.LOOPBACK) can.setfilter(0, CAN.LIST16, 0, (123, 124, 125, 126)) can.send('message!', 123) # send a message with id 123 can.recv(0) # receive message on FIFO 0 板载加速度传感器 ---------------------- 参考 :ref:`pyb.Accel `. :: from pyb import Accel accel = Accel() print(accel.x(), accel.y(), accel.z(), accel.tilt())