基于单片机的贪吃蛇游戏设计

发布者:oplndctkl出最新更新时间:2017-01-11 来源: eefocus关键字:单片机  贪吃蛇游戏 手机看文章 扫描二维码
随时随地手机看文章

利用三天假期把贪吃蛇游戏移植到了我的单片机开发板上,单片机是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);

    LCD_WriteCmd(0x00,0x84); LCD_WriteData(0x00,0x00);

    LCD_WriteCmd(0x00,0x85); LCD_WriteData(0x00,0x00);

    /*-------------- Panel Control -------------------*/

    LCD_WriteCmd(0x00,0x90); LCD_WriteData(0x00,0x10);

    LCD_WriteCmd(0x00,0x92); LCD_WriteData(0x06,0x00);

    LCD_WriteCmd(0x00,0x07); LCD_WriteData(0x01,0x33); // 262K color and display ON

    LCD_CS_H  /*关闭片选使能 */


}


void LCD_FillScreen(uint16 color)

{

    int i,j;

    /*打开片选使能*/

    LCD_CS_L  

    /*设置绘图坐标原点*/

    LCD_SetWindow(0,0,LCD_WIDTH-1,LCD_HEIGHT-1);

    for(i=0;i

    {

      for (j=0;j

           {

             LCD_WriteData(color>>8,color);

        }


    }

    /*关闭片选使能*/

     LCD_CS_H  

}


/*========================    定位函数    ========================*/

/* 函数参数:u8 x,u8 y                                            */

/* 返回值:     void                                                */

/*==============================================================*/

void LCD_SetCursor(unsigned int  x,unsigned int y)

{


    LCD_WriteReg(0x0020,x);            /* 设置x坐标 */

    LCD_WriteReg(0x0021,y);            /* 设置y坐标 */

    LCD_WriteCmd(0x00,0x22);        /* 开始写数据 */


}

/* 设置字符显示窗口,默认是8x16的大小,具体根据字模大小而定 */

/*

void LCD_SetCharWindow(unsigned int x,unsigned int y)

{


    LCD_SetWindow(x, y, x+7, y+15);

}

*/


/*******************************************************************************

* Function Name  : LCD_Line

* Description    : 在s_x、s_y为起始坐标,e_x、e_y为结束坐标绘制一条直线

* Input          : x  X轴坐标     y  Y轴坐标

* Output         : None

* Return         : None

* 使用前景色

*******************************************************************************/

void LCD_Line(unsigned int  s_x, unsigned int s_y, unsigned int e_x, unsigned int e_y, unsigned int color)

{  

    signed int Offset_x,Offset_y,Offset_k = 0;

    signed int Err_d = 1;

    if(s_y > e_y)

    {

        Offset_x = s_x;

        s_x = e_x;

        e_x = Offset_x;

        Offset_x = s_y;

        s_y = e_y;

        e_y = Offset_x;

    } 

    Offset_x = e_x-s_x;

    Offset_y = e_y-s_y;

    

    LCD_SetCursor(s_x,s_y);

    LCD_WriteData(color>>8, color);


    if(Offset_x < 0)

    {

        Offset_x = s_x-e_x;

        Err_d = -1;

    }

    if(0 == Offset_x )

    {

        while(s_y < e_y)

        {

              s_y++;

              if(s_y > Dis_Y_MAX) return;

              LCD_SetCursor(s_x,s_y);

              LCD_WriteData(color>>8, color);

        }

         return;

    }

    else if(Offset_y == 0)

    {

        while(s_x != e_x)

        {    

              s_x += Err_d;

              if(s_x > Dis_X_MAX) return;

              LCD_SetCursor(s_x,s_y);

              LCD_WriteData(color>>8,color);

    }

    return;

    }

    if(Offset_x > Offset_y)

    {

        Offset_k += Offset_y;

        while(s_x != e_x)

        {

          if(Offset_k>0)

          {

               s_y += 1;

            Offset_k += (Offset_y-Offset_x);

      }

      else Offset_k += Offset_y; 

          s_x += Err_d;

      if(s_x > Dis_X_MAX||s_y > Dis_Y_MAX) break;

          LCD_SetCursor(s_x,s_y);

        LCD_WriteData(color>>8 ,color);

    }    

    }

    else

    {

    Offset_k += Offset_x;

    while(s_y != e_y)

    {

      if(Offset_k > 0)

      {

        s_x += Err_d;

        Offset_k += (Offset_x-Offset_y);

      }

      else Offset_k += Offset_x;

      s_y += 1;

      if(s_x > Dis_X_MAX||s_y > Dis_Y_MAX) break;

      LCD_SetCursor(s_x,s_y);

      LCD_WriteData(color>>8, color);

    }

    }    

}



/*******************************************************************************

* Function Name  : LCD_Rectangle

* Description    : 画一个矩形(mode = 0) or 矩形面(mode = 1)

* Input          : left - 矩形的左上角横坐标,范围0到118

*         top - 矩形的左上角纵坐标,范围0到50

*         right - 矩形的右下角横坐标,范围1到119

*         bottom - 矩形的右下角纵坐标,范围1到51

*         Mode - 绘制模式,可以是下列数值之一:

*                0:    矩形框(空心矩形)

*                1:    矩形面(实心矩形)

* Output         : None

* Return         : None

*******************************************************************************/

void LCD_Rectangle(unsigned int left, unsigned int  top, 

                   unsigned int right, unsigned int bottom, 

                   unsigned char Mode,unsigned int color)

{

  unsigned int uiTemp;

  if(Mode==0)

  {

    LCD_Line(left,top,left,bottom,color);

    LCD_Line(left,top,right,top,color);

    LCD_Line(right,bottom,left,bottom,color);

    LCD_Line(right,bottom,right,top,color);

  }

  else

  {

    if(left>right)

    {

      uiTemp = left;

      left = right;

      right = uiTemp;

    }

    if(top>bottom)

    {

      uiTemp = top;

      top = bottom;

      bottom = uiTemp;

    }

    for(uiTemp=top;uiTemp<=bottom;uiTemp++)

    {

      LCD_Line(left,uiTemp,right,uiTemp,color);

    }

  }

}


void LCD_Point(uint16 Xpos, uint16 Ypos, uint16 color) 

{

    if ( (Xpos>320)||(Ypos>240) ) return;

    LCD_SetCursor(Xpos, Ypos);

    LCD_WriteData(color>>8, color);

}


/****************************************************************************

* 名    称:void LCD_PutChar(u16 x,u16 y,u8 c,u16 charColor,u16 bkColor)

* 功    能:在指定座标显示一个8x16点阵的ascii字符

* 入口参数:x          行座标

*           y          列座标

*           charColor  字符的颜色

*           bkColor    字符背景颜色

* 出口参数:无

* 说    明:显示范围限定为可显示的ascii码

* 调用方法:LCD_DrawChar(10,10,'a',0x0000,0xffff);

****************************************************************************/

void LCD_DrawChar(uint16 x,uint16 y,uint8 c,uint16 color,uint16 bgcolor)

{

  uint16 i=0;

  uint16 j=0;

  

  uint8 tmp_char=0;


  for (i=0; i<16 ;i++)

  {

    tmp_char = Ascii_8x16[((c-0x20)*16)+i];       /* 相对位置转换 */

    for (j=0 ; j<8 ;j++)

    {

      if ( (tmp_char >> 7-j) & 0x01 == 0x01)

        {

          LCD_Point(x+j ,y+i ,color); // 字符颜色

        }

        else

        {

          LCD_Point(x+j ,y+i ,bgcolor);  // 背景颜色

        }

    }

  }



/*******************************************************************************

*    函数名: LCD_DisplayStr

*    参  数:

*        Xpos : X坐标);

*        Ypos : Y坐标)

*        Ascii : 字符的ASCII代码,在0x20和0x7E之间

*    返  回: 无

*    功  能: 在LCD上显示一个字符(8x16)

*/


void LCD_DisplayStr(uint16 x,uint16 y,unsigned char *str,uint16 dcolor,uint16 bgcolor)      

{  

    uint16 Xpos,Ypos;

    Xpos = x;

    Ypos = y;

    while(*str != '\0')

    {    

        LCD_DrawChar(Xpos,Ypos,*str,dcolor,bgcolor);

        Xpos += 7;

        str++;

    }    

}


/*******************************************************************************

*    函数名: LCD_DisplayChinese

*    参  数:

*        Xpos : X坐标);

*        Ypos : Y坐标)

*        Ascii : 字符的ASCII代码,在0x20和0x7E之间

*    返  回: 无

*    功  能: 在LCD上显示一个字符(8x16)

*/

/*

void LCD_DisplayChinese(uint16 Xpos, uint16 Ypos, uchar* pData, uint16 textColor, uint16 backColor)

{

    uchar i,j,b;

    

    LCD_SetWindow(Xpos, Ypos, Xpos+15, Ypos+15);

    for(j=0;j<32;j++)

    {

        b=*(pData+j);

        for(i=0;i<8;i++)

        {

            if(b&0x80)

            {

                LCD_WriteData(textColor>>8, textColor);    

            }

            else

            {

                LCD_WriteData(backColor>>8, backColor);

            }

            b=b<<1;            

        }    

    }

    LCD_SetWindow(0, 0, LCD_WIDTH-1, LCD_HEIGHT-1);

}

*/

/*******************************************************************************

*    函数名: LCD_DisplayChineseBig

*    参  数:

*        Xpos : X坐标);

*        Ypos : Y坐标)

*        Ascii : 字符的ASCII代码,在0x20和0x7E之间

*    返  回: 无

*    功  能: 在LCD上显示一个字符(8x16)

*/

/*

void LCD_DisplayChineseBig(uint16 Xpos, uint16 Ypos, uchar* pData, uint16 textColor, uint16 backColor)

{

    uchar i,j,b;

    

    LCD_SetWindow(Xpos, Ypos, Xpos+23, Ypos+23);

    for(j=0;j < 72;j++)

    {

        b=*(pData+j);

        for(i=0;i<8;i++)

        {

            if(b&0x80)

            {

                LCD_WriteData(textColor>>8, textColor);    

            }

            else

            {

                LCD_WriteData(backColor>>8, backColor);

            }

            b=b<<1;            

        }    

    }

    LCD_SetWindow(0, 0, LCD_WIDTH-1, LCD_HEIGHT-1);

}

*/


下面是lcd.h是上面的头文件:



#ifndef _LCD_H_

#define _LCD_H_


/*包含头文件*/

#include

/* 单片机相关的数据类型 */

#define uint16  unsigned int 

#define int16   int 

#define uint8   unsigned char

#define uint32  unsigned long

#define int32   long

#define uchar   unsigned char


/*管脚定义,与使用平台相关,移植修改部分*/


#define  LCD_DataPortH P0    //高8位数据口,8位模式下只使用高8位 

sbit LCD_CS=P2^3;             //片选    

sbit LCD_RS = P2^0;           //数据/命令切换

sbit LCD_WR = P2^1;             //写控制

sbit LCD_RD =P2^2;             //读控制

sbit LCD_RST = P2^5;         //复位   


/*与TFT彩屏相关的参数,即彩屏的分辨率*/

#define Dis_Y_MAX                       320                                      

#define Dis_X_MAX                       240


/********************************************************************

    defination of hardware

*********************************************************************/


#define        LCD_CS_L    LCD_CS = 0;     //CS管脚为低

#define        LCD_CS_H    LCD_CS = 1;        //CS管脚为高

#define        LCD_RD_L    LCD_RD = 0;        //RD管脚为低

#define        LCD_RD_H    LCD_RD = 1;        //RD管脚为高

#define     LCD_WR_L    LCD_WR = 0;        //WR管脚为低

#define     LCD_WR_H    LCD_WR = 1;        //WR管脚为高

#define        LCD_RS_L    LCD_RS = 0;        //RS管脚为低

#define        LCD_RS_H    LCD_RS = 1;        //RS管脚为高

#define        LCD_RST_L    LCD_RST = 0;    //RST管脚为低

#define        LCD_RST_H    LCD_RST = 1;    //RST管脚为高


/*******defination of color************/

#define COLOR_TO_MTK_COLOR_SIMUL(color) ((((color) >> 19) & 0x1f) << 11) \

                                            |((((color) >> 10) & 0x3f) << 5) \

                                            |(((color) >> 3) & 0x1f)


/* RGB565格式 */


#define RED         0xf800

#define GREEN         0x07E0

#define BLUE         0x001F

#define WHITE         0xffff

#define PURPLE      0xF81F

#define YELLOW         0xFFE0

#define CYAN        0x07FF

#define BLACK         0x0000


#define LIGHT_BLUE       COLOR_TO_MTK_COLOR_SIMUL(0x93DB70)

#define YELLOW_GREEN     COLOR_TO_MTK_COLOR_SIMUL(0x7FFF00)



#define LCD_WIDTH        240

#define LCD_HEIGHT        320

/*RGB 向RGB565转换,可能会有损失*/

#define RGB(r,g,b) ( (r<<8)&(RED) | ((g<<3)&GREEN) | ((b>>3)&BLUE) )



/* 不精确的延时函数 */

void Delayms(int count) ;

/* 并行数据写入函数,分高低字节写入*/

void LCD_WriteBus(char DataHigh,char DataoLow)  ;

/* 写命令字 */

void LCD_WriteCmd(char DataHigh,char DataoLow)    ;

/* 写数据 */

void LCD_WriteData(char DataHigh,char DataoLow);

/*往指定寄存器写入数据 */

void LCD_WriteReg(unsigned int Register,unsigned int Data);


/* 初始化LCD,写入工作模式相关命令字*/

void LCD_Init(void);

/* 填充屏幕 */

void LCD_FillScreen(uint16 color);


void LCD_SetCursor(unsigned int  x,unsigned int y);

/* 设置绘图坐标窗口,其中(x1,y1)为左上顶点,(x2,y2)为右下顶点 */

void LCD_SetWindow(unsigned int x1,unsigned int y1,unsigned int x2,unsigned int y2);


void LCD_DrawChar(uint16 x,uint16 y,uint8 c,uint16 color,uint16 bgcolor);

//void LCD_WriteBMP(const unsigned int *ptr);

void LCD_DisplayStr(uint16 x,uint16 y,unsigned char *str,uint16 dcolor,uint16 bgcolor);

//void LCD_DisplayChinese(uint16 Xpos, uint16 Ypos, uchar* pData, uint16 textColor, uint16 backColor);

//void LCD_DisplayChineseBig(uint16 Xpos, uint16 Ypos, uchar* pData, uint16 textColor, uint16 backColor);


void LCD_Line(unsigned int  s_x, unsigned int s_y, unsigned int e_x, unsigned int e_y, unsigned int color);

void LCD_Rectangle(unsigned int left, unsigned int  top, 

                   unsigned int right, unsigned int bottom, 

                   unsigned char Mode,unsigned int color);


//void LCD_Circle(unsigned int  x, unsigned int y, unsigned int r, unsigned char mode, unsigned int color);

#endif


下面是key.c红外键盘的驱动程序,采用的是中断方式:



#include "key.h"

#include


#define uchar unsigned char    

#define uint  unsigned int 


#define c(x) (x*120000/120000) 


sbit    IR   = P3^2;           //遥控接头接口    

sbit     beep   = P3^5;        //蜂鸣器接口


uchar    Data[4];            //识别码,操作码存放,Data[0] 为识别码,Data[2]为操作码


/*************这个Key全局变量很重要,他是系统中断中获取的键值************/

uchar   key = 0;


void delay_50ms(unsigned int t)

{

    unsigned int j;   

    for(;t>0;t--) 

      for(j=6245;j>0;j--) 

        {;}

}


void Beep()

{

      beep = 0;

      delay_50ms(2);

      beep = 1;

}


/********************** 外部中断函数************************/

void exint0() interrupt 0 

{      

   uint cnt;

   uchar i;


   EX0 = 0;

   cnt = 0;    

    

   while(!IR) cnt++;                               //记录引导码时间 

   if(cnt < 1000)

   {

       EX0=1;return;

   }                   //9ms的计数值(12MHz:1000< cnt <1500)

       

   cnt = 0;             

   while(IR) if(cnt++ > 400)

               {

                EX0=1;return;

            }        //防卡死,超时保护(12MHz: > 300)    

   if(cnt < 200)

               {

                EX0=1;return;

            }                    //(12MHz不分频: <260)        


   for(i=0; i<32; i++)                             //读取32位位码

       {

         cnt = 0;                                                                        

         while(!IR);               

         while(IR) if(cnt++ > 200)

                    {

                       EX0=1;return;

                   }  //超时保护(12MHz:>=200)

         Data[i/8] >>= 1;

         if(cnt>60) 

                   Data[i/8] |= 0x80;                            //0和1的计数界线(12MHz:< 109)    

       }


       if(Data[0] == ~Data[1] && Data[2] == ~Data[3])            //校验识别码,操作码    

       {

            key = Data[2];

       }


    Beep();

    EX0 = 1;         

复制代码

下面是key.h头文件:


#ifndef _KEY_H_

#define _KEY_H_

void Beep();

#endif

下面是贪吃蛇源文件snake.c



#include "lcd.h"

#include "key.h"

#include "snake.h"

#include

#include




extern uchar key;


Food food;

Snake snake;

int score = 0; 




/*******************************************************************************

* Function Name  : ShowStartMenu

* Description    : 绘制游戏开始界面 

* Input          : None

* Output         : None

* Return         : None

* 使用前景色

*******************************************************************************/

void ShowStartMenu(void)

{

    LCD_FillScreen(BLACK);                  

    LCD_Rectangle(0, 0, LCD_WIDTH-1, 20, 1, CYAN);

    LCD_Rectangle(0,20, LCD_WIDTH-1, 40, 1, BLUE);



    LCD_Rectangle(90, 140, 120, 170, 1, RED);

    LCD_Rectangle(120, 140, 150, 170, 1, YELLOW);

    LCD_Rectangle(90, 170, 120, 200, 1, BLUE);

    LCD_Rectangle(120,170, 150, 200, 1, GREEN);

    


    LCD_DisplayStr(75, 80, "Greedy Sanke", YELLOW_GREEN, BLACK);


    LCD_DisplayStr(50, 220, "Press Key 5 To Start", WHITE, BLACK);

    LCD_Rectangle(0, 280, LCD_WIDTH-1, 300, 1, BLUE);

    LCD_Rectangle(0, 300, LCD_WIDTH-1, LCD_HEIGHT-1, 1, CYAN);

}


/* 绘制游戏进行的主界面 */

void ShowGameWindow(void ) 

{

    char i = 0;

    

    

    /*清除上面的屏幕*/

    LCD_FillScreen(BLACK);

    /*游戏主界面绘制*/

    LCD_Rectangle(0, 0, LCD_WIDTH, 5,1 ,BLUE);

    LCD_Rectangle(0, LCD_HEIGHT, LCD_WIDTH-1, LCD_HEIGHT-5, 1, BLUE);

    LCD_Rectangle(0, 5, 5, LCD_HEIGHT-5, 1, BLUE);

    LCD_Rectangle(LCD_WIDTH-5, 5, LCD_WIDTH, LCD_HEIGHT-5,    1, BLUE);


    LCD_Rectangle(5, 5, LCD_WIDTH-5, 10,1, CYAN);

    LCD_Rectangle(5, LCD_HEIGHT-5, LCD_WIDTH-5, LCD_HEIGHT-10, 1, CYAN);

    LCD_Rectangle(5, 10, 10, LCD_HEIGHT-10, 1, CYAN);

    LCD_Rectangle(LCD_WIDTH-10, 10, LCD_WIDTH-5, LCD_HEIGHT-10, 1, CYAN);


    LCD_Line(10, 30, 230, 30, RED);

    LCD_Line(90, 10, 90, 30,  RED);

    LCD_Line(160, 10, 160, 30 ,RED);



    LCD_DisplayStr(15,12, "SCORE:", PURPLE, BLACK);

    LCD_DisplayStr(100,12, "GRADE:", PURPLE, BLACK);

    LCD_DisplayStr(170,12, "ABOUT", PURPLE, BLACK);

    LCD_DisplayStr(63,12,"0",YELLOW_GREEN, BLACK);

    LCD_DisplayStr(100+8*6,12,"0", YELLOW_GREEN, BLACK);


    LCD_DisplayStr(50, 180, "Press Key 5 To Start", WHITE, BLACK);

    LCD_DisplayStr(50, 200, "Press Key 0 To Escape", WHITE, BLACK);

    LCD_DisplayStr(50, 220, "Press Key 9 To About", WHITE,BLACK);

}



void GamePlay() 

{    

    int i;


    char Status = 0;    //游戏运行状态,0为暂停1为运行

    unsigned char prevKey = START;    /*前一个被按下的按键*/

    

    food.yes=1;                        /*1代表要出现食物,0表示以存在食物*/ 

    snake.life=0; 

    snake.direction = 1;            /* 开始的方向,向右移动 */ 

    snake.x[0]=100;snake.y[0]=100; 

    snake.x[1]=110;snake.y[1]=100; 

    snake.node = 2; 


    Status = 1;


    PrintScore(); 

    while(1) /*可以重复游戏*/ 

    { 

        while((key == prevKey) && (Status == 1)) /*在没有按键的情况下蛇自己移动*/ 

        { 

            prevKey = key;

            if(food.yes == 1) /*需要食物*/ 

            { 

                food.x = rand()%200+20; 

                food.y = rand()%200+80; /*使用rand函数随机产生食物坐标*/ 

                while(food.x%10!= 0) 

                food.x++; 

                while(food.y%10!=0) 

                food.y++; /*判断食物是否出现在整格里*/ 

                food.yes=0; /*现在有食物了*/ 

            } 

            if(food.yes == 0) /*有食物了就要显示出来*/ 

            { 

                LCD_Rectangle(food.x, food.y, food.x+10, food.y-10,1, RED);

            } 

            for(i = snake.node-1; i>0; i--) /*贪吃蛇的移动算法*/ 

            { 

                snake.x[i]=snake.x[i-1]; 

                snake.y[i]=snake.y[i-1]; /*贪吃蛇的身体移动算法*/ 

            }


            switch(snake.direction) /*贪吃蛇的头部移动算法,以此来控制移动*/ 

            { 

                case 1:snake.x[0] += 10;break; 

                case 2:snake.x[0] -= 10;break; 

                case 3:snake.y[0] -= 10;break; 

                case 4:snake.y[0] += 10;break; 

            } 

            for(i=3; i

            { 

                if(snake.x[i]==snake.x[0]&&snake.y[i]==snake.y[0]) 

                { 

                    GameOver(); 

                    snake.life=1; 

                    break; 

                } 

            } 

            /*下面是判断是否撞到墙壁*/ 

            if(snake.x[0]<10 || snake.x[0]>230 || snake.y[0]< 30 || snake.y[0]> 310) 

            { 

                GameOver(); 

                snake.life=1; 

            } 

            if(1 == snake.life) /*如果死亡就退出循环*/ 

                break; 

            if(snake.x[0]==food.x && snake.y[0]==food.y) /*判断蛇是否吃到食物*/ 

            { 

                

                LCD_Rectangle(food.x, food.y, food.x+10, food.y-10, 1, BLACK);

                snake.x[snake.node]=-20;snake.y[snake.node]=-20; /*现把增加的一节放到看不到的地方去*/


                snake.node++; 

                food.yes=1; 

                score+=10; 

                PrintScore(); 

            } 

            

            /*绘制贪吃蛇*/

             for(i=0; i

             {

                 LCD_Rectangle(snake.x[i], snake.y[i], snake.x[i]+10, snake.y[i]-10, 1, YELLOW);

             }


            DelayGameSpeed();

            /*擦掉最后一个方块 */

            LCD_Rectangle(snake.x[snake.node-1],snake.y[snake.node-1],snake.x[snake.node-1]+10,snake.y[snake.node-1]-10, 1, BLACK);


        } 

                  

            if(snake.life == 1)

                 break;     //退出游戏

            else if( key == ESC) 

                    break;  //退出游戏

            else if (key == START)

            {

                Status ^= 0x01;

                

                prevKey = key;

            }

            else if(key == ABOUT)

            {

                AboutGame();

            }

            else if((key==UP) && (snake.direction!=4))

            {

                snake.direction = 3; prevKey = key; 

            }

            else if( (key==RIGHT) && (snake.direction!=2)) 

            {

                snake.direction = 1;  prevKey = key; 

            }

            else if( (key==LEFT) && (snake.direction!=1))     

            {    

                snake.direction = 2; prevKey = key;

            }

            else if( (key==DOWN) && (snake.direction!=3)) 

            {

                snake.direction = 4; prevKey = key;

            }

         

    }

}


/*输出游戏的成绩*/

void PrintScore()

{

     int qian,bai,shi;

     char i = 0;

     qian = score/1000;

     bai = (score%1000)/100;

     shi = (score%100)/10;

     if(qian)    

     {    

         LCD_DrawChar(63,12, qian+48, YELLOW, BLACK);

        i++;    

     };

     if(bai)

     {        

         LCD_DrawChar(63+8*i,12, bai+48, YELLOW, BLACK);

        i++;

     }

     if(shi)        

     {    

         LCD_DrawChar(63+8*i,12, shi+48, YELLOW, BLACK);

        i++;

     }

     LCD_DrawChar(63+8*i,12,'0', YELLOW, BLACK);

}

/* 游戏结束 */

void GameOver() 

{     

     int qian,bai,shi;

     qian = score/1000;

     bai = (score%1000)/100;

     shi = (score%100)/10;


     LCD_FillScreen(BLACK);

     LCD_DisplayStr(100,120,"Game Over", RED, BLACK);

     LCD_DisplayStr(100, 140, "Final Score", RED, BLACK);

                           

     if(qian)        LCD_DrawChar(100,160, qian+48, YELLOW, BLACK);

     if(bai)        LCD_DrawChar(100+8,160, bai+48, YELLOW, BLACK);

     if(shi)        LCD_DrawChar(100+8*2,160, shi+48, YELLOW, BLACK);

     LCD_DrawChar(100+8*3,160,'0', YELLOW, BLACK);


     while( key != ESC)

     {

     }

     LCD_FillScreen(LIGHT_BLUE);

}


void DelayGameSpeed()

{

    Delayms(10*(100-5*(score/10)));

}


void AboutGame()

{

    LCD_FillScreen(BLACK);


    LCD_DisplayStr(20,30, "Greedy Snake Version 1.0", PURPLE, BLACK);


    LCD_Line(20,50,220,50, YELLOW);

    LCD_DisplayStr(20,60, "How To Play ?", BLUE, BLACK);

    LCD_DisplayStr(50,80, "Press Key 2 Upwords", CYAN, BLACK);

    LCD_DisplayStr(50,100, "Press Key 8 Downwords", CYAN, BLACK);

    LCD_DisplayStr(50,120, "Press Key 4 Left", CYAN, BLACK);

    LCD_DisplayStr(50,140, "Press Key 6 Left", CYAN, BLACK);


    LCD_Line(20,160,220, 160, YELLOW)    ;

    LCD_DisplayStr(20,170, "Aout This Game:", BLUE, BLACK);

    LCD_DisplayStr(50,190, "Author: FangYing", CYAN, BLACK);

    LCD_DisplayStr(50,210, "NEU EE 0903 ",CYAN, BLACK);

    LCD_DisplayStr(50,230, "QQ: 791429438 ",CYAN, BLACK);    


    while(key != START)

    {

    }

    

}


下面是snake.h



#ifndef _SNAKE_H_

#define _SNAKE_H_


#define N 20   /* 定义蛇的最大长度 */


#define ESC     0x16

#define UP         0x18

#define DOWN     0x52

#define RIGHT   0x5A

#define LEFT    0x08

#define START   0x1C

#define ABOUT   0x4A


typedef struct Food /*食物的结构体*/ 

int x; /*食物的横坐标*/ 

int y; /*食物的纵坐标*/ 

char yes; /*食物是否出现的变量*/ 

}Food; 


typedef struct Snack /*蛇的结构体*/ 

    int x[N]; 

    int y[N]; 

    int node; /*蛇的节数*/ 

    int direction; /*蛇的方向*/ 

    int life; /*蛇的生命,0活着,1死亡*/ 

}Snake; 


void ShowStartMenu(void);

void ShowGameWindow(void );

void GamePlay();

void PrintScore();

void GameOver();

void DelayGameSpeed();

void AboutGame();


#endif


最后是游戏的主程序:



#include "lcd.h"

#include "snake.h"

#include "key.h"

#include


extern uchar key;


void InterruptInit() 

{

  /*初始化中断,使用中断中读取红外线键值*/

  IT0 = 1; 

  EX0 = 1;

  EA  = 1;


}


void main()

 

       LCD_Init();

    LCD_FillScreen(BLACK);

   

    InterruptInit();

    start:

    ShowStartMenu();

    

     while(1) 

     {     

        if( key == START)

        {

            key = 0;

            break;

        }

     }

     

     LCD_Init();

     LCD_FillScreen(BLACK);

     ShowGameWindow();

     while(1)

     {

     

        if( key == START)

        {

            LCD_DisplayStr(50, 180, "                      ", BLACK, BLACK);

            LCD_DisplayStr(50, 200, "                      ", BLACK, BLACK);

            LCD_DisplayStr(50, 220, "                      ", BLACK, BLACK);

            break;

        }

        else if(key == ABOUT)

        {

             AboutGame();

        }

     }

     GamePlay();


}            


 


差点忘了还有Ascii_8x16的文件,这个是8x16的ASCII码头文件:



#ifndef _FONTS_ASCII_8x16_H_

#define _FONTS_ASCII_8x16_H_


unsigned char code Ascii_8x16[]=       

{

0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

0x00,0x00,0x18,0x3C,0x3C,0x3C,0x18,0x18,0x18,0x00,0x18,0x18,0x00,0x00,0x00,0x00,

0x00,0x66,0x66,0x66,0x24,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

0x00,0x00,0x00,0x6C,0x6C,0xFE,0x6C,0x6C,0x6C,0xFE,0x6C,0x6C,0x00,0x00,0x00,0x00,

0x18,0x18,0x7C,0xC6,0xC2,0xC0,0x7C,0x06,0x86,0xC6,0x7C,0x18,0x18,0x00,0x00,0x00,

0x00,0x00,0x00,0x00,0xC2,0xC6,0x0C,0x18,0x30,0x60,0xC6,0x86,0x00,0x00,0x00,0x00,

0x00,0x00,0x38,0x6C,0x6C,0x38,0x76,0xDC,0xCC,0xCC,0xCC,0x76,0x00,0x00,0x00,0x00,

0x00,0x30,0x30,0x30,0x60,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

0x00,0x00,0x0C,0x18,0x30,0x30,0x30,0x30,0x30,0x30,0x18,0x0C,0x00,0x00,0x00,0x00,

0x00,0x00,0x30,0x18,0x0C,0x0C,0x0C,0x0C,0x0C,0x0C,0x18,0x30,0x00,0x00,0x00,0x00,

0x00,0x00,0x00,0x00,0x00,0x66,0x3C,0xFF,0x3C,0x66,0x00,0x00,0x00,0x00,0x00,0x00,

0x00,0x00,0x00,0x00,0x00,0x18,0x18,0x7E,0x18,0x18,0x00,0x00,0x00,0x00,0x00,0x00,

0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x18,0x18,0x18,0x30,0x00,0x00,0x00,

0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFE,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x18,0x18,0x00,0x00,0x00,0x00,

0x00,0x00,0x00,0x00,0x02,0x06,0x0C,0x18,0x30,0x60,0xC0,0x80,0x00,0x00,0x00,0x00,

0x00,0x00,0x7C,0xC6,0xC6,0xCE,0xD6,0xD6,0xE6,0xC6,0xC6,0x7C,0x00,0x00,0x00,0x00,

0x00,0x00,0x18,0x38,0x78,0x18,0x18,0x18,0x18,0x18,0x18,0x7E,0x00,0x00,0x00,0x00,

0x00,0x00,0x7C,0xC6,0x06,0x0C,0x18,0x30,0x60,0xC0,0xC6,0xFE,0x00,0x00,0x00,0x00,

0x00,0x00,0x7C,0xC6,0x06,0x06,0x3C,0x06,0x06,0x06,0xC6,0x7C,0x00,0x00,0x00,0x00,

0x00,0x00,0x0C,0x1C,0x3C,0x6C,0xCC,0xFE,0x0C,0x0C,0x0C,0x1E,0x00,0x00,0x00,0x00,

0x00,0x00,0xFE,0xC0,0xC0,0xC0,0xFC,0x0E,0x06,0x06,0xC6,0x7C,0x00,0x00,0x00,0x00,

0x00,0x00,0x38,0x60,0xC0,0xC0,0xFC,0xC6,0xC6,0xC6,0xC6,0x7C,0x00,0x00,0x00,0x00,

0x00,0x00,0xFE,0xC6,0x06,0x06,0x0C,0x18,0x30,0x30,0x30,0x30,0x00,0x00,0x00,0x00,

0x00,0x00,0x7C,0xC6,0xC6,0xC6,0x7C,0xC6,0xC6,0xC6,0xC6,0x7C,0x00,0x00,0x00,0x00,

0x00,0x00,0x7C,0xC6,0xC6,0xC6,0x7E,0x06,0x06,0x06,0x0C,0x78,0x00,0x00,0x00,0x00,

0x00,0x00,0x00,0x00,0x18,0x18,0x00,0x00,0x00,0x18,0x18,0x00,0x00,0x00,0x00,0x00,

0x00,0x00,0x00,0x00,0x18,0x18,0x00,0x00,0x00,0x18,0x18,0x30,0x00,0x00,0x00,0x00,

0x00,0x00,0x00,0x06,0x0C,0x18,0x30,0x60,0x30,0x18,0x0C,0x06,0x00,0x00,0x00,0x00,

0x00,0x00,0x00,0x00,0x00,0x00,0xFE,0x00,0x00,0xFE,0x00,0x00,0x00,0x00,0x00,0x00,

0x00,0x00,0x00,0x60,0x30,0x18,0x0C,0x06,0x0C,0x18,0x30,0x60,0x00,0x00,0x00,0x00,

0x00,0x00,0x7C,0xC6,0xC6,0x0C,0x18,0x18,0x18,0x00,0x18,0x18,0x00,0x00,0x00,0x00,

0x00,0x00,0x00,0x7C,0xC6,0xC6,0xDE,0xDE,0xDE,0xDC,0xC0,0x7C,0x00,0x00,0x00,0x00,

0x00,0x00,0x10,0x38,0x6C,0xC6,0xC6,0xFE,0xC6,0xC6,0xC6,0xC6,0x00,0x00,0x00,0x00,

0x00,0x00,0xFC,0x66,0x66,0x66,0x7C,0x66,0x66,0x66,0x66,0xFC,0x00,0x00,0x00,0x00,

0x00,0x00,0x3C,0x66,0xC2,0xC0,0xC0,0xC0,0xC0,0xC2,0x66,0x3C,0x00,0x00,0x00,0x00,

0x00,0x00,0xF8,0x6C,0x66,0x66,0x66,0x66,0x66,0x66,0x6C,0xF8,0x00,0x00,0x00,0x00,

0x00,0x00,0xFE,0x66,0x62,0x68,0x78,0x68,0x60,0x62,0x66,0xFE,0x00,0x00,0x00,0x00,

0x00,0x00,0xFE,0x66,0x62,0x68,0x78,0x68,0x60,0x60,0x60,0xF0,0x00,0x00,0x00,0x00,

0x00,0x00,0x3C,0x66,0xC2,0xC0,0xC0,0xDE,0xC6,0xC6,0x66,0x3A,0x00,0x00,0x00,0x00,

0x00,0x00,0xC6,0xC6,0xC6,0xC6,0xFE,0xC6,0xC6,0xC6,0xC6,0xC6,0x00,0x00,0x00,0x00,

0x00,0x00,0x3C,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x3C,0x00,0x00,0x00,0x00,

0x00,0x00,0x1E,0x0C,0x0C,0x0C,0x0C,0x0C,0xCC,0xCC,0xCC,0x78,0x00,0x00,0x00,0x00,

0x00,0x00,0xE6,0x66,0x6C,0x6C,0x78,0x78,0x6C,0x66,0x66,0xE6,0x00,0x00,0x00,0x00,

0x00,0x00,0xF0,0x60,0x60,0x60,0x60,0x60,0x60,0x62,0x66,0xFE,0x00,0x00,0x00,0x00,

0x00,0x00,0xC6,0xEE,0xFE,0xFE,0xD6,0xC6,0xC6,0xC6,0xC6,0xC6,0x00,0x00,0x00,0x00,

0x00,0x00,0xC6,0xE6,0xF6,0xFE,0xDE,0xCE,0xC6,0xC6,0xC6,0xC6,0x00,0x00,0x00,0x00,

0x00,0x00,0x38,0x6C,0xC6,0xC6,0xC6,0xC6,0xC6,0xC6,0x6C,0x38,0x00,0x00,0x00,0x00,

0x00,0x00,0xFC,0x66,0x66,0x66,0x7C,0x60,0x60,0x60,0x60,0xF0,0x00,0x00,0x00,0x00,

0x00,0x00,0x7C,0xC6,0xC6,0xC6,0xC6,0xC6,0xC6,0xD6,0xDE,0x7C,0x0C,0x0E,0x00,0x00,

0x00,0x00,0xFC,0x66,0x66,0x66,0x7C,0x6C,0x66,0x66,0x66,0xE6,0x00,0x00,0x00,0x00,

0x00,0x00,0x7C,0xC6,0xC6,0x60,0x38,0x0C,0x06,0xC6,0xC6,0x7C,0x00,0x00,0x00,0x00,

0x00,0x00,0x7E,0x7E,0x5A,0x18,0x18,0x18,0x18,0x18,0x18,0x3C,0x00,0x00,0x00,0x00,

0x00,0x00,0xC6,0xC6,0xC6,0xC6,0xC6,0xC6,0xC6,0xC6,0xC6,0x7C,0x00,0x00,0x00,0x00,

0x00,0x00,0xC6,0xC6,0xC6,0xC6,0xC6,0xC6,0xC6,0x6C,0x38,0x10,0x00,0x00,0x00,0x00,

0x00,0x00,0xC6,0xC6,0xC6,0xC6,0xC6,0xD6,0xD6,0xFE,0x6C,0x6C,0x00,0x00,0x00,0x00,

0x00,0x00,0xC6,0xC6,0x6C,0x6C,0x38,0x38,0x6C,0x6C,0xC6,0xC6,0x00,0x00,0x00,0x00,

0x00,0x00,0x66,0x66,0x66,0x66,0x3C,0x18,0x18,0x18,0x18,0x3C,0x00,0x00,0x00,0x00,

0x00,0x00,0xFE,0xC6,0x86,0x0C,0x18,0x30,0x60,0xC2,0xC6,0xFE,0x00,0x00,0x00,0x00,

0x00,0x00,0x3C,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x3C,0x00,0x00,0x00,0x00,

0x00,0x00,0x00,0x80,0xC0,0xE0,0x70,0x38,0x1C,0x0E,0x06,0x02,0x00,0x00,0x00,0x00,

0x00,0x00,0x3C,0x0C,0x0C,0x0C,0x0C,0x0C,0x0C,0x0C,0x0C,0x3C,0x00,0x00,0x00,0x00,

0x10,0x38,0x6C,0xC6,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0x00,0x00,

0x30,0x30,0x18,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

0x00,0x00,0x00,0x00,0x00,0x78,0x0C,0x7C,0xCC,0xCC,0xCC,0x76,0x00,0x00,0x00,0x00,

0x00,0x00,0xE0,0x60,0x60,0x78,0x6C,0x66,0x66,0x66,0x66,0xDC,0x00,0x00,0x00,0x00,

0x00,0x00,0x00,0x00,0x00,0x7C,0xC6,0xC0,0xC0,0xC0,0xC6,0x7C,0x00,0x00,0x00,0x00,

0x00,0x00,0x1C,0x0C,0x0C,0x3C,0x6C,0xCC,0xCC,0xCC,0xCC,0x76,0x00,0x00,0x00,0x00,

0x00,0x00,0x00,0x00,0x00,0x7C,0xC6,0xFE,0xC0,0xC0,0xC6,0x7C,0x00,0x00,0x00,0x00,

0x00,0x00,0x38,0x6C,0x64,0x60,0xF0,0x60,0x60,0x60,0x60,0xF0,0x00,0x00,0x00,0x00,

0x00,0x00,0x00,0x00,0x00,0x76,0xCC,0xCC,0xCC,0xCC,0xCC,0x7C,0x0C,0xCC,0x78,0x00,

0x00,0x00,0xE0,0x60,0x60,0x6C,0x76,0x66,0x66,0x66,0x66,0xE6,0x00,0x00,0x00,0x00,

0x00,0x00,0x18,0x18,0x00,0x38,0x18,0x18,0x18,0x18,0x18,0x3C,0x00,0x00,0x00,0x00,

0x00,0x00,0x06,0x06,0x00,0x0E,0x06,0x06,0x06,0x06,0x06,0x06,0x66,0x66,0x3C,0x00,

0x00,0x00,0xE0,0x60,0x60,0x66,0x6C,0x78,0x78,0x6C,0x66,0xE6,0x00,0x00,0x00,0x00,

0x00,0x00,0x38,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x3C,0x00,0x00,0x00,0x00,

0x00,0x00,0x00,0x00,0x00,0xEC,0xFE,0xD6,0xD6,0xD6,0xD6,0xD6,0x00,0x00,0x00,0x00,

0x00,0x00,0x00,0x00,0x00,0xDC,0x66,0x66,0x66,0x66,0x66,0x66,0x00,0x00,0x00,0x00,

0x00,0x00,0x00,0x00,0x00,0x7C,0xC6,0xC6,0xC6,0xC6,0xC6,0x7C,0x00,0x00,0x00,0x00,

0x00,0x00,0x00,0x00,0x00,0xDC,0x66,0x66,0x66,0x66,0x66,0x7C,0x60,0x60,0xF0,0x00,

0x00,0x00,0x00,0x00,0x00,0x76,0xCC,0xCC,0xCC,0xCC,0xCC,0x7C,0x0C,0x0C,0x1E,0x00,

0x00,0x00,0x00,0x00,0x00,0xDC,0x76,0x62,0x60,0x60,0x60,0xF0,0x00,0x00,0x00,0x00,

0x00,0x00,0x00,0x00,0x00,0x7C,0xC6,0x60,0x38,0x0C,0xC6,0x7C,0x00,0x00,0x00,0x00,

0x00,0x00,0x10,0x30,0x30,0xFC,0x30,0x30,0x30,0x30,0x36,0x1C,0x00,0x00,0x00,0x00,

0x00,0x00,0x00,0x00,0x00,0xCC,0xCC,0xCC,0xCC,0xCC,0xCC,0x76,0x00,0x00,0x00,0x00,

0x00,0x00,0x00,0x00,0x00,0x66,0x66,0x66,0x66,0x66,0x3C,0x18,0x00,0x00,0x00,0x00,

0x00,0x00,0x00,0x00,0x00,0xC6,0xC6,0xC6,0xD6,0xD6,0xFE,0x6C,0x00,0x00,0x00,0x00,

0x00,0x00,0x00,0x00,0x00,0xC6,0x6C,0x38,0x38,0x38,0x6C,0xC6,0x00,0x00,0x00,0x00,

0x00,0x00,0x00,0x00,0x00,0xC6,0xC6,0xC6,0xC6,0xC6,0xC6,0x7E,0x06,0x0C,0xF8,0x00,

0x00,0x00,0x00,0x00,0x00,0xFE,0xCC,0x18,0x30,0x60,0xC6,0xFE,0x00,0x00,0x00,0x00,

0x00,0x00,0x0E,0x18,0x18,0x18,0x70,0x18,0x18,0x18,0x18,0x0E,0x00,0x00,0x00,0x00,

0x00,0x00,0x18,0x18,0x18,0x18,0x00,0x18,0x18,0x18,0x18,0x18,0x00,0x00,0x00,0x00,

0x00,0x00,0x70,0x18,0x18,0x18,0x0E,0x18,0x18,0x18,0x18,0x70,0x00,0x00,0x00,0x00,

0x00,0x00,0x76,0xDC,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

0x00,0x00,0x00,0x00,0x10,0x38,0x6C,0xC6,0xC6,0xC6,0xFE,0x00,0x00,0x00,0x00,0x00

};

#endif


关键字:单片机  贪吃蛇游戏 引用地址:基于单片机的贪吃蛇游戏设计

上一篇:单片机DA转换实现正弦波
下一篇:51地址总线(AB)

推荐阅读最新更新时间:2024-03-16 15:30

单片机系统在测控领域的稳定性探讨
近年来单片机系统在工业测控领域的应用越来越广泛,而对于环境恶劣的工业现场,这种新型的微控制器的可靠性,安全性就成为了一个大的问题。在实验室运行正常的程序到了工业现场却不能运行,或者出现运行不稳定经常死机等。在这里我们主要讨论一下如何通过软件技术来增强系统的稳定运行。 当系统的CPU部位受到干扰信号的作用时,将使系统失控。最典型的故障是破坏程序计数器PC的状态值。导致程序在地址空间内“乱飞”,或者陷入死循环。而我们对这种情况的处理主要有这么几种方法: 1、  指令冗余技术; 2、  软件陷阱技术; 3、  看门狗技术。 我们以MCS-51单片机来做以说明。 一、指令冗余技术 我们知道,指令由操作码和操作数
[单片机]
<font color='red'>单片机</font>系统在测控领域的稳定性探讨
单片机在智能报警系统设计方案
  该系统以凌阳公司的16位单片机SPCE061A作为主控制器,通过烟雾传感器与人体热释电红外传感器分别感应烟雾与人体红外信号,通过单片机做出报警动作。采用两片凌阳单片机作为从站,达到异地网络监控的目的。采用双光电开关的设计,感应人员的进出情况。采用射频信号发射与接收装置,感应贵重物品的进出。测试结果表明,系统工作良好,几天内的人员与贵重物品进出情况均有记录。该设计主要创新点是采用单片机进行控制,可以实现网络监控。   本系统设计主要是满足宿舍自动报警的需要,设计的目标是要实现语音报警、时间设定和显示、灯光报警并实现网络报警等功能。   1 设计方案   1.1 主控制器的选择   采用凌阳公司的16位单片机SPCE061
[单片机]
<font color='red'>单片机</font>在智能报警系统设计方案
从合泰2023新品发布会看创新的热情
合泰2023年度新品发布会11月9日于北京召开,作为公司疫情过后首次线下发布会,合泰带来了多项新品,涵盖32位MCU、智慧家居、绿能节电、健康测量、无线通信、安防及安全、倍创科技BMduino及模块,以触控MCU、USB-PD芯片、BLDC、安防感测等多项产品线切入真无线蓝牙耳机(TWS)、智慧照明、血糖/血氧测量、风扇马达、一氧化碳侦测等终端应用。 消费类MCU是合泰的主攻方向,应用于日常生活、居家相关小家电;生命财产安全相关:消防、烟雾报警器、一氧化碳报警器;健康相关:血压计、血糖仪、体脂秤、耳温枪、温度计;能源相关:电动车充电器、电机控制;主控MCU、触摸按键等领域。 本次发布会推出新品32-bit MCU HT32
[单片机]
从合泰2023新品发布会看创新的热情
PIC 单片机软件异步串行口实现技巧
在用单片机开发各种嵌入式应用系统时,异步串行通信是经常要用到的一种通信模式,很多应用中还要求实现多路异步串行通 信。大家平时熟悉的各种厂家的单片机,绝大部分片上只提供一个硬件UART模块,利用它可以方便实现一路串行通讯。PIC系列单片机也不例外,在其丰富的 产品家族成员中,除高端系列(PIC17/18)一些型号片上带有两路硬件UART模块外,其它大部分型号片上只有一路UART,一些低端廉价的PIC单 片机甚至还不带硬件 UART。为了提高系统的性能价格比,就要求设计工程师用软件增加实现一路或多路异步串行通信。很多工程师对用软件实现的UART在可靠性和效率方面持怀 疑态度,其实关键问题是看软件采用何种方式来实现可靠的UART功能。  
[单片机]
PIC <font color='red'>单片机</font>软件异步串行口实现技巧
51单片机DS18B20温度计制作 带仿真和源码
1、温度可通过调整DS18B20“+”“-”进行调整。 2、当温度超出范围,会有声光报警 3、因为在proteus中找不到三极管8550,所以拿了NPN管MSP8098代替,其原理是相同的,但程序稍做修改,位选本来用低电平,现在高电平位选。 4、如何装载程序?双击单片机—program file—找到相应的hex文件。 仿真原理图如下(proteus仿真工程文件可到本帖附件中下载) 单片机源程序如下: #include reg52.h //包含头文件,一般情况不需要改动,头文件包含特殊功能寄存器的定义 #include math.h #include INTRINS.H #define uchar unsigne
[单片机]
51<font color='red'>单片机</font>DS18B20温度计制作 带仿真和源码
新唐单片机keil驱动问题
1:新唐单片机keil编译工具,资料都可以在新唐官网下载。keil驱动软件要安装在keil安装的目录下,今天安装了很久,一直提示不成功,就是因为驱动Nu-link driver 没有安装到keil安装目录下。 2:刚开始项目选用M0518LC2AE单片机(36K FLASH),后来因程序存储区不够,又选用M0518LD2AE单片机(68K FLASH)。但keil编译时提示选用芯片的容量不够,keil重启无效,最后keil卸载后重装可以。
[单片机]
AVR单片机汇编器的部分伪指令详解
伪指令不属于单片机的指令系统,而是由汇编器提供的指令,用于调整存储器中程序的位置、定义宏、初始化存储器等。AVR单片机的汇编器共提供18条伪指令(见附表)。 其中,ORG、DB、DW、EQU读者比较熟悉,这里不再赘述。下面对部分伪指令加以说明。 BYTE-保存单字节数据到SRAM中。BYTE伪指令仅用在数据存储器。为提供数据保存的位置,在BYTE前应有标号。在由CSEG、ESEG定义的代码段和E2PROM段中不能使用BYTE伪指令。 格式LABEL:.BYTE表达式 CSEG-定义程序存储器代码段的起始位置一个汇编文件可以包括若干个代码段,汇编时这些代码段被连成一个代码段。在代码段中不能使用BYTE伪指令。 格式.CS
[单片机]
AVR<font color='red'>单片机</font>汇编器的部分伪指令详解
ZiLOG的8位MCU用于PC电视调谐卡,增强遥控功能
ZiLOG公司在推进8位集成微控制器和通用远程处理方案技术革新的同时,又宣布了与Hauppauge Computer Work公司的战略伙伴关系。Hauppauge公司是个人电脑数字视频、电视和信息广播接收机类产品的开发和生产商。在合作计划中ZiLOG的Z8 Encore! F08闪存微控制器系列将会使Hauppauge公司的WinTV-PVR-150型板卡先进的遥控功能得到进一步的增强。 WinTV-PVR是一款便于安装的个人电脑电视调谐卡,它使消费者可以通过他们的电脑观看、暂停或录制有线电视和卫星电视。消费者无需购买新的多媒体娱乐电脑就可以廉价快捷地在已有的电脑上收看电视或享受其它娱乐功能。 除了为Hauppauge公司提
[焦点新闻]
小广播
添点儿料...
无论热点新闻、行业分析、技术干货……
设计资源 培训 开发板 精华推荐

最新单片机文章
何立民专栏 单片机及嵌入式宝典

北京航空航天大学教授,20余年来致力于单片机与嵌入式系统推广工作。

更多每日新闻
电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved