pyboard 的快速参考 以下引脚分配适用于 PYBv1.1。您还可以查看其他版本的 pyboard 的引脚排列: PYBv1.0 或PYBLITEv1.0-AC 或 PYBLITEv1.0.  下面是 pyboard 的快速参考。如果这是您第一次使用该板,请考虑先阅读以下部分:
: S! p" x9 ?8 E, w) w) C' c通用板控制见 pyb. - import pyb
7 F- E7 O* ]/ B+ {9 q: g
3 c3 C+ ?% y" w4 u* q6 j2 C/ d- pyb.repl_uart(pyb.UART(1, 9600)) # duplicate REPL on UART(1)2 R3 Y+ r# x$ ?- ?* P
- pyb.wfi() # pause CPU, waiting for interrupt) o8 L% H) p$ F$ q* {6 Z2 l8 [
- pyb.freq() # get CPU and bus frequencies( m* s5 u5 N+ K: C5 Y
- pyb.freq(60000000) # set CPU freq to 60MHz* B# [% p$ z8 D; |+ k
- pyb.stop() # stop CPU, waiting for external interrupt
复制代码 # e5 c9 |. G" m* K6 Z. K" G1 r" z6 E
4 `" y1 o& L$ @
延迟和计时使用 time模块: - import time
" F3 w& G3 G$ e+ j& ^4 i" a - # \8 ?6 [7 o5 G" G+ k
- time.sleep(1) # sleep for 1 second. N$ A& _$ i% B; f P! t# [$ u
- time.sleep_ms(500) # sleep for 500 milliseconds
; Q1 a0 K3 Y7 W+ L! W$ Y. V. h3 O! f* O - time.sleep_us(10) # sleep for 10 microseconds
4 L. O; e5 ]8 j1 J& N - start = time.ticks_ms() # get value of millisecond counter/ D1 r' }1 E! Z! ~, s+ q
- delta = time.ticks_diff(time.ticks_ms(), start) # compute time difference
复制代码 % R% N7 T, w/ [8 T D3 h
# S1 ?# _3 H7 o% B; @: }内部 LED见 pyb.LED. - from pyb import LED
& d: W, z/ m& \0 T; c8 L! C/ w; V
& b* }0 v* Q1 A4 u. r4 o8 l% Q- led = LED(1) # 1=red, 2=green, 3=yellow, 4=blue
8 M+ z$ k. y' `( e - led.toggle()
' x9 V7 E* J! @. ^ - led.on()+ z' r+ o; _9 V) n, C4 s
- led.off()
& j. u+ J2 C1 ?; F. E9 w - 3 l& h; @" l) a. K4 c
- # LEDs 3 and 4 support PWM intensity (0-255)9 B' ]+ g9 K! ~ k. M
- LED(4).intensity() # get intensity/ ?. B3 |, C+ d) u1 a% n
- LED(4).intensity(128) # set intensity to half
复制代码
H0 t7 b& l& ^8 S h2 U, [3 k5 E% q1 ~/ S
内部开关请参阅 pyb.Switch. - from pyb import Switch+ i [3 w( J+ e8 \
: N% `8 `+ v7 y# I5 {. L- l, G" \, V- sw = Switch()
# F0 u, O0 c. Z, b1 ~ - sw.value() # returns True or False
. k5 }& k: E9 z - sw.callback(lambda: pyb.LED(1).toggle())
复制代码
# _! r& I8 ]2 M* Y3 X! E3 m+ z/ m0 U
" y: ~% I% c$ P1 T! ?引脚和 GPIO请参阅 pyb.Pin. - from pyb import Pin
# T8 h$ Z* n: E9 [. m - 0 t$ c% Q2 @4 u: w+ K
- p_out = Pin('X1', Pin.OUT_PP)/ Q% s7 r; D7 H! ?6 x7 `7 a
- p_out.high()! T2 t! |$ b, [; s( q: }! H
- p_out.low()- N! l9 ]0 y: a, F% `% ]
& _/ B4 l) _/ i( l6 ^- y. p" g- p_in = Pin('X2', Pin.IN, Pin.PULL_UP)4 j! z, `% q4 G$ o7 r
- p_in.value() # get value, 0 or 1
复制代码
" O( z5 h: G& Q
8 |$ d0 E) m8 a8 R O伺服控制请参阅pyb.Servo. - from pyb import Servo& K, l6 M3 A7 U( a1 B2 `0 F
0 u4 U/ I, Y+ E- s1 = Servo(1) # servo on position 1 (X1, VIN, GND)
6 N& {4 I& G2 o4 t# G - s1.angle(45) # move to 45 degrees
7 {3 |6 ^$ M8 b+ Q" k$ | - s1.angle(-60, 1500) # move to -60 degrees in 1500ms
/ n0 y+ H* c" b' J _: { - s1.speed(50) # for continuous rotation servos
复制代码 Q/ O3 A0 l- l; F
9 u3 |, {8 S; l, B0 ]+ t外部中断请参阅 pyb.ExtInt. - from pyb import Pin, ExtInt
- K# K) K- F' _4 a; b* F: a - & Q4 \7 z1 U7 y5 P9 t
- callback = lambda e: print("intr")4 o- k% I. d$ B+ K! Y* V6 S
- ext = ExtInt(Pin('Y1'), ExtInt.IRQ_RISING, Pin.PULL_NONE, callback)
复制代码
& O( m& M: R* F P6 I/ k
7 i' S' i8 ~1 e8 \ S' n计时器请参阅pyb.Timer. - from pyb import Timer4 Y1 }6 D; K$ g+ J. t, {' m, o- s
b$ X- a6 S/ m. X1 ~- tim = Timer(1, freq=1000)
7 k' t7 I* @" b2 c$ c - tim.counter() # get counter value
( i% H7 D& z! I7 U$ u - tim.freq(0.5) # 0.5 Hz
- u* D/ \8 k e! A8 _: m - tim.callback(lambda t: pyb.LED(1).toggle())
复制代码 , X$ f \: q) w, ]
RTC(实时时钟)请参阅 pyb.RTC - from pyb import RTC3 a9 B" K. u$ d1 @# f" T
, ?1 W: P2 J5 D6 d6 b. _- rtc = RTC()
! [" q. L0 u: \) ?; N - rtc.datetime((2017, 8, 23, 1, 12, 48, 0, 0)) # set a specific date and time
8 ?' C0 ~* B; P$ h+ I& e9 c& E9 Q - rtc.datetime() # get date and time
复制代码 0 L/ [+ {8 [8 W
PWM(脉宽调制)请参阅pyb.Pin 和 pyb.Timer. - from pyb import Pin, Timer/ d6 z, Y' w9 \: x- Q& I
9 W- T) D) b8 R3 s- p = Pin('X1') # X1 has TIM2, CH1
. q2 e7 B+ ^2 X5 ? - tim = Timer(2, freq=1000)$ x* v: H/ A) R& ~5 }3 D& O
- ch = tim.channel(1, Timer.PWM, pin=p)) H# b6 O! `" I* l: ?. e
- ch.pulse_width_percent(50)
复制代码
& `; H1 q! Y! [. {ADC(模数转换)请参阅pyb.Pin 和pyb.ADC. - from pyb import Pin, ADC
/ S2 W( z$ r9 H* F, _. ?: h" d
1 l4 c: ]+ X2 P* O* ` U- adc = ADC(Pin('X19'))) r# S; t2 f# F- ~4 _
- adc.read() # read value, 0-4095
复制代码 + s' m5 w3 P' ~
2 t. D# I8 l+ q3 p" U, F# O0 R& wDAC(数模转换)请参阅pyb.Pin 和 pyb.DAC. - from pyb import Pin, DAC. U$ W8 Z( X; v# c$ X& M) s9 e
3 T1 v% q0 O' Q- ~0 M+ g- dac = DAC(Pin('X5'))& \- E4 M% s q; [
- dac.write(120) # output between 0 and 255
复制代码
% l- @: ^: y/ _% T P% C% {* U4 X- F3 K3 D, f* u* q! O
UART(串行总线)请参阅 pyb.UART. - from pyb import UART
% n; M& H# I: C( f8 P - " M0 ?2 x; |1 E2 Y4 J' I% P
- uart = UART(1, 9600)
5 i% L, n+ I- X7 r- t) {% E- ]: u - uart.write('hello')
, U; L3 p+ j% E$ e - uart.read(5) # read up to 5 bytes
复制代码
* {: c! P2 y8 G5 p0 w9 o' n6 v. V5 X
SPI总线请参阅 pyb.SPI. - from pyb import SPI
* d) [4 g- ]! }5 b8 c& ]- e - 8 x( @3 @4 `7 \& o6 V2 k0 {% i* [
- spi = SPI(1, SPI.MASTER, baudrate=200000, polarity=1, phase=0)" |: X" d2 ?. C, x7 U
- spi.send('hello')/ o0 p/ m1 F/ J+ b# u
- spi.recv(5) # receive 5 bytes on the bus
" p, n3 x/ ?0 b: c' i$ j - spi.send_recv('hello') # send and receive 5 bytes
复制代码
. P! I: Z" }' m0 |% L
4 ^5 T7 K' X4 Z9 g* CI2C总线硬件 I2C 可通过I2C('X') 和用于 pyboard 的 X 和 Y 两半 I2C('Y')。或者,传入外围设备的整数标识符,例如I2C(1)。通过显式指定 scl 和 sda 引脚而不是总线名称,也可以使用软件 I2C 。有关更多详细信息,请参阅 machine.I2C. - from machine import I2C @, J. k4 ~+ T) s1 |: G% \# d
- # r7 t& F0 [- @- J+ M5 m
- i2c = I2C('X', freq=400000) # create hardware I2c object
" v$ x" z" R# O - i2c = I2C(scl='X1', sda='X2', freq=100000) # create software I2C object& X: k1 _* B5 h6 O8 T) v% ~4 j) T
) i7 t. ^( P3 t: E7 d8 _- i2c.scan() # returns list of slave addresses
" g, a: ~& p, S+ _3 z - i2c.writeto(0x42, 'hello') # write 5 bytes to slave with address 0x42
5 c. w; u: H ]! v8 W - i2c.readfrom(0x42, 5) # read 5 bytes from slave- K d5 b# ~' e4 R& X0 L
% }1 z' D6 Q2 M$ r$ f, E- i2c.readfrom_mem(0x42, 0x10, 2) # read 2 bytes from slave 0x42, slave memory 0x10
; r* ^/ } u% |/ C - i2c.writeto_mem(0x42, 0x10, 'xy') # write 2 bytes to slave 0x42, slave memory 0x10
复制代码
( n/ E3 ?- a4 ] u( ], M( g/ J6 ]7 u9 R5 a" K8 u; o- a7 T1 k, c8 f
注意:对于传统 I2C 支持,请参阅 pyb.I2C。 - ~: p* }; N* S$ T' M
I2S总线参见 machine.I2S. - from machine import I2S, Pin
0 g0 T9 G0 r {/ ^/ Z - 0 q) _% I2 b6 }% e& L/ @6 M5 l
- i2s = I2S(2, sck=Pin('Y6'), ws=Pin('Y5'), sd=Pin('Y8'), mode=I2S.TX, bits=16, format=I2S.STEREO, rate=44100, ibuf=40000) # create I2S object
5 G. F3 H* o& |4 j- |& {: A - i2s.write(buf) # write buffer of audio samples to I2S device% |! A/ G2 x5 v- `/ ^6 h, p+ J
- / N1 Y! i D5 l1 C: |
- i2s = I2S(1, sck=Pin('X5'), ws=Pin('X6'), sd=Pin('Y4'), mode=I2S.RX, bits=16, format=I2S.MONO, rate=22050, ibuf=40000) # create I2S object+ p; s$ y5 `/ P! {, ]" `
- i2s.readinto(buf) # fill buffer with audio samples from I2S device
复制代码 5 |' e5 ^7 U8 u0 k. V! S1 d0 L
I2S 类目前作为技术预览版提供。在预览期间,鼓励用户提供反馈。基于此反馈,I2S 类 API 和实现可能会更改。 PYBv1.0/v1.1 有一个 I2S 总线,id=2。PYBD-SFxW 有两条 I2S 总线,id=1 和 id=2。I2S 与 SPI 共享。 6 E' n4 U4 o$ ?6 n& V
CAN总线(控制器局域网)参见 pyb.CAN. - from pyb import CAN
1 J9 L8 k; _# ?5 I7 p7 V7 k
3 V7 _- D. y) K* V2 k2 B( y- can = CAN(1, CAN.LOOPBACK)( z, Z$ g, b, r( W1 y% w" M2 V' A
- can.setfilter(0, CAN.LIST16, 0, (123, 124, 125, 126))
" m/ S' O9 I# O7 H1 v9 h - can.send('message!', 123) # send a message with id 1230 n) v; H4 M+ v& c& k
- can.recv(0) # receive message on FIFO 0
复制代码 ! L- z* d" D5 d& V
8 n6 c5 s1 t! ^8 _' r
内部加速度计参见pyb.Accel. - from pyb import Accel" t, Q( @6 k* H" ^
* C5 f! [- u8 C1 o2 u6 _- accel = Accel(). t; q) y& x5 w4 O2 l
- print(accel.x(), accel.y(), accel.z(), accel.tilt())
复制代码 " x3 f; _7 t, A' o
( s1 E( d. U+ A |