⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 v02.c

📁 51下的 贪吃蛇 程序 用128864的LCD keil的开发环境
💻 C
字号:
//demo board system
//designed 2002/12/25
//modify   ---------
//version	V1.00	
#include <reg51.h>
typedef unsigned char byte;
typedef unsigned int word;
#define XTALL           11.0592					//crystal frequency
#define DLY_10MS        (word)(10000*XTALL/12)  // 10ms counter        
#define DLY_400US       (word)(400*XTALL/12)    // 400us counter     
struct position{
	byte x;
	byte y;
	};   
#define   xram       ( byte xdata *)(0x0000)
sbit      power 	  =		0x94;	//p1.4
sbit	  lcd_power   =		0x95;   //p1.5
sbit	  DI		  =     0x92;   //p1.2
sbit	  RW		  =	    0x91;   //p1.1
sbit      E			  =		0x90;   //p1.0
sbit	  LCD1		  =		0x93;   //p1.3
sbit	  LCD2        =	    0x96;   //p1.6
sbit	  RST		  =     0xB0;   //p3.0
sbit	  latch		  =		0x97;   //p1.7

bit		game_on,move_flag;
struct  position xdata point[1000] _at_ 0x1200;
byte 	sp_start,sp_end,sp,orient;
byte code chinese[500] ={    //lay model catch mod  resceive
/*--  文字:  枫  --*/  
/*--  宋体12;  此字体下对应的点阵为:宽x高=16x16   --*/
0x08,0xC8,0xFF,0x28,0xC8,0x00,0xFE,0x22,0x42,0x82,0x72,0x02,0xFE,0x00,0x00,0x00,
0x02,0x01,0xFF,0x00,0x20,0x18,0x07,0x04,0x02,0x01,0x02,0x04,0x0F,0x30,0x7C,0x00,

/*--  文字:  叶  --*/
/*--  宋体12;  此字体下对应的点阵为:宽x高=16x16   --*/
0xFC,0x04,0x04,0x04,0xFC,0x40,0x40,0x40,0x40,0xFF,0x40,0x40,0x40,0x40,0x40,0x00,
0x0F,0x02,0x02,0x02,0x07,0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,

/*--  文字:  电  --*/
/*--  宋体12;  此字体下对应的点阵为:宽x高=16x16   --*/
0x00,0x00,0xF8,0x48,0x48,0x48,0x48,0xFF,0x48,0x48,0x48,0x48,0xF8,0x00,0x00,0x00,
0x00,0x00,0x0F,0x04,0x04,0x04,0x04,0x3F,0x44,0x44,0x44,0x44,0x4F,0x40,0x70,0x00,

/*--  文字:  子  --*/
/*--  宋体12;  此字体下对应的点阵为:宽x高=16x16   --*/
0x00,0x00,0x02,0x02,0x02,0x02,0x02,0xE2,0x12,0x0A,0x06,0x02,0x00,0x80,0x00,0x00,
0x01,0x01,0x01,0x01,0x01,0x41,0x81,0x7F,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x00,
};  
//------------------------------10ms timer counter-----------------------------
void TIM0_isr() interrupt 1 using 1 {
	static idata word timercount;
   TR0=0;
   TL0+=-DLY_10MS&0xff;
   TH0+=-DLY_10MS>>8;
   TR0=1;
   if(timercount++ >10){
   		timercount=0;
		move_flag=1;
   }
}
//---------------------------reset the lcd controller chip--------------------- 
void reset_lcd(void){
	word i;
	RST=0;
	for(i=0;i<1000;i++)
	;
	RST=1;
	}
 //-------------------------write a command to left part of lcd ---------------
void write_l_com(byte com){
	byte i;
	LCD2=0;
	LCD1=1;
	DI=0;
	RW=1;
	RW=1;
	do{
		P2=0xff;
		E=1;
		i=P2;
		E=0;
	}
	while(i&0x80);
	RW=0;
	DI=0;
	P2=com;
	E=1;
	E=1;
	E=0;
	}
