|
| 1 | +Quick reference for the pyboard |
| 2 | +===================================== |
| 3 | + |
| 4 | +.. image:: http://micropython.org/static/resources/pybv10-pinout.jpg |
| 5 | + :alt: AMP skin |
| 6 | + :width: 600px |
| 7 | + |
| 8 | +General board control |
| 9 | +--------------------- |
| 10 | +:: |
| 11 | + |
| 12 | + import pyb |
| 13 | + |
| 14 | + pyb.delay(50) # wait 50 milliseconds |
| 15 | + pyb.millis() # number of milliseconds since bootup |
| 16 | + pyb.repl_uart(pyb.UART(1, 9600)) # duplicate REPL on UART(1) |
| 17 | + pyb.wfi() # pause CPU, waiting for interrupt |
| 18 | + pyb.freq() # get CPU and bus frequencies |
| 19 | + pyb.freq(60000000) # set CPU freq to 60MHz |
| 20 | + pyb.stop() # stop CPU, waiting for external interrupt |
| 21 | + |
| 22 | +LEDs |
| 23 | +---- |
| 24 | +:: |
| 25 | + |
| 26 | + from pyb import LED |
| 27 | + |
| 28 | + led = LED(1) # red led |
| 29 | + led.toggle() |
| 30 | + led.on() |
| 31 | + led.off() |
| 32 | + |
| 33 | +Pins and GPIO |
| 34 | +------------- |
| 35 | +:: |
| 36 | + |
| 37 | + from pyb import Pin |
| 38 | + |
| 39 | + p_out = Pin('X1', Pin.OUT_PP) |
| 40 | + p_out.high() |
| 41 | + p_out.low() |
| 42 | + |
| 43 | + p_in = Pin('X2', Pin.IN, Pin.PULL_UP) |
| 44 | + p_in.value() # get value, 0 or 1 |
| 45 | + |
| 46 | +External interrupts |
| 47 | +------------------- |
| 48 | +:: |
| 49 | + |
| 50 | + from pyb import Pin, ExtInt |
| 51 | + |
| 52 | + callback = lambda e: print("intr") |
| 53 | + ext = ExtInt(Pin('Y1'), ExtInt.IRQ_RISING, Pin.PULL_NONE, callback) |
| 54 | + |
| 55 | +Timers |
| 56 | +------ |
| 57 | +:: |
| 58 | + |
| 59 | + from pyb import Timer |
| 60 | + |
| 61 | + tim = Timer(1, freq=1000) |
| 62 | + tim.counter() # get counter value |
| 63 | + tim.freq(0.5) # 0.5 Hz |
| 64 | + tim.callback(lambda t: pyb.LED(1).toggle()) |
| 65 | + |
| 66 | +PWM (pulse width modulation) |
| 67 | +---------------------------- |
| 68 | +:: |
| 69 | + |
| 70 | + from pyb import Pin, Timer |
| 71 | + |
| 72 | + p = Pin('X1') # X1 has TIM2, CH1 |
| 73 | + tim = Timer(2, freq=1000) |
| 74 | + ch = tim.channel(1, Timer.PWM, pin=p) |
| 75 | + ch.pulse_width_percent(50) |
| 76 | + |
| 77 | +ADC (analog to digital conversion) |
| 78 | +---------------------------------- |
| 79 | +:: |
| 80 | + |
| 81 | + from pyb import Pin, ADC |
| 82 | + |
| 83 | + adc = ADC(Pin('X19')) |
| 84 | + adc.read() # read value, 0-4095 |
| 85 | + |
| 86 | +DAC (digital to analog conversion) |
| 87 | +---------------------------------- |
| 88 | +:: |
| 89 | + |
| 90 | + from pyb import Pin, DAC |
| 91 | + |
| 92 | + dac = DAC(Pin('X5')) |
| 93 | + dac.write(120) # output between 0 and 255 |
| 94 | + |
| 95 | +UART (serial bus) |
| 96 | +----------------- |
| 97 | +:: |
| 98 | + |
| 99 | + from pyb import UART |
| 100 | + |
| 101 | + uart = UART(1, 9600) |
| 102 | + uart.write('hello') |
| 103 | + uart.read(5) # read up to 5 bytes |
| 104 | + |
| 105 | +SPI bus |
| 106 | +------- |
| 107 | +:: |
| 108 | + |
| 109 | + from pyb import SPI |
| 110 | + |
| 111 | + spi = SPI(1, SPI.MASTER, baudrate=200000, polarity=1, phase=0) |
| 112 | + spi.send('hello') |
| 113 | + spi.recv(5) # receive 5 bytes on the bus |
| 114 | + spi.send_recv('hello') # send a receive 5 bytes |
| 115 | + |
| 116 | +I2C bus |
| 117 | +------- |
| 118 | +:: |
| 119 | + |
| 120 | + from pyb import I2C |
| 121 | + |
| 122 | + i2c = I2C(1, I2C.MASTER, baudrate=100000) |
| 123 | + i2c.scan() # returns list of slave addresses |
| 124 | + i2c.send('hello', 0x42) # send 5 bytes to slave with address 0x42 |
| 125 | + i2c.recv(5, 0x42) # receive 5 bytes from slave |
| 126 | + i2c.mem_read(2, 0x42, 0x10) # read 2 bytes from slave 0x42, slave memory 0x10 |
| 127 | + i2c.mem_write('xy', 0x42, 0x10) # write 2 bytes to slave 0x42, slave memory 0x10 |
0 commit comments