pyboard 的快速参考 以下引脚分配适用于 PYBv1.1。您还可以查看其他版本的 pyboard 的引脚排列: PYBv1.0 或PYBLITEv1.0-AC 或 PYBLITEv1.0.  下面是 pyboard 的快速参考。如果这是您第一次使用该板,请考虑先阅读以下部分: 5 F* P! X+ W o5 @# M
通用板控制见 pyb. - import pyb' `: ]6 B3 x# C6 Y
- ) b5 t6 D/ U. \7 ]) r5 G( B
- pyb.repl_uart(pyb.UART(1, 9600)) # duplicate REPL on UART(1)
2 ~4 ^9 H- M& J, X, j- g* ~ - pyb.wfi() # pause CPU, waiting for interrupt
6 D; S5 {% o' d5 ?" y - pyb.freq() # get CPU and bus frequencies9 }0 c8 m' A% C3 u: x
- pyb.freq(60000000) # set CPU freq to 60MHz
6 |% ?" K; E9 X' d9 Y8 V1 k1 R8 I - pyb.stop() # stop CPU, waiting for external interrupt
复制代码
; l2 T4 D# U) T* b3 Q/ e$ t) ^3 T' J" f, J$ f. t5 k( b
延迟和计时使用 time模块: - import time# Q2 b$ n4 P, s1 K5 p; z
- . }( n# d2 O' q8 F6 P
- time.sleep(1) # sleep for 1 second
1 a! j g* w# U) | @ - time.sleep_ms(500) # sleep for 500 milliseconds
& h; L3 O7 v$ A9 F( W, X7 Y3 A9 R - time.sleep_us(10) # sleep for 10 microseconds* q# ?' N6 L# O, }
- start = time.ticks_ms() # get value of millisecond counter
# M6 v9 _6 M2 ?7 v; N4 w - delta = time.ticks_diff(time.ticks_ms(), start) # compute time difference
复制代码 1 I: C9 y( y" h
9 w* J- ~* U3 \内部 LED见 pyb.LED. - from pyb import LED
- d+ R2 G% a- z& g4 G' x8 E0 x; d1 F
) C. b: ~/ z) [" r9 e- led = LED(1) # 1=red, 2=green, 3=yellow, 4=blue: G0 U7 v5 h# l2 h' z) }
- led.toggle()
: d9 H; n1 `- I% O - led.on(): B7 {5 }/ j" w
- led.off()8 d. n2 X1 ]. i. U# B
- & u7 k1 G' i& b9 D# t$ }7 i$ Q0 \
- # LEDs 3 and 4 support PWM intensity (0-255)
8 t$ g- O: V/ u1 m- ~ - LED(4).intensity() # get intensity
; ~. ^5 a. j% S. U - LED(4).intensity(128) # set intensity to half
复制代码 7 T' h5 j/ X& w3 |
. K5 N F9 h2 s- ?: p% k& q, I& w
内部开关请参阅 pyb.Switch. - from pyb import Switch# T; J0 g5 @" h% i1 }
- % U, a+ G6 I* g& o! _0 @2 A- s+ k
- sw = Switch()! f$ E# Q, M) _# V1 y
- sw.value() # returns True or False: L8 K% X: ]) V
- sw.callback(lambda: pyb.LED(1).toggle())
复制代码 - L4 h7 |- ^# F$ A4 m: I6 f& K
( a8 u" |3 X5 B/ K
引脚和 GPIO请参阅 pyb.Pin. - from pyb import Pin4 X& j9 @$ X b( @7 c
9 q/ M* M3 F- X$ D1 f( t- p_out = Pin('X1', Pin.OUT_PP)
0 ]+ f: `8 h+ T+ C$ c U - p_out.high()
9 H4 `7 K& ]( {- P, i - p_out.low()1 ^1 D2 w( ^( _7 ?; H, _
- 0 T* l \7 h& f; c' P" s
- p_in = Pin('X2', Pin.IN, Pin.PULL_UP)
2 i: D0 |) S1 J' H: W5 i$ p - p_in.value() # get value, 0 or 1
复制代码
, w% M& o; e n; j O
2 R3 k& L2 d- D5 i; G伺服控制请参阅pyb.Servo. - from pyb import Servo
. }3 Y& Q% E3 l
! Y/ E9 L6 O* ^+ [: b- s1 = Servo(1) # servo on position 1 (X1, VIN, GND): p$ u9 H' c. ~4 e2 X x C5 _
- s1.angle(45) # move to 45 degrees% d0 }2 o: U3 ]% Z+ A
- s1.angle(-60, 1500) # move to -60 degrees in 1500ms
: M1 }: x$ [& d5 I. z9 Y u) ^ - s1.speed(50) # for continuous rotation servos
复制代码
: S0 |. n% r1 m; [
6 C! n6 @# R$ L0 x7 U6 T3 _6 S+ g0 V$ j) J外部中断请参阅 pyb.ExtInt. - from pyb import Pin, ExtInt3 c8 c# l; a5 V8 v) `
- $ e( J% h, L: Q" ]
- callback = lambda e: print("intr")+ l9 Y( n! L: M1 Q7 z0 t1 z" n
- ext = ExtInt(Pin('Y1'), ExtInt.IRQ_RISING, Pin.PULL_NONE, callback)
复制代码
6 ?8 u& w( v( a+ R6 L5 h
# @ N6 f' s. s X- s/ z3 V* `计时器请参阅pyb.Timer. - from pyb import Timer9 E* Q/ K3 s5 P; b2 q) ] w% u
- 3 N+ C1 L' o h0 E8 d
- tim = Timer(1, freq=1000)
1 j! w& a! n6 ?7 S - tim.counter() # get counter value
% z$ |$ I2 e& t - tim.freq(0.5) # 0.5 Hz: A) c& b& g; V$ k* x1 E% q- `9 @' E
- tim.callback(lambda t: pyb.LED(1).toggle())
复制代码 + G6 R, m: [: T
RTC(实时时钟)请参阅 pyb.RTC - from pyb import RTC+ K o; F- e2 i$ s( b
- 7 ]# q+ {' W9 \; a
- rtc = RTC()
5 ~, q& T$ d, @( x% i - rtc.datetime((2017, 8, 23, 1, 12, 48, 0, 0)) # set a specific date and time
# T+ c# ~( ^: n - rtc.datetime() # get date and time
复制代码 2 Y8 B5 Z5 S4 _: \* t; a$ G ?
PWM(脉宽调制)请参阅pyb.Pin 和 pyb.Timer. - from pyb import Pin, Timer, a* _# k0 L2 V1 U/ b2 W
; Z6 i2 T# x7 }7 E- p = Pin('X1') # X1 has TIM2, CH1
! u9 `; y( a3 u - tim = Timer(2, freq=1000)4 N) x/ J4 ?2 q
- ch = tim.channel(1, Timer.PWM, pin=p), I, B, ~& {2 @- z) N1 t8 G& s
- ch.pulse_width_percent(50)
复制代码
" a1 n( s& D4 b# R$ C) L, JADC(模数转换)请参阅pyb.Pin 和pyb.ADC. - from pyb import Pin, ADC
% y) q) ~( A$ F7 }
6 ]; q! X5 n. X2 N. y$ Z- adc = ADC(Pin('X19')): t6 z3 R: o; ?$ j7 x
- adc.read() # read value, 0-4095
复制代码 % p( ?! R9 O, |5 a w
' a# X8 ]" M% P8 `
DAC(数模转换)请参阅pyb.Pin 和 pyb.DAC. - from pyb import Pin, DAC
/ z9 D9 Z5 Q: V, ^5 ? - # N* g# f m" O3 o
- dac = DAC(Pin('X5'))" U+ r6 q1 x2 {1 P0 ^
- dac.write(120) # output between 0 and 255
复制代码 7 t1 ]. x# B1 Q6 |3 q. V, N5 e
& o" \& n4 r2 |, i- Q% N) `
UART(串行总线)请参阅 pyb.UART. - from pyb import UART
. f9 n' I9 Q. Q+ I! H
' m0 E# Q3 I7 Q O h1 R) c; j- uart = UART(1, 9600)6 v0 B R" P% k# [' Q7 y4 X" `
- uart.write('hello')
- u6 ^0 j1 l2 J% r: W2 i$ S - uart.read(5) # read up to 5 bytes
复制代码 5 M' Z8 M: u% P2 J6 w+ R O
& ?' ?" y$ O }" P0 A+ @: u, v* B2 DSPI总线请参阅 pyb.SPI. - from pyb import SPI
7 W5 R6 }& z* H o6 _, o
- p" v6 N8 D( ^! g/ U) m3 N8 r, w8 O- spi = SPI(1, SPI.MASTER, baudrate=200000, polarity=1, phase=0)
& z. C1 g6 Z, t2 B - spi.send('hello')
/ J+ {6 W1 j0 _. N2 B4 n/ B9 N9 ~ F) j - spi.recv(5) # receive 5 bytes on the bus
6 C, t v& q P; J, }2 f - spi.send_recv('hello') # send and receive 5 bytes
复制代码 i+ O/ f R! l, _/ S- s8 l2 p
o# u, m/ }& L, B8 ~, m% X
I2C总线硬件 I2C 可通过I2C('X') 和用于 pyboard 的 X 和 Y 两半 I2C('Y')。或者,传入外围设备的整数标识符,例如I2C(1)。通过显式指定 scl 和 sda 引脚而不是总线名称,也可以使用软件 I2C 。有关更多详细信息,请参阅 machine.I2C. - from machine import I2C
# M" y! e4 Z9 `0 g5 A3 L/ [$ V - ; g! _6 A! I% ^7 O
- i2c = I2C('X', freq=400000) # create hardware I2c object) m% F4 Y+ t2 h& Y2 ~
- i2c = I2C(scl='X1', sda='X2', freq=100000) # create software I2C object3 F- l" k( ~8 K+ [
- 2 h) y1 b* P% V, r: `& ]
- i2c.scan() # returns list of slave addresses+ ?6 u* h+ u( \7 w8 f" U) T
- i2c.writeto(0x42, 'hello') # write 5 bytes to slave with address 0x42
$ {, p" S1 w0 q+ N" Y' K; l - i2c.readfrom(0x42, 5) # read 5 bytes from slave& r" ?8 q6 L$ Z. n; b
- + t0 {) ?( w0 u9 v
- i2c.readfrom_mem(0x42, 0x10, 2) # read 2 bytes from slave 0x42, slave memory 0x101 v% W2 f! k3 T U
- i2c.writeto_mem(0x42, 0x10, 'xy') # write 2 bytes to slave 0x42, slave memory 0x10
复制代码
8 ]" J+ ?, ~: _* K0 z
4 g, ~0 a6 S+ a) g( w2 a注意:对于传统 I2C 支持,请参阅 pyb.I2C。
& N" K4 W& x' c, yI2S总线参见 machine.I2S. - from machine import I2S, Pin ~6 X. Y1 s% L6 I
% q, z! I' |% \! E1 }: ]6 l6 g- 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: V2 a' @# d( @# M' K8 p5 Q. g
- i2s.write(buf) # write buffer of audio samples to I2S device
9 ?( Z! i0 v8 |1 C7 H7 s, @% `! b
" [. K9 Q$ r' s- X7 t7 B" Z! q' h, L- 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
/ g/ R8 q" ~# _, [ - i2s.readinto(buf) # fill buffer with audio samples from I2S device
复制代码 : J, Z- j4 q: S; \' u9 g
I2S 类目前作为技术预览版提供。在预览期间,鼓励用户提供反馈。基于此反馈,I2S 类 API 和实现可能会更改。 PYBv1.0/v1.1 有一个 I2S 总线,id=2。PYBD-SFxW 有两条 I2S 总线,id=1 和 id=2。I2S 与 SPI 共享。
; |3 u, O1 t' i& }8 n; lCAN总线(控制器局域网)参见 pyb.CAN. - from pyb import CAN
+ J1 ?8 W1 q% _7 S" v; v" W- t9 Z7 u - , I# _% N1 k1 F" P4 s b: ?
- can = CAN(1, CAN.LOOPBACK)
9 A( i, ?( S. I9 |2 s+ U! R' e - can.setfilter(0, CAN.LIST16, 0, (123, 124, 125, 126))+ e" V/ i ^2 {
- can.send('message!', 123) # send a message with id 123( r, G0 ~) z& R& P; o
- can.recv(0) # receive message on FIFO 0
复制代码 B: e0 X$ o0 F7 i b
2 U/ j$ _& ?0 A2 [
内部加速度计参见pyb.Accel. - from pyb import Accel
0 A7 W: m; r( f4 N" R, v
( f7 J; x1 M- y9 e4 o' t5 y- accel = Accel()1 E7 Z7 d5 J+ F
- print(accel.x(), accel.y(), accel.z(), accel.tilt())
复制代码 : y. V7 p: O8 P1 @& S
' e) `* [ U5 \4 R) }2 L
|