通过显示屏实现呼吸灯的对外表现
本项目是通过苏老师提供的Pico的扩展板实现Pico的对外展示。通过了解本案例,你可以学到呼吸灯的基本使用方法,并学会使用显示屏的基本操作。
标签
测试
TiankuoJiao
更新2021-03-15
1711

本项目将首先将介绍Pico的基本点LED的方法,并学习高级呼吸灯的编程方法,最后通过对显示屏的操作实现呼吸灯的显示。

第一部分:Pico所需要的的软件安装

      Pico的入门操作在许多同学的项目中多有介绍,官网中也有完整的介绍。因此本案例将不会着重介绍入门时基本操作。这部分的资料可以在文末的链接中完整且清晰地查看。

第二部分:通过Pico实现呼吸灯

     呼吸灯代码如下:

from machine import Pin, PWM
from time import sleep

pwm = PWM(Pin(15))

pwm.freq(1000)

while True:
    for duty in range(65025):
		pwm.duty_u16(duty)
		sleep(0.0001)
	for duty in range(65025, 0, -1):
		pwm.duty_u16(duty)
		sleep(0.0001)

frequency(pwm.freq)告诉Raspberry Pi Pico多久切换一次LED的电源开关。

Duty cycle告诉LED每次应该亮多久。对于Pico,这个值从0到65025不等。65025将是100%的时间,所以LED将保持明亮。约为32512的值表示它应该在一半的时间内处于开启状态。

第三部分:显示屏的使用

  首先,需要用到在github的库。具体的操作方法如下:

      1.我们首先需要把库存到Pico中。fonts里面包含vga1_16x32和vga2_8x8两个库,在fonts外面是st7789库。我们接下来的操作就需要用到这三个库。

      2.使用显示屏代码

import uos
import machine
from st7789 import ST7789
import st7789 as st7789
from fonts import vga2_8x8 as font1
from fonts import vga1_16x32 as font2
import random


st7789_res = 0
st7789_dc  = 1

disp_width = 240
disp_height = 240
CENTER_Y = int(disp_width/2)
CENTER_X = int(disp_height/2)


print(uos.uname())

spi_sck=machine.Pin(2)
spi_tx=machine.Pin(3)
spi0=machine.SPI(0,baudrate=4000000, phase=0, polarity=1, sck=spi_sck, mosi=spi_tx)

print(spi0)
display = st7789.ST7789(spi0, disp_width, disp_width,
                          reset=machine.Pin(st7789_res, machine.Pin.OUT),
                          dc=machine.Pin(st7789_dc, machine.Pin.OUT),
                          xstart=0, ystart=0, rotation=0)

potentiometer = machine.ADC(26)

其中,程序已经将管脚选择好,接下来可以直接控制显示屏。

第四部分:库函数的解释

        1.在给定的位置和颜色上画一个像素。

    def pixel(self, x, y, color):
        """
        Draw a pixel at the given location and color.

        Args:
            x (int): x coordinate
            Y (int): y coordinate
            color (int): 565 encoded color
        """
        self.set_window(x, y, x, y)
        self.write(None, _encode_pixel(color))

         2.画竖直线

    def hline(self, x, y, length, color):
        """
        Draw horizontal line at the given location and color.

        Args:
            x (int): x coordinate
            Y (int): y coordinate
            length (int): length of line
            color (int): 565 encoded color
        """
        self.fill_rect(x, y, length, 1, color)

         3.画垂直线

    def vline(self, x, y, length, color):
        """
        Draw vertical line at the given location and color.

        Args:
            x (int): x coordinate
            Y (int): y coordinate
            length (int): length of line
            color (int): 565 encoded color
        """
        self.fill_rect(x, y, 1, length, color)

第四部分:和呼吸灯的代码进行组合

         1.我们要把下面的代码加到开头:

from machine import Pin, PWM
from time import sleep

          2. 接下来,把下面的代码放到显示屏代码的后面

display.fill(st7789.BLACK)
pwm = PWM(Pin(25))
pwm.freq(1000)
i=0
while True:   
    for duty in range(32512):
        pwm.duty_u16(duty)
        sleep(0.0001)        
    for i in range(80):       
        display.pixel(3*i,i*3,st7789.color565(255,255,255))
        display.pixel(3*i,240-i*3,st7789.color565(255,255,255)) 
        utime.sleep(0.05)   
    display.fill(st7789.BLACK)

其中,display.fill(st7789.BLACK)可以控制整个显示屏的颜色。i为自己的定义的变量,用来在显示屏上显示坐标.读者可以在display这部分修改我的代码来实现自己的显示屏操作。至于具体dispaly的操作可以仿照上面库函数的介绍来自己写。最后的display.fill(st7789.BLACK)为刷屏操作,把屏幕重新变黑,如果还有问题,可以在b站给我私信。

 

官方的简单上手教程:https://projects.raspberrypi.org/en/projects/getting-started-with-the-pico/4

附件下载
2.PNG
Pico添加库的操作步骤1
f.PNG
Pico添加库的操作步骤
团队介绍
Pico的爱好者,学生尝试
团队成员
焦天阔 Tiankuo.Jiao
西交利物浦大学大二学生,电子科学与技术专业
评论
0 / 100
查看更多
目录
硬禾服务号
关注最新动态
0512-67862536
info@eetree.cn
江苏省苏州市苏州工业园区新平街388号腾飞创新园A2幢815室
苏州硬禾信息科技有限公司
Copyright © 2024 苏州硬禾信息科技有限公司 All Rights Reserved 苏ICP备19040198号