黄土马家

[Course Design] Dot matrix greedy snake based on 51 microcontroller

 
Overview

Note: The following content is for personal study reference only and reproduction is strictly prohibited! ! !

Video link: Bilibili (please give it a like!)

 

Snake design based on microcontroller

      Summary   

This design is a Snake game system with a microcontroller as the control core. The designed greedy snake can control its up, down, left and right movements, eat food and automatically place food with four buttons. After the game starts, there will be two sections of the snake body and one piece of food by default. When the snake eats the food, the snake body will automatically lengthen. The hardware design includes four 16*16 dot matrix composed of 8*8 dot matrix and its driving circuit, independent buttons, lithium battery charging and discharging circuit and download circuit. The software is programmed in C language, which is convenient and flexible, greatly speeding up software development, shortening the development cycle, and making it easy to transplant. The designed Snake game system is lightweight, portable, suitable for all ages, has low cost, and has certain market prospects.

Keywords : 51 microcontroller , dot matrix , game, snake

 

 

Snake design based on single chip microcomputer

      Abstract 

This design is a snake game system with single chip computer as the control core. The designed snake can move up, down, left and right, eat food and place food automatically by four buttons. When the game starts, there will be two snake bodies and one food by default. When the snake eats the food, the snake body will lengthen automatically. The hardware design includes 4 8*8 dot matrix composed of 16*16 dot matrix and its drive circuit, independent keys, lithium battery charge and discharge circuit and download circuit. Software using C language programming, convenient and flexible, greatly accelerated the speed of software development, shorten the development cycle, and easy to transplant. The snake game system designed by the light and portable, and the old and young are suitable, and the cost is low, has a certain market prospect.

Key words: 51 MCU, dot matrix, game, snake

 

 

1          Introduction

1.1 Design purpose

Today in the 21st century, people's lives have begun to become more colorful. After busy work, entertainment has become an indispensable part of people's lives, and games, as a form of entertainment that has gradually emerged in recent years, have become more and more popular among people. After work and study, playing games is a good choice to relax and adjust the intense study and work pressure ; however, large-scale online games are time-consuming to play and cannot be played anytime and anywhere. So it would be a good choice to have a small game that is simple and easy to carry and can relieve stress. Therefore, I designed such a simple and easy-to-carry classic game - Snake.

1.2 Design requirements

① Make a Snake game. The system uses a microcontroller as the controller and uses four input terminals to represent the four control keys (up, down, left, and right).

②The game initializes the number of snake sections and obstacle walls.

③It ends automatically when the snake's head hits the obstacle wall or the snake's body.

2          Overall plan

2.1 Overall block diagram

 

Figure 2-1 Overall block diagram

2.2 Solution selection

2.2.1             Dot matrix drive circuit selection

Due to the limited driving capability of the microcontroller IO port, when driving a 16*16 dot matrix, the row control IO port needs to control 16 LEDs . The driving capability of a single IO port cannot achieve good display effects, so a driver circuit is required to control 16*16 Dot matrix, specifically there are two options to choose from:

Option 1: Choose 74HC595 as the dot matrix driver. 74HC595 is an 8 -bit serial input, parallel output shift register. Four 74HC595s can be used to control the array of dots. Only 6 IO ports are needed to control the dot matrix , but data needs to be sent 16 times for each control .

方案二:选用74LS138配合74HC373作为点阵驱动。74LS1383线-8线译码器,可由274LS138构成一个4线-16线译码器来驱动点阵的行。74HC373是八路D型锁存器,可用来并行驱动点阵的列,每次控制只需执行一次命令。但在此方案中需要20IO口来控制电路。

根据以上两种方案的描述,再结合我们的系统设计,IO口还未完全利用,为使刷新速度更快,故采取用方案二。

2.2.2            按键电路设计方案

在本设计中,我们需要通过按键来实现改变贪吃蛇的运动方向。那么,键盘就是整个系统中不可缺少的一部分。具体有以下两种方案以供选择:

方案一:矩阵式键盘,这种键盘I/O口利用率高,但电路连接复杂,软件编程也比较复杂,适用于需要大量使用案件的系统。

方案二:独立式键盘,这种键盘每个按键之间是相互独立的,每个按键占用一个IO口,当按键数量较多时,I/0口利用率不高,但程序编写简单,适用于所需按键较少的场合。

根据以上两种方案的描述,再结合我们的系统设计,显而易见,我们需要独立式键盘,所以,这里我们选择方案一。

2.2.3            电源供电选择

电源电路是一个电子产品的重要组成部分,本设计中的单片机与驱动电路都需要电源。在本设计中的单片机、74HC37374LS138的电压典型值都为5V,故需要一个5V的电源供电。具体有以下三种方案以供选择:

方案一:使用直流电源供电,例如手机充电器,可以直接获取5V的电源,并且可以长时间供电,但必须有电线连接,不具有便携性。

