(三)MicroPython——数码管

2022-12-12 15:23:14 浏览数 (1)

目录

学习目标

成果展示 

硬件知识

代码 

总结


学习目标

        本节我们来使用ESP32和MicroPython来控制数码管,包括一位数码管和四位数码管,具体的原理部分在51已经讲解的非常详细了,就不再赘述。

成果展示 

https://player.bilibili.com/player.html?aid=859905932

数码管1

https://player.bilibili.com/player.html?aid=987384504

数码管4

硬件知识

(四)51单片机基础——数码管_花园宝宝小点点的博客-CSDN博客_单片机数码管

https://blog.csdn.net/weixin_66578482/article/details/124769180        

这就是硬件原理部分,不懂得可以看一下之前的笔记。

        我觉得本节最难的对方就是面包板的使用,因为是第一次使用。但是后来了解了面包板的构造之后,就觉得比较简单,两边一列连通,中间半排连通,两边彼此不连通,记住这些基本操作之后就觉得简单许多了。 

代码 

代码语言:javascript复制
import machine
import time


a = machine.Pin(13, machine.Pin.OUT)
b = machine.Pin(12, machine.Pin.OUT)
c = machine.Pin(14, machine.Pin.OUT)
d = machine.Pin(27, machine.Pin.OUT)
e = machine.Pin(26, machine.Pin.OUT)
f = machine.Pin(25, machine.Pin.OUT)
g = machine.Pin(33, machine.Pin.OUT)
h = machine.Pin(32, machine.Pin.OUT)

number_led = [a, b, c, d, e, f, g, h]

number_dict = {
    0: "11111100",
    1: "01100000",
    2: "11011010",
    3: "11110010",
    4: "01100110",
    5: "10110110",
    6: "10111110",
    7: "11100000",
    8: "11111110",
    9: "11110110",
}

def show_number(number):
    if number_dict.get(number):
        i = 0
        for bit in number_dict.get(number):
            if bit == "1":
                number_led[i].value(1)
            else:
                number_led[i].value(0)
            i  = 1

def main():
    for i in range(10):
        show_number(i)
        time.sleep(1)

if __name__ == "__main__":
    main()
代码语言:javascript复制
import machine
import time

led1 = machine.Pin(5, machine.Pin.OUT)
led2 = machine.Pin(18, machine.Pin.OUT)
led3 = machine.Pin(19, machine.Pin.OUT)
led4 = machine.Pin(21, machine.Pin.OUT)

led1.value(0)
led2.value(0)
led3.value(0)
led4.value(0)

number_led_list = [led1, led2, led3, led4]

a = machine.Pin(13, machine.Pin.OUT)
b = machine.Pin(12, machine.Pin.OUT)
c = machine.Pin(14, machine.Pin.OUT)
d = machine.Pin(27, machine.Pin.OUT)
e = machine.Pin(26, machine.Pin.OUT)
f = machine.Pin(25, machine.Pin.OUT)
g = machine.Pin(33, machine.Pin.OUT)
h = machine.Pin(32, machine.Pin.OUT)

number_led = [a, b, c, d, e, f, g, h]

number_dict = {
    0: "11111100",
    1: "01100000",
    2: "11011010",
    3: "11110010",
    4: "01100110",
    5: "10110110",
    6: "10111110",
    7: "11100000",
    8: "11111110",
    9: "11110110"
}

def show_number(number):
    if number_dict.get(number):
        i = 0
        for bit in number_dict.get(number):
            if bit == "1":
                number_led[i].value(1)
            else:
                number_led[i].value(0)
            i  = 1       
            
def show_4_number(number):
    if 0 <= number <= 9999:
        i = 0
        for num in "d" % number:  # 转换为字符串1
            print(num)
            show_number(int(num)) # 控制显示数字
            light_on(i) # 控制哪一个灯
            time.sleep_ms(5)
            led1.value(1)
            led2.value(1)
            led3.value(1)
            led4.value(1)
            i  = 1
            
def light_on(i):
    # 全部设置为0
    for led in number_led_list:
        led.value(1)
        
    number_led_list[i].value(0)

for i in range(1000, 10000):
    for j in range(10):
        show_4_number(i)

总结 

        总体来说还是比较简单的,就是第一次使用面包板,有点不太会,但是学习一下就能正常操作了。 

0 人点赞