pyboard 的快速参考 以下引脚分配适用于 PYBv1.1。您还可以查看其他版本的 pyboard 的引脚排列: PYBv1.0 或PYBLITEv1.0-AC 或 PYBLITEv1.0. 下面是 pyboard 的快速参考。如果这是您第一次使用该板,请考虑先阅读以下部分: ( A& o3 B. |9 t# K
通用板控制见 pyb. - import pyb
3 J- {; ~/ S* Y8 V; g
5 A; d7 e- g$ W/ m. [' S8 J- pyb.repl_uart(pyb.UART(1, 9600)) # duplicate REPL on UART(1)8 a2 t, {, |1 L j+ j
- pyb.wfi() # pause CPU, waiting for interrupt- U, a& V: U4 u$ Z8 \
- pyb.freq() # get CPU and bus frequencies
H$ O' }3 L! t, t8 m - pyb.freq(60000000) # set CPU freq to 60MHz, {8 ~$ f8 D! k
- pyb.stop() # stop CPU, waiting for external interrupt
复制代码 / u$ Y8 X+ U# ]: _+ t
: i. M! L, p2 P' ^延迟和计时使用 time模块: - import time# s# t; A. O& |! x& ?
' L2 w. V! H2 _- z# @# j- time.sleep(1) # sleep for 1 second: C# s" S: a) T6 q) p& w
- time.sleep_ms(500) # sleep for 500 milliseconds
8 P. H" g& |: K% |5 Y$ ? - time.sleep_us(10) # sleep for 10 microseconds
% _, f2 P+ j. g% A - start = time.ticks_ms() # get value of millisecond counter! ?& F" q- O5 ]9 h
- delta = time.ticks_diff(time.ticks_ms(), start) # compute time difference
复制代码 7 f7 [3 ~8 n3 Z
' K. [( a' c) W' a
内部 LED见 pyb.LED. - from pyb import LED
0 z/ {! p+ @/ z( Q4 ?7 C- ~$ K% E
9 q: U1 B% D& E% i J9 C# K3 P- led = LED(1) # 1=red, 2=green, 3=yellow, 4=blue
& f/ I7 N1 L/ v8 I5 i - led.toggle()! N* e) {- j" J T
- led.on()( C6 x) q) i" s7 {3 w- T2 N9 y3 T! a
- led.off()& k# O, _* }( N8 ?: T
- ( i* p% E2 k" |+ S" I( I
- # LEDs 3 and 4 support PWM intensity (0-255)
# u- r6 u/ U$ c3 E/ ?7 e3 P - LED(4).intensity() # get intensity Q) O U1 E& z
- LED(4).intensity(128) # set intensity to half
复制代码 / r1 b- Q: P1 l4 t( q
, Y2 @# q% i8 q/ b; M. W内部开关请参阅 pyb.Switch. - from pyb import Switch
8 v( Q3 ^3 |: G4 c) w - / C$ F% O9 } x
- sw = Switch()
2 M7 e6 e' w; I - sw.value() # returns True or False' K1 y7 F4 y: o n! v
- sw.callback(lambda: pyb.LED(1).toggle())
复制代码
" d' y: x% h) o r4 y3 Y1 L6 q1 n2 @ D+ r' | W" D! |6 m
引脚和 GPIO请参阅 pyb.Pin. - from pyb import Pin" h8 c8 J% x" M [' m# k" i
- 0 O# F- D" O2 I! P) |7 B
- p_out = Pin('X1', Pin.OUT_PP)
) w; n% o! ~7 N - p_out.high()
9 g1 d. i/ q, G( m& p - p_out.low()$ U: a8 |) `/ g
; N) H; P0 w- r5 @& O- p_in = Pin('X2', Pin.IN, Pin.PULL_UP)
3 x4 O/ _6 Y+ r% }2 l7 b0 e - p_in.value() # get value, 0 or 1
复制代码 ) I' A& H/ B: P A- w5 Q! ?
+ r# f, d% v9 z4 B
伺服控制请参阅pyb.Servo. - from pyb import Servo; Y3 n$ g+ q# D+ j& \
$ ], c5 o/ B2 ?* X' K: w7 S- s1 = Servo(1) # servo on position 1 (X1, VIN, GND)
5 n4 s! l/ J1 E" ?$ i: u3 y - s1.angle(45) # move to 45 degrees
' F4 M5 `: l7 L3 c) I6 l - s1.angle(-60, 1500) # move to -60 degrees in 1500ms
( M# x7 B4 T: _* } ^: z; U* N - s1.speed(50) # for continuous rotation servos
复制代码
& j4 [% r( p& f1 T5 C! x: C) w
5 h/ S" b, a5 |外部中断请参阅 pyb.ExtInt. - from pyb import Pin, ExtInt9 @# u( M! k/ N& p' O" l. [+ j
- 6 g. K6 [4 {7 q; ]4 H7 N+ ^& \
- callback = lambda e: print("intr")2 ~- G* }' n( K. \ M! E
- ext = ExtInt(Pin('Y1'), ExtInt.IRQ_RISING, Pin.PULL_NONE, callback)
复制代码
# E; I b6 u$ ]1 X4 D
* q. z# n5 W* |# p; g K9 D计时器请参阅pyb.Timer. - from pyb import Timer: G' O% n. l/ X$ A/ B5 ]; @
- 3 Y$ R" R0 K# | y2 r4 G" O$ ?
- tim = Timer(1, freq=1000); |5 [. i5 `( C2 B9 h8 D! u4 Y
- tim.counter() # get counter value
! w5 G5 e- z- K" H, @ - tim.freq(0.5) # 0.5 Hz7 W$ r9 Y# G4 g' @* c4 ?/ W
- tim.callback(lambda t: pyb.LED(1).toggle())
复制代码
8 G4 y5 b7 R9 L+ d4 c+ SRTC(实时时钟)请参阅 pyb.RTC - from pyb import RTC
) U) d& B' ]* d" [6 ]+ Y0 b
7 ]& X1 |" E5 E& Z! J9 Z- rtc = RTC()
) r/ h! ^: E; {- f- J! S - rtc.datetime((2017, 8, 23, 1, 12, 48, 0, 0)) # set a specific date and time7 o: Q( I+ e( d1 R! E
- rtc.datetime() # get date and time
复制代码
! a7 t2 h/ f4 |PWM(脉宽调制)请参阅pyb.Pin 和 pyb.Timer. - from pyb import Pin, Timer3 }- |% |. }; U+ B
3 T* H) T" ?* y3 c3 C- p = Pin('X1') # X1 has TIM2, CH12 y- [+ R2 M: I9 o7 w7 P
- tim = Timer(2, freq=1000)
0 @+ e( `3 D- ~- U5 e3 ]' X - ch = tim.channel(1, Timer.PWM, pin=p)
3 ^% F$ p' q/ O7 Z - ch.pulse_width_percent(50)
复制代码 4 `1 j& W+ W# q% |
ADC(模数转换)请参阅pyb.Pin 和pyb.ADC. - from pyb import Pin, ADC9 d# y: E& n6 D) e3 ^2 S
- ' i+ v$ d' b/ W2 w+ c+ u+ r
- adc = ADC(Pin('X19'))" J) Y+ i* W/ `' [7 R: y# ` h% _
- adc.read() # read value, 0-4095
复制代码 ; [) }6 u' y. L) P8 U+ v
* I7 y$ t; J6 P$ b" y: A# ?/ CDAC(数模转换)请参阅pyb.Pin 和 pyb.DAC. - from pyb import Pin, DAC
/ J3 W' {. n) u2 m. A - 1 o# E+ ]; I7 H8 ?, u
- dac = DAC(Pin('X5'))6 v* x% A5 O p2 l$ l
- dac.write(120) # output between 0 and 255
复制代码 / k' H+ `% ]2 l% z* r. N
1 R1 v1 o9 @1 u7 RUART(串行总线)请参阅 pyb.UART. - from pyb import UART3 C8 f+ ^. i/ c0 b- y2 ?# J
( _1 r8 n8 f( y' j& s- uart = UART(1, 9600)1 S$ ~9 o9 {1 u& |" u9 O- G! K
- uart.write('hello')
( n) q# i2 Q+ W6 v+ b - uart.read(5) # read up to 5 bytes
复制代码 ; o6 t- U ^& u
, O+ Z! m# C, Q# Q1 x9 LSPI总线请参阅 pyb.SPI. - from pyb import SPI! |! r) _ d2 y7 J
- + E; \5 b5 s: h0 V* ~9 X
- spi = SPI(1, SPI.MASTER, baudrate=200000, polarity=1, phase=0)
' Y3 N6 ]3 {; C; Y7 ~ - spi.send('hello')
! T6 {% u- @6 }& s; l! ? - spi.recv(5) # receive 5 bytes on the bus9 P4 p7 R) d: B0 Y
- spi.send_recv('hello') # send and receive 5 bytes
复制代码
' ^6 N/ X; ^$ M7 }* U+ E- t, E8 X
, c3 u; V( r. g$ R' xI2C总线硬件 I2C 可通过I2C('X') 和用于 pyboard 的 X 和 Y 两半 I2C('Y')。或者,传入外围设备的整数标识符,例如I2C(1)。通过显式指定 scl 和 sda 引脚而不是总线名称,也可以使用软件 I2C 。有关更多详细信息,请参阅 machine.I2C. - from machine import I2C
" a& z: C/ }* h5 Z& L' ? - 7 B3 v8 V, w" |1 w
- i2c = I2C('X', freq=400000) # create hardware I2c object
% x! g. r0 t0 F$ Y7 j# R* l2 m! V' t - i2c = I2C(scl='X1', sda='X2', freq=100000) # create software I2C object. B/ {5 @6 \! k6 ~8 ^( {' o3 ]1 H: r
! b' V# x1 r# E- i2c.scan() # returns list of slave addresses$ w, h# F$ M% a9 P- f
- i2c.writeto(0x42, 'hello') # write 5 bytes to slave with address 0x420 u6 s4 v& Z0 M2 g+ Q( n8 F
- i2c.readfrom(0x42, 5) # read 5 bytes from slave
: b: J8 p/ k5 [8 Q) N7 e4 K2 U
% Q. ?0 A, J0 v" D. u- z! L- i2c.readfrom_mem(0x42, 0x10, 2) # read 2 bytes from slave 0x42, slave memory 0x10+ j i. b; G: R- z
- i2c.writeto_mem(0x42, 0x10, 'xy') # write 2 bytes to slave 0x42, slave memory 0x10
复制代码
- K8 a# l; k, i- |
& ~: | J- e$ d* P7 a注意:对于传统 I2C 支持,请参阅 pyb.I2C。 , h, e0 j0 \ ?+ s
I2S总线参见 machine.I2S. - from machine import I2S, Pin
' S9 f* H, G' X3 ^6 T L3 r% ~
! r0 l0 r+ u- v0 Q. o- 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) W* m! K" S" {/ e k8 X4 V/ ^9 N
- i2s.write(buf) # write buffer of audio samples to I2S device( o$ B) \) L3 s# ?/ A7 h
- 0 N* ~- J% s$ T" |$ r
- 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
& j9 N* ~& K3 r& i& `6 |( `6 { - i2s.readinto(buf) # fill buffer with audio samples from I2S device
复制代码
- [. q# I3 p8 ~& yI2S 类目前作为技术预览版提供。在预览期间,鼓励用户提供反馈。基于此反馈,I2S 类 API 和实现可能会更改。 PYBv1.0/v1.1 有一个 I2S 总线,id=2。PYBD-SFxW 有两条 I2S 总线,id=1 和 id=2。I2S 与 SPI 共享。 - g! _6 ]2 X8 i3 m6 M
CAN总线(控制器局域网)参见 pyb.CAN. - from pyb import CAN
8 C8 i0 S! j$ N3 I, ]$ w
5 m* m4 K$ `7 l3 o, v- D& l. i- can = CAN(1, CAN.LOOPBACK)
$ v2 s$ i( f' V% G; Z - can.setfilter(0, CAN.LIST16, 0, (123, 124, 125, 126))
7 w" T1 r1 R" i% ^ - can.send('message!', 123) # send a message with id 1230 ]. n) p6 Z7 }* p
- can.recv(0) # receive message on FIFO 0
复制代码
4 t( [( d; P2 P. j0 W8 \
9 a& t6 h4 f3 s0 w/ O" \内部加速度计参见pyb.Accel. - from pyb import Accel
" [2 }. Z7 p$ X: j) }, l# [0 Y - 0 }& e9 C. P; H9 C5 x: j
- accel = Accel()+ H7 N) a2 t/ a2 }- F- o
- print(accel.x(), accel.y(), accel.z(), accel.tilt())
复制代码 ( `5 F3 k& F, t6 E8 u( H+ S: x
5 n) l3 `- R; n5 {: u
|