前言
又是一年毕业季,只有到了毕业季才会意识到自己又离青春远了一大步。毕业后,大家都各奔东西了,如果这个时候有喜欢的人还是藏在心底的话,不免会给人生留下遗憾。为了不留遗憾,不老套,制作一款表白神器,是不是会更容易表白成功呢?
之前使用8*8LED点阵模块做过一款跳动的心,因为是直接用的点阵模块,所以控制就用了16个IO,未免有些繁琐,浪费资源。最近发现了一个好东西——MAX7219多位LED 显示驱动器。使用该驱动器加上点阵屏或者数码管,驱动起来特别方便简单,而且加上电源脚也不过就5根线而已。
MAX7219介绍
Python学习交流群:1004391443,这里是python学习者聚集地,有大牛答疑,有资源共享!小编也准备了一份python学习资料,有想学习python编程的,或是转行,或是大学生,还有工作中想提升自己能力的,正在学习的小伙伴欢迎加入学习。
MAX7219 是美国MAXIM 公司推出的多位LED 显示驱动器,采用3 线串行接口传送数据,可直接与单片机接口连接,用户能方便修改其内部参数,以实现多位LED 显示。它内含硬件动态扫描电路、BCD译码器、段驱动器和位驱动器。此外,其内部还含有8X8 位静态RAM,用于存放8个数字的显示数据。显然,它可直接驱动64 段LED点阵显示器。当多片MAX7219 级联时,可控制更多的LED点阵显示器。显示的数据通过单片机数据处理后,送给MAX7219 显示。
引脚图:
<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1560238906192" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image<input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>
当然了,直接拿到这个驱动器一时也不知道该怎么用,于是就直接买了现成的MAX7219驱动器结合8*8LED点阵屏模块。
<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1560238906196" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image <input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image> <tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1560238906200" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image<input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>
看反面图可以看出,该模块不仅有DIN输入端口还有DOUT输出端口,用于多个模块级联。使用的单个模块时,开发板只接入模块的输入端口即可;当多个模块级联时,第一个模块的输出端口接第二个模块的输入端口,第二个输出端口接第三个输入端口,依次类推。
制作过程
话不多说了,马上动手实践起来。接线很简单,见下表:
TPYBoard v102 MAX7219点阵模块 VIN VCC GND GND X3(普通IO都可) DIN X2 CS X1 CLK TPYBoard v102开发板若没有使用过,附上链接看先了解下。 http://docs.tpyboard.com/zh/latest/tpyboard/tutorial/v10x/sta
rt/
程序呈上:
主程序 main.py。
<pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> 1 import MAX7219 #导入模块
2
3 #CLK -> X1,CS -> X2,DIN -> X3
4 #num级联的模块数量
5 led = MAX7219.Lattice('X1','X2','X3',num=1)
6 #要显示的内容,显示的内容根据字库文件而定,见font_max7219.py。
7 #大家也可以自定义图案添加上。
8 msg = 'LOVE大小'
9
10 while True:
11 #显示
12 led.display(msg)
</pre>
字库文件 font_max7219.py。
<pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">FONT8_8= {
'0':[0x3C,0x42,0x42,0x42,0x42,0x42,0x42,0x3C],#0
'1':[0x10,0x18,0x14,0x10,0x10,0x10,0x10,0x10],#1
'2':[0x7E,0x2,0x2,0x7E,0x40,0x40,0x40,0x7E],#2
'3':[0x3E,0x2,0x2,0x3E,0x2,0x2,0x3E,0x0],#3
'4':[0x8,0x18,0x28,0x48,0xFE,0x8,0x8,0x8],#4
'5':[0x3C,0x20,0x20,0x3C,0x4,0x4,0x3C,0x0],#5
'6':[0x3C,0x20,0x20,0x3C,0x24,0x24,0x3C,0x0],#6
'7':[0x3E,0x22,0x4,0x8,0x8,0x8,0x8,0x8],#7
'8':[0x0,0x3E,0x22,0x22,0x3E,0x22,0x22,0x3E],#8
'9':[0x3E,0x22,0x22,0x3E,0x2,0x2,0x2,0x3E],#9
'A':[0x8,0x14,0x22,0x3E,0x22,0x22,0x22,0x22],#A
'B':[0x3C,0x22,0x22,0x3E,0x22,0x22,0x3C,0x0],#B
'C':[0x3C,0x40,0x40,0x40,0x40,0x40,0x3C,0x0],#C
'D':[0x7C,0x42,0x42,0x42,0x42,0x42,0x7C,0x0],#D
'E':[0x7C,0x40,0x40,0x7C,0x40,0x40,0x40,0x7C],#E
'F':[0x7C,0x40,0x40,0x7C,0x40,0x40,0x40,0x40],#F
'G':[0x3C,0x40,0x40,0x40,0x40,0x44,0x44,0x3C],#G
'H':[0x44,0x44,0x44,0x7C,0x44,0x44,0x44,0x44],#H
'I':[0x7C,0x10,0x10,0x10,0x10,0x10,0x10,0x7C],#I
'J':[0x3C,0x8,0x8,0x8,0x8,0x8,0x48,0x30],#J
'K':[0x0,0x24,0x28,0x30,0x20,0x30,0x28,0x24],#K
'L':[0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x7C],#L
'M':[0x81,0xC3,0xA5,0x99,0x81,0x81,0x81,0x81],#M
'N':[0x0,0x42,0x62,0x52,0x4A,0x46,0x42,0x0],#N
'O':[0x3C,0x42,0x42,0x42,0x42,0x42,0x42,0x3C],#O
'P':[0x3C,0x22,0x22,0x22,0x3C,0x20,0x20,0x20],#P
'Q':[0x1C,0x22,0x22,0x22,0x22,0x26,0x22,0x1D],#Q
'R':[0x3C,0x22,0x22,0x22,0x3C,0x24,0x22,0x21],#R
'S':[0x0,0x1E,0x20,0x20,0x3E,0x2,0x2,0x3C],#S
'T':[0x0,0x3E,0x8,0x8,0x8,0x8,0x8,0x8],#T
'U':[0x42,0x42,0x42,0x42,0x42,0x42,0x22,0x1C],#U
'V':[0x42,0x42,0x42,0x42,0x42,0x42,0x24,0x18],#V
'W':[0x0,0x49,0x49,0x49,0x49,0x2A,0x1C,0x0],#W
'X':[0x0,0x41,0x22,0x14,0x8,0x14,0x22,0x41],#X
'Y':[0x41,0x22,0x14,0x8,0x8,0x8,0x8,0x8],#Y
'Z':[0x0,0x7F,0x2,0x4,0x8,0x10,0x20,0x7F],#Z
'大':[0x00,0x66,0xFF,0xFF,0x7E,0x3C,0x18,0x00],#大心
'小':[0x00,0x00,0x66,0x7E,0x3C,0x18,0x00,0x00],#小心
'中':[0x8,0x7F,0x49,0x49,0x7F,0x8,0x8,0x8],#中
'国':[0xFE,0xBA,0x92,0xBA,0x92,0x9A,0xBA,0xFE],#国
}
</pre>
模块驱动程序max7219.py就不在这里呈现了,文章末有链接,大家可以去下载。
演示效果
https://v.qq.com/x/page/n08814kmrfc.html
无线遥控升级版
这样虽然已经很炫了,不过加上无线遥控的效果是不是就更棒了。于是,找来了以前买的HC-05(HC-06也一样)蓝牙模块,结合一下整上。
蓝牙模块的连接线:
TPYBoard v102 HC-05/06蓝牙模块 VIN VCC GND GND Y1 RX Y2 TX 其他文件不用动,改动下main.py,增加上串口读写操作即可。
<pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> 1 import MAX7219 #导入模块
2 from pyb import UART
3 #初始化串口参数,使用串口6 波特率9600 超时时间50毫秒
4 #串口6 TX->Y1 RX->Y2
5 #其他串口对应的引脚编号详见针脚图:http://old.tpyboard.com/document/documents/10x/TPYBoardv10xPCBpng.pdf
6 uart = UART(6,9600,timeout=50)
7
8 #CLK -> X1,CS -> X2,DIN -> X3
9 #num级联的模块数量
10 led = MAX7219.Lattice('X1','X2','X3',num=1)
11 led.display('大') #默认显示大心图案
12 while True:
13 if uart.any() > 0: #any()返回串口缓存区的数据长度,返回值大于0即表示有数据
14 DATA = uart.read().decode() #读取缓存区全部数据,返回值为bytes,decode()转为字符串
15 led.display(DATA) #进行显示
</pre>
网友评论