pyboard 的快速参考 以下引脚分配适用于 PYBv1.1。您还可以查看其他版本的 pyboard 的引脚排列: PYBv1.0 或PYBLITEv1.0-AC 或 PYBLITEv1.0.  下面是 pyboard 的快速参考。如果这是您第一次使用该板,请考虑先阅读以下部分: 3 {9 y G# Z. ?9 c; d# I
通用板控制见 pyb. - import pyb
r" r6 q; C9 \. J
1 C$ p3 _0 `. u+ C' H7 t, F( ~ m- pyb.repl_uart(pyb.UART(1, 9600)) # duplicate REPL on UART(1)
1 L b* a& R# z. C+ Z - pyb.wfi() # pause CPU, waiting for interrupt
1 j: U4 l G& w% G: V - pyb.freq() # get CPU and bus frequencies
; j+ _6 C# R ^* y5 G2 w - pyb.freq(60000000) # set CPU freq to 60MHz3 Y% H) i4 u# Z# y& H! B' F9 n
- pyb.stop() # stop CPU, waiting for external interrupt
复制代码
% H' X8 z9 x3 j4 w: J
- Z$ d: ` x- Y' l! }延迟和计时使用 time模块: - import time0 z# g* V# K# F
6 {( [4 b; W4 B5 h- time.sleep(1) # sleep for 1 second
+ d5 c S# a# e& H - time.sleep_ms(500) # sleep for 500 milliseconds
, I% s2 G' F" c8 K1 ` - time.sleep_us(10) # sleep for 10 microseconds
9 Z( A$ j- H" }% N8 D+ D* }+ w$ { - start = time.ticks_ms() # get value of millisecond counter" Y4 x# Q- i* `# G, R7 r! S
- delta = time.ticks_diff(time.ticks_ms(), start) # compute time difference
复制代码 3 S! r# H& U" Q1 C2 E6 K0 ~( n+ K
" ]+ r6 e4 K4 E+ B( h( p
内部 LED见 pyb.LED. - from pyb import LED
6 z# L) s7 \, D; }! o - & O6 V2 R% j |' s4 O9 O% p& T& r
- led = LED(1) # 1=red, 2=green, 3=yellow, 4=blue# @ D. W, c2 [4 F7 a& M% ]
- led.toggle()
* E8 x C8 O! t, g6 ` - led.on()
& X9 K4 l1 g7 F - led.off()( P) ?* I6 ^* \1 Z
8 H1 M5 K/ V/ B) P- # LEDs 3 and 4 support PWM intensity (0-255)
F5 @3 M% F, D - LED(4).intensity() # get intensity/ U" G$ u: R6 l9 V! ]6 \9 E6 }
- LED(4).intensity(128) # set intensity to half
复制代码
' I8 s- z' X4 k& [9 V
+ Y# f. I& t, C* R7 E内部开关请参阅 pyb.Switch. - from pyb import Switch' ^9 A G+ F0 x! g8 [
- 9 N$ D2 f1 e: I2 R0 t9 x n
- sw = Switch()6 w9 a0 F) {: L( r
- sw.value() # returns True or False
- T: m' N# `; Z# M+ H - sw.callback(lambda: pyb.LED(1).toggle())
复制代码
' h. i4 R9 b' v0 S4 F/ e5 a5 |, Q3 g7 U$ h- h) ~
引脚和 GPIO请参阅 pyb.Pin. - from pyb import Pin0 o* b* L& i6 e5 ?. k K) J
* k3 {3 s$ ?7 u# h' G ?) r! V" m- p_out = Pin('X1', Pin.OUT_PP)+ Z9 h5 L& t7 w5 \% X" a- T1 T# b
- p_out.high()
1 g& i. w; D7 x3 r# l - p_out.low()1 W7 X/ J. M, ~1 h2 @
* f0 E( |1 G6 K6 [- p_in = Pin('X2', Pin.IN, Pin.PULL_UP)
! G1 p% G5 T. I- ?" w - p_in.value() # get value, 0 or 1
复制代码
( h c% ^+ V" w" X7 @4 K* X/ {( Z5 I3 ^& e2 B2 O
伺服控制请参阅pyb.Servo. - from pyb import Servo
( H' z$ `& ^8 z @
+ L/ |7 N% O$ G" ` G, C- s1 = Servo(1) # servo on position 1 (X1, VIN, GND)9 c0 {$ X6 h; {' \1 X; n9 [
- s1.angle(45) # move to 45 degrees! ~7 O; s: S; I) g; H" r
- s1.angle(-60, 1500) # move to -60 degrees in 1500ms ]/ a2 E, k( D* }# d; e% E
- s1.speed(50) # for continuous rotation servos
复制代码 + T# A1 G! ^& p7 z5 h0 ?. Q/ W
7 x% {! y `: A7 p6 e) p* E外部中断请参阅 pyb.ExtInt. - from pyb import Pin, ExtInt2 z6 Z U, {4 l7 B0 d, r* y$ J V$ R" \
: x( t# |0 @$ b- C9 z) I- callback = lambda e: print("intr")
; Z4 _( L4 j0 L4 E# c ]$ ? - ext = ExtInt(Pin('Y1'), ExtInt.IRQ_RISING, Pin.PULL_NONE, callback)
复制代码
8 p1 K! w' c) B1 \% Q! U
) S1 B4 l' C# ~计时器请参阅pyb.Timer. - from pyb import Timer. U, H8 r; R' y8 C6 r
- 3 U, b3 \0 V# H: X3 x6 o4 D; ~; j
- tim = Timer(1, freq=1000)
g& }& w' x$ b/ Q* x( }6 L - tim.counter() # get counter value
* z3 v- z3 e; m0 Q - tim.freq(0.5) # 0.5 Hz5 z9 u. i; w0 `% |8 ?4 Y1 y
- tim.callback(lambda t: pyb.LED(1).toggle())
复制代码
Y h6 Z8 T, B$ @/ T$ {RTC(实时时钟)请参阅 pyb.RTC - from pyb import RTC
' K) g/ m+ z( e6 T* v - w: i$ j9 _% G* r! m
- rtc = RTC(): Y8 t. J' T7 ~ |
- rtc.datetime((2017, 8, 23, 1, 12, 48, 0, 0)) # set a specific date and time2 M3 W, U* M& g- h
- rtc.datetime() # get date and time
复制代码
. U- a1 z, K* O7 a3 W" B D! U9 t JPWM(脉宽调制)请参阅pyb.Pin 和 pyb.Timer. - from pyb import Pin, Timer
3 l6 ^" x, J" a6 G7 [1 N, X - " _# [3 a4 p& P) m
- p = Pin('X1') # X1 has TIM2, CH1
' C$ H% G7 f8 o: P9 I - tim = Timer(2, freq=1000)2 l9 q5 r, ^, H V8 u5 Q. t
- ch = tim.channel(1, Timer.PWM, pin=p)
3 H7 Z3 x+ U: f9 ^8 p$ h% H - ch.pulse_width_percent(50)
复制代码 / C$ k" B, s) p# C" y# |7 Y
ADC(模数转换)请参阅pyb.Pin 和pyb.ADC. - from pyb import Pin, ADC
$ F( `2 Y- z* c* r
, U& ]# z: K& Y9 }. v1 R- adc = ADC(Pin('X19'))
# X) _& h# j; u: o) [8 t - adc.read() # read value, 0-4095
复制代码 + f, m5 a% B$ N5 r7 }
6 b) K& I9 `. X" Y% y0 K, q4 F' J* ^DAC(数模转换)请参阅pyb.Pin 和 pyb.DAC. - from pyb import Pin, DAC$ O* b- T0 q: {) L. D3 k6 ]
1 K3 Z3 P8 h X+ m$ I! `) N) v- dac = DAC(Pin('X5'))& W: o6 O2 Y3 B" H/ @9 ^" a O$ X/ a
- dac.write(120) # output between 0 and 255
复制代码
" |4 x* @1 } }4 R$ d
7 J5 R n+ L! _2 g9 QUART(串行总线)请参阅 pyb.UART. - from pyb import UART4 ~: f+ J+ q' {$ r' ^
- 8 k: q/ D- ~7 M$ T2 |) R& u8 X
- uart = UART(1, 9600)* n. ]9 \9 i! @3 h1 T0 V% u
- uart.write('hello')4 y4 n% u* M5 ]* K. p) @
- uart.read(5) # read up to 5 bytes
复制代码
" a Z! C5 V4 S/ O2 A. ?' D
. }& R) d) v. _! G6 e4 \# JSPI总线请参阅 pyb.SPI. - from pyb import SPI
" ~5 q* q6 G% g
8 y8 `! y# z- K! x( ?* G2 {! q- spi = SPI(1, SPI.MASTER, baudrate=200000, polarity=1, phase=0)# r% s1 ?9 h' s8 [5 g+ ^/ y4 F
- spi.send('hello')
2 V2 O5 d4 b# H4 U - spi.recv(5) # receive 5 bytes on the bus3 }4 L$ E9 a0 O k: j
- spi.send_recv('hello') # send and receive 5 bytes
复制代码
/ k8 d5 ]/ @; c" Y3 [1 Q) N6 K3 u( r" L( | s6 M
I2C总线硬件 I2C 可通过I2C('X') 和用于 pyboard 的 X 和 Y 两半 I2C('Y')。或者,传入外围设备的整数标识符,例如I2C(1)。通过显式指定 scl 和 sda 引脚而不是总线名称,也可以使用软件 I2C 。有关更多详细信息,请参阅 machine.I2C. - from machine import I2C( [- g0 B. B0 J# @+ G5 N; j3 @
- 7 w1 H( y2 b$ J$ ^8 h
- i2c = I2C('X', freq=400000) # create hardware I2c object0 q: G. q; f& N+ \
- i2c = I2C(scl='X1', sda='X2', freq=100000) # create software I2C object- v D' q. @) z; ?/ k# u: k+ R2 [$ q4 {
- ' ?* o! n* w, A+ }1 {( L0 K( x0 M
- i2c.scan() # returns list of slave addresses Q& O4 d! A, r* \ x5 U6 O( Z+ y
- i2c.writeto(0x42, 'hello') # write 5 bytes to slave with address 0x42
1 R7 M* i4 z% P' x - i2c.readfrom(0x42, 5) # read 5 bytes from slave
' i9 ]8 k3 c n9 m" h - 3 C) B- m1 Z x: ~" n
- i2c.readfrom_mem(0x42, 0x10, 2) # read 2 bytes from slave 0x42, slave memory 0x10
0 V( y' y& D8 J: I1 v - i2c.writeto_mem(0x42, 0x10, 'xy') # write 2 bytes to slave 0x42, slave memory 0x10
复制代码 * S: u% a# x% G- {( Y! K. _! {$ _
( `# x W2 P! u) h注意:对于传统 I2C 支持,请参阅 pyb.I2C。
& G0 @% W$ }0 } @% f {( RI2S总线参见 machine.I2S. - from machine import I2S, Pin
9 u. Z7 ~& j) {1 ]! [
' i# d4 Q2 F$ j, f- 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
/ `! j8 v! N. Y. r' `% J. W& w - i2s.write(buf) # write buffer of audio samples to I2S device" S. v8 t/ b0 A8 f5 Q9 B
+ O5 k- C- |: R# q# L2 {) x- 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
' W5 {) B7 N; z e U7 x - i2s.readinto(buf) # fill buffer with audio samples from I2S device
复制代码 & f/ j) S% Y$ z
I2S 类目前作为技术预览版提供。在预览期间,鼓励用户提供反馈。基于此反馈,I2S 类 API 和实现可能会更改。 PYBv1.0/v1.1 有一个 I2S 总线,id=2。PYBD-SFxW 有两条 I2S 总线,id=1 和 id=2。I2S 与 SPI 共享。 8 H& J" ^' e' L* S! b) Z
CAN总线(控制器局域网)参见 pyb.CAN. - from pyb import CAN; G! f1 }- U0 E1 ?/ I" L. v/ A, ]/ I
- ( |9 e6 S' c8 O9 p& |: M
- can = CAN(1, CAN.LOOPBACK)
* m4 c# } m0 X - can.setfilter(0, CAN.LIST16, 0, (123, 124, 125, 126))( O/ ]5 u ^& B+ |. I2 z1 C
- can.send('message!', 123) # send a message with id 1237 J' z4 G" F/ [7 x& z. N4 T
- can.recv(0) # receive message on FIFO 0
复制代码
8 A: W" I1 N( ^" r8 D. j$ C* r, C" T; e3 V1 x* x/ T- p1 a4 b
内部加速度计参见pyb.Accel. - from pyb import Accel
0 o! ~! v, |! }0 V; g# T0 W
+ m2 G; C" p: {$ S! l6 o- accel = Accel()" R6 V9 A0 @* O6 @( j% g2 D/ }
- print(accel.x(), accel.y(), accel.z(), accel.tilt())
复制代码
+ L# w, a2 K6 y* r1 B& f- j6 k* u
: ] w: j0 z* P6 d7 W9 ^$ U% X |