├── 00_UF2 ├── Pico_MircoPython_210418.uf2 ├── README.md └── image │ ├── MicroPython.gif │ └── cover.jpg ├── 01_Blink ├── Blink.py └── README.md ├── 02_GPIO ├── GPIO.py ├── README.md └── image │ ├── Schematic.png │ └── physical.png ├── 03_PWM ├── PWM.py └── README.md ├── 04_ADC ├── ADC.py ├── README.md └── image │ ├── Schematic.png │ └── physical.png ├── 05_UART ├── README.md ├── UART.py └── image │ └── Schematic.png ├── 06_I2C ├── I2C_TEST │ ├── I2C_TEST.py │ └── README.md ├── ICM20948 │ ├── ICM20948.py │ └── README.md ├── README.md └── image │ └── Schematic.png ├── 07_SPI ├── LCD │ ├── LCD_TEST.py │ └── README.md ├── README.md ├── XPT2046 │ ├── README.md │ └── XPT2046_TEST.py └── image │ ├── Schematic1.png │ └── Schematic2.png ├── 08_PIO ├── README.md ├── image │ ├── Schematic1.png │ ├── Schematic2.png │ └── Schematic3.png └── ws2812b │ └── ws2812b.py └── README.md /00_UF2/Pico_MircoPython_210418.uf2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/waveshareteam/Pico_MircoPython_Examples/be781912874381175753128423dd5b4b3d501d13/00_UF2/Pico_MircoPython_210418.uf2 -------------------------------------------------------------------------------- /00_UF2/README.md: -------------------------------------------------------------------------------- 1 | # UF2 2 | ## 介绍 3 | Pico系列教程使用的MircoPython固件 4 | ![封面](./image/cover.jpg) 5 | ## 使用方法 6 | ![HowToUse](./image/MicroPython.gif) 7 | -------------------------------------------------------------------------------- /00_UF2/image/MicroPython.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/waveshareteam/Pico_MircoPython_Examples/be781912874381175753128423dd5b4b3d501d13/00_UF2/image/MicroPython.gif -------------------------------------------------------------------------------- /00_UF2/image/cover.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/waveshareteam/Pico_MircoPython_Examples/be781912874381175753128423dd5b4b3d501d13/00_UF2/image/cover.jpg -------------------------------------------------------------------------------- /01_Blink/Blink.py: -------------------------------------------------------------------------------- 1 | from machine import Pin 2 | import time 3 | led = Pin(25, Pin.OUT) 4 | while True: 5 | led.toggle() 6 | time.sleep(1) -------------------------------------------------------------------------------- /01_Blink/README.md: -------------------------------------------------------------------------------- 1 | # Blink 2 | ## 实现功能 3 | 对GPIO25每秒进行一次反转,使Pico板载LED循环亮一秒灭一秒. -------------------------------------------------------------------------------- /02_GPIO/GPIO.py: -------------------------------------------------------------------------------- 1 | from machine import Pin 2 | import utime 3 | #Initialize GPIO15, and set the pull-up input mode 4 | button_num = 15 5 | button = Pin(button_num,Pin.IN,Pin.PULL_UP) 6 | 7 | #Initialize GPIO16, and set the output mode 8 | external_led_num =16 9 | external_led = Pin(external_led_num,Pin.OUT) 10 | 11 | #Initialize GPIO25, and set the output mode 12 | led_num = 25 13 | led = Pin(led_num,Pin.OUT) 14 | 15 | print("button gpio={0}".format(button_num)) 16 | 17 | while True: 18 | #led turn off 19 | led.off() 20 | #Read button value 21 | if(button.value()==0): 22 | utime.sleep(0.01) 23 | if(button.value()==0): 24 | #Toggle led 25 | external_led.toggle() 26 | #led turn on 27 | led.on() 28 | 29 | print("button is pressed") 30 | while(button.value()==0): 31 | utime.sleep(0.01) -------------------------------------------------------------------------------- /02_GPIO/README.md: -------------------------------------------------------------------------------- 1 | # GPIO 2 | ## 器件清单 3 | 器件 | 数量 4 | ---|--- 5 | 焊接排针的Pico | 1 6 | 面包板 | 1 7 | 直插按键 | 1 8 | 直插LED | 1 9 | 适当阻值的直插电阻 | 1 10 | 双公头杜邦线 | 若干 11 | ## 原理图 12 | ![原理图](./image/Schematic.png) 13 | ## 实物连接图 14 | ![实物连接图](./image/physical.png) 15 | ## 实现功能 16 | 当按键(GPIO25)按下时,外部LED电平翻转(GPIO16)并点亮板载LED(GPIO25). 17 | 当按键(GPIO25)释放后,熄灭板载LED(GPIO25). 18 | ## machine.Pin类 19 | * machine.Pin(id, mode=None, pull=None, value) 20 | * Pin对象构造函数 21 | * id:GPIO编号,数值为0-29,如使用GPIO13则此处填写13,。; 22 | * mode:模式,可选None、Pin.IN(0)、Pin.OUT(1)、Pin.OPEN_DRAIN(2); 23 | * pull:使用内部上下拉电阻,仅在输入模式下有效,可选 None、Pin.PULL_UP(1)、Pin.PULL_DOWN(2); 24 | * value:输出或开漏模式下端口值,0为低(off)、1为高(on); 25 | * Pin.init(mode=None, pull=None) 26 | * 重新初始化GPIO口; 27 | * mode:模式,可选None、Pin.IN(0)、Pin.OUT(1)、Pin.OPEN_DRAIN(2); 28 | * pull:使用内部上下拉电阻,仅在输入模式下有效,可选 None、Pin.PULL_UP(1)、Pin.PULL_DOWN(2); 29 | * Pin.value([x]) 30 | * 不填写参数的情况下返回GPIO端口数值,在填写参数的情况下将参数写入GPIO端口中,参数可为0或者1; 31 | * Pin.toggle() 32 | * 输出或开漏模式下将端口设置翻转 33 | * Pin.low() 34 | * 输出或开漏模式下将端口设置为低; 35 | * Pin.off() 36 | * 输出或开漏模式下将端口设置为低; 37 | * Pin.high() 38 | * 输出或开漏模式下将端口设置为高; 39 | * Pin.on() 40 | * 输出或开漏模式下将端口设置为高; 41 | * Pin.irq(handler=None,trigger=(Pin.IRQ_FALLING | Pin.IRQ_RISING)) 42 | * 用于设置外部中断 43 | * handler:中断触发回调函数; 44 | * trigger:中断触发条件,设置为: 45 | * Pin.IRQ_FALLING 下降沿中断。 46 | * Pin.IRQ_RISING 在上升沿中断 -------------------------------------------------------------------------------- /02_GPIO/image/Schematic.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/waveshareteam/Pico_MircoPython_Examples/be781912874381175753128423dd5b4b3d501d13/02_GPIO/image/Schematic.png -------------------------------------------------------------------------------- /02_GPIO/image/physical.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/waveshareteam/Pico_MircoPython_Examples/be781912874381175753128423dd5b4b3d501d13/02_GPIO/image/physical.png -------------------------------------------------------------------------------- /03_PWM/PWM.py: -------------------------------------------------------------------------------- 1 | from machine import Pin, PWM 2 | import utime 3 | 4 | #Set GPIO25 output pwm 5 | LED = PWM(Pin(25)) 6 | 7 | #Set pwm frequency to 1000HZ 8 | LED.freq(10000) 9 | 10 | LED_duty = 0 11 | LED_direction = 1 12 | 13 | while True: 14 | LED_duty += LED_direction 15 | if LED_duty >= 100: 16 | LED_duty = 100 17 | LED_direction = -1 18 | elif LED_duty <= 0: 19 | LED_duty = 0 20 | LED_direction = 1 21 | LED.duty_u16(int(LED_duty * 655.36)) 22 | if LED_duty%5 == 0: 23 | print(LED_duty) 24 | utime.sleep(0.01) 25 | 26 | 27 | -------------------------------------------------------------------------------- /03_PWM/README.md: -------------------------------------------------------------------------------- 1 | # PWM 2 | ## 器件清单 3 | 器件 | 数量 4 | ---|--- 5 | Pico | 1 6 | 7 | ## 实现功能 8 | 使用PWM驱动LED(GPIO25)逐渐变亮,再逐渐熄灭。 9 | ## machine.PWM类 10 | * machine.PWM(pin): 11 | * PWM对象构造函数 12 | * pin:需要设置为PWM输出的GPIO对象; 13 | * PWM.deinit(): 14 | * PWM反初始化 15 | * PWM.freq([value]): 16 | * 设置PWM输出频率函数。 17 | * value: PWM输出频率,数值应符合PWM频率计算公式,过大或过小都会导致分辨率变小; 18 | * PWM.duty_u16([value]): 19 | * 设定计数器比较值, 20 | * value: 设置占空比比例,数值应在0-65536间; 21 | * PWM.duty_ns([value]): 22 | * 设定高电平的时间; 23 | * value: 设置高电平时间,单位为ns -------------------------------------------------------------------------------- /04_ADC/ADC.py: -------------------------------------------------------------------------------- 1 | from machine import Pin, ADC 2 | import utime 3 | 4 | #Initialize the ADC channel 5 | ADC0= ADC(Pin(26)) 6 | sensor_temp = ADC(4) 7 | 8 | while True: 9 | 10 | #Read ADC channel 0 voltage 11 | reading = ADC0.read_u16()*3.3/65535 12 | print("ADC0 voltage = {0:.2f}V \r\n".format(reading)) 13 | 14 | #Temperature is captured using an internal temperature sensor 15 | reading = sensor_temp.read_u16()*3.3/65535 16 | temperature = 27 - (reading - 0.706)/0.001721 17 | print("temperature = {0:.2f}℃ \r\n".format(temperature)) 18 | 19 | utime.sleep_ms(1000) 20 | -------------------------------------------------------------------------------- /04_ADC/README.md: -------------------------------------------------------------------------------- 1 | # ADC 2 | ## 器件清单 3 | 器件 | 数量 4 | ---|--- 5 | 焊接排针的Pico | 1 6 | 面包板 | 1 7 | 蓝白电位器 | 1 8 | 双公头杜邦线 | 若干 9 | ## 原理图 10 | ![原理图](./image/Schematic.png) 11 | ## 实物连接图 12 | ![实物连接图](./image/physical.png) 13 | ## 实现功能 14 | 每秒读取一次读取GPIO26上的电压,并使用片内温度传感器采集温度。 15 | ## machine.ADC类 16 | * machine.ADC(id): 17 | * ADC对象构造函数,并初始化对应通道。 18 | * id:可为GPIO对象,也可为ADC通道; 19 | * ADC.read_u16(): 20 | * 读取对应通道ADC数值 -------------------------------------------------------------------------------- /04_ADC/image/Schematic.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/waveshareteam/Pico_MircoPython_Examples/be781912874381175753128423dd5b4b3d501d13/04_ADC/image/Schematic.png -------------------------------------------------------------------------------- /04_ADC/image/physical.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/waveshareteam/Pico_MircoPython_Examples/be781912874381175753128423dd5b4b3d501d13/04_ADC/image/physical.png -------------------------------------------------------------------------------- /05_UART/README.md: -------------------------------------------------------------------------------- 1 | # UART 2 | ## 器件清单 3 | 器件 | 数量 4 | ---|--- 5 | 焊接排针的Pico | 1 6 | Pico-Eval-Board | 1 7 | USB TO Micro USB数据线 | 2 8 | ## 原理图 9 | ![原理图](./image/Schematic.png) 10 | ## 实现功能 11 | 根据UART接收到数据,开启或者关闭LED 12 | ## machine.UART类 13 | * machine.UART(id,baudrate=115200,bits=8,parity=None,stop=1,tx=None,rx=None): 14 | * UART对象构造函数,作用为初始化对应通道和引脚. 15 | * id:使用UART通道,可为0或者1; 16 | * baudrate: 波特率参数 17 | * bits:数据位长度 18 | * parity:奇偶校验位 19 | * stop:停止位长度 20 | * tx:TXD引脚,应为Pin对象 21 | * rx:RXD引脚,应为Pin对象 22 | 23 | machine.UART为UART对象构造函数,作用为初始化对应通道和引脚. 24 | 25 | 第一个参数id为使用UART通道,可为0或者1 26 | 27 | 第二个参数baudrate为使用波特率 28 | 29 | 第三个参数bits为数据位长度(现阶段仅8位有效) 30 | 31 | 第四个参数parity为是否使用奇偶校验位 32 | 33 | 第五个参数stop为停止位长度 34 | 35 | 第六和第七个参数tx和rx 为收发引脚,应为Pin对象 36 | 37 | * UART.any(): 38 | * any函数,用于检测当前接收缓冲区是否有数据,接收缓冲区有数据就返回1,否则返回0. 39 | 40 | * UART.read([nbytes]): 41 | * read函数,用于读取字符串。 42 | * nbytes:如果指定了'nbytes,则最多读取这么多字节,否则读取尽可能多的数据。 43 | 44 | * UART.readline() 45 | * readline函数,读取一行,以换行符为结束标志。 46 | 47 | * UART.readinto(buf[, nbytes]) 48 | * readinto:将读取字符串存入指定缓存中 49 | * buf:用于指定缓存 50 | * nbytes:如果指定了'nbytes,则最多读取这么多字节,否则读取尽可能多的数据。 51 | 52 | readinto函数,将读取字符串存入指定缓存中 53 | buf用于指定缓存 54 | nbytes和上文中read函数nbytes作用一致 55 | 56 | * UART.write(buf) 57 | * write函数,用于发送字符串,返回值发送的字节数。 58 | * buf: 发送字符串 59 | write函数用于发送字符串,并返回发送的字节数。 60 | 参数buf为需要发送的字符串 61 | 62 | * UART.sendbreak() 63 | * sendbreak函数在总线上发送停止信号。 64 | 这会将总线驱动为低电平的时间比字符正常传输所需要的时间更长。 -------------------------------------------------------------------------------- /05_UART/UART.py: -------------------------------------------------------------------------------- 1 | from machine import UART,Pin 2 | import utime 3 | 4 | uart = UART(0,baudrate=115200,tx=Pin(0),rx=Pin(1)) 5 | led = Pin(25,Pin.OUT) 6 | uart.write("Waveshare Uart Test\r\n") 7 | uart.write("Please enter character 0 or 1 to switch the LED on and off\r\n") 8 | while True: 9 | if uart.any() != False: 10 | print(uart.any()) 11 | buf=uart.read(1) 12 | if buf == b'1': 13 | led.on() 14 | print("LED ON") 15 | uart.write("LED ON\r\n") 16 | elif buf == b'0': 17 | led.off() 18 | print("LED OFF") 19 | uart.write("LED OFF\r\n") 20 | else : 21 | print("Please enter character 0 or 1 to switch the LED on and off") 22 | uart.write("Please enter character 0 or 1 to switch the LED on and off\r\n") 23 | 24 | -------------------------------------------------------------------------------- /05_UART/image/Schematic.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/waveshareteam/Pico_MircoPython_Examples/be781912874381175753128423dd5b4b3d501d13/05_UART/image/Schematic.png -------------------------------------------------------------------------------- /06_I2C/I2C_TEST/I2C_TEST.py: -------------------------------------------------------------------------------- 1 | from machine import I2C,Pin 2 | #Initialize the I2C 3 | i2c = I2C(id=1,scl=Pin(7),sda=Pin(6),freq=100_000) 4 | # Get the address of all devices under the I2C bus 5 | addr_list = i2c.scan() 6 | # Resolving device information 7 | if len(addr_list) == 1: 8 | who = i2c.readfrom_mem(addr_list[0],0x00,1) 9 | # Identify ICM20948 10 | if who[0] == 0xEA : 11 | print("Just a ICM20948 connected") 12 | else : 13 | print("Have a device connected but it is not ICM20948") 14 | # Nothing connected 15 | elif len(addr_list) == 0: 16 | print("Nothing connected") 17 | # More than one device is connected 18 | else: 19 | print("More than one device is connected") -------------------------------------------------------------------------------- /06_I2C/I2C_TEST/README.md: -------------------------------------------------------------------------------- 1 | # I2C_TEST 2 | ## 实现功能 3 | * 判断是否只有一个设备连接到到I2C总线,并判断是否为ICM20948. -------------------------------------------------------------------------------- /06_I2C/ICM20948/ICM20948.py: -------------------------------------------------------------------------------- 1 | from machine import I2C 2 | import time 3 | import math 4 | 5 | Gyro = [0,0,0] 6 | Accel = [0,0,0] 7 | Mag = [0,0,0] 8 | pitch = 0.0 9 | roll = 0.0 10 | yaw = 0.0 11 | pu8data=[0,0,0,0,0,0,0,0] 12 | U8tempX=[0,0,0,0,0,0,0,0,0] 13 | U8tempY=[0,0,0,0,0,0,0,0,0] 14 | U8tempZ=[0,0,0,0,0,0,0,0,0] 15 | GyroOffset=[0,0,0] 16 | Ki = 1.0 17 | Kp = 4.50 18 | q0 = 1.0 19 | q1=q2=q3=0.0 20 | angles=[0.0,0.0,0.0] 21 | true =0x01 22 | false =0x00 23 | # define ICM-20948 Device I2C address 24 | I2C_ADD_ICM20948 = 0x68 25 | I2C_ADD_ICM20948_AK09916 = 0x0C 26 | I2C_ADD_ICM20948_AK09916_READ = 0x80 27 | I2C_ADD_ICM20948_AK09916_WRITE = 0x00 28 | # define ICM-20948 Register 29 | # user bank 0 register 30 | REG_ADD_WIA = 0x00 31 | REG_VAL_WIA = 0xEA 32 | REG_ADD_USER_CTRL = 0x03 33 | REG_VAL_BIT_DMP_EN = 0x80 34 | REG_VAL_BIT_FIFO_EN = 0x40 35 | REG_VAL_BIT_I2C_MST_EN = 0x20 36 | REG_VAL_BIT_I2C_IF_DIS = 0x10 37 | REG_VAL_BIT_DMP_RST = 0x08 38 | REG_VAL_BIT_DIAMOND_DMP_RST = 0x04 39 | REG_ADD_PWR_MIGMT_1 = 0x06 40 | REG_VAL_ALL_RGE_RESET = 0x80 41 | REG_VAL_RUN_MODE = 0x01 # Non low-power mode 42 | REG_ADD_LP_CONFIG = 0x05 43 | REG_ADD_PWR_MGMT_1 = 0x06 44 | REG_ADD_PWR_MGMT_2 = 0x07 45 | REG_ADD_ACCEL_XOUT_H = 0x2D 46 | REG_ADD_ACCEL_XOUT_L = 0x2E 47 | REG_ADD_ACCEL_YOUT_H = 0x2F 48 | REG_ADD_ACCEL_YOUT_L = 0x30 49 | REG_ADD_ACCEL_ZOUT_H = 0x31 50 | REG_ADD_ACCEL_ZOUT_L = 0x32 51 | REG_ADD_GYRO_XOUT_H = 0x33 52 | REG_ADD_GYRO_XOUT_L = 0x34 53 | REG_ADD_GYRO_YOUT_H = 0x35 54 | REG_ADD_GYRO_YOUT_L = 0x36 55 | REG_ADD_GYRO_ZOUT_H = 0x37 56 | REG_ADD_GYRO_ZOUT_L = 0x38 57 | REG_ADD_EXT_SENS_DATA_00 = 0x3B 58 | REG_ADD_REG_BANK_SEL = 0x7F 59 | REG_VAL_REG_BANK_0 = 0x00 60 | REG_VAL_REG_BANK_1 = 0x10 61 | REG_VAL_REG_BANK_2 = 0x20 62 | REG_VAL_REG_BANK_3 = 0x30 63 | 64 | # user bank 1 register 65 | # user bank 2 register 66 | REG_ADD_GYRO_SMPLRT_DIV = 0x00 67 | REG_ADD_GYRO_CONFIG_1 = 0x01 68 | REG_VAL_BIT_GYRO_DLPCFG_2 = 0x10 # bit[5:3] 69 | REG_VAL_BIT_GYRO_DLPCFG_4 = 0x20 # bit[5:3] 70 | REG_VAL_BIT_GYRO_DLPCFG_6 = 0x30 # bit[5:3] 71 | REG_VAL_BIT_GYRO_FS_250DPS = 0x00 # bit[2:1] 72 | REG_VAL_BIT_GYRO_FS_500DPS = 0x02 # bit[2:1] 73 | REG_VAL_BIT_GYRO_FS_1000DPS = 0x04 # bit[2:1] 74 | REG_VAL_BIT_GYRO_FS_2000DPS = 0x06 # bit[2:1] 75 | REG_VAL_BIT_GYRO_DLPF = 0x01 # bit[0] 76 | REG_ADD_ACCEL_SMPLRT_DIV_2 = 0x11 77 | REG_ADD_ACCEL_CONFIG = 0x14 78 | REG_VAL_BIT_ACCEL_DLPCFG_2 = 0x10 # bit[5:3] 79 | REG_VAL_BIT_ACCEL_DLPCFG_4 = 0x20 # bit[5:3] 80 | REG_VAL_BIT_ACCEL_DLPCFG_6 = 0x30 # bit[5:3] 81 | REG_VAL_BIT_ACCEL_FS_2g = 0x00 # bit[2:1] 82 | REG_VAL_BIT_ACCEL_FS_4g = 0x02 # bit[2:1] 83 | REG_VAL_BIT_ACCEL_FS_8g = 0x04 # bit[2:1] 84 | REG_VAL_BIT_ACCEL_FS_16g = 0x06 # bit[2:1] 85 | REG_VAL_BIT_ACCEL_DLPF = 0x01 # bit[0] 86 | 87 | # user bank 3 register 88 | REG_ADD_I2C_SLV0_ADDR = 0x03 89 | REG_ADD_I2C_SLV0_REG = 0x04 90 | REG_ADD_I2C_SLV0_CTRL = 0x05 91 | REG_VAL_BIT_SLV0_EN = 0x80 92 | REG_VAL_BIT_MASK_LEN = 0x07 93 | REG_ADD_I2C_SLV0_DO = 0x06 94 | REG_ADD_I2C_SLV1_ADDR = 0x07 95 | REG_ADD_I2C_SLV1_REG = 0x08 96 | REG_ADD_I2C_SLV1_CTRL = 0x09 97 | REG_ADD_I2C_SLV1_DO = 0x0A 98 | 99 | # define ICM-20948 Register end 100 | 101 | # define ICM-20948 MAG Register 102 | REG_ADD_MAG_WIA1 = 0x00 103 | REG_VAL_MAG_WIA1 = 0x48 104 | REG_ADD_MAG_WIA2 = 0x01 105 | REG_VAL_MAG_WIA2 = 0x09 106 | REG_ADD_MAG_ST2 = 0x10 107 | REG_ADD_MAG_DATA = 0x11 108 | REG_ADD_MAG_CNTL2 = 0x31 109 | REG_VAL_MAG_MODE_PD = 0x00 110 | REG_VAL_MAG_MODE_SM = 0x01 111 | REG_VAL_MAG_MODE_10HZ = 0x02 112 | REG_VAL_MAG_MODE_20HZ = 0x04 113 | REG_VAL_MAG_MODE_50HZ = 0x05 114 | REG_VAL_MAG_MODE_100HZ = 0x08 115 | REG_VAL_MAG_MODE_ST = 0x10 116 | # define ICM-20948 MAG Register end 117 | 118 | MAG_DATA_LEN =6 119 | 120 | class ICM20948(object): 121 | def __init__(self,address=I2C_ADD_ICM20948): 122 | self._address = address 123 | self._bus = I2C(1) 124 | bRet=self.icm20948Check() #Initialization of the device multiple times after power on will result in a return error 125 | # while true != bRet: 126 | # print("ICM-20948 Error\n" ) 127 | # time.sleep(0.5) 128 | # print("ICM-20948 OK\n" ) 129 | time.sleep(0.5) #We can skip this detection by delaying it by 500 milliseconds 130 | # user bank 0 register 131 | self._write_byte( REG_ADD_REG_BANK_SEL , REG_VAL_REG_BANK_0) 132 | self._write_byte( REG_ADD_PWR_MIGMT_1 , REG_VAL_ALL_RGE_RESET) 133 | time.sleep(0.1) 134 | self._write_byte( REG_ADD_PWR_MIGMT_1 , REG_VAL_RUN_MODE) 135 | #user bank 2 register 136 | self._write_byte( REG_ADD_REG_BANK_SEL , REG_VAL_REG_BANK_2) 137 | self._write_byte( REG_ADD_GYRO_SMPLRT_DIV , 0x07) 138 | self._write_byte( REG_ADD_GYRO_CONFIG_1 , REG_VAL_BIT_GYRO_DLPCFG_6 | REG_VAL_BIT_GYRO_FS_1000DPS | REG_VAL_BIT_GYRO_DLPF) 139 | self._write_byte( REG_ADD_ACCEL_SMPLRT_DIV_2 , 0x07) 140 | self._write_byte( REG_ADD_ACCEL_CONFIG , REG_VAL_BIT_ACCEL_DLPCFG_6 | REG_VAL_BIT_ACCEL_FS_2g | REG_VAL_BIT_ACCEL_DLPF) 141 | #user bank 0 register 142 | self._write_byte( REG_ADD_REG_BANK_SEL , REG_VAL_REG_BANK_0) 143 | time.sleep(0.1) 144 | self.icm20948GyroOffset() 145 | self.icm20948MagCheck() 146 | self.icm20948WriteSecondary( I2C_ADD_ICM20948_AK09916|I2C_ADD_ICM20948_AK09916_WRITE,REG_ADD_MAG_CNTL2, REG_VAL_MAG_MODE_20HZ) 147 | def icm20948_Gyro_Accel_Read(self): 148 | self._write_byte( REG_ADD_REG_BANK_SEL , REG_VAL_REG_BANK_0) 149 | data =self._read_block(REG_ADD_ACCEL_XOUT_H, 12) 150 | self._write_byte( REG_ADD_REG_BANK_SEL , REG_VAL_REG_BANK_2) 151 | Accel[0] = (data[0]<<8)|data[1] 152 | Accel[1] = (data[2]<<8)|data[3] 153 | Accel[2] = (data[4]<<8)|data[5] 154 | Gyro[0] = ((data[6]<<8)|data[7]) - GyroOffset[0] 155 | Gyro[1] = ((data[8]<<8)|data[9]) - GyroOffset[1] 156 | Gyro[2] = ((data[10]<<8)|data[11]) - GyroOffset[2] 157 | if Accel[0]>=32767: #Solve the problem that Python shift will not overflow 158 | Accel[0]=Accel[0]-65535 159 | elif Accel[0]<=-32767: 160 | Accel[0]=Accel[0]+65535 161 | if Accel[1]>=32767: 162 | Accel[1]=Accel[1]-65535 163 | elif Accel[1]<=-32767: 164 | Accel[1]=Accel[1]+65535 165 | if Accel[2]>=32767: 166 | Accel[2]=Accel[2]-65535 167 | elif Accel[2]<=-32767: 168 | Accel[2]=Accel[2]+65535 169 | if Gyro[0]>=32767: 170 | Gyro[0]=Gyro[0]-65535 171 | elif Gyro[0]<=-32767: 172 | Gyro[0]=Gyro[0]+65535 173 | if Gyro[1]>=32767: 174 | Gyro[1]=Gyro[1]-65535 175 | elif Gyro[1]<=-32767: 176 | Gyro[1]=Gyro[1]+65535 177 | if Gyro[2]>=32767: 178 | Gyro[2]=Gyro[2]-65535 179 | elif Gyro[2]<=-32767: 180 | Gyro[2]=Gyro[2]+65535 181 | def icm20948MagRead(self): 182 | counter=20 183 | while(counter>0): 184 | time.sleep(0.01) 185 | self.icm20948ReadSecondary( I2C_ADD_ICM20948_AK09916|I2C_ADD_ICM20948_AK09916_READ , REG_ADD_MAG_ST2, 1) 186 | if ((pu8data[0] & 0x01)!= 0): 187 | break 188 | counter-=1 189 | if counter!=0: 190 | for i in range(0,8): 191 | self.icm20948ReadSecondary( I2C_ADD_ICM20948_AK09916|I2C_ADD_ICM20948_AK09916_READ , REG_ADD_MAG_DATA , MAG_DATA_LEN) 192 | U8tempX[i] = (pu8data[1]<<8)|pu8data[0] 193 | U8tempY[i] = (pu8data[3]<<8)|pu8data[2] 194 | U8tempZ[i] = (pu8data[5]<<8)|pu8data[4] 195 | Mag[0]=(U8tempX[0]+U8tempX[1]+U8tempX[2]+U8tempX[3]+U8tempX[4]+U8tempX[5]+U8tempX[6]+U8tempX[7])/8 196 | Mag[1]=-(U8tempY[0]+U8tempY[1]+U8tempY[2]+U8tempY[3]+U8tempY[4]+U8tempY[5]+U8tempY[6]+U8tempY[7])/8 197 | Mag[2]=-(U8tempZ[0]+U8tempZ[1]+U8tempZ[2]+U8tempZ[3]+U8tempZ[4]+U8tempZ[5]+U8tempZ[6]+U8tempZ[7])/8 198 | if Mag[0]>=32767: #Solve the problem that Python shift will not overflow 199 | Mag[0]=Mag[0]-65535 200 | elif Mag[0]<=-32767: 201 | Mag[0]=Mag[0]+65535 202 | if Mag[1]>=32767: 203 | Mag[1]=Mag[1]-65535 204 | elif Mag[1]<=-32767: 205 | Mag[1]=Mag[1]+65535 206 | if Mag[2]>=32767: 207 | Mag[2]=Mag[2]-65535 208 | elif Mag[2]<=-32767: 209 | Mag[2]=Mag[2]+65535 210 | def icm20948ReadSecondary(self,u8I2CAddr,u8RegAddr,u8Len): 211 | u8Temp=0 212 | self._write_byte( REG_ADD_REG_BANK_SEL, REG_VAL_REG_BANK_3) #swtich bank3 213 | self._write_byte( REG_ADD_I2C_SLV0_ADDR, u8I2CAddr) 214 | self._write_byte( REG_ADD_I2C_SLV0_REG, u8RegAddr) 215 | self._write_byte( REG_ADD_I2C_SLV0_CTRL, REG_VAL_BIT_SLV0_EN|u8Len) 216 | 217 | self._write_byte( REG_ADD_REG_BANK_SEL, REG_VAL_REG_BANK_0) #swtich bank0 218 | 219 | u8Temp = self._read_byte(REG_ADD_USER_CTRL) 220 | u8Temp |= REG_VAL_BIT_I2C_MST_EN 221 | self._write_byte( REG_ADD_USER_CTRL, u8Temp) 222 | time.sleep(0.01) 223 | u8Temp &= ~REG_VAL_BIT_I2C_MST_EN 224 | self._write_byte( REG_ADD_USER_CTRL, u8Temp) 225 | 226 | for i in range(0,u8Len): 227 | pu8data[i]= self._read_byte( REG_ADD_EXT_SENS_DATA_00+i) 228 | 229 | self._write_byte( REG_ADD_REG_BANK_SEL, REG_VAL_REG_BANK_3) #swtich bank3 230 | 231 | u8Temp = self._read_byte(REG_ADD_I2C_SLV0_CTRL) 232 | u8Temp &= ~((REG_VAL_BIT_I2C_MST_EN)&(REG_VAL_BIT_MASK_LEN)) 233 | self._write_byte( REG_ADD_I2C_SLV0_CTRL, u8Temp) 234 | 235 | self._write_byte( REG_ADD_REG_BANK_SEL, REG_VAL_REG_BANK_0) #swtich bank0 236 | def icm20948WriteSecondary(self,u8I2CAddr,u8RegAddr,u8data): 237 | u8Temp=0 238 | self._write_byte( REG_ADD_REG_BANK_SEL, REG_VAL_REG_BANK_3) #swtich bank3 239 | self._write_byte( REG_ADD_I2C_SLV1_ADDR, u8I2CAddr) 240 | self._write_byte( REG_ADD_I2C_SLV1_REG, u8RegAddr) 241 | self._write_byte( REG_ADD_I2C_SLV1_DO, u8data) 242 | self._write_byte( REG_ADD_I2C_SLV1_CTRL, REG_VAL_BIT_SLV0_EN|1) 243 | 244 | self._write_byte( REG_ADD_REG_BANK_SEL, REG_VAL_REG_BANK_0) #swtich bank0 245 | 246 | u8Temp = self._read_byte(REG_ADD_USER_CTRL) 247 | u8Temp |= REG_VAL_BIT_I2C_MST_EN 248 | self._write_byte( REG_ADD_USER_CTRL, u8Temp) 249 | time.sleep(0.01) 250 | u8Temp &= ~REG_VAL_BIT_I2C_MST_EN 251 | self._write_byte( REG_ADD_USER_CTRL, u8Temp) 252 | 253 | self._write_byte( REG_ADD_REG_BANK_SEL, REG_VAL_REG_BANK_3) #swtich bank3 254 | 255 | u8Temp = self._read_byte(REG_ADD_I2C_SLV0_CTRL) 256 | u8Temp &= ~((REG_VAL_BIT_I2C_MST_EN)&(REG_VAL_BIT_MASK_LEN)) 257 | self._write_byte( REG_ADD_I2C_SLV0_CTRL, u8Temp) 258 | 259 | self._write_byte( REG_ADD_REG_BANK_SEL, REG_VAL_REG_BANK_0) #swtich bank0 260 | def icm20948GyroOffset(self): 261 | s32TempGx = 0 262 | s32TempGy = 0 263 | s32TempGz = 0 264 | for i in range(0,32): 265 | self.icm20948_Gyro_Accel_Read() 266 | s32TempGx += Gyro[0] 267 | s32TempGy += Gyro[1] 268 | s32TempGz += Gyro[2] 269 | time.sleep(0.01) 270 | GyroOffset[0] = s32TempGx >> 5 271 | GyroOffset[1] = s32TempGy >> 5 272 | GyroOffset[2] = s32TempGz >> 5 273 | def _read_byte(self,cmd): 274 | rec=self._bus.readfrom_mem(int(self._address),int(cmd),1) 275 | return rec[0] 276 | def _read_block(self, reg, length=1): 277 | rec=self._bus.readfrom_mem(int(self._address),int(reg),length) 278 | return rec 279 | def _read_u16(self,cmd): 280 | LSB = self._bus.readfrom_mem(int(self._address),int(cmd),1) 281 | MSB = self._bus.readfrom_mem(int(self._address),int(cmd)+1,1) 282 | return (MSB[0] << 8) + LSB[0] 283 | 284 | def _write_byte(self,cmd,val): 285 | self._bus.writeto_mem(int(self._address),int(cmd),bytes([int(val)])) 286 | time.sleep(0.0001) 287 | def imuAHRSupdate(self,gx, gy,gz,ax,ay,az,mx,my,mz): 288 | norm=0.0 289 | hx = hy = hz = bx = bz = 0.0 290 | vx = vy = vz = wx = wy = wz = 0.0 291 | exInt = eyInt = ezInt = 0.0 292 | ex=ey=ez=0.0 293 | halfT = 0.024 294 | global q0 295 | global q1 296 | global q2 297 | global q3 298 | q0q0 = q0 * q0 299 | q0q1 = q0 * q1 300 | q0q2 = q0 * q2 301 | q0q3 = q0 * q3 302 | q1q1 = q1 * q1 303 | q1q2 = q1 * q2 304 | q1q3 = q1 * q3 305 | q2q2 = q2 * q2 306 | q2q3 = q2 * q3 307 | q3q3 = q3 * q3 308 | 309 | norm = float(1/math.sqrt(ax * ax + ay * ay + az * az)) 310 | ax = ax * norm 311 | ay = ay * norm 312 | az = az * norm 313 | 314 | norm = float(1/math.sqrt(mx * mx + my * my + mz * mz)) 315 | mx = mx * norm 316 | my = my * norm 317 | mz = mz * norm 318 | 319 | # compute reference direction of flux 320 | hx = 2 * mx * (0.5 - q2q2 - q3q3) + 2 * my * (q1q2 - q0q3) + 2 * mz * (q1q3 + q0q2) 321 | hy = 2 * mx * (q1q2 + q0q3) + 2 * my * (0.5 - q1q1 - q3q3) + 2 * mz * (q2q3 - q0q1) 322 | hz = 2 * mx * (q1q3 - q0q2) + 2 * my * (q2q3 + q0q1) + 2 * mz * (0.5 - q1q1 - q2q2) 323 | bx = math.sqrt((hx * hx) + (hy * hy)) 324 | bz = hz 325 | 326 | # estimated direction of gravity and flux (v and w) 327 | vx = 2 * (q1q3 - q0q2) 328 | vy = 2 * (q0q1 + q2q3) 329 | vz = q0q0 - q1q1 - q2q2 + q3q3 330 | wx = 2 * bx * (0.5 - q2q2 - q3q3) + 2 * bz * (q1q3 - q0q2) 331 | wy = 2 * bx * (q1q2 - q0q3) + 2 * bz * (q0q1 + q2q3) 332 | wz = 2 * bx * (q0q2 + q1q3) + 2 * bz * (0.5 - q1q1 - q2q2) 333 | 334 | # error is sum of cross product between reference direction of fields and direction measured by sensors 335 | ex = (ay * vz - az * vy) + (my * wz - mz * wy) 336 | ey = (az * vx - ax * vz) + (mz * wx - mx * wz) 337 | ez = (ax * vy - ay * vx) + (mx * wy - my * wx) 338 | 339 | if (ex != 0.0 and ey != 0.0 and ez != 0.0): 340 | exInt = exInt + ex * Ki * halfT 341 | eyInt = eyInt + ey * Ki * halfT 342 | ezInt = ezInt + ez * Ki * halfT 343 | 344 | gx = gx + Kp * ex + exInt 345 | gy = gy + Kp * ey + eyInt 346 | gz = gz + Kp * ez + ezInt 347 | 348 | q0 = q0 + (-q1 * gx - q2 * gy - q3 * gz) * halfT 349 | q1 = q1 + (q0 * gx + q2 * gz - q3 * gy) * halfT 350 | q2 = q2 + (q0 * gy - q1 * gz + q3 * gx) * halfT 351 | q3 = q3 + (q0 * gz + q1 * gy - q2 * gx) * halfT 352 | 353 | norm = float(1/math.sqrt(q0 * q0 + q1 * q1 + q2 * q2 + q3 * q3)) 354 | q0 = q0 * norm 355 | q1 = q1 * norm 356 | q2 = q2 * norm 357 | q3 = q3 * norm 358 | def icm20948Check(self): 359 | bRet=false 360 | if REG_VAL_WIA == self._read_byte(REG_ADD_WIA): 361 | bRet = true 362 | return bRet 363 | def icm20948MagCheck(self): 364 | self.icm20948ReadSecondary( I2C_ADD_ICM20948_AK09916|I2C_ADD_ICM20948_AK09916_READ,REG_ADD_MAG_WIA1, 2) 365 | if (pu8data[0] == REG_VAL_MAG_WIA1) and ( pu8data[1] == REG_VAL_MAG_WIA2) : 366 | bRet = true 367 | return bRet 368 | def icm20948CalAvgValue(self): 369 | MotionVal[0]=Gyro[0]/32.8 370 | MotionVal[1]=Gyro[1]/32.8 371 | MotionVal[2]=Gyro[2]/32.8 372 | MotionVal[3]=Accel[0] 373 | MotionVal[4]=Accel[1] 374 | MotionVal[5]=Accel[2] 375 | MotionVal[6]=Mag[0] 376 | MotionVal[7]=Mag[1] 377 | MotionVal[8]=Mag[2] 378 | 379 | if __name__ == '__main__': 380 | import time 381 | print("\nSense HAT Test Program ...\n") 382 | MotionVal=[0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0] 383 | icm20948=ICM20948() 384 | while True: 385 | icm20948.icm20948_Gyro_Accel_Read() 386 | icm20948.icm20948MagRead() 387 | icm20948.icm20948CalAvgValue() 388 | time.sleep(0.1) 389 | icm20948.imuAHRSupdate(MotionVal[0] * 0.0175, MotionVal[1] * 0.0175,MotionVal[2] * 0.0175, 390 | MotionVal[3],MotionVal[4],MotionVal[5], 391 | MotionVal[6], MotionVal[7], MotionVal[8]) 392 | pitch = math.asin(-2 * q1 * q3 + 2 * q0* q2)* 57.3 393 | roll = math.atan2(2 * q2 * q3 + 2 * q0 * q1, -2 * q1 * q1 - 2 * q2* q2 + 1)* 57.3 394 | yaw = math.atan2(-2 * q1 * q2 - 2 * q0 * q3, 2 * q2 * q2 + 2 * q3 * q3 - 1) * 57.3 395 | print("\r\n /-------------------------------------------------------------/ \r\n") 396 | print('\r\n Roll = %.2f , Pitch = %.2f , Yaw = %.2f\r\n'%(roll,pitch,yaw)) 397 | print('\r\nAcceleration: X = %d , Y = %d , Z = %d\r\n'%(Accel[0],Accel[1],Accel[2])) 398 | print('\r\nGyroscope: X = %d , Y = %d , Z = %d\r\n'%(Gyro[0],Gyro[1],Gyro[2])) 399 | print('\r\nMagnetic: X = %d , Y = %d , Z = %d'%((Mag[0]),Mag[1],Mag[2])) 400 | 401 | 402 | 403 | 404 | 405 | 406 | 407 | 408 | 409 | 410 | 411 | 412 | 413 | 414 | -------------------------------------------------------------------------------- /06_I2C/ICM20948/README.md: -------------------------------------------------------------------------------- 1 | # ICM20948 2 | ## 实现功能 3 | * 读取ICM20948数据并计算出对应的翻滚角、俯仰角和偏航角。 -------------------------------------------------------------------------------- /06_I2C/README.md: -------------------------------------------------------------------------------- 1 | # I2C 2 | ## 器件清单 3 | 器件 | 数量 4 | ---|--- 5 | 焊接排针的Pico | 1 6 | Pico-Eval-Board | 1 7 | USB TO Micro USB数据线 | 1 8 | ## 原理图 9 | ![原理图](./image/Schematic.png) 10 | ## machine.I2C类 11 | * machine.I2C(i2c_id,scl,sda,freq=400000) 12 | * I2C对象构造函数,作用为初始化对应通道和引脚. 13 | * i2c_id:使用I2C通道,可为0或者1; 14 | * scl:SCL引脚,应为Pin对象(I2C0默认为9,I2C1默认为7) 15 | * sda:SDA引脚,应为Pin对象(I2C0默认为8,I2C1默认为6) 16 | * freq:I2C时钟频率,默认为400Kb/S 17 | * I2C.scan() 18 | * 扫描从设备函数,返回所有I2C总线上挂载从设备7位地址的列表。 19 | 20 | * I2C.readfrom(addr, nbytes, stop=True) 21 | * readfrom函数其作用为通过I2C总线从设备读取数据并返回字节串。 22 | * addr:从设备地址。 23 | * nbytes:读取字符长度。 24 | * stop:是否在接收完成数据后发送结束信号。 25 | * I2C.readfrom_into(addr, buf, stop=True) 26 | * readfrom_into函数其作用为readfro函数的升级版,可以将读取数据指定存放在字符数组中。 27 | * addr:从设备地址 28 | * buf:字符数组,用于存放数据 29 | * stop:是否在接收完成数据后发送结束信号 30 | * I2C.writeto(addr, buf, stop=True) 31 | * writeto函数其作用为向从设备写入数据。 32 | * addr:从设备地址 33 | * buf:发送字符串 34 | * stop:是否在接收完成数据后发送结束信号 35 | ## 内存操作 36 | * I2C.readfrom_mem(addr, memaddr, nbytes, *, addrsize=8) 37 | * readfrom_mem函数其作用为从设备的寄存器中读取数据。 38 | * addr:从设备地址 39 | * memaddr:寄存器地址 40 | * nbytes:读取字节长度 41 | * addrsize:寄存器地址长度 42 | * I2C.readfrom_mem_into(addr, memaddr, buf, *, addrsize=8) 43 | * readfrom_mem_into函数其作用为从设备的寄存器中读取数据到指定字符数组。 44 | * addr:从设备地址 45 | * memaddr:寄存器地址 46 | * buf:字符数组,用于存放数据 47 | * addrsize:寄存器地址长度 48 | * I2C.writeto_mem(addr, memaddr, buf, *, addrsize=8) 49 | * writeto_mem函数其作用为将数据写入从设备的寄存器 50 | * addr:从设备地址 51 | * memaddr:寄存器地址 52 | * buf:发送字符串 53 | * addrsize:寄存器地址长度 54 | 55 | 56 | ## 软件I2C 57 | PS:以下函数,未经测试,仅通过官方文档描述编写 58 | * I2C.start() 59 | * start函数其作用为在I2C总线中发送开始信号(SCL保持高电平,SDA转为低电平) 60 | * I2C.stop() 61 | * start函数其作用为在I2C总线中发送终止信号(SCL保持高电平,SDA转为低电平) 62 | * I2C.readinto(buf, nack=True,) 63 | * readinto函数其作用为从I2C总线读取数据到指定的字符数组,读取长度为字符数组的长度。 64 | * buf:存放数据的字符数组 65 | * nack:在接受最后一个数据后,是否发送nack信号 66 | * I2C.write(buf) 67 | * write函数其作用为将buf字符数组的数据写入到I2C总线中。 68 | * buf:需要发送的字符数组 69 | 70 | 71 | -------------------------------------------------------------------------------- /06_I2C/image/Schematic.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/waveshareteam/Pico_MircoPython_Examples/be781912874381175753128423dd5b4b3d501d13/06_I2C/image/Schematic.png -------------------------------------------------------------------------------- /07_SPI/LCD/LCD_TEST.py: -------------------------------------------------------------------------------- 1 | 2 | from machine import Pin,SPI,PWM 3 | import framebuf 4 | import time 5 | import os 6 | 7 | LCD_DC = 8 8 | LCD_CS = 9 9 | LCD_SCK = 10 10 | LCD_MOSI = 11 11 | LCD_MISO = 12 12 | LCD_BL = 13 13 | LCD_RST = 15 14 | TP_CS = 16 15 | TP_IRQ = 17 16 | 17 | class LCD_3inch5(framebuf.FrameBuffer): 18 | 19 | def __init__(self): 20 | self.RED = 0x07E0 21 | self.GREEN = 0x001f 22 | self.BLUE = 0xf800 23 | self.WHITE = 0xffff 24 | self.BLACK = 0x0000 25 | 26 | self.width = 480 27 | self.height = 160 28 | 29 | self.cs = Pin(LCD_CS,Pin.OUT) 30 | self.rst = Pin(LCD_RST,Pin.OUT) 31 | self.dc = Pin(LCD_DC,Pin.OUT) 32 | 33 | self.tp_cs =Pin(TP_CS,Pin.OUT) 34 | self.irq = Pin(TP_IRQ,Pin.IN) 35 | 36 | self.cs(1) 37 | self.dc(1) 38 | self.rst(1) 39 | self.tp_cs(1) 40 | self.spi = SPI(1,60_000_000,sck=Pin(LCD_SCK),mosi=Pin(LCD_MOSI),miso=Pin(LCD_MISO)) 41 | 42 | self.buffer = bytearray(self.height * self.width * 2) 43 | super().__init__(self.buffer, self.width, self.height, framebuf.RGB565) 44 | self.init_display() 45 | 46 | 47 | def write_cmd(self, cmd): 48 | self.cs(1) 49 | self.dc(0) 50 | self.cs(0) 51 | self.spi.write(bytearray([cmd])) 52 | self.cs(1) 53 | 54 | def write_data(self, buf): 55 | self.cs(1) 56 | self.dc(1) 57 | self.cs(0) 58 | #self.spi.write(bytearray([0X00])) 59 | self.spi.write(bytearray([buf])) 60 | self.cs(1) 61 | 62 | 63 | def init_display(self): 64 | """Initialize dispaly""" 65 | self.rst(1) 66 | time.sleep_ms(5) 67 | self.rst(0) 68 | time.sleep_ms(10) 69 | self.rst(1) 70 | time.sleep_ms(5) 71 | self.write_cmd(0x21) 72 | self.write_cmd(0xC2) 73 | self.write_data(0x33) 74 | self.write_cmd(0XC5) 75 | self.write_data(0x00) 76 | self.write_data(0x1e) 77 | self.write_data(0x80) 78 | self.write_cmd(0xB1) 79 | self.write_data(0xB0) 80 | self.write_cmd(0x36) 81 | self.write_data(0x28) 82 | self.write_cmd(0XE0) 83 | self.write_data(0x00) 84 | self.write_data(0x13) 85 | self.write_data(0x18) 86 | self.write_data(0x04) 87 | self.write_data(0x0F) 88 | self.write_data(0x06) 89 | self.write_data(0x3a) 90 | self.write_data(0x56) 91 | self.write_data(0x4d) 92 | self.write_data(0x03) 93 | self.write_data(0x0a) 94 | self.write_data(0x06) 95 | self.write_data(0x30) 96 | self.write_data(0x3e) 97 | self.write_data(0x0f) 98 | self.write_cmd(0XE1) 99 | self.write_data(0x00) 100 | self.write_data(0x13) 101 | self.write_data(0x18) 102 | self.write_data(0x01) 103 | self.write_data(0x11) 104 | self.write_data(0x06) 105 | self.write_data(0x38) 106 | self.write_data(0x34) 107 | self.write_data(0x4d) 108 | self.write_data(0x06) 109 | self.write_data(0x0d) 110 | self.write_data(0x0b) 111 | self.write_data(0x31) 112 | self.write_data(0x37) 113 | self.write_data(0x0f) 114 | self.write_cmd(0X3A) 115 | self.write_data(0x55) 116 | self.write_cmd(0x11) 117 | time.sleep_ms(120) 118 | self.write_cmd(0x29) 119 | 120 | self.write_cmd(0xB6) 121 | self.write_data(0x00) 122 | self.write_data(0x62) 123 | 124 | self.write_cmd(0x36) 125 | self.write_data(0x28) 126 | def show_up(self): 127 | self.write_cmd(0x2A) 128 | self.write_data(0x00) 129 | self.write_data(0x00) 130 | self.write_data(0x01) 131 | self.write_data(0xdf) 132 | 133 | self.write_cmd(0x2B) 134 | self.write_data(0x00) 135 | self.write_data(0x00) 136 | self.write_data(0x00) 137 | self.write_data(0x9f) 138 | 139 | self.write_cmd(0x2C) 140 | 141 | self.cs(1) 142 | self.dc(1) 143 | self.cs(0) 144 | self.spi.write(self.buffer) 145 | self.cs(1) 146 | def show_down(self): 147 | self.write_cmd(0x2A) 148 | self.write_data(0x00) 149 | self.write_data(0x00) 150 | self.write_data(0x01) 151 | self.write_data(0xdf) 152 | 153 | self.write_cmd(0x2B) 154 | self.write_data(0x00) 155 | self.write_data(0xA0) 156 | self.write_data(0x01) 157 | self.write_data(0x3f) 158 | 159 | self.write_cmd(0x2C) 160 | 161 | self.cs(1) 162 | self.dc(1) 163 | self.cs(0) 164 | self.spi.write(self.buffer) 165 | self.cs(1) 166 | def bl_ctrl(self,duty): 167 | pwm = PWM(Pin(LCD_BL)) 168 | pwm.freq(1000) 169 | if(duty>=100): 170 | pwm.duty_u16(65535) 171 | else: 172 | pwm.duty_u16(655*duty) 173 | def draw_point(self,x,y,color): 174 | self.write_cmd(0x2A) 175 | 176 | 177 | self.write_data((x-2)>>8) 178 | self.write_data((x-2)&0xff) 179 | self.write_data(x>>8) 180 | self.write_data(x&0xff) 181 | 182 | self.write_cmd(0x2B) 183 | self.write_data((y-2)>>8) 184 | self.write_data((y-2)&0xff) 185 | self.write_data(y>>8) 186 | self.write_data(y&0xff) 187 | 188 | self.write_cmd(0x2C) 189 | 190 | self.cs(1) 191 | self.dc(1) 192 | self.cs(0) 193 | for i in range(0,9): 194 | h_color = bytearray(color>>8) 195 | l_color = bytearray(color&0xff) 196 | self.spi.write(h_color) 197 | self.spi.write(l_color) 198 | self.cs(1) 199 | def touch_get(self): 200 | if self.irq() == 0: 201 | self.spi = SPI(1,5_000_000,sck=Pin(LCD_SCK),mosi=Pin(LCD_MOSI),miso=Pin(LCD_MISO)) 202 | self.tp_cs(0) 203 | X_Point = 0 204 | Y_Point = 0 205 | for i in range(0,3): 206 | self.spi.write(bytearray([0XD0])) 207 | Read_date = self.spi.read(2) 208 | time.sleep_us(10) 209 | X_Point=X_Point+(((Read_date[0]<<8)+Read_date[1])>>3) 210 | 211 | self.spi.write(bytearray([0X90])) 212 | Read_date = self.spi.read(2) 213 | Y_Point=Y_Point+(((Read_date[0]<<8)+Read_date[1])>>3) 214 | 215 | X_Point=X_Point/3 216 | Y_Point=Y_Point/3 217 | 218 | self.tp_cs(1) 219 | self.spi = SPI(1,60_000_000,sck=Pin(LCD_SCK),mosi=Pin(LCD_MOSI),miso=Pin(LCD_MISO)) 220 | Result_list = [X_Point,Y_Point] 221 | #print(Result_list) 222 | return(Result_list) 223 | if __name__=='__main__': 224 | 225 | LCD = LCD_3inch5() 226 | LCD.bl_ctrl(100) 227 | #color BRG 228 | LCD.fill(LCD.WHITE) 229 | LCD.fill_rect(140,5,200,30,LCD.RED) 230 | LCD.text("Raspberry Pi Pico",170,17,LCD.WHITE) 231 | display_color = 0x001F 232 | LCD.text("3.5' IPS LCD TEST",170,57,LCD.BLACK) 233 | for i in range(0,12): 234 | LCD.fill_rect(i*30+60,100,30,50,(display_color)) 235 | display_color = display_color << 1 236 | LCD.show_up() 237 | 238 | while True: 239 | get = LCD.touch_get() 240 | if get != None: 241 | X_Point = int((get[1]-430)*480/3270) 242 | if(X_Point>480): 243 | X_Point = 480 244 | elif X_Point<0: 245 | X_Point = 0 246 | Y_Point = 320-int((get[0]-430)*320/3270) 247 | if(Y_Point>220): 248 | LCD.fill(LCD.WHITE) 249 | if(X_Point<120): 250 | LCD.fill_rect(0,60,120,100,LCD.RED) 251 | LCD.text("Button0",20,110,LCD.WHITE) 252 | elif(X_Point<240): 253 | LCD.fill_rect(120,60,120,100,LCD.RED) 254 | LCD.text("Button1",150,110,LCD.WHITE) 255 | elif(X_Point<360): 256 | LCD.fill_rect(240,60,120,100,LCD.RED) 257 | LCD.text("Button2",270,110,LCD.WHITE) 258 | else: 259 | LCD.fill_rect(360,60,120,100,LCD.RED) 260 | LCD.text("Button3",400,110,LCD.WHITE) 261 | else : 262 | LCD.fill(LCD.WHITE) 263 | LCD.text("Button0",20,110,LCD.BLACK) 264 | LCD.text("Button1",150,110,LCD.BLACK) 265 | LCD.text("Button2",270,110,LCD.BLACK) 266 | LCD.text("Button3",400,110,LCD.BLACK) 267 | 268 | LCD.show_down() 269 | time.sleep(0.1) 270 | 271 | 272 | -------------------------------------------------------------------------------- /07_SPI/LCD/README.md: -------------------------------------------------------------------------------- 1 | # LCD_TEST 2 | ## 实现功能 3 | * LCD显示和触摸测试程序 -------------------------------------------------------------------------------- /07_SPI/README.md: -------------------------------------------------------------------------------- 1 | # SPI 2 | ## 器件清单 3 | 器件 | 数量 4 | ---|--- 5 | 焊接排针的Pico | 1 6 | Pico-Eval-Board | 1 7 | USB TO Micro USB数据线 | 1 8 | ## 原理图 9 | ![原理图](./image/Schematic1.png) 10 | ![原理图](./image/Schematic2.png) 11 | ## machine.I2C类 12 | * machine.SPI(id,baudrate=1000000, polarity=0, phase=0, bits=8, firstbit=SPI.MSB, sck=None, mosi=None, miso=None) 13 | * SPI对象构造函数,作用为初始化对应通道和引脚 14 | * id:使用SPI通道,可为0或者1 15 | * baudrate: SPI通讯速率,也就是SCK引脚上的频率 16 | * polarity:时钟极性,若为0则总线空闲时SCK输出低电平,反之则输出高电平。 17 | * phase:时钟相位,若为0则在第一个时钟边缘捕获数据,反之则在第二个时钟边缘捕获数据。 18 | * bits:每次传输的数据位数 19 | * firstbit:先传输高位还是低位 20 | * sck:SCK引脚,应为Pin对象 21 | * mosi:MOSI引脚,应为Pin对象 22 | * miso:MISO引脚,应为Pin对象 23 | * sck、mosi和miso均为SPI使用的引脚,应为Pin对象 24 | * SPI.init() 25 | * init函数,用于重新开启SPI 26 | * SPI.deinit() 27 | * deinit函数,用于关闭SPI 28 | * SPI.read(nbytes,write=0x00) 29 | * read函数,用于读取从设备数据并返回 30 | * nbytes:读取字节数 31 | * write:读取数据时,MOSI输出数据。 32 | * SPI.readinto(buf,write=0x00) 33 | * readinto函数,用于读取从设备数据并存入指定字符数组中。 34 | * buf:字符数组,用于存放接收数据 35 | * write:读取数据时,MOSI输出数据。 36 | * SPI.write(buf) 37 | * write函数,将字符数组写入从设备。 38 | * buf:字符数组,用于存放传输数据 39 | * SPI.write_readinto(write_buf, read_buf) 40 | * write_readinto函数,用于同时发送和接收数据 41 | * write_buf:字符数组,用于存放传输数据 42 | * read_buf:字符数组,用于存放接收数据 43 | * PS: 这里传输和接收数据的字符数组的长度要求一致。 -------------------------------------------------------------------------------- /07_SPI/XPT2046/README.md: -------------------------------------------------------------------------------- 1 | # XPT2046_TEST 2 | ## 实现功能 3 | * 触摸测试,在命令行中显示当前触摸信息 -------------------------------------------------------------------------------- /07_SPI/XPT2046/XPT2046_TEST.py: -------------------------------------------------------------------------------- 1 | from machine import SPI,Pin 2 | import time 3 | 4 | spi = SPI(1,baudrate=5_000_000,sck=Pin(10),mosi=Pin(11),miso=Pin(12)) 5 | IRQ = Pin(17,Pin.IN) 6 | TP_CS =Pin(16,Pin.OUT) 7 | 8 | TP_CS(1) 9 | 10 | while True: 11 | if IRQ() == 0: 12 | TP_CS(0) 13 | spi.write(bytearray([0XD0])) 14 | Read_date = spi.read(2) 15 | time.sleep_us(10) 16 | X_Point=((Read_date[0]<<8)+Read_date[1])>>3 17 | 18 | spi.write(bytearray([0X90])) 19 | Read_date = spi.read(2) 20 | Y_Point=((Read_date[0]<<8)+Read_date[1])>>3 21 | TP_CS(1) 22 | 23 | print("******************************") 24 | print("*********TP_Read_ADC**********") 25 | print("******************************") 26 | print("X_Point ={} ".format(X_Point)) 27 | print("Y_Point ={}".format(Y_Point)) 28 | time.sleep(0.5) 29 | -------------------------------------------------------------------------------- /07_SPI/image/Schematic1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/waveshareteam/Pico_MircoPython_Examples/be781912874381175753128423dd5b4b3d501d13/07_SPI/image/Schematic1.png -------------------------------------------------------------------------------- /07_SPI/image/Schematic2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/waveshareteam/Pico_MircoPython_Examples/be781912874381175753128423dd5b4b3d501d13/07_SPI/image/Schematic2.png -------------------------------------------------------------------------------- /08_PIO/README.md: -------------------------------------------------------------------------------- 1 | # PIO 2 | ## 器件清单 3 | 器件 | 数量 4 | ---|--- 5 | 焊接排针的Pico | 1 6 | Pico-Eval-Board | 1 7 | USB TO Micro USB数据线 | 1 8 | 9 | ## 原理图 10 | ### WS2812B RBG LED 11 | ![原理图](./image/Schematic1.png) 12 | ### I2S 13 | ![原理图](./image/Schematic2.png) 14 | ### SDIO 15 | ![原理图](./image/Schematic3.png) 16 | ## 函数详解 17 | ### @asm_pio装饰器 18 | @asm_pio( 19 | out_init=None, 20 | set_init=None, 21 | sideset_init=None, 22 | in_shiftdir=0, 23 | out_shiftdir=0, 24 | autopush=False, 25 | autopull=False, 26 | push_thresh=32, 27 | pull_thresh=32, 28 | fifo_join=0 29 | ) 30 | * out_init :输出引脚初始化 31 | * set_init :设置引脚初始化 32 | * sideset_init :侧置引脚初始化 33 | * in_shiftdir : 数据输入方向 34 | * out_shiftdir:数据输出方向 35 | * autopush :自动推送,若开启,当ISR达到阈值,自动将ISR传输到RX-FIFO。 36 | * autopull :自动拉取,若开启,当OSR达到阈值,自动将TX-FIFO传输到OSR。 37 | * push_thresh :推送阈值 38 | * pull_thresh :拉取阈值 39 | * fifo_join :fifo组合,指定一个FIFO,将另一个FIFO关闭并加入改FIFO,获取更深位数的FIFO. 40 | ### StateMachine状态机 41 | * rp2.StateMachine(sm_id ,program, freq=-1, *, in_base=None, out_base=None, set_base=None, jmp_pin=None, sideset_base=None, in_shiftdir=None, out_shiftdir=None, push_thresh=None, pull_thresh=None) 42 | * sm_id:使用状态机ID 43 | * program:PIO运行程序 44 | * freq:状态机运行频率,默认为系统时钟频率, 45 | * 因为时钟分频器的分频因子计算公式为“系统时钟频率/频率”,所以可能存在轻微的舍入误差。 46 | * in_base:用于in()指令的第一个引脚 47 | * out_base:用于out()指令的第一个引脚 48 | * set_base:用于set()指令的第一个引脚 49 | * jmp_pin:用于jmp(pin, ...)指令的第一个引脚 50 | * sideset_base:是用于侧置的第一个引脚。 51 | * in_shiftdir:ISR将移动的方向,可为PIO.SHIFT_LEFT或者PIO.SHIFT_RIGHT 52 | * out_shiftdir: OSR 将移动的方向,可为PIO.SHIFT_LEFT或者PIO.SHIFT_RIGHT 53 | * push_thresh:推送阈值 54 | * pull_thresh:拉取阈值 55 | * StateMachine.active([value]) 56 | * 获取或设置状态机当前是否正在运行。 57 | * 当value不为空时,设置状态机,反之获取运行状态。 58 | * StateMachine.restart() 59 | * 重新启动状态机并跳转到程序的开头。 60 | * StateMachine.exec(instr) 61 | * 执行单个 PIO 指令。 62 | * instr:指令字符串。 63 | * StateMachine.get(buf=None, shift=0) 64 | * 从状态机的RX-FIFO中提取一个字,如果FIFO为空,它会阻塞直到数据到达(即状态机推一个字)。 65 | * 该值在返回之前右移 *shift* 位,即返回值是“word >> shift” 66 | * StateMachine.put(value, shift=0) 67 | * 将一个字推送到状态机的 TX FIFO。 68 | * 如果 FIFO已满,它将阻塞直到有空间(即状态机拉一个字)。 69 | * 该值在返回之前右移 *shift* 位,即返回值是“word >> shift” 70 | * StateMachine.rx_fifo() 71 | * 返回状态机的 RX FIFO 中的字数。值为0表示 FIFO 为空。 72 | * 用于在调用之前检查数据是否正在等待读取`StateMachine.get()`。 73 | * StateMachine.tx_fifo() 74 | * 返回状态机的 RX FIFO 中的字数。值为0表示 FIFO 为空。 75 | * 用于在调用之前检查数据是否正在等待读取`StateMachine.put()`。 76 | * StateMachine.irq(handler=None, trigger=0|1, hard=False) 77 | * 返回给定 StateMachine 的 IRQ 对象。 -------------------------------------------------------------------------------- /08_PIO/image/Schematic1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/waveshareteam/Pico_MircoPython_Examples/be781912874381175753128423dd5b4b3d501d13/08_PIO/image/Schematic1.png -------------------------------------------------------------------------------- /08_PIO/image/Schematic2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/waveshareteam/Pico_MircoPython_Examples/be781912874381175753128423dd5b4b3d501d13/08_PIO/image/Schematic2.png -------------------------------------------------------------------------------- /08_PIO/image/Schematic3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/waveshareteam/Pico_MircoPython_Examples/be781912874381175753128423dd5b4b3d501d13/08_PIO/image/Schematic3.png -------------------------------------------------------------------------------- /08_PIO/ws2812b/ws2812b.py: -------------------------------------------------------------------------------- 1 | import time 2 | from machine import Pin 3 | import rp2 4 | 5 | max_lum =100 6 | r=0 7 | g=0 8 | b=0 9 | 10 | @rp2.asm_pio(sideset_init=rp2.PIO.OUT_LOW, out_shiftdir=rp2.PIO.SHIFT_LEFT, autopull=True, pull_thresh=24) 11 | def ws2812(): 12 | T1 = 2 13 | T2 = 5 14 | T3 = 3 15 | wrap_target() 16 | label("bitloop") 17 | out(x, 1) .side(0) [T3 - 1] 18 | jmp(not_x, "do_zero") .side(1) [T1 - 1] 19 | jmp("bitloop") .side(1) [T2 - 1] 20 | label("do_zero") 21 | nop() .side(0) [T2 - 1] 22 | wrap() 23 | 24 | 25 | # Create the StateMachine with the ws2812 program, outputting on Pin(4). 26 | sm = rp2.StateMachine(0, ws2812, freq=8_000_000, sideset_base=Pin(4)) 27 | 28 | # Start the StateMachine, it will wait for data on its FIFO. 29 | sm.active(1) 30 | 31 | # Color change 32 | while True: 33 | for i in range(0,max_lum): 34 | r=i 35 | b=max_lum-i 36 | rgb =(g<<24) | (r<<16) | (b<<8) 37 | sm.put(rgb) 38 | time.sleep_ms(10) 39 | time.sleep_ms(300) 40 | for i in range(0,max_lum): 41 | g=i 42 | r=max_lum-i 43 | rgb =(g<<24) | (r<<16) | (b<<8) 44 | sm.put(rgb) 45 | time.sleep_ms(10) 46 | time.sleep_ms(300) 47 | for i in range(0,max_lum): 48 | b=i 49 | g=max_lum-i 50 | rgb =(g<<24) | (r<<16) | (b<<8) 51 | sm.put(rgb) 52 | time.sleep_ms(10) 53 | time.sleep_ms(300) -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # MircoPython_Examples 2 | ## 介绍 3 | 4 | 本仓库用于存放微雪电子发布的Pico系列教程使用的MircoPython例程。 5 | 6 | ![Cover](/00_UF2/image/cover.jpg) 7 | 8 | ## 使用方法 9 | * 参考视频 10 | * [教程合集](https://space.bilibili.com/477406871/channel/detail?cid=172621) 11 | * [Pico-Eval-Board介绍](https://www.bilibili.com/video/BV1Hv411W7YZ) 12 | 1. [教程1——基础介绍](https://www.bilibili.com/video/BV1CV411e7ZQ) 13 | 2. [教程2——外设GPIO](https://www.bilibili.com/video/BV1nK4y1U79B) 14 | 3. [教程3——PWM](https://www.bilibili.com/video/BV1SV411Y7o3) 15 | 4. [教程4——ADC](https://www.bilibili.com/video/BV1mp4y1b7fj) 16 | 5. [教程5——UART](https://www.bilibili.com/video/BV15B4y1u7cv) 17 | 6. [教程6——I2C](https://www.bilibili.com/video/BV1GU4y1j763) 18 | 7. [教程7——SPI](https://www.bilibili.com/video/BV1w54y1G7g1) 19 | --------------------------------------------------------------------------------