Răsfoiți Sursa

【添加】w601 硬件控制模块示例

SummerGift 6 ani în urmă
părinte
comite
776a108797

+ 16 - 0
examples/w601_iot_board/adc.py

@@ -0,0 +1,16 @@
+/*
+ * Copyright (c) 2006-2019, RT-Thread Development Team
+ *
+ * SPDX-License-Identifier: MIT License
+ *
+ * Change Logs:
+ * Date           Author       Notes
+ * 2019-06-13     SummerGift   first version
+ */
+
+from machine import ADC     # 从 machine 导入 ADC 类
+
+adc = ADC(2, 5)             # 创建 ADC 对象,当前使用编号为 2 的 ADC 设备的 5 通道
+adc.read()                  # 获取 ADC 对象采样值
+adc.deinit()                # 关闭 ADC 对象
+adc.init(5)                 # 开启并重新配置 ADC 对象

+ 20 - 0
examples/w601_iot_board/i2c.py

@@ -0,0 +1,20 @@
+/*
+ * Copyright (c) 2006-2019, RT-Thread Development Team
+ *
+ * SPDX-License-Identifier: MIT License
+ *
+ * Change Logs:
+ * Date           Author       Notes
+ * 2019-06-13     SummerGift   first version
+ */
+
+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

+ 27 - 0
examples/w601_iot_board/lcd.py

@@ -0,0 +1,27 @@
+/*
+ * Copyright (c) 2006-2019, RT-Thread Development Team
+ *
+ * SPDX-License-Identifier: MIT License
+ *
+ * Change Logs:
+ * Date           Author       Notes
+ * 2019-06-13     SummerGift   first version
+ */
+
+from machine import LCD     # 从 machine 导入 LCD 类
+
+lcd = LCD()                             # 创建一个 lcd 对象
+lcd.light(False)                        # 关闭背光
+lcd.light(True)                         # 打开背光
+lcd.fill(lcd.BLACK)                     # 将整个 LCD 填充为黑色
+lcd.fill(lcd.RED)                       # 将整个 LCD 填充为红色
+lcd.fill(lcd.GRAY)                      # 将整个 LCD 填充为灰色
+lcd.fill(lcd.WHITE)                     # 将整个 LCD 填充为白色
+lcd.pixel(50, 50, lcd.BLUE)             # 将(50,50)位置的像素填充为蓝色
+lcd.text("hello RT-Thread", 0, 0, 16)   # 在(0, 0) 位置以 16 字号打印字符串
+lcd.text("hello RT-Thread", 0, 16, 24)  # 在(0, 16)位置以 24 字号打印字符串
+lcd.text("hello RT-Thread", 0, 48, 32)  # 在(0, 48)位置以 32 字号打印字符串
+lcd.line(0, 50, 239, 50)                # 以起点(0,50),终点(239,50)画一条线
+lcd.line(0, 50, 239, 50)                # 以起点(0,50),终点(239,50)画一条线
+lcd.rectangle(100, 100, 200, 200)       # 以左上角为(100,100),右下角(200,200)画矩形
+lcd.circle(150, 150, 80)                # 以圆心位置(150,150),半径为 80 画圆

+ 18 - 0
examples/w601_iot_board/pin.py

@@ -0,0 +1,18 @@
+/*
+ * Copyright (c) 2006-2019, RT-Thread Development Team
+ *
+ * SPDX-License-Identifier: MIT License
+ *
+ * Change Logs:
+ * Date           Author       Notes
+ * 2019-06-13     SummerGift   first version
+ */
+
+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
+
+p_in = Pin(("X2", 32), Pin.IN, Pin.PULL_UP)
+p_in.value()                # get value, 0 or 1

+ 19 - 0
examples/w601_iot_board/pwm.py

@@ -0,0 +1,19 @@
+/*
+ * Copyright (c) 2006-2019, RT-Thread Development Team
+ *
+ * SPDX-License-Identifier: MIT License
+ *
+ * Change Logs:
+ * Date           Author       Notes
+ * 2019-06-13     SummerGift   first version
+ */
+
+from machine import PWM     # 从 machine 导入 PWM 类
+
+pwm = PWM(1, 4, 1000, 100)  # 创建 PWM 对象,当前使用编号为 1 的 PWM 设备的 4 通道,初始化的频率为 1000Hz,占空比数值为 100(占空比为 100/255 = 39.22%)
+pwm.freq(2000)              # 设置 PWM 对象频率
+pwm.freq()                  # 获取 PWM 对象频率
+pwm.duty(200)               # 设置 PWM 对象占空比数值
+pwm.duty()                  # 获取 PWM 对象占空比数值
+pwm.deinit()                # 关闭 PWM 对象
+pwm.init(4, 1000, 100)      # 开启并重新配置 PWM 对象

+ 17 - 0
examples/w601_iot_board/rtc.py

@@ -0,0 +1,17 @@
+/*
+ * Copyright (c) 2006-2019, RT-Thread Development Team
+ *
+ * SPDX-License-Identifier: MIT License
+ *
+ * Change Logs:
+ * Date           Author       Notes
+ * 2019-06-13     SummerGift   first version
+ */
+
+from machine import RTC
+
+rtc = RTC()                        # 创建 RTC 设备对象
+rtc.init((2019,6,5,2,10,22,30,0))  # 设置初始化时间
+rtc.now()                          # 获取当前时间
+rtc.deinit()                       # 重置时间到2015年1月1日
+rtc.now()                          # 获取当前时间

+ 19 - 0
examples/w601_iot_board/spi.py

@@ -0,0 +1,19 @@
+/*
+ * Copyright (c) 2006-2019, RT-Thread Development Team
+ *
+ * SPDX-License-Identifier: MIT License
+ *
+ * Change Logs:
+ * Date           Author       Notes
+ * 2019-06-13     SummerGift   first version
+ */
+
+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)
+spi = SPI(-1, 500000, polarity = 0, phase = 0, bits = 8, firstbit = 0, sck = clk, mosi = mosi, miso = miso)
+print(spi)
+spi.write("hello rt-thread!")
+spi.read(10)

+ 19 - 0
examples/w601_iot_board/uart.py

@@ -0,0 +1,19 @@
+/*
+ * Copyright (c) 2006-2019, RT-Thread Development Team
+ *
+ * SPDX-License-Identifier: MIT License
+ *
+ * Change Logs:
+ * Date           Author       Notes
+ * 2019-06-13     SummerGift   first version
+ */
+
+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