pyboard 的快速参考 以下引脚分配适用于 PYBv1.1。您还可以查看其他版本的 pyboard 的引脚排列: PYBv1.0 或PYBLITEv1.0-AC 或 PYBLITEv1.0.  下面是 pyboard 的快速参考。如果这是您第一次使用该板,请考虑先阅读以下部分: 9 r8 g& \" n, E1 s
通用板控制见 pyb. - import pyb
( L3 h" W; G6 f' [9 o" N
) W$ ?0 q1 z' ^% d" M- R- pyb.repl_uart(pyb.UART(1, 9600)) # duplicate REPL on UART(1)% a O/ a& ~* T+ ^
- pyb.wfi() # pause CPU, waiting for interrupt
3 t) w/ u9 T1 S$ R' @( B/ c2 E1 l9 R6 z - pyb.freq() # get CPU and bus frequencies& Z' l0 j+ a: {: f$ d% U& v
- pyb.freq(60000000) # set CPU freq to 60MHz
! I, f- B% P' d% y - pyb.stop() # stop CPU, waiting for external interrupt
复制代码 ; W# ]& `9 j; O, j2 g
7 i% {1 N/ f6 m7 x: y- T延迟和计时使用 time模块: - import time( o! ?: o8 ?* o4 L* V) N( [# B
- # E6 D) G7 f. ^% r3 b
- time.sleep(1) # sleep for 1 second
2 S5 o& p" g* G7 P7 g - time.sleep_ms(500) # sleep for 500 milliseconds5 q0 v1 h' v+ `8 j# C1 g: i
- time.sleep_us(10) # sleep for 10 microseconds
' _; T) L! ^7 D2 t( n" ? - start = time.ticks_ms() # get value of millisecond counter
2 }" h' ]+ Y! K# p4 k - delta = time.ticks_diff(time.ticks_ms(), start) # compute time difference
复制代码 # k( w' |% L2 S+ \
. M, c Q& {( n, X& C
内部 LED见 pyb.LED. - from pyb import LED1 q: v9 Y- g, n3 R
# s$ V4 U: A# m7 Z- led = LED(1) # 1=red, 2=green, 3=yellow, 4=blue( a& X1 ~" e# m- D9 L0 a9 T7 q" y
- led.toggle()
, e% S" V, T1 P# F7 v; r - led.on()) E n! f" e3 }
- led.off()
7 C+ h. A2 w# L) Z
, [7 @8 x ~% I: F& d) h& y8 }- # LEDs 3 and 4 support PWM intensity (0-255)$ [+ a2 C" x- c( l" w9 ^% r
- LED(4).intensity() # get intensity9 T3 @% d. Z8 C2 a7 t% h7 F
- LED(4).intensity(128) # set intensity to half
复制代码
- G0 u! I1 R/ b* ~. k2 A
$ v- V+ \- j" n, ?4 \$ p内部开关请参阅 pyb.Switch. - from pyb import Switch
/ U/ I$ [- I) ]* ^ - : P/ X3 r: q3 m# O- {4 f
- sw = Switch()
4 A! T* t& ?+ c( D7 H! d( w - sw.value() # returns True or False
. m8 A" }! D# C6 o) S* v* y4 M; i; S - sw.callback(lambda: pyb.LED(1).toggle())
复制代码
" ~/ x% q. E7 z( r
8 d0 U7 `' h- O! L4 D4 P引脚和 GPIO请参阅 pyb.Pin. - from pyb import Pin- n) P! v* [1 U$ m/ l8 w5 S
8 x: n( z. k$ Q$ G- p_out = Pin('X1', Pin.OUT_PP)/ Z# W6 E; T% T- z9 |/ T
- p_out.high()% z6 {% ? `, K/ N( W
- p_out.low()9 z8 E/ _# G/ d" Z |7 o
: q9 L0 S4 |2 _3 G* ]- A2 R- p_in = Pin('X2', Pin.IN, Pin.PULL_UP)
/ D8 h. t; ~6 W) Z/ `2 G - p_in.value() # get value, 0 or 1
复制代码 : \2 |" K/ O7 h9 y7 n7 |
9 t2 C; E* j% }: i. @9 E4 B伺服控制请参阅pyb.Servo. - from pyb import Servo
: q9 V+ a5 K1 R" B* d) E
: D3 i/ }3 H$ {7 m' u! b- s1 = Servo(1) # servo on position 1 (X1, VIN, GND)1 m* U, z4 }, u6 ?! V% {# Y9 {/ J
- s1.angle(45) # move to 45 degrees3 h' n: c* R) O! [) s' x
- s1.angle(-60, 1500) # move to -60 degrees in 1500ms7 U& ~% f- n/ h7 f$ s5 H5 I& _
- s1.speed(50) # for continuous rotation servos
复制代码 , E! {4 ]7 g7 f/ \. w7 T+ G5 _7 w
" X1 z: `! k L) v- v6 E
外部中断请参阅 pyb.ExtInt. - from pyb import Pin, ExtInt
+ s, o' k) d7 Z, r - ' H( k2 W; E* X
- callback = lambda e: print("intr")
' Z) Q0 X+ ^- ~1 P# k2 k9 ~, V - ext = ExtInt(Pin('Y1'), ExtInt.IRQ_RISING, Pin.PULL_NONE, callback)
复制代码 ! P% W( k) i, m- T) i, {4 T# _
. T/ T% Y8 B9 {& S X* L) b
计时器请参阅pyb.Timer. - from pyb import Timer. C) C6 F7 ?' \* i2 R! F
7 i, e: z" g C, [, L$ w- tim = Timer(1, freq=1000)
, A- M6 i& L* O0 | - tim.counter() # get counter value; Y9 h. c% a5 `
- tim.freq(0.5) # 0.5 Hz
+ ~: u' R" Q/ V9 v - tim.callback(lambda t: pyb.LED(1).toggle())
复制代码 ' j. \ B. H+ Y( |
RTC(实时时钟)请参阅 pyb.RTC - from pyb import RTC
+ ?4 C% i* X: _: d9 F# } - . ^' e; W" R4 G- W
- rtc = RTC(); N5 @" z5 X6 J8 q; B
- rtc.datetime((2017, 8, 23, 1, 12, 48, 0, 0)) # set a specific date and time
* L4 F9 o2 ^ T, {0 i. o2 G) K - rtc.datetime() # get date and time
复制代码 " w2 x5 r2 t" L* a( o- e7 `$ I
PWM(脉宽调制)请参阅pyb.Pin 和 pyb.Timer. - from pyb import Pin, Timer
( h+ S, z/ n0 `7 q1 x2 `
" G' j( _+ L; F$ m$ M+ g- p = Pin('X1') # X1 has TIM2, CH1
+ Y( U/ _3 F3 A5 U - tim = Timer(2, freq=1000). X/ y5 l3 n: N2 |- n5 ^0 x
- ch = tim.channel(1, Timer.PWM, pin=p)
3 ?) R3 }: D6 v5 } - ch.pulse_width_percent(50)
复制代码
/ d% l4 k0 t2 q! a6 l! kADC(模数转换)请参阅pyb.Pin 和pyb.ADC. - from pyb import Pin, ADC
: e3 Q$ A+ Q# A5 q- H3 x, o! z
6 o/ T: v7 R. `7 h6 u, i& y- adc = ADC(Pin('X19'))
) a# }% B& F/ r - adc.read() # read value, 0-4095
复制代码 - a8 K- ~2 C: Z: R% w* p9 ^9 m: e0 b
1 y7 ^5 l7 F- b+ aDAC(数模转换)请参阅pyb.Pin 和 pyb.DAC. - from pyb import Pin, DAC0 V. e% g W6 x$ K; ^( U4 y
4 i- C2 D' _8 l- e- dac = DAC(Pin('X5'))
3 l5 v7 I6 t" Z3 V - dac.write(120) # output between 0 and 255
复制代码
% I. {0 K% U! c0 }1 R1 G
4 e8 M. }& w* A2 g8 UUART(串行总线)请参阅 pyb.UART. - from pyb import UART2 W* c, m. H% B/ r8 L3 d8 P0 j
8 w# F. S- s. J5 Z6 W! D8 e% J- Z- uart = UART(1, 9600)
1 y: |0 ^8 O5 X: `6 W& s - uart.write('hello')9 \ ]3 F/ [# @0 t, r
- uart.read(5) # read up to 5 bytes
复制代码
. U$ N M z5 I% E7 t$ { p
+ P' T8 u1 a# r# X0 ?SPI总线请参阅 pyb.SPI. - from pyb import SPI
1 C* [5 M' I; x% Q# E% _ - W( A0 b, s+ d6 d- o. E' m! [% k
- spi = SPI(1, SPI.MASTER, baudrate=200000, polarity=1, phase=0) o( e* W* L8 [2 p+ v
- spi.send('hello')
W/ S) M/ b, n0 w" e7 e c1 Q - spi.recv(5) # receive 5 bytes on the bus& y0 z1 O; E' {0 ]
- spi.send_recv('hello') # send and receive 5 bytes
复制代码 ! a9 G3 s3 I2 y3 V
$ s9 O) \9 W( P" t1 eI2C总线硬件 I2C 可通过I2C('X') 和用于 pyboard 的 X 和 Y 两半 I2C('Y')。或者,传入外围设备的整数标识符,例如I2C(1)。通过显式指定 scl 和 sda 引脚而不是总线名称,也可以使用软件 I2C 。有关更多详细信息,请参阅 machine.I2C. - from machine import I2C
- f* M7 G2 x+ [, r$ Q
4 I) r/ S; q) s4 Q4 B+ a4 V- i2c = I2C('X', freq=400000) # create hardware I2c object
( ^6 x3 h4 N: b8 }6 j! j$ x - i2c = I2C(scl='X1', sda='X2', freq=100000) # create software I2C object
5 ]: N3 C3 t) z0 B, ^ - , q2 p) K8 F6 D% U
- i2c.scan() # returns list of slave addresses
; \1 E0 s3 v- {1 R - i2c.writeto(0x42, 'hello') # write 5 bytes to slave with address 0x42
( x9 b' s ^5 n, Q& k/ b$ Z - i2c.readfrom(0x42, 5) # read 5 bytes from slave
2 L: n. B4 e" f
" n) T. n) G) H( q8 N# b5 J6 \' \- i2c.readfrom_mem(0x42, 0x10, 2) # read 2 bytes from slave 0x42, slave memory 0x10# T: l) @' T# B5 p! @9 S% p
- i2c.writeto_mem(0x42, 0x10, 'xy') # write 2 bytes to slave 0x42, slave memory 0x10
复制代码
: f3 F" r6 ?7 h+ v* d+ X, \: }6 D3 g* b8 d
注意:对于传统 I2C 支持,请参阅 pyb.I2C。 . N. u: O8 s& n& h
I2S总线参见 machine.I2S. - from machine import I2S, Pin' v7 {1 W8 h! Z/ c% y, t$ [
& k9 a# |0 _8 o+ I! 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
9 m3 I7 h& b7 y# F2 o" n& J - i2s.write(buf) # write buffer of audio samples to I2S device
+ z, b3 b& t" r2 N8 i1 o - / c( a# K/ e d8 o5 r3 U
- 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
# M6 C# O- n3 q9 A/ \ - i2s.readinto(buf) # fill buffer with audio samples from I2S device
复制代码
% y# l/ {1 D% ^I2S 类目前作为技术预览版提供。在预览期间,鼓励用户提供反馈。基于此反馈,I2S 类 API 和实现可能会更改。 PYBv1.0/v1.1 有一个 I2S 总线,id=2。PYBD-SFxW 有两条 I2S 总线,id=1 和 id=2。I2S 与 SPI 共享。
/ v# h9 C j. f3 g' `. V: M# A# h- UCAN总线(控制器局域网)参见 pyb.CAN. - from pyb import CAN, `: ]- L# I$ K2 I: z* s
- : L8 n/ j. a( |7 \: W
- can = CAN(1, CAN.LOOPBACK)
. i! M% }8 [; ]0 j - can.setfilter(0, CAN.LIST16, 0, (123, 124, 125, 126))
9 q2 O0 d8 [: ?8 R9 Y - can.send('message!', 123) # send a message with id 123
6 X; x( H: A+ m$ j1 E7 `! u - can.recv(0) # receive message on FIFO 0
复制代码 # S& R+ A( c- x& j$ V. Z- Y# x
% p1 ]' C; {' e# ]
内部加速度计参见pyb.Accel. - from pyb import Accel- w. J1 q% L4 L J
- 9 _2 P. ^8 r! E6 `2 E. B k
- accel = Accel()
7 R" Z! U* ?: P/ S3 d - print(accel.x(), accel.y(), accel.z(), accel.tilt())
复制代码 + ]% }1 H7 Q4 L% Y5 Z- u3 \- @
5 i$ u8 N. N, c- Z
|