基于单片机的贪吃蛇游戏设计
扫描二维码
随时随地手机看文章
利用三天假期把贪吃蛇游戏移植到了我的单片机开发板上,单片机是STC89C52只有8K的FLASH,本来想移植俄罗斯方块的但是移植编译之后大约有10K的空间。
下面是我拍的视频,优酷的链接:http://v.youku.com/v_show/id_XNjIxNjM2MzUy.html高手勿喷呐!
程序中使用的器件有:
(1)STC82C52配置有片内RAM512B,片内FLASH存储器 8KB,时钟为8MHz。
(2)2.4寸TFT彩屏,最大支持RGB565格式有65536种颜色。主控制器为ILI9325,程序中使用的是8位数据模式,所以运行比较慢。
(3)红外遥控器,采用的是HS0038芯片。红外协议是NEC协议,32位数据位,第一字节为标识码,第二字节为其反码,第三字节为数据位,第四字节为数据位反码。
下面是我的程序:
lcd.c是TFT液晶屏的驱动程序,key.c是红外芯片的驱动程序,snake.c是贪吃蛇游戏的程序。lcd.h,key.h以及snake.h是对应的头文件,里面有一部分与平台相关的定义
移植的时候只要做对应的修改即可。下面上一下游戏的界面:
下面开始贴源代码啦:
第一代码,lcd.c是TFT彩屏ILI9325的驱动程序。
#include "lcd.h"
#include "fonts_ascii8x16.h"
#include
/* 不精确的延时函数 */
void Delayms(int count)
{
int i,j;
for(i=0;i for(j=0;j<100;j++); } /* 并行数据写入函数,分高低字节写入*/ void LCD_WriteBus(char DataHigh,char DataoLow) { LCD_DataPortH = DataHigh; LCD_WR_L LCD_WR_H LCD_DataPortH = DataoLow; LCD_WR_L LCD_WR_H } /* 写命令字 */ void LCD_WriteCmd(char DataHigh,char DataoLow) { LCD_RS_L LCD_CS_L LCD_WriteBus(DataHigh,DataoLow); LCD_RS_H LCD_CS_H } /* 写数据 */ void LCD_WriteData(char DataHigh,char DataoLow) { LCD_CS_L LCD_RS_H LCD_WriteBus(DataHigh,DataoLow); LCD_CS_H } /*往指定寄存器写入数据 */ void LCD_WriteReg(unsigned int Register,unsigned int Data) { LCD_WriteCmd(Register>>8, Register); LCD_WriteData(Data>>8, Data); } /* 设置绘图坐标窗口,其中(x1,y1)为左上顶点,(x2,y2)为右下顶点 */ void LCD_SetWindow(unsigned int x1,unsigned int y1,unsigned int x2,unsigned int y2) { /* 设置显示的矩形区域 */ LCD_WriteCmd(0x00,0x50);LCD_WriteData(x1>>8,x1); //开始X LCD_WriteCmd(0x00,0x52);LCD_WriteData(y1>>8,y1); //开始Y LCD_WriteCmd(0x00,0x51);LCD_WriteData(x2>>8,x2); //结束X LCD_WriteCmd(0x00,0x53);LCD_WriteData(y2>>8,y2); //结束Y /* 设置矩形绘图区域的起始点 */ LCD_WriteCmd(0x00,0x20);LCD_WriteData(x1>>8,x1); //设置X绘图起始坐标位置 LCD_WriteCmd(0x00,0x21);LCD_WriteData(y1>>8,y1); //设置Y绘图起始坐标位置 LCD_WriteCmd(0x00,0x22); } /* 初始化LCD,写入工作模式相关命令字*/ void LCD_Init(void) { LCD_RST_H Delayms(5); LCD_RST_L Delayms(5); LCD_RST_H LCD_CS_H LCD_RD_H LCD_WR_H Delayms(5); LCD_CS_L /*打开片选使能*/ //************* Start Initial Sequence **********// LCD_WriteCmd(0x00,0xE5); LCD_WriteData(0x78,0xF0); // set SRAM internal timing LCD_WriteCmd(0x00,0x01); LCD_WriteData(0x01,0x00); // set SS and SM bit LCD_WriteCmd(0x00,0x02); LCD_WriteData(0x07,0x00); // set 1 line inversion LCD_WriteCmd(0x00,0x03); LCD_WriteData(0x10,0x30); // set GRAM write direction and BGR=1. LCD_WriteCmd(0x00,0x04); LCD_WriteData(0x00,0x00); // Resize register LCD_WriteCmd(0x00,0x08); LCD_WriteData(0x02,0x07); // set the back porch and front porch LCD_WriteCmd(0x00,0x09); LCD_WriteData(0x00,0x00); // set non-display area refresh cycle ISC[3:0] LCD_WriteCmd(0x00,0x0A); LCD_WriteData(0x00,0x00); // FMARK function LCD_WriteCmd(0x00,0x0C); LCD_WriteData(0x00,0x00); // RGB interface setting LCD_WriteCmd(0x00,0x0D); LCD_WriteData(0x00,0x00); // Frame marker Position LCD_WriteCmd(0x00,0x0F); LCD_WriteData(0x00,0x00); // RGB interface polarity //*************Power On sequence ****************// LCD_WriteCmd(0x00,0x10); LCD_WriteData(0x00,0x00); // SAP, BT[3:0], AP, DSTB, SLP, STB LCD_WriteCmd(0x00,0x11); LCD_WriteData(0x00,0x07); // DC1[2:0], DC0[2:0], VC[2:0] LCD_WriteCmd(0x00,0x12); LCD_WriteData(0x00,0x00); // VREG1OUT voltage LCD_WriteCmd(0x00,0x13); LCD_WriteData(0x00,0x00); // VDV[4:0] for VCOM amplitude LCD_WriteCmd(0x00,0x07); LCD_WriteData(0x00,0x01); Delayms(50); // Dis-charge capacitor power voltage LCD_WriteCmd(0x00,0x10); LCD_WriteData(0x10,0x90); // 1490//SAP, BT[3:0], AP, DSTB, SLP, STB LCD_WriteCmd(0x00,0x11); LCD_WriteData(0x02,0x27); // DC1[2:0], DC0[2:0], VC[2:0] Delayms(50); // Delay 50ms LCD_WriteCmd(0x00,0x12); LCD_WriteData(0x00,0x1F); //001C// Internal reference voltage= Vci; Delayms(50); // Delay 50ms LCD_WriteCmd(0x00,0x13); LCD_WriteData(0x15,0x00); //0x1000//1400 Set VDV[4:0] for VCOM amplitude 1A00 LCD_WriteCmd(0x00,0x29); LCD_WriteData(0x00,0x27); //0x0012 //001a Set VCM[5:0] for VCOMH //0x0025 0034 LCD_WriteCmd(0x00,0x2B); LCD_WriteData(0x00,0x0D); // Set Frame Rate 000C Delayms(50); // Delay 50ms LCD_WriteCmd(0x00,0x20); LCD_WriteData(0x00,0x00); // GRAM horizontal Address LCD_WriteCmd(0x00,0x21); LCD_WriteData(0x00,0x00); // GRAM Vertical Address // ----------- Adjust the Gamma Curve ----------// LCD_WriteCmd(0x00,0x30); LCD_WriteData(0x00,0x00); LCD_WriteCmd(0x00,0x31); LCD_WriteData(0x07,0x07); LCD_WriteCmd(0x00,0x32); LCD_WriteData(0x03,0x07); LCD_WriteCmd(0x00,0x35); LCD_WriteData(0x02,0x00); LCD_WriteCmd(0x00,0x36); LCD_WriteData(0x00,0x08);//0207 LCD_WriteCmd(0x00,0x37); LCD_WriteData(0x00,0x04);//0306 LCD_WriteCmd(0x00,0x38); LCD_WriteData(0x00,0x00);//0102 LCD_WriteCmd(0x00,0x39); LCD_WriteData(0x07,0x07);//0707 LCD_WriteCmd(0x00,0x3C); LCD_WriteData(0x00,0x02);//0702 LCD_WriteCmd(0x00,0x3D); LCD_WriteData(0x1D,0x04);//1604 /*------------------ Set GRAM area ---------------*/ LCD_WriteCmd(0x00,0x50); LCD_WriteData(0x00,0x00); // Horizontal GRAM Start Address LCD_WriteCmd(0x00,0x51); LCD_WriteData(0x00,0xEF); // Horizontal GRAM End Address LCD_WriteCmd(0x00,0x52); LCD_WriteData(0x00,0x00); // Vertical GRAM Start Address LCD_WriteCmd(0x00,0x53); LCD_WriteData(0x01,0x3F); // Vertical GRAM Start Address LCD_WriteCmd(0x00,0x60); LCD_WriteData(0xA7,0x00); // Gate Scan Line LCD_WriteCmd(0x00,0x61); LCD_WriteData(0x00,0x01); // NDL,DataoLowE, REV LCD_WriteCmd(0x00,0x6A); LCD_WriteData(0x00,0x00); // set scrolling line /*-------------- Partial Display Control ---------*/ LCD_WriteCmd(0x00,0x80); LCD_WriteData(0x00,0x00); LCD_WriteCmd(0x00,0x81); LCD_WriteData(0x00,0x00); LCD_WriteCmd(0x00,0x82); LCD_WriteData(0x00,0x00); LCD_WriteCmd(0x00,0x83); LCD_WriteData(0x00,0x00);