pyboard 的快速参考 以下引脚分配适用于 PYBv1.1。您还可以查看其他版本的 pyboard 的引脚排列: PYBv1.0 或PYBLITEv1.0-AC 或 PYBLITEv1.0.  下面是 pyboard 的快速参考。如果这是您第一次使用该板,请考虑先阅读以下部分:
. A6 ~& s' R, F- f. U6 b$ w通用板控制见 pyb. - import pyb
; Q8 c; r/ F" x7 O+ }( A - 4 j q! F" g7 U5 ?9 l8 J- Y
- pyb.repl_uart(pyb.UART(1, 9600)) # duplicate REPL on UART(1)
- R; z* p. j; Z' z! G# k0 } - pyb.wfi() # pause CPU, waiting for interrupt* b, j$ Q# f5 J2 o; s$ p% V
- pyb.freq() # get CPU and bus frequencies: C; J8 M& q) n: n
- pyb.freq(60000000) # set CPU freq to 60MHz
$ E7 O* U8 I5 j* k" u7 b - pyb.stop() # stop CPU, waiting for external interrupt
复制代码 7 G1 U$ N% l6 ~( G
+ H, u& I) L+ T4 o" c7 |0 x" T
延迟和计时使用 time模块: - import time
0 Z8 q: r8 l0 q% V8 D' t - ) m$ M1 I; A- r' k* S
- time.sleep(1) # sleep for 1 second0 P2 x6 H3 V5 T3 f( W8 P
- time.sleep_ms(500) # sleep for 500 milliseconds6 H" }: O" ~, B9 c3 o9 M
- time.sleep_us(10) # sleep for 10 microseconds
1 M% o' r" G! Y - start = time.ticks_ms() # get value of millisecond counter# X8 `% h% l* h4 y3 F: p
- delta = time.ticks_diff(time.ticks_ms(), start) # compute time difference
复制代码 + \/ f" u+ Q5 _1 q
, e# ~* H% b/ y) l! X& x内部 LED见 pyb.LED. - from pyb import LED* q% c+ x5 P) r
- ( i, h4 A) T# I
- led = LED(1) # 1=red, 2=green, 3=yellow, 4=blue
- x8 u8 L/ a0 Y - led.toggle()( A- R G' Z7 @% G3 ^5 `
- led.on()
# K/ G& i1 f0 M8 j! H - led.off()
5 |0 z; X/ g* z! g4 V% e - ! n( b5 K! E1 D. M4 X
- # LEDs 3 and 4 support PWM intensity (0-255)0 [" v6 i) z6 j
- LED(4).intensity() # get intensity
8 ]# g: P! T/ p4 O9 G( w! \1 \ - LED(4).intensity(128) # set intensity to half
复制代码 # [$ S- E) s5 c) l
3 n% d1 g ?. S+ }9 R1 ^内部开关请参阅 pyb.Switch. - from pyb import Switch
9 i2 v8 Q) A2 r7 S d1 _
- t% a# K% P4 G1 K' E- J8 M Z1 A- sw = Switch()
! r$ v# K1 Y: [ - sw.value() # returns True or False$ b6 L% T9 n/ }) v. Q2 Q' L- [
- sw.callback(lambda: pyb.LED(1).toggle())
复制代码
0 A+ v& x8 k) _% V5 M/ w+ F3 Q: O% D* n" [! Z/ u8 J& r& w
引脚和 GPIO请参阅 pyb.Pin. - from pyb import Pin& Y, i( o9 _; Y# Y. k* _1 j
- % R2 Q5 Q T: F+ \
- p_out = Pin('X1', Pin.OUT_PP)1 w; O. s7 C# Y) `7 A
- p_out.high()
0 \- Z+ Y$ w1 T - p_out.low()
7 a! b( |2 d. U# l) v - 6 L7 v" m% i: I) k/ ~
- p_in = Pin('X2', Pin.IN, Pin.PULL_UP)
+ x0 O- }( P+ l - p_in.value() # get value, 0 or 1
复制代码
, Y1 w3 l# T' c" K0 q, L3 |6 L9 |: }6 v3 \: Z: T
伺服控制请参阅pyb.Servo. - from pyb import Servo
( o- n$ S. h; T" ~" Y( F; a - $ x. I+ A6 V6 E) _5 A# F0 a. G
- s1 = Servo(1) # servo on position 1 (X1, VIN, GND) ~" a0 m* c- e6 d
- s1.angle(45) # move to 45 degrees, J" L- h3 R, q+ ^2 `$ ~
- s1.angle(-60, 1500) # move to -60 degrees in 1500ms! W3 \4 c2 S& ^. y6 Z7 U6 B
- s1.speed(50) # for continuous rotation servos
复制代码
m; ` w/ J5 M' M
9 P+ g8 o" ]: w外部中断请参阅 pyb.ExtInt. - from pyb import Pin, ExtInt" I' e) |: |) v! B" l/ F
" _ S% q- g7 _. g1 ^' `% Z3 j- callback = lambda e: print("intr")8 u' x8 \1 ^/ F. S6 f* u
- ext = ExtInt(Pin('Y1'), ExtInt.IRQ_RISING, Pin.PULL_NONE, callback)
复制代码 , H( Y4 a; f- Q, W* B: E
7 d7 G5 R: W( \" I" p计时器请参阅pyb.Timer. - from pyb import Timer* l$ O# f$ }0 a0 J% s) b$ c$ Z
$ J- C! l7 Z }- tim = Timer(1, freq=1000), W; A! I* Z! A O; q) s/ E8 p) ]
- tim.counter() # get counter value
: H( x4 @2 k4 S; f5 ~1 j - tim.freq(0.5) # 0.5 Hz
8 v$ Z- V5 O4 p. d6 { - tim.callback(lambda t: pyb.LED(1).toggle())
复制代码 3 l% R" ]# y8 r4 v6 A. a" O4 z
RTC(实时时钟)请参阅 pyb.RTC - from pyb import RTC
& K* ]) P3 W3 ~& z* m' i3 N6 u - : A+ \! ` Y. t
- rtc = RTC()3 i5 K6 t* n( `. g6 w& |
- rtc.datetime((2017, 8, 23, 1, 12, 48, 0, 0)) # set a specific date and time6 [* M8 o! y o1 H# I# c0 d8 a3 H
- rtc.datetime() # get date and time
复制代码 : Z( _/ W5 [: A# g! O3 o1 ~
PWM(脉宽调制)请参阅pyb.Pin 和 pyb.Timer. - from pyb import Pin, Timer
! c, @5 N: M! \. m* z# q
8 I: T( u/ V* K- p = Pin('X1') # X1 has TIM2, CH1- H- F# C+ f( I7 Q6 i; \8 X5 T
- tim = Timer(2, freq=1000)! T; e& X& V. y" g: M% P
- ch = tim.channel(1, Timer.PWM, pin=p)
6 m. m9 [6 N1 T/ b4 e - ch.pulse_width_percent(50)
复制代码 . v7 ~: k2 C3 m! ]
ADC(模数转换)请参阅pyb.Pin 和pyb.ADC. - from pyb import Pin, ADC
p5 Z% ?4 `, b: H3 y! G; L9 g5 P - m3 f' b1 N& c* N& N3 v
- adc = ADC(Pin('X19'))
; K. a+ H: U8 k - adc.read() # read value, 0-4095
复制代码 8 m9 m2 n: a o* q' a& |
9 {$ Q3 ~" g3 P7 g0 C! N9 ADAC(数模转换)请参阅pyb.Pin 和 pyb.DAC. - from pyb import Pin, DAC: {6 F+ ?7 V% k+ [+ \
9 y' F$ i' O7 W4 }9 P+ ^( T2 K- dac = DAC(Pin('X5'))
" q; I' A3 Q i( t) p8 A$ t8 Z - dac.write(120) # output between 0 and 255
复制代码
; U& ]8 r$ v' L
+ P. p0 B# m3 G6 R* mUART(串行总线)请参阅 pyb.UART. - from pyb import UART
! L/ u, k0 c+ c5 N8 @2 w/ P. O
! u* o0 G8 g% z. ~0 G% o- uart = UART(1, 9600)2 H, j- O4 A6 i2 p" [
- uart.write('hello')9 A4 H; Q$ ]7 w
- uart.read(5) # read up to 5 bytes
复制代码
l I! s8 _; }. o+ v! n: I# p- o
SPI总线请参阅 pyb.SPI. - from pyb import SPI
& T$ ]' u. Q" N* V5 I - % f; _. ~: e8 m. x0 F# C
- spi = SPI(1, SPI.MASTER, baudrate=200000, polarity=1, phase=0)
3 g6 j9 I& K: X/ ? - spi.send('hello')% t% P) O2 {2 D% \ n
- spi.recv(5) # receive 5 bytes on the bus! }0 _' L5 F% H3 z8 ^4 C! K V z
- spi.send_recv('hello') # send and receive 5 bytes
复制代码 9 U' t- G. w" E* o* |, P
) @ S) ]1 d2 |: Y& ^I2C总线硬件 I2C 可通过I2C('X') 和用于 pyboard 的 X 和 Y 两半 I2C('Y')。或者,传入外围设备的整数标识符,例如I2C(1)。通过显式指定 scl 和 sda 引脚而不是总线名称,也可以使用软件 I2C 。有关更多详细信息,请参阅 machine.I2C. - from machine import I2C1 X! e7 ?- I4 t* I, Y' A% x
- 2 z1 c7 W! M% I2 E
- i2c = I2C('X', freq=400000) # create hardware I2c object
: r7 ?/ B1 n$ M4 N/ Z* K# @ - i2c = I2C(scl='X1', sda='X2', freq=100000) # create software I2C object
& A" z- p& I3 B4 h5 a# o - ' B' k5 Z( L% A0 I# Z
- i2c.scan() # returns list of slave addresses6 w$ ~1 O, ], m! u; Q# k
- i2c.writeto(0x42, 'hello') # write 5 bytes to slave with address 0x42
" p* R8 i, V, c7 i - i2c.readfrom(0x42, 5) # read 5 bytes from slave( g+ G) R" L* G: Q# i X7 \
- 6 J Q" S4 ~* {% F" f2 R9 n9 N
- i2c.readfrom_mem(0x42, 0x10, 2) # read 2 bytes from slave 0x42, slave memory 0x108 Y7 o+ K5 D& f0 l" ?, m1 |
- i2c.writeto_mem(0x42, 0x10, 'xy') # write 2 bytes to slave 0x42, slave memory 0x10
复制代码 + h$ X; y* ~' n# R* [8 Z
2 L: V2 \* ^) Y& t2 c6 x6 o4 n注意:对于传统 I2C 支持,请参阅 pyb.I2C。 + ~% I. o6 A1 I
I2S总线参见 machine.I2S. - from machine import I2S, Pin
$ P5 b6 B- M; v8 O- s) q
9 R, [, t' D( v. I( L- 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 object3 v! p7 g( D* O
- i2s.write(buf) # write buffer of audio samples to I2S device0 D8 m Z- F! d: H% q$ w# C
5 X* F6 ^+ p5 [, r; G- 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 object9 C: T9 m; Q9 l3 @! E+ ?. Y5 S V4 Y
- i2s.readinto(buf) # fill buffer with audio samples from I2S device
复制代码 7 t% y A& C: Q- u; ~8 b! ~; p
I2S 类目前作为技术预览版提供。在预览期间,鼓励用户提供反馈。基于此反馈,I2S 类 API 和实现可能会更改。 PYBv1.0/v1.1 有一个 I2S 总线,id=2。PYBD-SFxW 有两条 I2S 总线,id=1 和 id=2。I2S 与 SPI 共享。
; f* N5 w* X* h' \3 ^4 a$ M: d3 D2 gCAN总线(控制器局域网)参见 pyb.CAN. - from pyb import CAN( v$ L0 N+ `% n1 j8 @6 A
- ' T( R# b( d% `; c
- can = CAN(1, CAN.LOOPBACK)
0 K0 Z# F# X/ _ - can.setfilter(0, CAN.LIST16, 0, (123, 124, 125, 126))
1 y" E( L+ M. _$ B% L& J4 h" t6 l, K - can.send('message!', 123) # send a message with id 123
, e- w6 _4 P) ~; y - can.recv(0) # receive message on FIFO 0
复制代码 ' u4 K9 |# }& W z3 N, j' z
/ ^1 b9 @- e8 q# ?; \+ I. l内部加速度计参见pyb.Accel. - from pyb import Accel% _: T' h7 i# [, k; z
* P: e9 M S6 H0 ?- accel = Accel()
& q- P8 j! j* M6 S - print(accel.x(), accel.y(), accel.z(), accel.tilt())
复制代码 3 E( G. j' W7 m* ? B' I8 J
3 P- g- }4 _ k H) t |