pyboard 的快速参考 以下引脚分配适用于 PYBv1.1。您还可以查看其他版本的 pyboard 的引脚排列: PYBv1.0 或PYBLITEv1.0-AC 或 PYBLITEv1.0.  下面是 pyboard 的快速参考。如果这是您第一次使用该板,请考虑先阅读以下部分:
/ R1 m4 w# F$ w. e X" c通用板控制见 pyb. - import pyb
* T# s! [2 f) D Q- ~
+ }! @6 N8 k) G# w/ J* Q- pyb.repl_uart(pyb.UART(1, 9600)) # duplicate REPL on UART(1)
* q0 k p9 F' q$ H% P$ B5 p - pyb.wfi() # pause CPU, waiting for interrupt8 c0 T( B o0 I( P* T
- pyb.freq() # get CPU and bus frequencies5 [' K0 b7 C0 N8 z
- pyb.freq(60000000) # set CPU freq to 60MHz
6 a0 {, M5 ^9 e - pyb.stop() # stop CPU, waiting for external interrupt
复制代码 * H) q* w+ y8 a
* J& V. E8 ]4 j/ y6 E* @' X延迟和计时使用 time模块: - import time
( ?, u1 ^8 Q' c3 M - ( W* u9 Q' {2 Y) u% i
- time.sleep(1) # sleep for 1 second& U G& ?; F3 q4 `( h
- time.sleep_ms(500) # sleep for 500 milliseconds
. N& G6 b. p6 w% a2 L% |# `# d' u - time.sleep_us(10) # sleep for 10 microseconds
; q) w; d. z& a) p" J2 @2 h, [( Y, K - start = time.ticks_ms() # get value of millisecond counter$ [7 ?. P5 _3 o: ^' s) S
- delta = time.ticks_diff(time.ticks_ms(), start) # compute time difference
复制代码
1 D* U: N8 ~3 l3 Q# t6 y3 i# ? m8 N Z t- {
内部 LED见 pyb.LED. - from pyb import LED1 V3 ^5 A6 C. r- a$ n9 ~- \. |
/ C0 C! B4 d+ S4 e) y6 {0 j+ T- led = LED(1) # 1=red, 2=green, 3=yellow, 4=blue# Y4 C2 i' p$ X) R1 ~- F, O+ ]
- led.toggle()
6 ?, b: I+ k( n$ P4 H - led.on()" x ]% U- o/ z$ s" ~2 y; `
- led.off()$ N: `( T% w( x; i$ B5 A4 m# U! r) n
- # C- ?9 a7 H( y. `! B7 P
- # LEDs 3 and 4 support PWM intensity (0-255)- M/ {4 `' G% e' ^5 T
- LED(4).intensity() # get intensity# t7 N' a2 @" T+ I6 Y4 c: X5 r
- LED(4).intensity(128) # set intensity to half
复制代码 8 g+ b( d2 t, k6 J( l
+ e0 c% N _. g: v内部开关请参阅 pyb.Switch. - from pyb import Switch+ _2 ~3 H$ l$ y+ X5 Q
* B$ W# ?2 T4 e* Y; S* |- sw = Switch()
' b- c. n* u! d8 J2 C2 d3 d - sw.value() # returns True or False
6 K, r7 A! ~" p6 J8 }6 q! |4 n - sw.callback(lambda: pyb.LED(1).toggle())
复制代码 / [& l: N: `3 }" C, B8 V
1 ?# f4 S' O2 c+ j# ?. S0 Z7 g引脚和 GPIO请参阅 pyb.Pin. - from pyb import Pin
' c( L3 H0 w+ I. k
$ v' k9 Q R0 V4 F4 S- p_out = Pin('X1', Pin.OUT_PP)
& D) s8 Y5 W2 y P( Z - p_out.high()
9 n# W: P( F3 | - p_out.low()
9 [+ I4 h# f1 n9 r& u - 7 X% m% w6 j3 L0 D% J3 h8 m
- p_in = Pin('X2', Pin.IN, Pin.PULL_UP)
! l1 T- u: [* V5 g+ [ - p_in.value() # get value, 0 or 1
复制代码 p/ D1 B( t4 T) |3 D
' _1 K2 @ F! o9 f2 |
伺服控制请参阅pyb.Servo. - from pyb import Servo8 _+ u8 i3 F% Y. P" D
5 ?' p$ f: `3 M: l- s1 = Servo(1) # servo on position 1 (X1, VIN, GND)
$ E2 S% J5 Y" l& e. D m4 n6 X - s1.angle(45) # move to 45 degrees
, `& ]4 D3 Q* s0 F! T J - s1.angle(-60, 1500) # move to -60 degrees in 1500ms
) m& D0 m. S3 \! M9 U( H" e( E - s1.speed(50) # for continuous rotation servos
复制代码 & A5 l. H. ?1 g3 p8 j# x
& {. h- [: T3 _8 H8 ~, v( L" Z% b外部中断请参阅 pyb.ExtInt. - from pyb import Pin, ExtInt
, m! o7 D- D% Y7 i - # q) P, W& N! ~* w& H* x; r
- callback = lambda e: print("intr")
7 x% i6 k K# W& q0 p* c2 o6 T - ext = ExtInt(Pin('Y1'), ExtInt.IRQ_RISING, Pin.PULL_NONE, callback)
复制代码
% X2 r3 o; {; k) u2 {, }
8 i" c; Q- d6 D9 V计时器请参阅pyb.Timer. - from pyb import Timer1 G4 K' U0 g, V" t
- 1 d' l* C( b: Q4 A
- tim = Timer(1, freq=1000): G- R+ Q I7 K$ s
- tim.counter() # get counter value
; Y: {( E5 f. M1 f6 F - tim.freq(0.5) # 0.5 Hz+ o2 n# @7 z7 b1 T; o1 b8 s
- tim.callback(lambda t: pyb.LED(1).toggle())
复制代码
, _3 Q g- S5 j- t7 cRTC(实时时钟)请参阅 pyb.RTC - from pyb import RTC) g. u$ W( y/ C% w7 p. u. ]
- 0 q: A1 b- s! w0 k" L
- rtc = RTC()" q! _/ V: P! F: j4 O
- rtc.datetime((2017, 8, 23, 1, 12, 48, 0, 0)) # set a specific date and time
& ~) ~. p* n/ ?; G$ x - rtc.datetime() # get date and time
复制代码 ! \+ V+ t w, I+ V$ z) D8 ?
PWM(脉宽调制)请参阅pyb.Pin 和 pyb.Timer. - from pyb import Pin, Timer
' L7 Y$ u X; k. X6 g% o - / p( ^0 Q W+ M# C, m8 Z2 n! T
- p = Pin('X1') # X1 has TIM2, CH1
+ |7 P, b; }6 W C3 b* r7 E - tim = Timer(2, freq=1000)2 j* t. g- _! v+ ^% b6 V
- ch = tim.channel(1, Timer.PWM, pin=p)# z( N+ L$ v' U/ l7 R& _
- ch.pulse_width_percent(50)
复制代码 4 B1 |# I# \6 k
ADC(模数转换)请参阅pyb.Pin 和pyb.ADC. - from pyb import Pin, ADC( d* L1 l. T1 r# K
- ; H% t! J8 a) ~" n
- adc = ADC(Pin('X19'))
) P* p' W) m: o! w - adc.read() # read value, 0-4095
复制代码
$ q& U& a# Y j: T, T. L( Q1 t* t: d8 Q9 Q2 Y+ V
DAC(数模转换)请参阅pyb.Pin 和 pyb.DAC. - from pyb import Pin, DAC
% j Q0 J3 n, ?2 S" }+ e* [, k
8 z/ W9 F- e" h, f. `- dac = DAC(Pin('X5'))2 F* G5 \) M* c* t, `+ m+ p6 L8 a
- dac.write(120) # output between 0 and 255
复制代码 * V3 F$ ^! Z+ U8 |
* ]2 w1 S, X! g+ i- h. U: ^( D4 SUART(串行总线)请参阅 pyb.UART. - from pyb import UART
1 |( N- }- o9 i% ` - - g% X# w: P6 W, e% o
- uart = UART(1, 9600)9 ~1 p2 ?& o; Z. {
- uart.write('hello')
, F5 X1 Y6 {9 P, Y - uart.read(5) # read up to 5 bytes
复制代码
% q. s8 b# W, T4 t& z& b3 e) ?- Y$ P, v Z# T- u( i! D9 v
SPI总线请参阅 pyb.SPI. - from pyb import SPI
. v% ^' }" `9 v# F1 o c - 2 p! ^. y* Q! b' R+ G6 F
- spi = SPI(1, SPI.MASTER, baudrate=200000, polarity=1, phase=0) c. q+ f1 X& x2 R+ D6 O- }7 S
- spi.send('hello')( R3 l6 M _( o3 b
- spi.recv(5) # receive 5 bytes on the bus
* }$ H6 |$ O+ P% V8 L - spi.send_recv('hello') # send and receive 5 bytes
复制代码 $ D- r+ D7 P; {) J, k: j
8 A! N' S, F! `4 R D2 E; V
I2C总线硬件 I2C 可通过I2C('X') 和用于 pyboard 的 X 和 Y 两半 I2C('Y')。或者,传入外围设备的整数标识符,例如I2C(1)。通过显式指定 scl 和 sda 引脚而不是总线名称,也可以使用软件 I2C 。有关更多详细信息,请参阅 machine.I2C. - from machine import I2C& r. q3 L4 [. ~ ]2 k5 k2 a; s
6 `! {1 X) l9 ~7 E- i2c = I2C('X', freq=400000) # create hardware I2c object
1 H1 f+ f+ `- c% W8 W% e4 ] - i2c = I2C(scl='X1', sda='X2', freq=100000) # create software I2C object
! W1 J5 b5 \& j7 v: ~7 w$ h
4 g6 N8 [; J. Z8 L) \( k- i2c.scan() # returns list of slave addresses
, {* h9 ]* e4 ^3 o k9 H* R2 } - i2c.writeto(0x42, 'hello') # write 5 bytes to slave with address 0x429 H' i y- [" x7 c1 Z4 ?3 e0 F
- i2c.readfrom(0x42, 5) # read 5 bytes from slave; l- r H3 ^5 m! W$ o: U9 D
2 t6 F6 ]& u8 z p- i2c.readfrom_mem(0x42, 0x10, 2) # read 2 bytes from slave 0x42, slave memory 0x10! _* `3 i5 |+ M& o* P
- i2c.writeto_mem(0x42, 0x10, 'xy') # write 2 bytes to slave 0x42, slave memory 0x10
复制代码 ) o& t# A6 O4 v3 P4 y$ H
) M u5 e; H' I% t- ~4 f# M# P注意:对于传统 I2C 支持,请参阅 pyb.I2C。
3 [& Y; C+ J+ Y$ Z u/ RI2S总线参见 machine.I2S. - from machine import I2S, Pin
3 a7 ]) C/ m: u# N5 Z( ?7 |
8 l6 e W+ X. q: [- 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# s; f9 H+ r u8 H6 t" G' g
- i2s.write(buf) # write buffer of audio samples to I2S device
/ k9 _& I: T6 H. i8 y. U* u! m
: g5 z7 \% I; ]4 D4 a- _3 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
5 g. H# N0 [/ V9 f8 u# D6 h - i2s.readinto(buf) # fill buffer with audio samples from I2S device
复制代码 / O/ y/ X K9 f& b" D. b* p
I2S 类目前作为技术预览版提供。在预览期间,鼓励用户提供反馈。基于此反馈,I2S 类 API 和实现可能会更改。 PYBv1.0/v1.1 有一个 I2S 总线,id=2。PYBD-SFxW 有两条 I2S 总线,id=1 和 id=2。I2S 与 SPI 共享。
* p' ~& T+ {6 C% oCAN总线(控制器局域网)参见 pyb.CAN. - from pyb import CAN
7 \* g5 _: |' B) [/ ~- t - 7 }$ y2 E. R) O2 Y3 a4 r8 S
- can = CAN(1, CAN.LOOPBACK)& h4 D. Q, G- |4 ?4 g1 A
- can.setfilter(0, CAN.LIST16, 0, (123, 124, 125, 126))
2 y4 h' ^- F# F, [6 K/ H2 X - can.send('message!', 123) # send a message with id 123* {/ C: n" Z! h# |$ M
- can.recv(0) # receive message on FIFO 0
复制代码 ! @7 Y' Z9 |6 W: m. j
# W) v, E) ]& m8 x内部加速度计参见pyb.Accel. - from pyb import Accel
# G9 x0 t) j" F* m9 Q
6 s' i) N+ d6 K- accel = Accel()
* `5 `9 h/ [6 S; E - print(accel.x(), accel.y(), accel.z(), accel.tilt())
复制代码
4 x0 g) w& W) H) h; R* j' e$ Y0 O* ~6 ?2 S
|