pyboard 的快速参考 以下引脚分配适用于 PYBv1.1。您还可以查看其他版本的 pyboard 的引脚排列: PYBv1.0 或PYBLITEv1.0-AC 或 PYBLITEv1.0. 下面是 pyboard 的快速参考。如果这是您第一次使用该板,请考虑先阅读以下部分: 1 |1 r& T7 _$ M* B
通用板控制见 pyb. - import pyb. {0 y4 i& W- _+ E' w$ B
- ( t, ~. Z( V" C' g! t
- pyb.repl_uart(pyb.UART(1, 9600)) # duplicate REPL on UART(1)
X% A" b+ j% }' B - pyb.wfi() # pause CPU, waiting for interrupt
' o) z O# ?# Z- ~1 ? - pyb.freq() # get CPU and bus frequencies
' i! \" r- ]) K - pyb.freq(60000000) # set CPU freq to 60MHz
3 C: _ q' ]/ J( t - pyb.stop() # stop CPU, waiting for external interrupt
复制代码
* D8 i6 ~( N2 T: Z" l7 s; I! D& C2 N! z
延迟和计时使用 time模块: - import time
1 ?' @6 m$ ?) s2 }( b
8 Q f( i9 E+ U# ]5 O4 N- time.sleep(1) # sleep for 1 second' o$ |# [1 _, O6 @, z( l+ W
- time.sleep_ms(500) # sleep for 500 milliseconds X; S; `- q: R/ f2 L
- time.sleep_us(10) # sleep for 10 microseconds
! g* o4 n: A- o X1 E1 @: ? - start = time.ticks_ms() # get value of millisecond counter" z3 o1 K4 j, ~3 C8 W* u
- delta = time.ticks_diff(time.ticks_ms(), start) # compute time difference
复制代码
0 p, Y) d' V( [: c8 a+ P5 J( h4 E
内部 LED见 pyb.LED. - from pyb import LED
& s* M. [- W3 i8 m$ W9 T - $ @+ k5 a/ Y- \& o
- led = LED(1) # 1=red, 2=green, 3=yellow, 4=blue9 {% ?# {7 S1 ]7 y1 P; R
- led.toggle()
0 u4 T+ j z$ _! F0 k - led.on()
u7 q) y, ]1 I: L4 U% s/ C3 e - led.off()
' b5 c' g, K) C0 {4 {7 h6 m - : E' _, i9 k+ E. q9 ?
- # LEDs 3 and 4 support PWM intensity (0-255)% u& ?- Q5 l6 e; V9 z
- LED(4).intensity() # get intensity
$ Z [9 d" [6 U! q& r @( | - LED(4).intensity(128) # set intensity to half
复制代码 6 P% {/ t9 s! o1 V0 a
1 B% e" E y% I- |3 s- ]. S内部开关请参阅 pyb.Switch. - from pyb import Switch
' q5 i% r: p) `* k; i: j - 0 W) e0 k b, U
- sw = Switch()4 M3 i+ q/ ` v7 z1 D8 Y6 E) R2 l3 y
- sw.value() # returns True or False
1 z, \- [. e" H" f% g5 \( Q$ _ - sw.callback(lambda: pyb.LED(1).toggle())
复制代码
( ~* l- w2 }4 M3 |8 G: S* p0 ?( l. L* h% r) y0 [) Q- O3 C
引脚和 GPIO请参阅 pyb.Pin. - from pyb import Pin& y' {) L1 i$ q0 J
- % E% Q9 y' C. ~
- p_out = Pin('X1', Pin.OUT_PP)
) u# W0 Z; w+ _$ {& L) Q4 W - p_out.high()
: i$ A7 C& t* K& V a* J- p: t: u, Y - p_out.low()& r" S! S8 p1 E
! `2 s4 O: F3 I3 X4 [% K7 b- p_in = Pin('X2', Pin.IN, Pin.PULL_UP)! `/ x' j% v/ T6 K) v0 M/ Q N
- p_in.value() # get value, 0 or 1
复制代码 . g, n& P4 ^- K3 o8 N! i
7 s! |# v7 r5 y. O8 E( K: w伺服控制请参阅pyb.Servo. - from pyb import Servo
; W* G+ J& R& P6 t
, g1 t4 _+ I; ?$ r% z' o, @& o- s1 = Servo(1) # servo on position 1 (X1, VIN, GND)
6 \1 k4 a( ^* ^8 S: Q# }1 z7 s" ]! C - s1.angle(45) # move to 45 degrees+ i4 Y8 g# b( c! I& C
- s1.angle(-60, 1500) # move to -60 degrees in 1500ms \9 P. c, l/ L
- s1.speed(50) # for continuous rotation servos
复制代码
m& T r- C3 |% f: v$ ^9 V6 h! c$ B5 Y8 n
外部中断请参阅 pyb.ExtInt. - from pyb import Pin, ExtInt
9 P' h4 y* j( v4 _" { - * L$ C6 U* \/ j# A; G4 j; R
- callback = lambda e: print("intr")
& V( U5 N! n. C& k - ext = ExtInt(Pin('Y1'), ExtInt.IRQ_RISING, Pin.PULL_NONE, callback)
复制代码
" p' X& u5 n# M; U5 o" g8 Q9 ^& h8 J
6 C x7 s+ k' ]5 I计时器请参阅pyb.Timer. - from pyb import Timer$ W/ H$ f' R# \. \& H
, P+ q0 P/ A" i( l3 p- tim = Timer(1, freq=1000)$ z6 F+ ?. J9 B) x7 p a
- tim.counter() # get counter value& ?. n: B9 M* E# s3 C" z8 S
- tim.freq(0.5) # 0.5 Hz
" u5 j3 G9 b+ O4 @, y P - tim.callback(lambda t: pyb.LED(1).toggle())
复制代码
0 p, s, M- B$ Q9 j) mRTC(实时时钟)请参阅 pyb.RTC - from pyb import RTC5 ^' G$ Q7 i# u; v
+ W% a) q7 F, c% u2 t B# Y- rtc = RTC()
7 {4 R2 T' n- R9 M3 r+ X @ - rtc.datetime((2017, 8, 23, 1, 12, 48, 0, 0)) # set a specific date and time4 ?; n% Q' w+ D( Y
- rtc.datetime() # get date and time
复制代码
2 O7 }+ y* x+ U1 V( s% a- E6 S& mPWM(脉宽调制)请参阅pyb.Pin 和 pyb.Timer. - from pyb import Pin, Timer
" s9 [9 d9 Z( [6 g- M
+ [: ~$ A9 X0 [- p = Pin('X1') # X1 has TIM2, CH1
% q. U- Q' n6 q& J - tim = Timer(2, freq=1000)
3 y! ^; J3 W# J - ch = tim.channel(1, Timer.PWM, pin=p)
5 C% F, [# B) v8 x% B0 ~ e - ch.pulse_width_percent(50)
复制代码
" ~1 J5 O- ?: d+ T9 j7 a( ]& YADC(模数转换)请参阅pyb.Pin 和pyb.ADC. - from pyb import Pin, ADC
! d+ K9 D' q" l: x% \, H5 X$ J+ l
# C$ ~$ h- y/ U0 |; N+ U- adc = ADC(Pin('X19'))" i9 d4 u* g& h) L$ s9 ?
- adc.read() # read value, 0-4095
复制代码
0 c6 C) y3 g! W3 ^% u
0 o2 V8 L; g& N$ P0 e; YDAC(数模转换)请参阅pyb.Pin 和 pyb.DAC. - from pyb import Pin, DAC
% {/ R' X* ]4 `: r7 T4 O - # y( t, V7 l4 k
- dac = DAC(Pin('X5'))
9 J# ?9 n. k/ T# i# ?* y - dac.write(120) # output between 0 and 255
复制代码 : A% G( ^( u# m3 ~
# _7 `, {4 }# [1 \. a. i$ w2 rUART(串行总线)请参阅 pyb.UART. - from pyb import UART3 Q" J; B3 Z+ n P
- - t6 p2 u, C% s- E
- uart = UART(1, 9600); R) U5 J' F# v) x# s2 e) v% Q% a
- uart.write('hello')$ |# A, s) k+ ?3 ~: {* g6 U
- uart.read(5) # read up to 5 bytes
复制代码 u; H% G3 x. L o" e
1 f2 ~, t& R' _8 t+ i: ESPI总线请参阅 pyb.SPI. - from pyb import SPI Q4 {$ L2 }' J* \; Y# z+ e; M( `
/ r" U' r& }. }+ I$ f4 @- spi = SPI(1, SPI.MASTER, baudrate=200000, polarity=1, phase=0)
# L) U- }% ]' c7 y- Y - spi.send('hello')$ K, h; A" {( Z2 G
- spi.recv(5) # receive 5 bytes on the bus& `& i0 O+ t7 b& Q7 _& b
- spi.send_recv('hello') # send and receive 5 bytes
复制代码
: \4 O) d' b; }$ Y8 o9 K1 P2 r2 h, J3 e. x8 H) s/ }1 C; U
I2C总线硬件 I2C 可通过I2C('X') 和用于 pyboard 的 X 和 Y 两半 I2C('Y')。或者,传入外围设备的整数标识符,例如I2C(1)。通过显式指定 scl 和 sda 引脚而不是总线名称,也可以使用软件 I2C 。有关更多详细信息,请参阅 machine.I2C. - from machine import I2C F b7 p' ^: x0 ]' }5 \
- 2 W7 ]' x3 V- H6 |4 U
- i2c = I2C('X', freq=400000) # create hardware I2c object
$ i1 n. V6 x( C1 r - i2c = I2C(scl='X1', sda='X2', freq=100000) # create software I2C object; m8 Z" R& _" p7 u b/ L- \# T
J8 x6 ], _8 x0 S- i2c.scan() # returns list of slave addresses
6 {) }1 M1 t& U% }) {; c- I - i2c.writeto(0x42, 'hello') # write 5 bytes to slave with address 0x42
9 D h( v) ~3 M4 F/ X0 r- z* `+ U - i2c.readfrom(0x42, 5) # read 5 bytes from slave2 ?+ u+ _8 e$ T" c: W2 S: a8 U, R
- ' A( e. y0 v) {1 b
- i2c.readfrom_mem(0x42, 0x10, 2) # read 2 bytes from slave 0x42, slave memory 0x10
3 [% J0 M& ~( V) W1 D - i2c.writeto_mem(0x42, 0x10, 'xy') # write 2 bytes to slave 0x42, slave memory 0x10
复制代码
2 \! l7 H& E$ Y$ Y' G0 k1 h5 l$ j( V1 J* H2 }. }% p: E6 ?2 }# O' L( e8 W; p
注意:对于传统 I2C 支持,请参阅 pyb.I2C。
) l" A7 L3 a- r2 f3 g+ Z4 yI2S总线参见 machine.I2S. - from machine import I2S, Pin0 {$ B0 L5 _5 F2 ~3 ~
- : F6 V9 t/ T) A# A; ~4 y
- 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
& h% K" n) q% [) R9 c9 I+ _( l - i2s.write(buf) # write buffer of audio samples to I2S device" i, p$ D" E) t* o1 f. k
- * H3 C9 Q: Q- S, R/ z3 S' O: h
- 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
' k7 _! H; K% d, l - i2s.readinto(buf) # fill buffer with audio samples from I2S device
复制代码
. ]. V) V! B: j% v- W6 I$ KI2S 类目前作为技术预览版提供。在预览期间,鼓励用户提供反馈。基于此反馈,I2S 类 API 和实现可能会更改。 PYBv1.0/v1.1 有一个 I2S 总线,id=2。PYBD-SFxW 有两条 I2S 总线,id=1 和 id=2。I2S 与 SPI 共享。
; M7 g6 _) N7 S& |- cCAN总线(控制器局域网)参见 pyb.CAN. - from pyb import CAN
" |: ?3 q; y9 j
& {8 [' O5 |. M% L+ Z0 V- can = CAN(1, CAN.LOOPBACK)
( i9 @) |' D' @: V - can.setfilter(0, CAN.LIST16, 0, (123, 124, 125, 126)). F/ M. H) d1 [1 b* g9 u
- can.send('message!', 123) # send a message with id 123
1 r8 W+ i U- J, V - can.recv(0) # receive message on FIFO 0
复制代码 2 d5 {0 _% l" k7 v
6 C! H7 _5 H( J; ]/ |内部加速度计参见pyb.Accel. - from pyb import Accel5 B9 Q! ^# F9 {; @
- # ^( e2 u$ C8 [! M% T
- accel = Accel()
9 u# U: R0 n. t - print(accel.x(), accel.y(), accel.z(), accel.tilt())
复制代码
Z( R, `9 f9 L) W* f9 _" s' ]1 u3 D4 |4 u0 Q
|