pyboard 的快速参考 以下引脚分配适用于 PYBv1.1。您还可以查看其他版本的 pyboard 的引脚排列: PYBv1.0 或PYBLITEv1.0-AC 或 PYBLITEv1.0. 下面是 pyboard 的快速参考。如果这是您第一次使用该板,请考虑先阅读以下部分:
5 u8 `% J; c7 f E" E通用板控制见 pyb. - import pyb
4 s5 m2 Z# P) C& w4 }, X1 Q0 I
2 D0 o7 x0 s( O) p/ B% `: w8 k7 N- pyb.repl_uart(pyb.UART(1, 9600)) # duplicate REPL on UART(1)
5 b Y5 _. R" Y5 G - pyb.wfi() # pause CPU, waiting for interrupt
. u* u7 q/ K+ G6 L" [# t( n - pyb.freq() # get CPU and bus frequencies( i2 M: A& \: X) e
- pyb.freq(60000000) # set CPU freq to 60MHz
5 F% Z3 O/ N4 b+ `4 a( l9 w - pyb.stop() # stop CPU, waiting for external interrupt
复制代码
. ?* v$ P* F; D: d! r2 W. E, ]4 U' Y! r* E/ [! r
延迟和计时使用 time模块: - import time
3 @$ C3 P5 D c, f/ w8 j+ l - $ W6 N* B: I; g; ?
- time.sleep(1) # sleep for 1 second
7 @3 w% V( u7 `2 ]* b8 ` - time.sleep_ms(500) # sleep for 500 milliseconds
" Y' \: D {; ` x4 D& u0 G4 B - time.sleep_us(10) # sleep for 10 microseconds
9 J0 `2 S# f7 n" U$ q. ?9 M; H - start = time.ticks_ms() # get value of millisecond counter
W& e) U6 O/ j3 \/ G- K7 x - delta = time.ticks_diff(time.ticks_ms(), start) # compute time difference
复制代码 b4 I5 D5 w6 Q; a- \% K2 d
/ m# i( z/ `: j内部 LED见 pyb.LED. - from pyb import LED" M. ?: k e/ a7 m
- ) ? b% t/ e: L) [# ?: {8 c. n
- led = LED(1) # 1=red, 2=green, 3=yellow, 4=blue
, V8 O* ]/ I5 w1 x - led.toggle()8 t# F4 N) e$ r- E
- led.on(): R1 Z* S3 k+ F+ E
- led.off()' K; _7 w8 [: }! ]! ^
) A5 N% w3 j! c4 P. J9 K% s+ x; M- # LEDs 3 and 4 support PWM intensity (0-255)4 l9 \' a8 F' q8 }# v% j
- LED(4).intensity() # get intensity
% M8 l/ u; u. F+ h2 k! x& V' Y. E - LED(4).intensity(128) # set intensity to half
复制代码 5 v/ U* D) P& [1 w" t7 K0 t, r
; H: y' t) t" }8 h! l内部开关请参阅 pyb.Switch. - from pyb import Switch
+ v9 ]0 I1 H1 R2 M. B) A - . q8 C9 o: Y( H" |
- sw = Switch()
) ^% F- H5 h3 W- Y. x, B" R' g - sw.value() # returns True or False
4 I( D" {5 u0 _' j1 w - sw.callback(lambda: pyb.LED(1).toggle())
复制代码
, }3 ?2 X+ X- D z2 w( i6 B0 A. q
5 y/ v. F$ B9 }; A' j2 F引脚和 GPIO请参阅 pyb.Pin. - from pyb import Pin
8 V$ a& Y: w! ~" `
- w0 _4 ?% u: v. q" q2 P- p_out = Pin('X1', Pin.OUT_PP)- O/ M4 v, Y* A+ O5 a
- p_out.high()
5 R3 i$ n6 k5 Q - p_out.low(), O8 l& W+ n: R* W. m
9 I$ G5 f0 Y6 R# x6 S) m+ S, Q- p_in = Pin('X2', Pin.IN, Pin.PULL_UP)
) S; `+ B3 l7 T - p_in.value() # get value, 0 or 1
复制代码
0 S9 g# a- X6 [5 e) R2 `" o i7 F8 w1 j" V" @& P8 F
伺服控制请参阅pyb.Servo. - from pyb import Servo
, [+ g$ @$ O8 R0 _' y/ L; C; U+ c: _ - 6 l0 }4 A3 t3 i8 _- x* G
- s1 = Servo(1) # servo on position 1 (X1, VIN, GND)* s" F I! ^6 t( o0 J; Z
- s1.angle(45) # move to 45 degrees( ~, j5 T5 F! T: w, `" P
- s1.angle(-60, 1500) # move to -60 degrees in 1500ms, d) Y* U- Y, Z7 A m
- s1.speed(50) # for continuous rotation servos
复制代码 $ u& H. z- \* N) b& X$ `" r
8 w! g+ A" [) \# V+ ~9 l外部中断请参阅 pyb.ExtInt. - from pyb import Pin, ExtInt J2 p8 g$ U0 s: W% U1 i* B! S% q! A7 t, l
- 4 T! b2 P- ]+ O5 \7 W
- callback = lambda e: print("intr")
( f0 X! r3 B8 {7 ^, h - ext = ExtInt(Pin('Y1'), ExtInt.IRQ_RISING, Pin.PULL_NONE, callback)
复制代码 % \' Y; J. c; ^, j A* k! ]& q
, M4 F4 r) s$ |! g计时器请参阅pyb.Timer. - from pyb import Timer
1 T) s' ^: ]6 t' M9 [ - 4 i2 B I2 _% ?+ D0 J; h8 ~% ~. P# @
- tim = Timer(1, freq=1000)
; K; Q- ~4 N: n, J% T - tim.counter() # get counter value# B2 U8 d# ^2 C
- tim.freq(0.5) # 0.5 Hz
" M! Q& ^; e1 J. K/ o* k: H - tim.callback(lambda t: pyb.LED(1).toggle())
复制代码 0 S8 r4 U9 M/ ^/ W( e
RTC(实时时钟)请参阅 pyb.RTC - from pyb import RTC
- S& v% z/ ?) c. Z6 U
0 D: W; C* X s% t/ i- rtc = RTC()
4 U; ]& m5 s" [) _ - rtc.datetime((2017, 8, 23, 1, 12, 48, 0, 0)) # set a specific date and time- K0 i& f9 c. E/ j3 L. u
- rtc.datetime() # get date and time
复制代码
0 U! w/ S: f m. j" A8 u1 RPWM(脉宽调制)请参阅pyb.Pin 和 pyb.Timer. - from pyb import Pin, Timer# E1 y) v4 n# O3 C7 z6 {. K+ s, q
6 N; Y- A1 B( o! J) R- p = Pin('X1') # X1 has TIM2, CH1. M/ F" B. L3 |
- tim = Timer(2, freq=1000)' `6 _# f8 v* R, H% ~! p! N
- ch = tim.channel(1, Timer.PWM, pin=p)
4 C+ E- H1 W9 U+ A5 D) f, R4 A - ch.pulse_width_percent(50)
复制代码 * {3 p! h6 N# B
ADC(模数转换)请参阅pyb.Pin 和pyb.ADC. - from pyb import Pin, ADC0 C# i$ q3 F2 d. U& Q. `
% f+ T6 I8 ], ?; @7 [" z- adc = ADC(Pin('X19'))
n3 X5 Q4 H' [5 l& d% W0 I - adc.read() # read value, 0-4095
复制代码
7 k" y! ^- |! Y. o: t: v0 V
; M/ ], r% ?& iDAC(数模转换)请参阅pyb.Pin 和 pyb.DAC. - from pyb import Pin, DAC
6 L: `/ Z. S6 {0 T: Z* W6 `
( C1 r! O0 H2 B" p- dac = DAC(Pin('X5'))) j/ \8 [8 K8 ?# l
- dac.write(120) # output between 0 and 255
复制代码
1 O( r( e; [* d$ n) {8 o1 J
" e$ m) X2 j/ q1 q) eUART(串行总线)请参阅 pyb.UART. - from pyb import UART
; E4 J" F% i V: {% p# {
4 p& V, |) g% M1 e- uart = UART(1, 9600)
+ S5 g& `" C+ F4 k6 _7 X5 V V" C - uart.write('hello')
) O$ T X& h; R6 E- y- e - uart.read(5) # read up to 5 bytes
复制代码
% I# @, R/ R9 m+ M( C6 P8 Q% q$ j
SPI总线请参阅 pyb.SPI. - from pyb import SPI) P" T, c9 X9 z$ _/ ?+ r- |6 \
9 D- s: [6 Q w# |1 p- L# s- spi = SPI(1, SPI.MASTER, baudrate=200000, polarity=1, phase=0)4 p0 i, o6 b9 A! A* V H( z
- spi.send('hello')9 S; k9 f0 \' r* l
- spi.recv(5) # receive 5 bytes on the bus- Q5 H8 K4 p0 w Q& s1 \
- spi.send_recv('hello') # send and receive 5 bytes
复制代码
w0 h; m* ^: q; I8 F0 v1 s1 `! F$ f' J3 {$ g
I2C总线硬件 I2C 可通过I2C('X') 和用于 pyboard 的 X 和 Y 两半 I2C('Y')。或者,传入外围设备的整数标识符,例如I2C(1)。通过显式指定 scl 和 sda 引脚而不是总线名称,也可以使用软件 I2C 。有关更多详细信息,请参阅 machine.I2C. - from machine import I2C
8 H8 n3 n$ h, p
0 R8 `; X& ^/ @& G3 z- i2c = I2C('X', freq=400000) # create hardware I2c object/ m; _! K8 D3 l3 p9 ]
- i2c = I2C(scl='X1', sda='X2', freq=100000) # create software I2C object
+ E- c# V0 |! A8 \$ l3 `) l
' e3 p+ O8 s1 {- i2c.scan() # returns list of slave addresses
% T5 Q) s" o' }+ G6 H% C- P# \ - i2c.writeto(0x42, 'hello') # write 5 bytes to slave with address 0x421 ~6 u4 o0 ]5 V v
- i2c.readfrom(0x42, 5) # read 5 bytes from slave6 f2 b. V- F( O* ]; g, m8 b A
- ' b& e8 Q9 N3 T7 B
- i2c.readfrom_mem(0x42, 0x10, 2) # read 2 bytes from slave 0x42, slave memory 0x10' C L: Y6 }1 O
- i2c.writeto_mem(0x42, 0x10, 'xy') # write 2 bytes to slave 0x42, slave memory 0x10
复制代码 ! e5 S1 V' |$ U7 @
, n( Y( z: n8 n0 E9 ~( a
注意:对于传统 I2C 支持,请参阅 pyb.I2C。
6 j, O, s, e2 n0 P) ~' v: @I2S总线参见 machine.I2S. - from machine import I2S, Pin
) [; B9 O( |$ _
/ v! x. L* F8 ^$ X- 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, a2 X9 w, H1 ^6 ]- r$ }
- i2s.write(buf) # write buffer of audio samples to I2S device% V+ s( h5 p$ K' J
! y8 j" K) B: s) ]# k, y! v; q' 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 i) e& K( H( C8 r* o- b( p# R
- i2s.readinto(buf) # fill buffer with audio samples from I2S device
复制代码
$ @& P8 k1 F+ RI2S 类目前作为技术预览版提供。在预览期间,鼓励用户提供反馈。基于此反馈,I2S 类 API 和实现可能会更改。 PYBv1.0/v1.1 有一个 I2S 总线,id=2。PYBD-SFxW 有两条 I2S 总线,id=1 和 id=2。I2S 与 SPI 共享。 . N: n( c% G8 o! [' w* M0 }# y
CAN总线(控制器局域网)参见 pyb.CAN. - from pyb import CAN- F# v1 C5 p2 E4 l9 _
- 3 H* R9 d, ^- h* y* [
- can = CAN(1, CAN.LOOPBACK)
7 E+ |/ R2 S! V. ?% T( x( P. @ - can.setfilter(0, CAN.LIST16, 0, (123, 124, 125, 126))
. p' }8 h, X; a) d+ C Y$ { - can.send('message!', 123) # send a message with id 1239 L! g8 m: E3 c1 S9 |5 X3 X
- can.recv(0) # receive message on FIFO 0
复制代码
! Y$ s2 e+ g& _) _1 J/ }+ d k0 \% N4 b9 v' O
内部加速度计参见pyb.Accel. - from pyb import Accel" I0 z" ]) a; G/ ]
% g- W* @" K$ o5 v- accel = Accel()
6 ^' u' n: \7 ~9 j, v - print(accel.x(), accel.y(), accel.z(), accel.tilt())
复制代码 : d. g1 {: z$ j& R) \7 j9 c
' T0 H% x: r- ~# e9 c% u6 l: J7 v |