pyboard 的快速参考 以下引脚分配适用于 PYBv1.1。您还可以查看其他版本的 pyboard 的引脚排列: PYBv1.0 或PYBLITEv1.0-AC 或 PYBLITEv1.0. 下面是 pyboard 的快速参考。如果这是您第一次使用该板,请考虑先阅读以下部分:
8 u P& N/ }4 O* n8 M2 T$ j通用板控制见 pyb. - import pyb6 u9 A9 O0 _& ]4 |: _
- " w2 i( F4 M- P7 S ]; T( h
- pyb.repl_uart(pyb.UART(1, 9600)) # duplicate REPL on UART(1); o" ]( |1 d; q6 [# d5 F1 C8 J, {
- pyb.wfi() # pause CPU, waiting for interrupt
* I1 W3 V" o9 d) m+ A9 D - pyb.freq() # get CPU and bus frequencies
2 n6 m6 A9 @5 w - pyb.freq(60000000) # set CPU freq to 60MHz8 B$ _- E- Z1 }3 e& N
- pyb.stop() # stop CPU, waiting for external interrupt
复制代码 8 Z6 V6 G7 u# k8 X7 L
5 [' f. i. y$ M; S
延迟和计时使用 time模块: - import time8 f: y/ ?3 o- j" m, R6 A8 n5 a1 I
- - p# G9 l( b; w. ?2 |" N5 j2 Y4 l
- time.sleep(1) # sleep for 1 second/ i( ?3 V. c3 x& A8 q
- time.sleep_ms(500) # sleep for 500 milliseconds
s3 P5 \, i8 X* R& g. g - time.sleep_us(10) # sleep for 10 microseconds8 k0 C2 |2 r& f' J: @0 d3 k
- start = time.ticks_ms() # get value of millisecond counter
8 x/ L+ p; e2 m - delta = time.ticks_diff(time.ticks_ms(), start) # compute time difference
复制代码 ) |5 A q& g6 J6 x0 G
# i' x2 |( K' a& V! m内部 LED见 pyb.LED. - from pyb import LED
0 I% P _4 x& c$ ^" q% G
! D! d7 C. F( O, S; s- led = LED(1) # 1=red, 2=green, 3=yellow, 4=blue
^! \3 ]% Q) K - led.toggle()7 t2 ~# a% H; `" m; `' X3 W O
- led.on()! D7 h, v. Z3 q: y. w( a
- led.off()
$ v. A# l. Z, }
% K+ H1 Q; C( T! r# @7 D. R- # LEDs 3 and 4 support PWM intensity (0-255)
3 E* \! P* x0 E0 [0 \+ c - LED(4).intensity() # get intensity
- B- r7 A: u+ L) a1 O - LED(4).intensity(128) # set intensity to half
复制代码 & e t8 v* u4 ]/ |0 n4 w) {
+ z, v+ @/ U6 E' k9 g3 m0 ~内部开关请参阅 pyb.Switch. - from pyb import Switch
- i% a$ [/ {( ^ - 1 ? _ E1 V2 C8 l( B4 S9 U
- sw = Switch()
$ E' D! |5 M6 {- b: C- k( U* G - sw.value() # returns True or False* T5 u. P6 Y' l# A& ^' I
- sw.callback(lambda: pyb.LED(1).toggle())
复制代码 % K" U" `. c' z) ~8 X0 H
" ]9 b' R5 n @0 m& t
引脚和 GPIO请参阅 pyb.Pin. - from pyb import Pin3 P% K! ]9 ~" v) D! E7 S8 J. r
+ x) w7 |' B( E" L6 Y- p_out = Pin('X1', Pin.OUT_PP)1 `% v$ |7 ?7 ^
- p_out.high()4 h1 Y3 k* ~) ^6 z+ M. j
- p_out.low()% w! T2 |. `' c8 E
- ; `' O! ] ^ `5 w+ w
- p_in = Pin('X2', Pin.IN, Pin.PULL_UP)
( m! ?8 i, ^3 t, m6 g! z% L - p_in.value() # get value, 0 or 1
复制代码
4 M0 Q5 @, n" ?- ~, F! D% A! z N8 M' h- c3 \
伺服控制请参阅pyb.Servo. - from pyb import Servo
( j' N1 d7 t0 Q! r5 b! `+ i' a - 0 I$ r$ z4 r8 {8 B
- s1 = Servo(1) # servo on position 1 (X1, VIN, GND)9 ]4 m+ B6 ]" |, @1 H2 r' O3 {
- s1.angle(45) # move to 45 degrees
' r4 W+ z2 ]( C) z& W' S4 q - s1.angle(-60, 1500) # move to -60 degrees in 1500ms8 b2 N/ e1 x! Y0 P1 h% C
- s1.speed(50) # for continuous rotation servos
复制代码 9 p7 M z; c' A$ n: v& |5 N! x
; Y" Z% Y6 H- K: I2 z l, F外部中断请参阅 pyb.ExtInt. - from pyb import Pin, ExtInt
1 Z( n8 U' f% f6 _; U; K6 l
: N5 J4 c# T2 H7 S$ U2 {3 s5 Q- callback = lambda e: print("intr")
* Y: `5 ^- a5 D; u8 M5 Q" \& k/ } - ext = ExtInt(Pin('Y1'), ExtInt.IRQ_RISING, Pin.PULL_NONE, callback)
复制代码
9 p6 ~! e: d: `# g1 {/ B
* s$ F! O6 m/ I- d* U) q7 e计时器请参阅pyb.Timer. - from pyb import Timer# q8 N. P' s- N- g: K8 D7 t
8 k% f/ g" C7 z" P# p$ L; V- tim = Timer(1, freq=1000)9 d9 U( v* g3 L6 ?
- tim.counter() # get counter value. A% O- T$ y/ O% `
- tim.freq(0.5) # 0.5 Hz
9 B! I0 P1 T4 U8 y o7 Z8 D7 W - tim.callback(lambda t: pyb.LED(1).toggle())
复制代码 ! N, f0 ?2 Z3 Q, z
RTC(实时时钟)请参阅 pyb.RTC - from pyb import RTC
' F3 G% i& L( k* I' g8 t
: l c D- Z* U; @" v8 U- rtc = RTC()9 k, P( h* ^( n' A, Q- I
- rtc.datetime((2017, 8, 23, 1, 12, 48, 0, 0)) # set a specific date and time% q" w; e& i: c5 Q, d
- rtc.datetime() # get date and time
复制代码 / b6 @1 n( ~( f1 Q6 E6 P5 [
PWM(脉宽调制)请参阅pyb.Pin 和 pyb.Timer. - from pyb import Pin, Timer# V3 j" D% _6 t0 ]
# c! c) f0 U* \+ Z( F; s* Q- T- p = Pin('X1') # X1 has TIM2, CH18 w/ W' S0 U1 _) j( Q4 v7 [3 k
- tim = Timer(2, freq=1000)7 |7 f; C/ V+ I6 r
- ch = tim.channel(1, Timer.PWM, pin=p)
$ I/ x% e% j, ^& D% A - ch.pulse_width_percent(50)
复制代码 , ^& x/ D0 W# |6 ]
ADC(模数转换)请参阅pyb.Pin 和pyb.ADC. - from pyb import Pin, ADC _) g# v6 P) P" a8 M0 F1 C
1 ?0 Y: K* J. l: J! z+ r6 t% }7 j- adc = ADC(Pin('X19')), Y' E% h" x$ K& B. h0 S. k3 W
- adc.read() # read value, 0-4095
复制代码
: N0 x* o: Z m7 ]' `) b( Z: \" n2 h
- T) n% U- V" \) BDAC(数模转换)请参阅pyb.Pin 和 pyb.DAC. - from pyb import Pin, DAC
9 w$ j; y% A0 X2 i+ ?
4 S8 P+ }# X6 r, ]: F, c' R- dac = DAC(Pin('X5'))% H. ~% j& t" \1 T. ]; F( r0 g
- dac.write(120) # output between 0 and 255
复制代码
k% e/ M. N0 R ?7 q( M0 |. }* j. C
UART(串行总线)请参阅 pyb.UART. - from pyb import UART! U& _7 L* |. H
1 G) r. }3 W2 Z/ Y: {- uart = UART(1, 9600)
0 v1 t/ N* Y7 O @0 Q! H6 \ - uart.write('hello')5 \" P" b$ I7 I, P' `* U
- uart.read(5) # read up to 5 bytes
复制代码 0 W0 m6 K- \" ~' |
- X! ?! \% {5 v1 H; b- z1 o
SPI总线请参阅 pyb.SPI. - from pyb import SPI
- b' {/ c& X6 f - $ E( ~5 m" d" P7 B
- spi = SPI(1, SPI.MASTER, baudrate=200000, polarity=1, phase=0)5 l F! A& a. N8 w K. I
- spi.send('hello')7 i' `3 L1 {! \$ c M) s. V
- spi.recv(5) # receive 5 bytes on the bus
. S3 t4 ?% d/ x$ B - spi.send_recv('hello') # send and receive 5 bytes
复制代码
6 ~4 ^+ h5 v% f3 a c- T
5 m7 L2 P p1 l& C6 |3 u$ [! w* @I2C总线硬件 I2C 可通过I2C('X') 和用于 pyboard 的 X 和 Y 两半 I2C('Y')。或者,传入外围设备的整数标识符,例如I2C(1)。通过显式指定 scl 和 sda 引脚而不是总线名称,也可以使用软件 I2C 。有关更多详细信息,请参阅 machine.I2C. - from machine import I2C* ]! T% M1 Z2 T' ^: G x2 p
- k8 W6 L; a% S. R3 A) J- F- i2c = I2C('X', freq=400000) # create hardware I2c object
2 C' l; M' x' T: D1 g+ t - i2c = I2C(scl='X1', sda='X2', freq=100000) # create software I2C object% p2 J4 u$ w% r9 J% [
/ D& d) ?6 f! u8 n! e' e! O' e- i2c.scan() # returns list of slave addresses
4 H" k* u |3 z' T8 O; D1 r - i2c.writeto(0x42, 'hello') # write 5 bytes to slave with address 0x42
% L$ Q, d& s* y9 [# r$ [" l - i2c.readfrom(0x42, 5) # read 5 bytes from slave
: [% D3 f' |# n
1 K0 N8 Y* L8 j/ }, N- i2c.readfrom_mem(0x42, 0x10, 2) # read 2 bytes from slave 0x42, slave memory 0x103 |+ Q9 k9 }! Y) v8 p
- i2c.writeto_mem(0x42, 0x10, 'xy') # write 2 bytes to slave 0x42, slave memory 0x10
复制代码 ; u0 h, z/ {* c9 g6 ^( c* C& [
" |. O1 {2 d9 ], {( Y# W! s( N3 ~
注意:对于传统 I2C 支持,请参阅 pyb.I2C。 4 Q1 e. L' B( b" U V7 j
I2S总线参见 machine.I2S. - from machine import I2S, Pin
, X* O1 C% D' n# V, Q0 F
/ s$ I' v: l! g( C5 f) S- 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
2 B9 R" g, v2 o* y - i2s.write(buf) # write buffer of audio samples to I2S device
O- J! {! v [6 k: E1 W6 E" ~ - : l) [1 ]% T6 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
$ Y0 ]/ M8 l. o: J$ G8 x2 \ - i2s.readinto(buf) # fill buffer with audio samples from I2S device
复制代码 # c; a i' x& N- ]' z
I2S 类目前作为技术预览版提供。在预览期间,鼓励用户提供反馈。基于此反馈,I2S 类 API 和实现可能会更改。 PYBv1.0/v1.1 有一个 I2S 总线,id=2。PYBD-SFxW 有两条 I2S 总线,id=1 和 id=2。I2S 与 SPI 共享。 % z I$ M j) [3 \* l
CAN总线(控制器局域网)参见 pyb.CAN. - from pyb import CAN
; _) G( t6 `; E- T2 c* P3 E- u$ ]
# J2 S7 {( p" Q! E9 ]. I! D% V- can = CAN(1, CAN.LOOPBACK)! @ }; O. C: I8 U+ w
- can.setfilter(0, CAN.LIST16, 0, (123, 124, 125, 126))% \, a/ D- v( v$ ]8 G
- can.send('message!', 123) # send a message with id 123: d1 l+ G% X5 `! e. m
- can.recv(0) # receive message on FIFO 0
复制代码 4 _: a! a; ^+ e h; K& ]& H: U
5 N2 @$ l' x( F6 V5 g8 R! [! w/ R% _0 T
内部加速度计参见pyb.Accel. - from pyb import Accel& R: R% D, {6 s5 _" r: {) Q- ]
" l: w9 a' C/ q9 U- accel = Accel()& c# q0 u7 R9 I2 W) F+ g8 f
- print(accel.x(), accel.y(), accel.z(), accel.tilt())
复制代码
+ U. e5 w6 v6 D6 U1 R4 M, x2 Q9 b
|