方案二:使用干电池供电,电池无需电线连接,具有便携性。并且单节干电池由1.5V的电压,3节干电池串联即可得到4.5V的电压,基本满足需求。但干电池仅能单次使用,没电后需更换电池,造成浪费。

方案三:使用锂电池供电,电池无需电线连接,比干电池更加轻便,更具有便携性。锂电池还可以重复充电循环使用,设计产品可有更长的寿命。另外锂电池典型电压为3.7V,不满足系统所需的5V,但市面上有类似于FM3209M之类的锂电池管理芯片只需搭配简单的外围电路即可获得5V的电压。

根据以上两种方案的描述,我们选用方案三,使用锂电池为本设计供电,以便使设计产品更加便捷。

3         硬件设计

3.1 单片机最小系统

3.1.1            单片机选型

本设计选用STC89C52单片机作为系统的主控芯片,控制该贪吃蛇系统的整体运行。STC89C52单片机的优点为功耗低、八位CMOS微处理器性能高,片内具有8k在线编程Flash存储器,采用MCS-51内核,指令完全兼容MCS-51,具有开发简单、可在线编程下载、成本低等优点。[1]

最小系统主要由STC89C52单片机、晶振电路、复位电路构成。电源采用锂电池供电,晶振电路为12M石英晶振,并联两个电容,可以起到频率微调作用。复位电路有上电自动复位和开关复位两种复位方式。

3.1.2            单片机最小系统

单片机最小系统原理图如 3‑1所示:


 

31单片机最小系统

[1]    时钟电路


 

32单片机最小系统-时钟电路

单片机内部具有一个高增益反相放大器,用于构成振荡器。通常在引脚XTALIXTAL2跨接石英晶体和两个补偿电容构成自激振荡器,系统时钟电路结构如 3‑2所示,在本设计中时钟电路采用12M石英晶振作为基准时钟。

[2]    复位电路


 

33单片机最小系统-复位电路

复位电路用于重新启动系统,使得单片机回到原始状态。设置复位电路的目的是当系统失去控制或程序跑飞时,通过复位按钮恢复默认设置,系统重新启动运行。[2]

复位电路由电容串联电阻构成,由 3‑3并结合“电容电压不能突变”的性质,可以知道,当系统一上电,RST脚将会出现高电平,并且,这个高电平持续的时间由电路的RC值来决定。典型的51单片机当RST脚的高电平持续两个机器周期以上就将复位,所以,适当组合RC的取值就可以保证可靠的复位。一般教科书推荐C 10uR8.2K。当然也有其他取法的,原则就是要让RC组合可以在RST脚上产生不少于2个机器周期的高电平。

31电容充电时间计算公式

在本设计中C10uR10K。由 3‑1计算得 =0.1s远大于两个机器周期(2us),故可以实现上电复位功能,另外还设置了一个按键作为复位按键,即按下后直接将单片机RST端接至5V复位单片机,当按键松开后再重复电容充电的过程恢复正常运行。

3.2 点阵驱动电路


 

 

35点阵LED内部原理图

点阵LED内部原理图如 3‑5所示,外侧的就是点阵LED的引脚号,左侧的8个引脚是接的内部LED的阴极,上侧的8个引脚接的是内部LED的阳极。从图上可以看出来,我们的9脚如果是低电平,13脚是高电平的话,最左上角的那个LED小灯就会亮。

控制一个8*8的点阵需要16个引脚,将四个点阵行与行分别连接,列与列分别连接,控制这个16*16点阵仍然需要32个引脚,占用了过多的单片机IO口,所以需要一定的驱动电路来控制点阵。具体驱动方式如下述所示。

3.2.1            4-16线译码器(行驱动)


 

36由两片74LS138构成的4~16线译码器

设计中采用74LS138译码器译码器每一时刻的输出口会根据输入信号的译码来选择同一时刻仅有一个端口输出与其他端口的输出不同该电平信号就是设计所需的只要在输入端给出连续的编码信号输出端就会产生由低位端口到高位端口输出的电平信号由于16×16点阵给出的行引脚为1674LS138仅有8位输出引脚所以要想实现译码选择行线的话一片74LS138显然是不够的因此该设计中采用两片74LS138译码器级联成416线译码器那么怎样才能实现两片74LS138级联成为416线译码器呢?级联原理如 3‑6所示从图中可以看到两片74LS138的输入端被相应地连在一起组成416译码器的低三位关键是第四位的连接方式从图中可以看出D3=0会使73LS138(1)对输入信号译码而输出端也仅会在73LS138(1)Y0Y7之间进行选择这与一片74LS138的译码关系一样74LS138(2)则不参与译码而全部输出默认电平但从输入端看是输入了四位信号而产生了16位数据再当D3=173LS138(2)对输入的低三位信号译码73LS138(2)被屏蔽输出端全部输出高电平这样只要将416线译码器的输入端口接入单片机的IO端口上16位输出端接在16×16点阵的行线接口上即可完成点阵的行驱动[3]

