|
本人在點陣屏上實現了貪吃蛇游戲,這篇文檔里包括了硬件連接圖、流程圖、全部的程序和效果圖。完全是自己做的,謝謝各位支持。當然還可以繼續完善程序功能。
1.003.jpg (48.63 KB, 下載次數: 139)
下載附件
2016-6-12 00:34 上傳
1.004.jpg (45.68 KB, 下載次數: 128)
下載附件
2016-6-12 00:34 上傳
1.005.jpg (49.23 KB, 下載次數: 117)
下載附件
2016-6-12 00:34 上傳
1.006.jpg (28.11 KB, 下載次數: 103)
下載附件
2016-6-12 00:34 上傳
| |
實驗內容: 此單片機項目實驗的主要功能是在LED點陣屏上實現貪吃蛇游戲。首先在LCD1602液晶屏顯示貪吃蛇游戲的注意事項(游戲開始,按鍵對蛇的控制K2上,K6下,K2+K6右);然后在LED點陣屏上展現出游戲主體,通過兩個按鍵控制蛇的移動;每當蛇吃掉一個食物得分加一,如果蛇撞到自己或者墻游戲結束,點陣屏上將出現“囧”,最后蜂鳴器響起音樂表示祝賀,數碼管顯示所得分數。 |
1.001.jpg (51.34 KB, 下載次數: 122)
下載附件
2016-6-12 00:34 上傳
二、流程圖
1.002.jpg (28.66 KB, 下載次數: 148)
下載附件
2016-6-12 00:34 上傳
三、主要程序 |
- /*************************************************
- 全局頭文件 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)
2016-6-11 21:29 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|
評分
-
查看全部評分
|