Ver código fonte

Merge pull request #87 from SummerGGift/optimize_axample

【完善】micropython 例程
朱天龙 (Armink) 6 anos atrás
pai
commit
4c8c6acc63

+ 11 - 8
examples/stm32l4_pandora/i2c.py

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

+ 1 - 1
examples/stm32l4_pandora/key.py

@@ -11,7 +11,7 @@
 from machine import Pin
 from machine import Pin
 
 
 PIN_LED_R   = 71    # PE7, get the pin number from get_pin_number.py
 PIN_LED_R   = 71    # PE7, get the pin number from get_pin_number.py
-PIN_KEY0    = 58    # PD10, get the pin number from get_pin_number.py
+PIN_KEY0    = 58    # PD10
 KEY_PRESSED = 0
 KEY_PRESSED = 0
 
 
 # create led object from pin PIN_LED_R, Set pin PIN_LED_R to output mode
 # create led object from pin PIN_LED_R, Set pin PIN_LED_R to output mode

+ 8 - 5
examples/stm32l4_pandora/pin.py

@@ -10,9 +10,12 @@
 
 
 from machine import Pin
 from machine import Pin
 
 
-p_out = Pin(("X1", 33), Pin.OUT_PP)
-p_out.value(1)              # set io high
-p_out.value(0)              # set io low
+PIN_OUT = 31   # PB15, get the pin number from get_pin_number.py
+PIN_IN  = 58   # PD10
 
 
-p_in = Pin(("X2", 32), Pin.IN, Pin.PULL_UP)
-p_in.value()                # get value, 0 or 1
+p_out = Pin(("PB15", PIN_OUT), Pin.OUT_PP)
+p_out.value(1)                 # set io high
+p_out.value(0)                 # set io low
+
+p_in = Pin(("key_0", PIN_IN), Pin.IN, Pin.PULL_UP)
+print(p_in.value() )           # get value, 0 or 1

+ 8 - 3
examples/stm32l4_pandora/spi.py

@@ -10,9 +10,14 @@
 
 
 from machine import Pin, SPI
 from machine import Pin, SPI
 
 
-clk = Pin(("clk", 43), Pin.OUT_PP)
-mosi = Pin(("mosi", 44), Pin.OUT_PP)
-miso = Pin(("miso", 45), Pin.IN)
+PIN_CLK  = 26   # PB10, get the pin number from get_pin_number.py
+PIN_MOSI = 27   # PB11
+PIN_MISO = 28   # PB12
+
+clk = Pin(("clk", PIN_CLK), Pin.OUT_PP)          # Select the PIN_CLK pin device as the clock
+mosi = Pin(("mosi", PIN_MOSI), Pin.OUT_PP)       # Select the PIN_MOSI pin device as the mosi
+miso = Pin(("miso", PIN_MISO), Pin.IN)           # Select the PIN_MISO pin device as the miso
+
 spi = SPI(-1, 500000, polarity = 0, phase = 0, bits = 8, firstbit = 0, sck = clk, mosi = mosi, miso = miso)
 spi = SPI(-1, 500000, polarity = 0, phase = 0, bits = 8, firstbit = 0, sck = clk, mosi = mosi, miso = miso)
 print(spi)
 print(spi)
 spi.write("hello rt-thread!")
 spi.write("hello rt-thread!")

+ 6 - 7
examples/stm32l4_pandora/uart.py

@@ -10,10 +10,9 @@
 
 
 from machine import UART
 from machine import UART
 
 
-uart = UART(1, 9600)                          # init with given baudrate
-uart.init(9600, bits=8, parity=None, stop=1)  # init with given parameters
-uart.read(10)                                 # read 10 characters, returns a bytes object
-uart.read()                                   # read all available characters
-uart.readline()                               # read a line
-uart.readinto(buf)                            # read and store into the given buffer
-uart.write('abc')                             # write the 3 characters
+uart = UART(1, 115200)                          # init with given baudrate
+uart.init(115200, bits=8, parity=None, stop=1)  # init with given parameters
+uart.read(10)                                   # read 10 characters, returns a bytes object
+uart.read()                                     # read all available characters
+uart.readline()                                 # read a line
+uart.write('abc')                               # write the 3 characters