Skip to content

Commit 4c8c6ac

Browse files
authored
Merge pull request #87 from SummerGGift/optimize_axample
【完善】micropython 例程
2 parents 2559d28 + 44f59ac commit 4c8c6ac

File tree

5 files changed

+34
-24
lines changed

5 files changed

+34
-24
lines changed

examples/stm32l4_pandora/i2c.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,15 @@
1010

1111
from machine import Pin, I2C
1212

13-
clk = Pin(("clk", 43), Pin.OUT_OD) # Select the 43 pin device as the clock
14-
sda = Pin(("sda", 44), Pin.OUT_OD) # Select the 44 pin device as the data line
15-
i2c = I2C(-1, clk, sda, freq=100000) # create I2C peripheral at frequency of 100kHz
16-
i2c.scan() # scan for slaves, returning a list of 7-bit addresses
17-
i2c.writeto(0x51, b'123') # write 3 bytes to slave with 7-bit address 42
18-
i2c.readfrom(0x51, 4) # read 4 bytes from slave with 7-bit address 42
19-
i2c.readfrom_mem(0x51, 0x02, 1) # read 1 bytes from memory of slave 0x51(7-bit)
20-
i2c.writeto_mem(0x51, 2, b'\x10') # write 1 byte to memory of slave 42
13+
PIN_CLK = 29 # PB13, get the pin number from get_pin_number.py
14+
PIN_SDA = 30 # PB14
15+
16+
clk = Pin(("clk", PIN_CLK), Pin.OUT_OD) # Select the PIN_CLK pin device as the clock
17+
sda = Pin(("sda", PIN_SDA), Pin.OUT_OD) # Select the PIN_SDA pin device as the data line
18+
i2c = I2C(-1, clk, sda, freq=100000) # create I2C peripheral at frequency of 100kHz
19+
i2c.scan() # scan for slaves, returning a list of 7-bit addresses
20+
i2c.writeto(0x51, b'123') # write 3 bytes to slave with 7-bit address 42
21+
i2c.readfrom(0x51, 4) # read 4 bytes from slave with 7-bit address 42
22+
i2c.readfrom_mem(0x51, 0x02, 1) # read 1 bytes from memory of slave 0x51(7-bit)
23+
i2c.writeto_mem(0x51, 2, b'\x10') # write 1 byte to memory of slave 42
2124

examples/stm32l4_pandora/key.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from machine import Pin
1212

1313
PIN_LED_R = 71 # PE7, get the pin number from get_pin_number.py
14-
PIN_KEY0 = 58 # PD10, get the pin number from get_pin_number.py
14+
PIN_KEY0 = 58 # PD10
1515
KEY_PRESSED = 0
1616

1717
# create led object from pin PIN_LED_R, Set pin PIN_LED_R to output mode

examples/stm32l4_pandora/pin.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@
1010

1111
from machine import Pin
1212

13-
p_out = Pin(("X1", 33), Pin.OUT_PP)
14-
p_out.value(1) # set io high
15-
p_out.value(0) # set io low
13+
PIN_OUT = 31 # PB15, get the pin number from get_pin_number.py
14+
PIN_IN = 58 # PD10
1615

17-
p_in = Pin(("X2", 32), Pin.IN, Pin.PULL_UP)
18-
p_in.value() # get value, 0 or 1
16+
p_out = Pin(("PB15", PIN_OUT), Pin.OUT_PP)
17+
p_out.value(1) # set io high
18+
p_out.value(0) # set io low
19+
20+
p_in = Pin(("key_0", PIN_IN), Pin.IN, Pin.PULL_UP)
21+
print(p_in.value() ) # get value, 0 or 1

examples/stm32l4_pandora/spi.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,14 @@
1010

1111
from machine import Pin, SPI
1212

13-
clk = Pin(("clk", 43), Pin.OUT_PP)
14-
mosi = Pin(("mosi", 44), Pin.OUT_PP)
15-
miso = Pin(("miso", 45), Pin.IN)
13+
PIN_CLK = 26 # PB10, get the pin number from get_pin_number.py
14+
PIN_MOSI = 27 # PB11
15+
PIN_MISO = 28 # PB12
16+
17+
clk = Pin(("clk", PIN_CLK), Pin.OUT_PP) # Select the PIN_CLK pin device as the clock
18+
mosi = Pin(("mosi", PIN_MOSI), Pin.OUT_PP) # Select the PIN_MOSI pin device as the mosi
19+
miso = Pin(("miso", PIN_MISO), Pin.IN) # Select the PIN_MISO pin device as the miso
20+
1621
spi = SPI(-1, 500000, polarity = 0, phase = 0, bits = 8, firstbit = 0, sck = clk, mosi = mosi, miso = miso)
1722
print(spi)
1823
spi.write("hello rt-thread!")

examples/stm32l4_pandora/uart.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,9 @@
1010

1111
from machine import UART
1212

13-
uart = UART(1, 9600) # init with given baudrate
14-
uart.init(9600, bits=8, parity=None, stop=1) # init with given parameters
15-
uart.read(10) # read 10 characters, returns a bytes object
16-
uart.read() # read all available characters
17-
uart.readline() # read a line
18-
uart.readinto(buf) # read and store into the given buffer
19-
uart.write('abc') # write the 3 characters
13+
uart = UART(1, 115200) # init with given baudrate
14+
uart.init(115200, bits=8, parity=None, stop=1) # init with given parameters
15+
uart.read(10) # read 10 characters, returns a bytes object
16+
uart.read() # read all available characters
17+
uart.readline() # read a line
18+
uart.write('abc') # write the 3 characters

0 commit comments

Comments
 (0)