pyboard 的快速参考 以下引脚分配适用于 PYBv1.1。您还可以查看其他版本的 pyboard 的引脚排列: PYBv1.0 或PYBLITEv1.0-AC 或 PYBLITEv1.0.  下面是 pyboard 的快速参考。如果这是您第一次使用该板,请考虑先阅读以下部分: 1 M8 X1 W. a, S/ B0 \0 \6 t
通用板控制见 pyb. - import pyb4 z: v: {& W; l2 T* X( j
- 7 t( ^3 f4 F0 u; R( M7 P
- pyb.repl_uart(pyb.UART(1, 9600)) # duplicate REPL on UART(1)
2 a: |, o) y7 U' c( P; f4 Q1 }* O! L - pyb.wfi() # pause CPU, waiting for interrupt6 w0 c3 l5 u$ D7 x
- pyb.freq() # get CPU and bus frequencies# P5 \5 V9 s- `" b0 Y
- pyb.freq(60000000) # set CPU freq to 60MHz% T2 S$ B c5 B' o% Y. Y+ m6 {: h
- pyb.stop() # stop CPU, waiting for external interrupt
复制代码 8 v" O# O; i$ r; \
9 T9 a! ?! p& a8 z% z0 d t4 [
延迟和计时使用 time模块: - import time
2 J" s3 D# \9 }. N# I- l - 1 g& x$ B0 B4 S! O3 W1 S; y
- time.sleep(1) # sleep for 1 second
. ^- l# k1 X3 g$ Z1 ^* s - time.sleep_ms(500) # sleep for 500 milliseconds( L- d6 J y. k0 b( S. y
- time.sleep_us(10) # sleep for 10 microseconds
3 _3 C; ]( S$ v, h$ {" i5 [7 a - start = time.ticks_ms() # get value of millisecond counter
; o) d* Y* c% O, X* _: s4 R% J - delta = time.ticks_diff(time.ticks_ms(), start) # compute time difference
复制代码
( I3 O! `# @) x9 P8 c7 } b2 H* i6 W9 C/ u9 d3 X3 ]5 d7 j1 H
内部 LED见 pyb.LED. - from pyb import LED& y* h5 W) z- ^7 {) e) B
- % a! @3 l. Z; Z4 r2 @* q, C) i
- led = LED(1) # 1=red, 2=green, 3=yellow, 4=blue6 m4 r& C; C4 K, X$ s2 b0 ^# Q
- led.toggle(); F' H0 h; m* N5 {
- led.on()( q Y/ d# H3 G) _; O, W; z- g
- led.off()5 w: |& u. f% u! X& E5 C
6 _" L4 q, }( P- # LEDs 3 and 4 support PWM intensity (0-255)( F7 j) m( s4 ?: \( y
- LED(4).intensity() # get intensity1 M6 G4 C- T7 S! c0 w0 Y
- LED(4).intensity(128) # set intensity to half
复制代码
; \+ n4 O" g2 o* q, q( i: N
" q+ y$ Y0 w8 U7 ]/ X0 H内部开关请参阅 pyb.Switch. - from pyb import Switch+ D4 I' K+ i9 y' J' H0 p }* _0 R
" O4 f6 h3 l2 j# H0 d, V. k- sw = Switch()/ p: f* a+ L+ a6 m/ K
- sw.value() # returns True or False
+ G @9 [9 d; {6 U - sw.callback(lambda: pyb.LED(1).toggle())
复制代码 2 m9 P) G$ L1 C! c _" ^; k
& W2 O* o' z/ x* Z引脚和 GPIO请参阅 pyb.Pin. - from pyb import Pin
A; A( H2 r# J" i - 4 D2 W! @' F$ |8 z3 Z% F7 }
- p_out = Pin('X1', Pin.OUT_PP), N7 Y8 M. L2 J2 |
- p_out.high()
+ \' y9 K5 Q4 ~$ ~0 Q/ x1 s1 j2 } - p_out.low()" l) A% p7 `& V$ E
+ v. ^7 I5 h; s2 d* L- p_in = Pin('X2', Pin.IN, Pin.PULL_UP)+ {- v [% n/ C/ p0 i1 w) r
- p_in.value() # get value, 0 or 1
复制代码 8 l l/ N# \9 Q
2 C- j0 y h$ z; g
伺服控制请参阅pyb.Servo. - from pyb import Servo
! S; k- Z" W' |) {2 @1 g
# r1 f2 E9 n7 B" k- s1 = Servo(1) # servo on position 1 (X1, VIN, GND)
4 y. c* k3 H3 N - s1.angle(45) # move to 45 degrees
0 W5 \" \+ `1 O - s1.angle(-60, 1500) # move to -60 degrees in 1500ms
% I4 j( w' w8 i( i) _+ ?& ? - s1.speed(50) # for continuous rotation servos
复制代码 . d$ Q }8 A0 c8 X: y6 V+ x" D# x, I
, F8 A" Y' d- S/ s外部中断请参阅 pyb.ExtInt. - from pyb import Pin, ExtInt/ Y [ J7 S# M/ u5 R
- " v8 J4 y7 q3 o) v D3 U
- callback = lambda e: print("intr")2 t0 `8 Y# ?, G
- ext = ExtInt(Pin('Y1'), ExtInt.IRQ_RISING, Pin.PULL_NONE, callback)
复制代码
1 M% V' U1 ^. C; X- G. Z# Y( I7 B _: n2 {, _6 o% S7 c9 l2 h; M: O
计时器请参阅pyb.Timer. - from pyb import Timer
: K" Q3 }1 i2 ]3 ~
8 w" r# ~9 u9 x j# y A0 h! b0 x- tim = Timer(1, freq=1000)
& }: k# P7 ~0 x: I' N: C - tim.counter() # get counter value
+ z8 P7 [, l! ?- J n2 D - tim.freq(0.5) # 0.5 Hz1 |: D" Z- G4 @0 Q
- tim.callback(lambda t: pyb.LED(1).toggle())
复制代码
U3 _& q# B$ y- ?+ p0 aRTC(实时时钟)请参阅 pyb.RTC - from pyb import RTC
1 `+ [, G! ?6 z0 ^, @. j/ O1 i - - M& e3 d. @3 K) a; P
- rtc = RTC()
; l$ Z8 E/ z' j: X1 v/ ^0 P - rtc.datetime((2017, 8, 23, 1, 12, 48, 0, 0)) # set a specific date and time
) i& Q9 k$ {# Y/ k9 ? - rtc.datetime() # get date and time
复制代码
2 M) X h W$ a8 g' a0 EPWM(脉宽调制)请参阅pyb.Pin 和 pyb.Timer. - from pyb import Pin, Timer
* x. `7 d0 J( l% J: V, ` J - 6 {' K5 R, {/ @* z' O) `
- p = Pin('X1') # X1 has TIM2, CH1
: K L$ _0 M- L. G - tim = Timer(2, freq=1000)
+ Y8 @) o4 k- x: t - ch = tim.channel(1, Timer.PWM, pin=p)
; M4 @; N5 a. l7 |& W - ch.pulse_width_percent(50)
复制代码
0 _ _8 K8 S) B- D+ iADC(模数转换)请参阅pyb.Pin 和pyb.ADC. - from pyb import Pin, ADC5 M; B$ p* s8 x
- $ ]" M! F5 c d( h2 w
- adc = ADC(Pin('X19'))* \$ A3 B2 |% R! l, |" X- U% Q& ~
- adc.read() # read value, 0-4095
复制代码 + W6 i. N# l2 u1 k% b, N- v
/ h/ J; [+ t5 X3 {) CDAC(数模转换)请参阅pyb.Pin 和 pyb.DAC. - from pyb import Pin, DAC
4 O2 K( \- _* ^3 N; ~8 g - 3 g( J( A1 ?* ^8 e( N2 T
- dac = DAC(Pin('X5'))
% T s w$ m1 J- r' v& c/ R. F - dac.write(120) # output between 0 and 255
复制代码
7 V$ ]' r9 i0 Z3 A4 T0 d% X; g% F
9 D( L4 f$ k3 @. zUART(串行总线)请参阅 pyb.UART. - from pyb import UART: g/ z* X5 F( o5 m4 q9 A
/ ~9 ]2 e5 F' e; y6 E/ d- uart = UART(1, 9600)
# T4 S9 X+ z' S" y8 {% T3 d' k - uart.write('hello'), k' z- I% l( Z. H8 D d. w
- uart.read(5) # read up to 5 bytes
复制代码 2 ]+ M! [1 ]6 y% y7 W# N
5 s |1 S* N/ x6 t+ g+ ESPI总线请参阅 pyb.SPI. - from pyb import SPI9 J& U& o3 O0 j5 m3 }1 `7 z
- 2 ~, @: V/ }/ c
- spi = SPI(1, SPI.MASTER, baudrate=200000, polarity=1, phase=0)& s& O. f# E. x1 c, v* u
- spi.send('hello')
( _" j' r* x# K4 ]7 W - spi.recv(5) # receive 5 bytes on the bus. C, l1 I, D% s- ~& m' l
- spi.send_recv('hello') # send and receive 5 bytes
复制代码
% ^' G' n1 |. K
7 L% V: n9 f" U: ~: gI2C总线硬件 I2C 可通过I2C('X') 和用于 pyboard 的 X 和 Y 两半 I2C('Y')。或者,传入外围设备的整数标识符,例如I2C(1)。通过显式指定 scl 和 sda 引脚而不是总线名称,也可以使用软件 I2C 。有关更多详细信息,请参阅 machine.I2C. - from machine import I2C/ g! Z/ u$ D$ J: R: O
- 7 P7 v9 L0 Q. V1 I/ A- t. ?
- i2c = I2C('X', freq=400000) # create hardware I2c object
, `. v( w% c2 J - i2c = I2C(scl='X1', sda='X2', freq=100000) # create software I2C object
1 a5 s! V% h, ]; F - % C. i1 P3 e5 Y, q `$ H
- i2c.scan() # returns list of slave addresses
+ O, U$ v2 N- a- u" x - i2c.writeto(0x42, 'hello') # write 5 bytes to slave with address 0x426 r b/ q9 K: H/ C& T
- i2c.readfrom(0x42, 5) # read 5 bytes from slave i! K! i, J" \1 N
- 4 z8 j2 w7 G$ F9 h8 V4 u
- i2c.readfrom_mem(0x42, 0x10, 2) # read 2 bytes from slave 0x42, slave memory 0x108 U" o1 J6 R% Z& ?1 E# F
- i2c.writeto_mem(0x42, 0x10, 'xy') # write 2 bytes to slave 0x42, slave memory 0x10
复制代码 _+ p; Z# k& O- ^$ ?4 c
2 q5 k' x, I0 R7 Y# W: l9 a注意:对于传统 I2C 支持,请参阅 pyb.I2C。 . b2 ~( k& R8 l
I2S总线参见 machine.I2S. - from machine import I2S, Pin+ ~$ S. I3 C, H3 U2 G' B4 \, M) v
4 N$ y: l8 ~, B+ E& P- 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- A4 ]8 b3 ^& Y: T9 N4 X3 M; n( V
- i2s.write(buf) # write buffer of audio samples to I2S device
( Z4 [1 J) R5 T4 o - $ o0 t: i2 F8 c! v/ C" Z# w
- 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/ b! W8 F2 C( H$ }5 ]* c
- i2s.readinto(buf) # fill buffer with audio samples from I2S device
复制代码 3 ^1 B- _- z0 r5 T8 q" S9 p
I2S 类目前作为技术预览版提供。在预览期间,鼓励用户提供反馈。基于此反馈,I2S 类 API 和实现可能会更改。 PYBv1.0/v1.1 有一个 I2S 总线,id=2。PYBD-SFxW 有两条 I2S 总线,id=1 和 id=2。I2S 与 SPI 共享。 + t, ^; P: y' B! H# r# t3 ^; H
CAN总线(控制器局域网)参见 pyb.CAN. - from pyb import CAN
& ]. H' \. p6 a4 V7 q
; S$ B L0 q, P9 G- can = CAN(1, CAN.LOOPBACK)
7 Q% q# a/ e( A# Y - can.setfilter(0, CAN.LIST16, 0, (123, 124, 125, 126))
`5 k$ b8 q' X& C - can.send('message!', 123) # send a message with id 123& q8 B3 Q& b$ F
- can.recv(0) # receive message on FIFO 0
复制代码
/ B2 m4 J" e+ A1 {6 A
5 @$ _" a y% r内部加速度计参见pyb.Accel. - from pyb import Accel8 O" q0 R/ K" _3 ^# p: q
- t. s# f. E, s' D; q9 ^& I3 n- accel = Accel()0 n% L, p4 ]+ z
- print(accel.x(), accel.y(), accel.z(), accel.tilt())
复制代码 ( t* S# O1 I' b* }* W
5 y+ P n; s2 J
|