//----------------------write a data to left part of lcd-----------------------
void write_l_dat(byte dat){
	byte i=0x80;
	LCD2=0;
	LCD1=1;
	DI=0;
	RW=1;
	RW=1;
	do{
		P2=0xff;
		E=1;
		i=P2;
		E=0;
	}
	while((i&0x80));
	RW=0;
	DI=1;
	P2=dat;
	E=1;
	E=1;
	E=0;
	}
//--------------------------write a command to right part of lcd --------------
void write_r_com(byte com){
	byte i;
	LCD2=1;
	LCD1=0;
	DI=0;
	RW=1;
	RW=1;
	do{
		P2=0xff;
		E=1;
		i=P2;
		E=0;
	}
	while(i&0x80);
	RW=0;
	DI=0;
	P2=com;
	E=1;
	E=1;
	E=0;
	}
//------------------------write a data to right part of lcd--------------------
void write_r_dat(byte dat){
	byte i=0x80;
	LCD2=1;
	LCD1=0;
	DI=0;
	RW=1;
	RW=1;
	do{
		P2=0xff;
		E=1;
		i=P2;
		E=0;
	}
	while((i&0x80));
	RW=0;
	DI=1;
	P2=dat;
	E=1;
	E=1;
	E=0;
	}
//----------------------initialize lcd-----------------------------------------
 void init_lcd(){
 	byte i,j,page;
 	reset_lcd();
	write_l_com(0x3f);
	write_l_com(0xc0);
	write_r_com(0x3f);
	write_r_com(0xc0);
	page=0;
	for(i=0;i<8;i++){
		page=i|0xb8;
		write_l_com(page);
		write_l_com(0x40);
		write_r_com(page);
		write_r_com(0x40);
		for(j=0;j<64;j++){
			write_l_dat(0x00);
			write_r_dat(0x00);

		}
	}
 }
 //---------------------display the data from the xdata buffer-----------------
 void display(){  //the display buffer is from 0x0000 to 0x0400 total 1024 byte
 	byte i,j,page=0;
	for(i=0;i<8;i++){
		page=i|0xb8;
		write_l_com(page);			//write the pageof lcd display buffer memory
		write_l_com(40);			//set the start pixel
		for(j=0;j<64;j++){
			write_l_dat(*(xram+(128*i)+j));
		}
	}  //write the data to left part of lcd

	for(i=0;i<8;i++){
		page=i|0xb8;
		write_r_com(page);			//write the pageof lcd display buffer memory
		write_r_com(40);			//set the start pixel
		for(j=0;j<64;j++){
			write_r_dat(*(xram+(128*i)+64+j));
		}
	}  //write the data to right part of lcd
}
//-------------------------write a point at the lcd----------------------------
byte write_point(byte x,byte y){
	byte  idata temp,buffer;
	if(x>128)return 0x00;
	if(y>64) return 0x01;
	temp=*(xram+(y/8)*128+x);
	buffer=0x01;
	buffer<<=(y%8);
	temp=temp|buffer;
	*(xram+(y/8)*128+x)=temp;	
	}
//-------------------------write square at the lcd-----------------------------
void write_square(byte x0,y0,x1,y1,d){
	byte i;
	for(i=0;i<(x1-x0);i++){
		write_point((x0+i),y0);
		write_point((x0+i+1),y1);
	}
	for(i=0;i<(y1-y0);i++){
		write_point(x0,(y0+i+1));
		write_point(x1,(y0+i));
	}
	if(d){
		for(i=0;i<(x1-x0);i++){
			write_point((x0+i),y0+1);
			write_point((x0+i+1),y1-1);
		}
		for(i=0;i<(y1-y0);i++){
			write_point(x0+1,(y0+i+1));
			write_point(x1-1,(y0+i));
		}
	}
 }
 //--------------------put a char at lcd --------------------------------------
