pyboard 的快速参考 以下引脚分配适用于 PYBv1.1。您还可以查看其他版本的 pyboard 的引脚排列: PYBv1.0 或PYBLITEv1.0-AC 或 PYBLITEv1.0. 下面是 pyboard 的快速参考。如果这是您第一次使用该板,请考虑先阅读以下部分:
0 A8 N& A x2 _4 ?! X, C通用板控制见 pyb. - import pyb
2 ?. `; Y" V: [7 \7 ?: r
6 v" \* h# `" R' Y( l% y- I- pyb.repl_uart(pyb.UART(1, 9600)) # duplicate REPL on UART(1)( B, i6 s: M. |' @+ r
- pyb.wfi() # pause CPU, waiting for interrupt
$ y4 M% ~! }6 @/ q" E9 @ - pyb.freq() # get CPU and bus frequencies! ]9 j4 v3 V2 g3 h' q7 |
- pyb.freq(60000000) # set CPU freq to 60MHz. ?) j/ |* i# g' I7 A6 A
- pyb.stop() # stop CPU, waiting for external interrupt
复制代码 6 ]. E; S! F( W7 }' M' h& c4 G' o
% f; a4 b; M8 J! G# Y8 P
延迟和计时使用 time模块: - import time( F! a& a; [5 g8 k! v$ y- p
- % A q8 R% o5 Y
- time.sleep(1) # sleep for 1 second4 ^& ]- X# D/ b, }
- time.sleep_ms(500) # sleep for 500 milliseconds
2 J. E( T3 ~& z% v; k9 J: m - time.sleep_us(10) # sleep for 10 microseconds
* N; u7 g- G0 r& ~ - start = time.ticks_ms() # get value of millisecond counter/ _5 V; j0 W: K& _7 m6 e
- delta = time.ticks_diff(time.ticks_ms(), start) # compute time difference
复制代码 9 u7 w- i" Z; I+ l& w6 u! H8 X
& U+ P( j, W) ]# s; J0 H$ q* a
内部 LED见 pyb.LED. - from pyb import LED
. O, b+ y5 T' d' n k& C( \ - ! Q) e- a5 Y; X3 {+ h$ H
- led = LED(1) # 1=red, 2=green, 3=yellow, 4=blue9 v e- r' n ]8 u
- led.toggle()
9 ` o% h$ T- E/ _" Z: X - led.on()" x" ^$ i( S! o- C" o' T
- led.off()
( U! [1 U7 |; W- N H( j* i. n" H
; e: M% ], E- w1 {1 b8 X1 p- W- # LEDs 3 and 4 support PWM intensity (0-255), `8 X: e9 }' V; z% F. r
- LED(4).intensity() # get intensity) W H# Z& L3 v( v8 J$ T
- LED(4).intensity(128) # set intensity to half
复制代码
6 A) B% \, T% n2 g- Q P, P( O5 e- Q& Z1 t0 Q9 _; d7 X
内部开关请参阅 pyb.Switch. - from pyb import Switch
9 U5 v+ |( E$ [6 p' l - 4 M4 @ v! v/ u
- sw = Switch()
4 [5 H% n" R0 I - sw.value() # returns True or False' {% x \' j9 W& L
- sw.callback(lambda: pyb.LED(1).toggle())
复制代码
/ A/ T0 V, b1 }) }
( l" Z# E6 ^4 I# T引脚和 GPIO请参阅 pyb.Pin. - from pyb import Pin d" u5 {7 ]' e8 U% Y r
- 5 d8 z& I! e$ O9 H- o
- p_out = Pin('X1', Pin.OUT_PP)& s) b) p& H* X3 f; B, u: k1 ?
- p_out.high()$ B5 x% c, B9 S3 \& ^
- p_out.low()
. i. E% O K1 I) r; ?( ]: g u; r+ V
* {$ R! ~" P* M+ E# O- p_in = Pin('X2', Pin.IN, Pin.PULL_UP)7 n6 p, ?! _# A
- p_in.value() # get value, 0 or 1
复制代码
$ ~+ n- r9 g: P$ b% ^ I% w
$ p/ U% A* e3 t' P3 Q& c5 |/ F3 o伺服控制请参阅pyb.Servo. - from pyb import Servo5 x3 n; C, E7 ]* J
- ( k! ]! \! x# t; i5 ]. ]: u: j
- s1 = Servo(1) # servo on position 1 (X1, VIN, GND)
. \: j, k: F; S$ M! c - s1.angle(45) # move to 45 degrees
7 e$ g: P' Q4 e" ? o - s1.angle(-60, 1500) # move to -60 degrees in 1500ms
6 m! w+ C/ g0 ]" w( m: E' \9 ^ - s1.speed(50) # for continuous rotation servos
复制代码 # p9 Z) k" z7 a
' d3 J7 q0 u/ j. r/ C4 O
外部中断请参阅 pyb.ExtInt. - from pyb import Pin, ExtInt
. c$ k9 u: H) U/ D& a - - h0 a! a. Y' x. y2 U$ s
- callback = lambda e: print("intr")
@" ]: ]! l3 S0 P' E6 B7 k, m - ext = ExtInt(Pin('Y1'), ExtInt.IRQ_RISING, Pin.PULL_NONE, callback)
复制代码
3 t( E2 `+ c! {: K" V7 ^" v, ^: K( ?! ~% _% }9 n
计时器请参阅pyb.Timer. - from pyb import Timer4 p( ^3 b1 w% R1 ~, Q
: q0 v- L% t. D) i! O- tim = Timer(1, freq=1000)
$ N5 X) X7 V; _8 j- F - tim.counter() # get counter value# h( y2 b8 l6 z5 n1 v, a, a" l% N
- tim.freq(0.5) # 0.5 Hz
8 ]& p9 m( W' t5 t4 r5 D4 ~9 f) s - tim.callback(lambda t: pyb.LED(1).toggle())
复制代码
, r" Z8 W' n" U. {RTC(实时时钟)请参阅 pyb.RTC - from pyb import RTC
2 ]( ?* ^/ u$ @# d% ?1 }
3 }( v0 B2 D: w5 H4 O2 y- U- rtc = RTC()
6 J( c4 H+ K' h3 k - rtc.datetime((2017, 8, 23, 1, 12, 48, 0, 0)) # set a specific date and time( N0 Y U! N% B( L X, P
- rtc.datetime() # get date and time
复制代码
8 J* O2 W# t! \PWM(脉宽调制)请参阅pyb.Pin 和 pyb.Timer. - from pyb import Pin, Timer, @- r9 \8 ~! Q- y2 L
) R! k" I- I& |- I- Y3 \- p = Pin('X1') # X1 has TIM2, CH1
5 ^3 k- z& U D, p) F, |* [- p - tim = Timer(2, freq=1000)
$ G, Y7 E3 _' f2 ]' K2 a) J. R) H - ch = tim.channel(1, Timer.PWM, pin=p)6 r, [2 m, H9 M' x
- ch.pulse_width_percent(50)
复制代码
0 V' A+ a# w/ Y% Y$ a1 E2 r! PADC(模数转换)请参阅pyb.Pin 和pyb.ADC. - from pyb import Pin, ADC4 A8 g% o% L- l: }+ J# @
- 9 J# A) e# Y# r. ]" W% f. o+ c
- adc = ADC(Pin('X19'))
' U7 p( V1 F' \) z - adc.read() # read value, 0-4095
复制代码
( k* b! f7 E! P. [1 e R$ L& n0 D" W5 P& Y
DAC(数模转换)请参阅pyb.Pin 和 pyb.DAC. - from pyb import Pin, DAC
9 {( l t. c# k- Y
6 ?6 X! j8 N3 y- ]; l" T$ i- dac = DAC(Pin('X5'))
+ ?- }7 j' Z. P; ]" Y* \ - dac.write(120) # output between 0 and 255
复制代码 ' k; Y# E3 Z& W& v. v
( O. K4 V* G4 `" J8 ]1 o
UART(串行总线)请参阅 pyb.UART. - from pyb import UART3 `. D# B$ [0 E9 R% e4 H
9 H# q: a0 e4 T. [- uart = UART(1, 9600)
+ ]1 q0 C" b& V& X+ x - uart.write('hello')
( a8 v r, _* e" V; X3 v+ ^ - uart.read(5) # read up to 5 bytes
复制代码
8 U% {. p. P K/ v& f3 o3 C9 X( M) W% t3 x; z' Z6 F( K0 P
SPI总线请参阅 pyb.SPI. - from pyb import SPI/ H+ z; N) B& \
- 0 U' ]( J1 R& C- h, ^, x8 O5 t$ X
- spi = SPI(1, SPI.MASTER, baudrate=200000, polarity=1, phase=0)/ N# F+ [' r4 b9 b* d2 d
- spi.send('hello')
! w- r3 o0 w& s8 ^ - spi.recv(5) # receive 5 bytes on the bus
0 ]) q& c( w7 T* K - spi.send_recv('hello') # send and receive 5 bytes
复制代码
$ g) c! m( k$ J+ g* H- R! F. s7 E; F+ g! B0 w1 M
I2C总线硬件 I2C 可通过I2C('X') 和用于 pyboard 的 X 和 Y 两半 I2C('Y')。或者,传入外围设备的整数标识符,例如I2C(1)。通过显式指定 scl 和 sda 引脚而不是总线名称,也可以使用软件 I2C 。有关更多详细信息,请参阅 machine.I2C. - from machine import I2C" ]: ]3 w: e" A7 {7 X
- # N$ n% r ]9 [! R/ _+ Z
- i2c = I2C('X', freq=400000) # create hardware I2c object0 N4 j: l5 Y! ?( I
- i2c = I2C(scl='X1', sda='X2', freq=100000) # create software I2C object
) P) D1 s9 p, i
+ b. o1 Z6 ]8 {( W5 z2 |- h- i2c.scan() # returns list of slave addresses O( Q- y- r6 W9 t) v2 h" B
- i2c.writeto(0x42, 'hello') # write 5 bytes to slave with address 0x42+ [$ _8 a" L& T+ O; I
- i2c.readfrom(0x42, 5) # read 5 bytes from slave) @+ h$ } ^5 X+ N7 {! h* i& G+ Z
* x2 Z/ i5 x0 @' Y$ } p" b- i2c.readfrom_mem(0x42, 0x10, 2) # read 2 bytes from slave 0x42, slave memory 0x10
' Q8 X* I d( U - i2c.writeto_mem(0x42, 0x10, 'xy') # write 2 bytes to slave 0x42, slave memory 0x10
复制代码 " `: ?, O5 ]' k6 P) I& _
4 Z, p1 a$ _# l1 H
注意:对于传统 I2C 支持,请参阅 pyb.I2C。 ; x& h0 P. _0 ^" w
I2S总线参见 machine.I2S. - from machine import I2S, Pin
6 n% W) |& l# a, l, ], P - 5 D- l$ i7 E, Z% m& j# V* 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* V: K- @* r4 S; J) h7 ?6 j
- i2s.write(buf) # write buffer of audio samples to I2S device) D1 N- S0 @" Q* A" [8 ^: Z# d& w
* b9 Q. A* C9 t& l' S2 f# J; `- 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
' @) Y; F- q& a6 k- S" e - i2s.readinto(buf) # fill buffer with audio samples from I2S device
复制代码
# b; N L: Y- r8 \, s' VI2S 类目前作为技术预览版提供。在预览期间,鼓励用户提供反馈。基于此反馈,I2S 类 API 和实现可能会更改。 PYBv1.0/v1.1 有一个 I2S 总线,id=2。PYBD-SFxW 有两条 I2S 总线,id=1 和 id=2。I2S 与 SPI 共享。 ; i' m$ I) g& Z3 E- ` ^$ I
CAN总线(控制器局域网)参见 pyb.CAN. - from pyb import CAN
- r/ u" H6 l! H6 o
% `: U# c% a# A7 [- can = CAN(1, CAN.LOOPBACK)
$ A7 ?( Z4 Z' A9 Q! r% J! \ - can.setfilter(0, CAN.LIST16, 0, (123, 124, 125, 126))
3 L4 h# |5 n3 }' {* E - can.send('message!', 123) # send a message with id 1235 q6 k0 s/ w# h5 p
- can.recv(0) # receive message on FIFO 0
复制代码 9 m0 U2 g p7 V( h
. v1 u( j" M" q0 D6 f内部加速度计参见pyb.Accel. - from pyb import Accel3 T& J$ s( |5 z- U
- ! V6 V* J5 ?" D3 i' h/ L2 e0 y
- accel = Accel() ^( ^/ |3 }0 J# o+ ]4 v D
- print(accel.x(), accel.y(), accel.z(), accel.tilt())
复制代码
+ i4 m- E' n+ V4 @! m9 z* O8 K6 a/ H2 Z8 ^0 L) b
|