|
本人在点阵屏上实现了贪吃蛇游戏,这篇文档里包括了硬件连接图、流程图、全部的程序和效果图。完全是自己做的,谢谢各位支持。当然还可以继续完善程序功能。
| |
实验内容: 此单片机项目实验的主要功能是在LED点阵屏上实现贪吃蛇游戏。首先在LCD1602液晶屏显示贪吃蛇游戏的注意事项(游戏开始,按键对蛇的控制K2上,K6下,K2+K6右);然后在LED点阵屏上展现出游戏主体,通过两个按键控制蛇的移动;每当蛇吃掉一个食物得分加一,如果蛇撞到自己或者墙游戏结束,点阵屏上将出现“囧”,最后蜂鸣器响起音乐表示祝贺,数码管显示所得分数。 | |
- /*************************************************
- 全局头文件 config.h
- **************************************************/
-
- #ifndef _CONFIG_H
- #define _CONFIG_H
- //--包含你要使用的头文件--//
- #include
- #include
- #include
- //--定义重要关键词--//
- #ifndef uchar
- #define uchar unsigned char
- #endif
- #ifndef uint
- #define uint unsigned int
- #endif
- /--液晶屏IO口定义--//
- #define LCD1602_DATAPINS P0
- sbit LCD1602_E=P2^7;
- sbit LCD1602_RW=P2^5;
- sbit LCD1602_RS=P2^6;
- //--定义数码管要使用的IO口--//
- #define GPIO_DIG P1
- //--定义点阵屏要使用的 IO--//
- sbit MOSIO = P3^4;
- sbit R_CLK = P3^5;
- sbit S_CLK = P3^6;
- //--定义按键要使用的 IO--//
- sbit g2=P3^0; //k2
- sbit g1=P3^1; //k6
- //--定义蜂鸣器要使用的 IO--//
- sbit Beep = P3^7 ;
- extern int score; //得分
- #endif
- /********************************
- LED头文件 led.h
- ********************************/
- #ifndef _LED_H
- #define _LED_H
- extern uchar code x[]; //led列选
- extern uchar code y[]; //led行选
- extern uchar code tab13[]; //显示囧
- //显示倒计时//
- extern uchar code tab12[];
- extern uchar code tab11[];
- extern uchar code tab10[];
- extern uchar code tab9[];
- extern uchar code tab8[];
- extern uchar code tab0[]; //取模后选
- void st_led(void); //led初始化
- void HC595SendData( uchar BT3, uchar BT2,uchar BT1,uchar BT0); //led发送数据
- #endif
- /********************************
- snake头文件 snake.h
- ********************************/
- #ifndef _SNAKE_H
- #define _SNAKE_H
-
- #define MAX_LENGTH 33 //最长蛇长
-
- void createFood(); //创造食物
- void snakegame(); //游戏主体(此时游戏开始)
- void expandSnake(); //延长蛇身
- void moveSnake(); // 移动蛇
- void gameOver(); //游戏结束
- uchar touchSelf(); //撞到自己
- uchar touchWall(); //撞到墙
- int oppositeDirection(int t); //方向选取
- int foodEat(); // 吃食
- void init(); //蛇的初始化
- void draw(); //显示蛇长
- #endif
- /********************************
- LCD头文件 lcd.h
- ********************************/
- #ifndef __LCD_H_
- #define __LCD_H_
- #define LCD1602_4PINS
- //—定义重要关键词—//
- #ifndef uchar
- #define uchar unsigned char
- #endif
- #ifndef uint
- #define uint unsigned int
- #endif
- //—函数声明—//
- void Lcd1602_Delay1ms(uint c); //在51单片机12MHZ时钟下的延时函数
- void LcdWriteCom(uchar com); //LCD1602写入8位命令子函数
- void LcdWriteData(uchar dat); //LCD1602写入8位数据子函数
- void LcdInit();//LCD1602写入8位数据子函数
- void lcd_show(void);//液晶屏显示注意事项
- void Delay10ms(unsigned int c); //延时
- #endif
- /********************************
- 数码管头文件 tube.h
- ********************************/
- #ifndef __TUBE_H_
- #define __TUBE_H_
- //—定义全局变量—//
- //RAM,ROM
- unsigned char code DIG_CODE[16]={0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07,
- 0x7F, 0x6F, 0x77, 0x7C, 0x39, 0x5E, 0x79, 0x71};
- #endif
- /********************************
- 主函数 main.c
- ********************************/
- //—包含你要使用的头文件—//
- #include "config.h"
- #include "snake.h"
- #include "led.h"
- void main()
- {
- TMOD=0x01; //使用模式1,16位定时器,使用"|"符号可以在使用多个定时器时不受影响
- TH1=(65535-10000)/255; //给定初值,这里使用定时器最大值从0开始计数一直到65535溢出
- TL1=(65535-10000)%255;
- ET1=1; //定时器中断打开
- TR1=1; //定时器开关打开
- EA=1; //总中断打开
- init(); //蛇初始化
- lcd_show(); //液晶屏显示(K2 上,K6 下,K2+K6右)
- st_led(); //点阵屏显示游戏
- while(1)
- {
- snakegame(); //游戏主体(游戏开始 )
- }
- gameOver(); 游戏结束
- }
- /********************************
- Snake部分函数 snake.c
- ********************************/
- #define _SNAKE_C
- #include "config.h"
- #include "snake.h"
- #include "led.h"
- #include "tube.h"
-
- int lastx,lasty;
- int score=0; //分数初始化
- uint t=0; //方向选量
- uint left=0,top=0,right=15,bottom=15,jfood=0;
-
- struct Point
- {
- uchar x, y;
- };
- struct Snake
- {
- struct Point nodes[MAX_LENGTH];
- uchar length;
- uchar direction;
- } snake;
- struct Food
- {
- struct Point position;
- uchar exist;
- } food;
-
- void init() //蛇初始化
- {
- snake.nodes[0].x=15;
- snake.nodes[0].y=3;
- snake.nodes[1].x=14;
- snake.nodes[1].y=3;
- snake.length=2;
- snake.direction=2;
- food.exist=0;
- }
- void snakegame() //游戏主体
- {
- while(1)
- {
- if (touchWall() || touchSelf()) //撞墙或自己游戏结束
- gameOver();
- if (!food.exist) //是否有食物
- createFood(); //创造食物
- food.exist=1;
- draw();
- lastx=snake.nodes[snake.length-1].x;
- lasty=snake.nodes[snake.length-1].y;
- if (!oppositeDirection(t))
- {
- snake.direction =t;
- }
- moveSnake(); //移动蛇
- if (foodEat()) //是否吃食物
- {
- food.exist = 0;
- expandSnake(); 蛇身扩展
- }
- }
- }
- int foodEat() //吃食
- {
- if(snake.nodes[0].x==food.position.x&&snake.nodes[0].y==food.position.y)
- return 1;
- else
- return 0;
- }
- int oppositeDirection(int t) //方向选量
- {
- if(t==0&&snake.direction==2)
- {
- return 1;
- }
- else if(t==2&&snake.direction==0)
- {
- return 1;
- }
- else if(t==1&&snake.direction==3)
- {
- return 1;
- }
- else if(t==3&&snake.direction==1)
- {
- return 1;
- }
- else
- return 0;
- }
-
- void moveSnake() //移动蛇
- {
- int k;
- lastx=snake.nodes[snake.length-1].x;
- lasty=snake.nodes[snake.length-1].y;
- for(k=snake.length-1;k>0;k--)
- {
- snake.nodes[k].x=snake.nodes[k-1].x;
- snake.nodes[k].y=snake.nodes[k-1].y;
- }
- if(snake.direction==2)
- {
- snake.nodes[0].y-=1;
- }
- else if(snake.direction==0)
- {
- snake.nodes[0].y+=1;
- }
- else if(snake.direction==3)
- {
- snake.nodes[0].x-=1;
- }
- else if(snake.direction==1)
- {
- snake.nodes[0].x+=1;
- }
- else;
- }
- void T0_1() interrupt 3 using 2
- {
- TH1=(65535-10000)/255;
- TL1=(65535-10000)%255;
- if(g1==0&&g2==0) {t=1;}//left
- if(g1==1&&g2==0) {t=0;}//up
- if(g1==0&&g2==1) {t=2;}//down
- if(g1==1&&g2==1) {t=3;}//right
- }
- uchar touchSelf() //撞自己
- {
- uchar i;
- for (i=3;i<snake.length-1;i++)
- {
- if(snake.nodes[0].x==snake.nodes[i].x&&snake.nodes[0].y==snake.nodes[i].y)
- return 1;
- }
- return 0;
- }
- uchar touchWall() //撞墙
- {
- uchar x1=snake.nodes[0].x;
- uchar y1=snake.nodes[0].y;
- if(x1right||y1bottom)
- return 1;
- else
- return 0;
- }
- void gameOver() //游戏结束
- {
- int k,n;
- while(1)
- {
- for(n = 0; n < 50; n++)
- {
- for(k = 0; k < 16; k++)
- {
- HC595SendData(~tab13[2*k +1],~tab13[2*k],tab0[2*k],tab0[2*k + 1]);
- }
- }
- GPIO_DIG = ~DIG_CODE[score]; //数码管积分
- beep_show(); //蜂鸣器响
- }
- }
- void expandSnake() //蛇身扩展
- {
- snake.nodes[snake.length].x=lastx;
- snake.nodes[snake.length].y=lasty;
- snake.length++;
- score++;
- }
- void createFood() //创造食物
- {
- int i;
- label:
- jfood+=3;
- if(jfood>=500) jfood=0;
- food.position.x=((int)rand()%16);
- food.position.y=((int)rand()%16);
- for(i=0;i<=snake.length-1;i++)
- {
- if(snake.nodes[i].x==food.position.x&&snake.nodes[i].y==food.position.y)
- goto label;
- }
- }
- void draw() //显示蛇长
- {
- uint j,i,m,n;
- for(m=0;m<16;m++)
- {
- for(i=0;i<snake.length;i++)
- {
- for(j=0;j<snake.length;j++)
- if(m==i||m==j)
- HC595SendData( y[2*(snake.nodes[j].x)-2],y[2*(snake.nodes[j].x)-1],
- x[2*(snake.nodes[i].y)-2],x[2*(snake.nodes[i].y)-1]);
- }
- for(j=0;j<16;j++)
- {
- if(food.position.x==j||food.position.y==j)
- HC595SendData( y[2*(food.position.x)-2],y[2*(food.position.x)-1],
- x[2*(food.position.y)-2],x[2*(food.position.y)-1]);
- }
- for(n=25;n>0;n--)
- {
- HC595SendData(0xff,0xff,0,0);
- }
- }
- }
- /********************************
- LED部分函数 led.c
- ********************************/
- #define _LED_C
- #include "config.h"
- #include "led.h"
- #include "snake.h"
- //点阵显示数组
- uchar code y[]={ //选列
- 0xff,0xfe, 0xff,0xfd,0xff,0xfb,0xff,0xf7, 0xff,0xef,0xff,0xdf,0xff,0xbf,0xff,0x7f,
- 0xfe,0xff, 0xfd,0xff,0xfb,0xff,0xf7,0xff,0xef,0xff,0xdf,0xff,0xbf,0xff,0x7f,0xff
- };
- uchar code x[]={ //选行
- 0x00,0x01, 0x00,0x02, 0x00,0x04,0x00,0x08, 0x00,0x10,0x00,0x20,0x00,0x40, 0x00,0x80,
- 0x01,0x00, 0x02,0x00, 0x04,0x00 , 0x08,0x00, 0x10,0x00, 0x20,0x00,0x40,0x00, 0x80,0x00,
- };
- uchar code tab0[] = //取模后选
- {0x00, 0x01, 0x00, 0x02, 0x00, 0x04, 0x00, 0x08,0x00, 0x10, 0x00, 0x20, 0x00, 0x40, 0x00, 0x80,
- 0x01, 0x00, 0x02, 0x00, 0x04, 0x00, 0x08, 0x00,0x10, 0x00, 0x20, 0x00, 0x40, 0x00, 0x80, 0x00};
- uchar code tab8[] =
- {0,0,0,0,0,0,24,60,36,66,66,66,66,32,66,24,66,32,66,64,66,64,66,66,36,34,24,28,0,0,0,0};
- uchar code tab9[] =
- {0,0,0,0,0,0,24,60,36,66,66,66,66,66,66,32,66,32,66,16,66,8,66,4,36,66,24,126,0,0,0,0};
- uchar code tab10[] =
- {0,0,0,0,0,0,24,8,36,14,66,8,66,8,66,8,66,8,66,8,66,8,66,8,36,8,24,62,0,0,0,0};
- uchar code tab11[] =
- {0,0,0,0,0,0,24,24,36,36,66,66,66,66,66,66,66,66,66,66,66,66,66,66,36,36,24,24,0,0,0,0};
- uchar code tab12[] = //显示倒计时
- {0,0,0,0,0,0,60,28,34,34,34,65,1,65,1,65,1,65,113,65,33,65,34,65,34,34,28,28,0,0,0,0};
- uchar code tab13[] = //显示“囧”
- {0,0,0,0,254,63,6,48,102,51,54,54,62,60,30,60,6,48,246,55,54,54,54,54,54,54,54,54,254,63,0,0};
- void HC595SendData( uchar BT3, uchar BT2,uchar BT1,uchar BT0) // 595锁存器发送数据
- {
- uchar i;
- //发送第一个字节
- for(i=0;i<8;i++)
- {
- MOSIO = BT3 >> 7 ; //从高位到低位
- BT3 <<= 1;
-
- S_CLK = 0;
- S_CLK = 1;
- }
- //发送第一个字节
- for(i=0;i<8;i++)
- {
- MOSIO = BT2 >>7; //从高位到低位
- BT2 <<= 1;
-
- S_CLK = 0;
- S_CLK = 1;
- }
- //发送第一个字节
- for(i=0;i<8;i++)
- {
- MOSIO = BT1 >> 7; //从高位到低位
- BT1 <<= 1;
-
- S_CLK = 0;
- S_CLK = 1;
- }
- //发送第一个字节
- for(i=0;i<8;i++)
- {
- MOSIO = BT0 >> 7; //从高位到低位
-
- BT0 <<= 1;
- S_CLK = 0;
- S_CLK = 1;
- }
- //输出
- R_CLK = 0; //set dataline low
- R_CLK = 1; //片选
- R_CLK = 0; //set dataline low
- }
- void st_led(void) //点阵屏显示游戏
- {
- int k, i, ms;
- i = 60; //显示时间
- {
- HC595SendData(0xff,0xff,0,0);
- for(ms = i; ms > 0; ms--)
- {
- for(k = 0; k < 16; k++)
- {
- HC595SendData(~tab8[2*k +1],~tab8[2*k],tab0[2*k],tab0[2*k + 1]);
- }
- }
- HC595SendData(0xff,0xff,0,0); //清屏
- for(ms = i; ms > 0; ms--)
- {
- for(k = 0; k < 16; k++)
- {
- HC595SendData(~tab9[2*k +1],~tab9[2*k],tab0[2*k],tab0[2*k + 1]);
- }
- }
- HC595SendData(0xff,0xff,0,0); //清屏
- for(ms = i; ms > 0; ms--)
- {
- for(k = 0; k < 16; k++)
- {
- HC595SendData(~tab10[2*k +1],~tab10[2*k],tab0[2*k],tab0[2*k + 1]);
- }
- }
- HC595SendData(0xff,0xff,0,0); //清屏
- for(ms = i; ms > 0; ms--)
- {
- for(k = 0; k < 16; k++)
- {
- HC595SendData(~tab11[2*k +1],~tab11[2*k],tab0[2*k],tab0[2*k + 1]);
- }
- }
- HC595SendData(0xff,0xff,0,0); //清屏
- for(ms = 120; ms > 0; ms--)
- {
- for(k = 0; k < 16; k++)
- {
- HC595SendData(~tab12[2*k +1],~tab12[2*k],tab0[2*k],tab0[2*k + 1]);
- }
- }
- }
- }
- /********************************
- LCD部分函数 lcd.c
- ********************************/
- #define _LCD_C
- #include"lcd.h"
- #include "config.h"
- /*****************************************************************
- * 函 数 名 : Lcd1602_Delay1ms
- * 函数功能 : 延时函数,延时1ms
- * 输 入 : c
- ****************************************************************/
- void Lcd1602_Delay1ms(uint c) //误差 0us
- {
- uchar a,b;
- for (; c>0; c--)
- {
- for (b=199;b>0;b--)
- {
- for(a=1;a>0;a--);
- }
- }
- }
- /****************************************************************
- * 函 数 名 : LcdWriteCom
- * 函数功能 : 向LCD写入一个字节的命令
-
- *****************************************************************/
- #ifndef LCD1602_4PINS //当没有定义这个LCD1602_4PINS时
- void LcdWriteCom(uchar com) //写入命令
- {
- LCD1602_E = 0; //使能清零
- LCD1602_RS = 0; //选择发送命令
- LCD1602_RW = 0; //选择写入
-
- LCD1602_DATAPINS = com; //放入命令
- Lcd1602_Delay1ms(1); //等待数据稳定
-
- LCD1602_E = 1; //写入时序
- Lcd1602_Delay1ms(5); //保持时间
- LCD1602_E = 0;
- }
- #else
- void LcdWriteCom(uchar com) //写入命令
- {
- LCD1602_E = 0; //使能清零
- LCD1602_RS = 0; //选择写入命令
- LCD1602_RW = 0; //选择写入
-
- LCD1602_DATAPINS = com; //由于4位的接线是接到P0口的高四位,所以传送高四位不用改
- Lcd1602_Delay1ms(1);
-
- LCD1602_E = 1; //写入时序
- Lcd1602_Delay1ms(5);
- LCD1602_E = 0;
-
- LCD1602_DATAPINS = com << 4; //写入低四位
- Lcd1602_Delay1ms(1);
-
- LCD1602_E = 1; //写入时序
- Lcd1602_Delay1ms(5);
- LCD1602_E = 0;
- }
- #endif
- /****************************************************************
- * 函 数 名 : LcdWriteData
- * 函数功能 : 向LCD写入一个字节的数据
- * 输 入 : dat
- *****************************************************************/
- #ifndef LCD1602_4PINS
- void LcdWriteData(uchar dat) //写入数据
- {
- LCD1602_E = 0; //使能清零
- LCD1602_RS = 1; //选择输入数据
- LCD1602_RW = 0; //选择写入
-
- LCD1602_DATAPINS = dat; //写入数据
- Lcd1602_Delay1ms(1);
-
- LCD1602_E = 1; //写入时序
- Lcd1602_Delay1ms(5); //保持时间
- LCD1602_E = 0;
- }
- #else
- void LcdWriteData(uchar dat) //写入数据
- {
- LCD1602_E = 0; //使能清零
- LCD1602_RS = 1; //选择写入数据
- LCD1602_RW = 0; //选择写入
-
- LCD1602_DATAPINS = dat;//由于4位的接线是接到P0口的高四位,所以传送高四位不用改
- Lcd1602_Delay1ms(1);
-
- LCD1602_E = 1; //写入时序
- Lcd1602_Delay1ms(5);
- LCD1602_E = 0;
-
- LCD1602_DATAPINS = dat << 4; //写入低四位
- Lcd1602_Delay1ms(1);
-
- LCD1602_E = 1; //写入时序
- Lcd1602_Delay1ms(5);
- LCD1602_E = 0;
- }
- #endif
- /*********************************************************************
- * 函 数 名 : LcdInit()
- * 函数功能 : 初始化LCD屏
- ********************************************************************/
- #ifndef LCD1602_4PINS
- void LcdInit() //LCD初始化子程序
- {
- LcdWriteCom(0x38); //开显示
- LcdWriteCom(0x0c); //开显示不显示光标
- LcdWriteCom(0x06); //写一个指针加1
- LcdWriteCom(0x01); //清屏
- LcdWriteCom(0x80); //设置数据指针起点
- }
- #else
- void LcdInit() //LCD初始化子程序
- {
- LcdWriteCom(0x32); //将8位总线转为4位总线
- LcdWriteCom(0x28); //在四位线下的初始化
- LcdWriteCom(0x0c); //开显示不显示光标
- LcdWriteCom(0x06); //写一个指针加1
- LcdWriteCom(0x01); //清屏
- LcdWriteCom(0x80); //设置数据指针起点
- }
- #endif
- /********************************
- LCD部分函数 lcd_show.c
- ********************************/
- #define _LCD_C
- #include"lcd.h"
- #include "config.h"
-
- unsigned char PuZh[30] = " snake game snake game ";
- unsigned char CnCh[30] = " k2 up k6 down k2+k6 right";
-
- void Delay10ms(unsigned int c) //延时
- {
- unsigned char a, b;
- for (;c>0;c--)
- {
- for (b=38;b>0;b--)
- {
- for (a=130;a>0;a--);
- }
- }
- }
- void lcd_show(void)
- {
- unsigned char i;
- LcdInit(); //初始化
- //--写第一行--//
- for(i=0; i<30; i++)
- {
- LcdWriteData(PuZh[i]);
- }
- LcdWriteCom(0xC0);
- for(i=0; i<30; i++)
- {
- LcdWriteData(CnCh[i]);
- }
- LcdWriteCom(0x07);
- {
- LcdWriteCom(0xC0);
- for(i=0; i<30; i++)
- {
- LcdWriteData(CnCh[i]);
- Delay10ms(75);
- }
- }
- }
- /********************************
- beep部分函数 beep.c
- ********************************/
- #include "config.h"
- unsigned char n=0; //n为节拍常数变量
- unsigned char code music_tab[] ={
- 0x18, 0x30, 0x1C , 0x10, //格式为: 频率常数, 节拍常数, 频率常数, 节拍常数,
- 0x20, 0x40, 0x1C , 0x10,
- 0x18, 0x10, 0x20 , 0x10,
- 0x1C, 0x10, 0x18 , 0x40,
- 0x1C, 0x20, 0x20 , 0x20,
- 0x1C, 0x20, 0x18 , 0x20,
- 0x20, 0x80, 0xFF , 0x20,
- 0x30, 0x1C, 0x10 , 0x18,
- 0x20, 0x15, 0x20 , 0x1C,
- 0x20, 0x20, 0x20 , 0x26,
- 0x40, 0x20, 0x20 , 0x2B,
- 0x20, 0x26, 0x20 , 0x20,
- 0x20, 0x30, 0x80 , 0xFF,
- 0x20, 0x20, 0x1C , 0x10,
- 0x18, 0x10, 0x20 , 0x20,
- 0x26, 0x20, 0x2B , 0x20,
- 0x30, 0x20, 0x2B , 0x40,
- 0x20, 0x20, 0x1C , 0x10,
- 0x18, 0x10, 0x20 , 0x20,
- 0x26, 0x20, 0x2B , 0x20,
- 0x30, 0x20, 0x2B , 0x40,
- 0x20, 0x30, 0x1C , 0x10,
- 0x18, 0x20, 0x15 , 0x20,
- 0x1C, 0x20, 0x20 , 0x20,
- 0x26, 0x40, 0x20 , 0x20,
- 0x2B, 0x20, 0x26 , 0x20,
- 0x20, 0x20, 0x30 , 0x80,
- 0x20, 0x30, 0x1C , 0x10,
- 0x20, 0x10, 0x1C , 0x10,
- 0x20, 0x20, 0x26 , 0x20,
- 0x2B, 0x20, 0x30 , 0x20,
- 0x2B, 0x40, 0x20 , 0x15,
- 0x1F, 0x05, 0x20 , 0x10,
- 0x1C, 0x10, 0x20 , 0x20,
- 0x26, 0x20, 0x2B , 0x20,
- 0x30, 0x20, 0x2B , 0x40,
- 0x20, 0x30, 0x1C , 0x10,
- 0x18, 0x20, 0x15 , 0x20,
- 0x1C, 0x20, 0x20 , 0x20,
- 0x26, 0x40, 0x20 , 0x20,
- 0x2B, 0x20, 0x26 , 0x20,
- 0x20, 0x20, 0x30 , 0x30,
- 0x20, 0x30, 0x1C , 0x10,
- 0x18, 0x40, 0x1C , 0x20,
- 0x20, 0x20, 0x26 , 0x40,
- 0x13, 0x60, 0x18 , 0x20,
- 0x15, 0x40, 0x13 , 0x40,
- 0x18, 0x80, 0x00
- };
- void int0() interrupt 1 using 1 //采用中断控制节拍
- { TH0=0xd8;
- TL0=0xef;
- n--;
- }
- void delay (unsigned char m) //控制频率延时
- {
- unsigned i=3*m;
- while(--i);
- }
- void delayms(unsigned char a) //毫秒延时子程序
- {
- while(--a); //采用while(--a) 不要采用while(a--)
- }
-
- void beep_show()
- { unsigned char p,m; //m为频率常数变量
- unsigned char i=0;
- TMOD&=0x0f;
- TMOD|=0x01;
- TH0=0xd8;TL0=0xef;
- IE=0x82;
- play:
- while(1)
- {
- a: p=music_tab[i];
- if(p==0x00) { i=0, delayms(1000); goto play; //如果碰到结束符,延时1秒,回到开始再来一遍
- else if(p==0xff) { i=i+1;delayms(100),TR0=0; goto a;} //若碰到休止符,延时100ms,继续取下一音符
- else {m=music_tab[i++], n=music_tab[i++];} //取频率常数 和 节拍常数
- TR0=1; //开定时器
- while(n!=0) Beep=~Beep,delay(m); //等待节拍完成, 通过输出音频
- TR0=0; //关定时器
- }
- }
复制代码
本制作的完整文档下载:
点阵屏实现贪吃蛇游戏.docx
(3.06 MB, 下载次数: 120)
|
评分
-
查看全部评分
|