37点阵行驱动电路

在本设计中,如 3‑6所示通过单片机的P2.0P2.1P2.2P2.3四个引脚作为4~16线译码器的输入,4~16线译码器的输出接至四个点阵的阴极。

3.2.2            D锁存器(列驱动)

74HC373是八路D型锁存器,每个锁存器具有独立的D型输入,以及适用于面向总线的应用的三态输出。

锁存器的主要作用

[1]     

缓存

[2]     

完成高速的控制其与慢速的外设的不同步问题

[3]     

是解决驱动的问题(提供的电流比51IO口输出电流大)

[4]     

拓展I/O口(可以用锁存器幂叠加方法,即锁存器的Q再接锁存器实现IO口的无限拓展)

31锁存器的主要作用

在本设计中就是用到了锁存器的第三个功能,微控制器的IO口均不能流过过大的电流,LED点亮时有约10ms的电流,因此点阵阳极不要直接接单片机IO口,应先经过一个缓冲器74HC373。单片机IO口只需很小的电流控制74HC373即可间接的控制点阵阳极的显示,而74HC373输出也能负载约10mA的电流。设置数码管段的驱动电流为ID=15mA,这个电流点亮度好,并且有一定的裕度。


 

38点阵列驱动电路

在本设计中,如 3‑7所示将锁存器串如单片机与点阵之间,将锁存器的OE接低电平、LE接高电平,以使锁存器的输出始终等于输入。从而实现扩大电流的作用。

3.3 按键电路


 

39按键电路

在本设计中,需要4个按键来控制蛇的方向,因为设计中只需4个按键(上下左右)所以使用独立按键即可满足要求。四个独立按键的一端都接地,独立按键的另一端与单片机连接,其连接方式如 3‑9所示。KEY1-KEY4分别为上下左右。

3.4 锂电池充放电电路


 

34FM3209锂电池充放电电路

FM3209F是一款全集成充电管理,锂电池保护,DC-DC升压限流,手电筒照明及电量指示的多功能电源管理芯片。

FM3209F的同步升压系统提供最大2.1A输出电流,转换效率高至93%。芯片只需一个电感实现降压与升压功能。DC-DC转换器工作在1.5MHz,可以支持低成本电感和电容;芯片支持4LED灯的电量指示与手电筒功能,支持按键单按,双按及长按功能。当空载时,系统进入休眠状态,工作电流降至50uA

FM3209F同步开关充电技术,提供最大2.1A电流,大大缩短充电时间。芯片内置温度控制电路,根据IC温度和输入电压智能调节充电电流。

在本设计中,使用FM3209F从锂电池获取3.7V电压升高至5V为单片机以及外围电路供电。并且可以直接插入5V电源为锂电池充电。

4         软件设计

4.1 主函数


41主函数流程图

单片机复位后,单片机运行main()函数,初始化定时器并点亮由两个LED构成的蛇身和一个食物,蛇开始默认向左运动。初始化完成后单片机循环执行点阵扫描函数(点阵显示扫描函数)与按键扫描函数(检测按键是否被按下)。

Dot matrix scan function: The display principle of dot matrix is ​​to display only one line of data at each moment, and then display each line in turn, and continuously refresh it at a very fast speed, which produces a static display effect. In this design, the P2.0-P2.3 pins of the microcontroller control the 4~16 line decoder to control the rows of the dot matrix, and P0 and P1 control the columns of the dot matrix. When P2.0-P2.3 are 0 , 0 , 0 , 0 respectively , the first row of the dot matrix is ​​selected, and its display content is controlled by P0 and P1 . When the IO port is high level, the corresponding LED point on the dot matrix On the contrary, if it is low level, it will not light up. When P0 and P1 are 0xff and 0x00 respectively , the eight left LEDs in the first row will light up and the right eight will go out. When P2.0-P2.3 are 0 , 0 , 0 , and 1 respectively , the second row of the dot matrix is ​​selected, and so on to achieve the display of each row of the dot matrix. Control the P2.0-P2.3 pins to select rows 1-16 , cycle through them, and send data through P0 and P1 to display the entire dot matrix.

Button scan function: In this design, the buttons use P3.2-P3.5 of the microcontroller . There is a pull-up inside the microcontroller, so when the button is not pressed, the button input is high level, and the other end of the button is grounded. The input is low when the button is pressed. Therefore, the loop scans whether the button has become low level. When the input reaches low

参考设计图片
×
 
 
Search Datasheet?

Supported by EEWorld Datasheet

Forum More
Update:2025-05-07 10:36:43

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
community

Robot
development
community

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号