void put_char(byte x,y,att,chara){		//att:  0-> 16x16; 1->8x16; 2->24x24
	byte i;
	word scope;
	switch (att){
    case 0: 
			for(i=0;i<16;i++){
				*(xram+(y/8)*128+x+i)=chinese[chara*32+i];
				scope=&(xram+(y/8)*128+x+i);
				*(xram+(y/8)*128+x+128+i)=chinese[chara*32+16+i];
				scope=&(xram+(y/8)*128+x+128+i);
			}
			break;
	case 1:
			for(i=0;i<16;i++){
				*(xram+(y/8)*128+x+i)=chinese[chara*32+i];
				scope=&(xram+(y/8)*128+x+i);
				*(xram+(y/8)*128+x+128+i)=chinese[chara*32+16+i];
				scope=&(xram+(y/8)*128+x+128+i);
			}
			break;
	case 2:
			for(i=0;i<8;i++){
				*(xram+(y/8)*128+x)=chinese[chara*32+i];
				*(xram+(y/8)*128+x)=chinese[chara*32+i];
			}
			break;

	default:break;
   }
}
//----------------------write snake body---------------------------------------
void write_body(void){
	word i;
	byte j,temp,start;
	for(i=0;i<1;i++){
		if(point[sp_end-i].x==point[sp_end-i-1].x){	//write snake bode from snake head
			if(point[sp_end-i].y > point[sp_end-i-1].y){
				start=point[sp_end-i-1].y;
				temp=point[sp_end-i].y-point[sp_end-i-1].y;
			}
			else{
				start=point[sp_end-i].y;
				temp=point[sp_end-i-1].y-point[sp_end-i].y;
			}
			for(j=0;j<temp;j++){
				write_point(point[sp_end-i].x,start+j);
				write_point(point[sp_end-i].x+1,start+j);
			}
		}
		else{			
			if(point[sp_end-i].x > point[sp_end-i-1].x){
				start=point[sp_end-i-1].x;
				temp=point[sp_end-i].x-point[sp_end-i-1].x;
			}
			else{
				start=point[sp_end-i].x;
				temp=point[sp_end-i-1].x-point[sp_end-i].x;
			}
			for(j=0;j<temp;j++){
				write_point(start+j,point[sp_end-i].y);
				write_point(start+j,point[sp_end-i].y+1);
			}

		
		}
	}
}
//----------------------the game model-----------------------------------------
game(){								//i is y length;don,t bigger than 15
	byte temp;
	game_on=1;
	orient=0;
		sp_start=sp_end=sp=0;
		point[0].x=70;  
    	point[0].y=38;	// snake  tail
		point[1].x=60;
		point[1].y=38;  //snake	head			
		sp_end=1;			
		sp=1;			//initialize snake body
		write_body();
		display();
		while(game_on){
			if(move_flag){
				move_flag=0;
				switch (orient){
					case 0:			//the snake is walk front
						temp=point[sp_end].x;
						temp++;
						point[sp_end].x=temp;
						if(point[start-1].x==point[start].x){
							if(point[start].y > point[start+1].y){
								temp=point[start].y;
								temp++;
								point[start].y=temp;
							}
							else{
								temp=point[start].y;
								temp--;
								point[start].y=temp;
							}
						}
						else{
							if(point[start.x] > point[start+1].x){
								temp=point[start].x
							}
						}
						break;
					case 1:
						break;
					case 2:
						break;
					default break;
				}

			}
		}
}
//-----------------------CPU initialization-----------------------------------

void init(void) {
	TMOD=0x21 ;		// (T1 MODE 2_send_flagME,T0 MODE 1_send_flagME)
	TCON=0x51 ;		//0101 0000 (TR1=1 , TR0=1
	SCON=0x50 ;		//0101 0000 (MODE 1 ;RECEIVE ENABLE)
	IP=0x10  ;		//0001 0000 (S-PORT HIGH interrupt)
	IE=0x12   ;		//0001 0010 (S_PORT AND TIME0 ENABLE INT)
	TL1=TH1=0xfd;		//9600
	PCON=0x00;		//SMOD=0
	IE = 0x92;
	TR1=0;
	EA=1;       // enable all interrupt
}

//-----------------------------------------------------------------------------
 main()
{ word i;
power=0;
 lcd_power=0;
 init();
init_lcd();
for(i=0;i<1024;i++){
		*(xram+i)=0x00;
	}
while(1){
	write_square(00,16,127,63,1);
//	for(i=0;i<4;i++)
//	put_char((33+16*i),21,0,'b');
	game();
//------------------------------------------------
	display();
	while(1);
	}
 }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -