0
  • 聊天消息
  • 系统消息
  • 评论与回复
登录后你可以
  • 下载海量资料
  • 学习在线课程
  • 观看技术视频
  • 写文章/发帖/加入社区
会员中心
创作中心

完善资料让更多小伙伴认识你,还能领取20积分哦,立即完善>

3天内不再提示

基于STM32设计的拼图小游戏详解

DS小龙哥-嵌入式技术 来源:DS小龙哥-嵌入式技术 作者:DS小龙哥-嵌入式技 2022-02-28 13:45 次阅读

一、环境介绍

MCU:STM32F103ZET6

编程软件环境:keil5

LCD显示屏:正点原子的TFT 3.5寸显示屏

二、运行效果

poYBAGDYdXCAWkKMAAAAK8RNs4s030.png

poYBAGDYdXCAWkKMAAAAK8RNs4s030.png

poYBAGDYdXCAWkKMAAAAK8RNs4s030.png

三、功能介绍

因为代码原来是在2.8寸屏上设计的,但是2.8寸屏摔碎了,现在手上只有块3.5寸的LCD屏。就把代码移植到3.5寸屏上显示了,目前运行的效果可以看上面的效果图。

拼图游戏在小时候还是经常玩,玩法也比较简单,这里就使用STM32设计一个拼图小游戏分享出来一起学习。

目前游戏是通过开发板上4按键进行控制图片的移动,完成最终的拼图效果,如果想要在上面扩充其他游戏功能都还是比较容易。

当然,设计编写这个款小游戏不仅仅是为了玩游戏这么简单,主要是为了通过设计游戏的过程学习STM32编程、LCD的编程、按键、图片等等很多编程知识。

做拼图这款游戏主要的硬件就是LCD显示屏和几个按键,没有其他外设,如果自己有其他STM32最小系统板有LCD显示屏的话,完全可以把代码移植过去运行。当前的代码兼容正点原子战舰开发板,可以直接在正点原子战舰直接编译下载运行。其他的开发板上运行也是一样的道理,底层替换自己LCD画点函数即可。

目前的拼图的游戏工程里图片是提前通过取模软件取好模之后把数据放在工程里的,如果想要动态更加灵活的切换拼图的图片,可以加上SD卡和FATFS文件系统,把图片放在SD卡,通过按键选择指定图片切割进行拼图。

代码分为3个部分:

1. LCD屏驱动代码部分

2. 按键驱动代码部分

3. 拼图的逻辑代码部分

四、LCD屏驱动代码

4.1 LCD介绍

这款LCD显示屏的驱动芯片型号是NT35310,时序支持8080时序,普通的MCU可以采用IO模拟8080时序驱动,有硬件接口的可以采用硬件接口驱动。STM32F103ZET6的FSMC是可以输出8080时序,为了提高刷屏速度,这里代码就采用FSMC接口来驱动LCD屏。照着LCD的手册指令集,自己编写一遍LCD的初始化代码,可以深入学习一遍LCD编程原理。

poYBAGDYdXCAWkKMAAAAK8RNs4s030.png

4.2 LCD显示屏的初始化代码示例

/*
函数功能:写寄存器函数
参    数:regval:寄存器值
*/	 
void LcdWriteReg(u16 regval)
{
	LCD_REG=regval;//写入要写的寄存器序号	 
}


/*
函数功能:写LCD数据
函数参数:data:要写入的值	
*/
void LcdWriteData(u16 data)
{	 
	LCD_RAM=data;		 
}


/*
函数功能:读LCD数据
返 回 值:读到的值
*/
u16 LcdReadData(void)
{
	vu16 ram;			//防止被优化
	ram=LCD_RAM;	
	return ram;	 
}		

/*
函数功能:读寄存器
函数参数:LCD_Reg:寄存器地址
返 回 值:读到的数据
*/
u16 LcdReadReg(u16 LCD_Reg)
{										   
	LcdWriteReg(LCD_Reg);		//写入要读的寄存器序号
	DelayUs(5);		  
	return LcdReadData();		//返回读到的值
}


/*
函数功能:开始写GRAM
*/
void LcdWriteRAM_Prepare(void)
{
 	LCD_REG=0X2C;	  
}	


/*
函数功能:LCD写GRAM
函数参数:RGB_Code:颜色值
*/
void LcdWriteRAM(u16 RGB_Code)
{							    
	LCD_RAM = RGB_Code;//写十六位GRAM
}


/*
函数功能:设置光标位置
函数参数:
         Xpos:横坐标
         Ypos:纵坐标
*/
void LcdSetCursor(u16 Xpos, u16 Ypos)
{	 
	  LcdWriteReg(0X2A); 
		LcdWriteData(Xpos>>8);
	  LcdWriteData(Xpos&0XFF); 			 
		LcdWriteReg(0X2B); 
		LcdWriteData(Ypos>>8);
	  LcdWriteData(Ypos&0XFF); 
}


/*
功能:读取个某点的颜色值	
参数:x,y:坐标
返回值:此点的颜色
*/
u16 LcdReadPoint(u16 x,u16 y)
{
 	u16 r=0,g=0,b=0;   
	LcdSetCursor(x,y);	    
	LcdWriteReg(0X2E);
	
 	r=LcdReadData();								//dummy Read	   
	DelayUs(1);	  
 	r=LcdReadData();  		  						//实际坐标颜色

	DelayUs(1);	  
	b=LcdReadData(); 
	g=r&0XFF;		//5310第一次读取的是RG的值,R在前,G在后,各占8位
	g<<=8;
	
	return (((r>>11)<<11)|((g>>10)<<5)|(b>>11));
}	




/*
功  能: 初始化LCD屏幕
说  明: 用于3.5寸屏幕的初始化。 
         LCD ID:5310
*/
void LcdInit(void)
{ 										  
	RCC->AHBENR|=1<<8;     	 	//使能FSMC时钟	  
  RCC->APB2ENR|=1<<3;     	//使能PORTB时钟
	RCC->APB2ENR|=1<<5;     	//使能PORTD时钟
	RCC->APB2ENR|=1<<6;     	//使能PORTE时钟
 	RCC->APB2ENR|=1<<8;      	//使能PORTG时钟	 
	GPIOB->CRL&=0XFFFFFFF0;		//PB0 推挽输出 背光
	GPIOB->CRL|=0X00000003;	   
	
	//PORTD复用推挽输出 	
	GPIOD->CRH&=0X00FFF000;
	GPIOD->CRH|=0XBB000BBB; 
	GPIOD->CRL&=0XFF00FF00;
	GPIOD->CRL|=0X00BB00BB;   	 
	
	//PORTE复用推挽输出 	
	GPIOE->CRH&=0X00000000;
	GPIOE->CRH|=0XBBBBBBBB; 
	GPIOE->CRL&=0X0FFFFFFF;
	GPIOE->CRL|=0XB0000000;    	    	 											 
	
	//PORTG12复用推挽输出 	    	 											 
	GPIOG->CRH&=0XFFF0FFFF;
	GPIOG->CRH|=0X000B0000; 
	GPIOG->CRL&=0XFFFFFFF0;//PG0->RS
	GPIOG->CRL|=0X0000000B;  

	//寄存器清零
	//bank1有NE1~4,每一个有一个BCR+TCR,所以总共八个寄存器。
	//这里我们使用NE4 ,也就对应BTCR[6],[7]。				    
	FSMC_Bank1->BTCR[6]=0X00000000;
	FSMC_Bank1->BTCR[7]=0X00000000;
	FSMC_Bank1E->BWTR[6]=0X00000000;
	
	//操作BCR寄存器	使用异步模式
	FSMC_Bank1->BTCR[6]|=1<<12;		//存储器写使能
	FSMC_Bank1->BTCR[6]|=1<<14;		//读写使用不同的时序
	FSMC_Bank1->BTCR[6]|=1<<4; 		//存储器数据宽度为16bit 	   
	
	//操作BTR寄存器	
	//读时序控制寄存器 							    
	FSMC_Bank1->BTCR[7]|=0<<28;		//模式A 	 							  	 
	FSMC_Bank1->BTCR[7]|=1<<0; 		//地址建立时间(ADDSET)为2个HCLK 1/36M=27ns(实际>200ns)	 	 
	
	//因为液晶驱动IC的读数据的时候,速度不能太快,尤其对1289这个IC。
	FSMC_Bank1->BTCR[7]|=0XF<<8;  	//数据保存时间为16个HCLK	 	 
	
	//写时序控制寄存器  
	FSMC_Bank1E->BWTR[6]|=0<<28; 	//模式A 	 							    
	FSMC_Bank1E->BWTR[6]|=0<<0;		//地址建立时间(ADDSET)为1个HCLK 
 	
	//4个HCLK(HCLK=72M)因为液晶驱动IC的写信号脉宽,最少也得50ns。72M/4=24M=55ns  	 
	FSMC_Bank1E->BWTR[6]|=3<<8; 	//数据保存时间为4个HCLK	
	
	//使能BANK1,区域4
	FSMC_Bank1->BTCR[6]|=1<<0;		//使能BANK1,区域4	
	DelayMs(50); 					// delay 50 ms 
  
	lcddev.id=LcdReadReg(0x0000);	//读ID(9320/9325/9328/4531/4535等IC)   

	LcdWriteReg(0XD4);				   
	lcddev.id=LcdReadData();//dummy read  
	lcddev.id=LcdReadData();//读回0X01	 
	lcddev.id=LcdReadData();//读回0X53	
	lcddev.id<<=8;	 
	lcddev.id|=LcdReadData();	//这里读回0X10	 

	printf(" LCD ID:%x\r\n",lcddev.id); //打印LCD ID
	
		//if(lcddev.id==0x5310)
		LcdWriteReg(0xED);
		LcdWriteData(0x01);
		LcdWriteData(0xFE);

		LcdWriteReg(0xEE);
		LcdWriteData(0xDE);
		LcdWriteData(0x21);

		LcdWriteReg(0xF1);
		LcdWriteData(0x01);
		LcdWriteReg(0xDF);
		LcdWriteData(0x10);

		//VCOMvoltage//
		LcdWriteReg(0xC4);
		LcdWriteData(0x8F);	  //5f

		LcdWriteReg(0xC6);
		LcdWriteData(0x00);
		LcdWriteData(0xE2);
		LcdWriteData(0xE2);
		LcdWriteData(0xE2);
		LcdWriteReg(0xBF);
		LcdWriteData(0xAA);

		LcdWriteReg(0xB0);
		LcdWriteData(0x0D);
		LcdWriteData(0x00);
		LcdWriteData(0x0D);
		LcdWriteData(0x00);
		LcdWriteData(0x11);
		LcdWriteData(0x00);
		LcdWriteData(0x19);
		LcdWriteData(0x00);
		LcdWriteData(0x21);
		LcdWriteData(0x00);
		LcdWriteData(0x2D);
		LcdWriteData(0x00);
		LcdWriteData(0x3D);
		LcdWriteData(0x00);
		LcdWriteData(0x5D);
		LcdWriteData(0x00);
		LcdWriteData(0x5D);
		LcdWriteData(0x00);

		LcdWriteReg(0xB1);
		LcdWriteData(0x80);
		LcdWriteData(0x00);
		LcdWriteData(0x8B);
		LcdWriteData(0x00);
		LcdWriteData(0x96);
		LcdWriteData(0x00);

		LcdWriteReg(0xB2);
		LcdWriteData(0x00);
		LcdWriteData(0x00);
		LcdWriteData(0x02);
		LcdWriteData(0x00);
		LcdWriteData(0x03);
		LcdWriteData(0x00);

		LcdWriteReg(0xB3);
		LcdWriteData(0x00);
		LcdWriteData(0x00);
		LcdWriteData(0x00);
		LcdWriteData(0x00);
		LcdWriteData(0x00);
		LcdWriteData(0x00);
		LcdWriteData(0x00);
		LcdWriteData(0x00);
		LcdWriteData(0x00);
		LcdWriteData(0x00);
		LcdWriteData(0x00);
		LcdWriteData(0x00);
		LcdWriteData(0x00);
		LcdWriteData(0x00);
		LcdWriteData(0x00);
		LcdWriteData(0x00);
		LcdWriteData(0x00);
		LcdWriteData(0x00);
		LcdWriteData(0x00);
		LcdWriteData(0x00);
		LcdWriteData(0x00);
		LcdWriteData(0x00);
		LcdWriteData(0x00);
		LcdWriteData(0x00);

		LcdWriteReg(0xB4);
		LcdWriteData(0x8B);
		LcdWriteData(0x00);
		LcdWriteData(0x96);
		LcdWriteData(0x00);
		LcdWriteData(0xA1);
		LcdWriteData(0x00);

		LcdWriteReg(0xB5);
		LcdWriteData(0x02);
		LcdWriteData(0x00);
		LcdWriteData(0x03);
		LcdWriteData(0x00);
		LcdWriteData(0x04);
		LcdWriteData(0x00);

		LcdWriteReg(0xB6);
		LcdWriteData(0x00);
		LcdWriteData(0x00);

		LcdWriteReg(0xB7);
		LcdWriteData(0x00);
		LcdWriteData(0x00);
		LcdWriteData(0x3F);
		LcdWriteData(0x00);
		LcdWriteData(0x5E);
		LcdWriteData(0x00);
		LcdWriteData(0x64);
		LcdWriteData(0x00);
		LcdWriteData(0x8C);
		LcdWriteData(0x00);
		LcdWriteData(0xAC);
		LcdWriteData(0x00);
		LcdWriteData(0xDC);
		LcdWriteData(0x00);
		LcdWriteData(0x70);
		LcdWriteData(0x00);
		LcdWriteData(0x90);
		LcdWriteData(0x00);
		LcdWriteData(0xEB);
		LcdWriteData(0x00);
		LcdWriteData(0xDC);
		LcdWriteData(0x00);

		LcdWriteReg(0xB8);
		LcdWriteData(0x00);
		LcdWriteData(0x00);
		LcdWriteData(0x00);
		LcdWriteData(0x00);
		LcdWriteData(0x00);
		LcdWriteData(0x00);
		LcdWriteData(0x00);
		LcdWriteData(0x00);

		LcdWriteReg(0xBA);
		LcdWriteData(0x24);
		LcdWriteData(0x00);
		LcdWriteData(0x00);
		LcdWriteData(0x00);

		LcdWriteReg(0xC1);
		LcdWriteData(0x20);
		LcdWriteData(0x00);
		LcdWriteData(0x54);
		LcdWriteData(0x00);
		LcdWriteData(0xFF);
		LcdWriteData(0x00);

		LcdWriteReg(0xC2);
		LcdWriteData(0x0A);
		LcdWriteData(0x00);
		LcdWriteData(0x04);
		LcdWriteData(0x00);

		LcdWriteReg(0xC3);
		LcdWriteData(0x3C);
		LcdWriteData(0x00);
		LcdWriteData(0x3A);
		LcdWriteData(0x00);
		LcdWriteData(0x39);
		LcdWriteData(0x00);
		LcdWriteData(0x37);
		LcdWriteData(0x00);
		LcdWriteData(0x3C);
		LcdWriteData(0x00);
		LcdWriteData(0x36);
		LcdWriteData(0x00);
		LcdWriteData(0x32);
		LcdWriteData(0x00);
		LcdWriteData(0x2F);
		LcdWriteData(0x00);
		LcdWriteData(0x2C);
		LcdWriteData(0x00);
		LcdWriteData(0x29);
		LcdWriteData(0x00);
		LcdWriteData(0x26);
		LcdWriteData(0x00);
		LcdWriteData(0x24);
		LcdWriteData(0x00);
		LcdWriteData(0x24);
		LcdWriteData(0x00);
		LcdWriteData(0x23);
		LcdWriteData(0x00);
		LcdWriteData(0x3C);
		LcdWriteData(0x00);
		LcdWriteData(0x36);
		LcdWriteData(0x00);
		LcdWriteData(0x32);
		LcdWriteData(0x00);
		LcdWriteData(0x2F);
		LcdWriteData(0x00);
		LcdWriteData(0x2C);
		LcdWriteData(0x00);
		LcdWriteData(0x29);
		LcdWriteData(0x00);
		LcdWriteData(0x26);
		LcdWriteData(0x00);
		LcdWriteData(0x24);
		LcdWriteData(0x00);
		LcdWriteData(0x24);
		LcdWriteData(0x00);
		LcdWriteData(0x23);
		LcdWriteData(0x00);

		LcdWriteReg(0xC4);
		LcdWriteData(0x62);
		LcdWriteData(0x00);
		LcdWriteData(0x05);
		LcdWriteData(0x00);
		LcdWriteData(0x84);
		LcdWriteData(0x00);
		LcdWriteData(0xF0);
		LcdWriteData(0x00);
		LcdWriteData(0x18);
		LcdWriteData(0x00);
		LcdWriteData(0xA4);
		LcdWriteData(0x00);
		LcdWriteData(0x18);
		LcdWriteData(0x00);
		LcdWriteData(0x50);
		LcdWriteData(0x00);
		LcdWriteData(0x0C);
		LcdWriteData(0x00);
		LcdWriteData(0x17);
		LcdWriteData(0x00);
		LcdWriteData(0x95);
		LcdWriteData(0x00);
		LcdWriteData(0xF3);
		LcdWriteData(0x00);
		LcdWriteData(0xE6);
		LcdWriteData(0x00);

		LcdWriteReg(0xC5);
		LcdWriteData(0x32);
		LcdWriteData(0x00);
		LcdWriteData(0x44);
		LcdWriteData(0x00);
		LcdWriteData(0x65);
		LcdWriteData(0x00);
		LcdWriteData(0x76);
		LcdWriteData(0x00);
		LcdWriteData(0x88);
		LcdWriteData(0x00);

		LcdWriteReg(0xC6);
		LcdWriteData(0x20);
		LcdWriteData(0x00);
		LcdWriteData(0x17);
		LcdWriteData(0x00);
		LcdWriteData(0x01);
		LcdWriteData(0x00);

		LcdWriteReg(0xC7);
		LcdWriteData(0x00);
		LcdWriteData(0x00);
		LcdWriteData(0x00);
		LcdWriteData(0x00);

		LcdWriteReg(0xC8);
		LcdWriteData(0x00);
		LcdWriteData(0x00);
		LcdWriteData(0x00);
		LcdWriteData(0x00);

		LcdWriteReg(0xC9);
		LcdWriteData(0x00);
		LcdWriteData(0x00);
		LcdWriteData(0x00);
		LcdWriteData(0x00);
		LcdWriteData(0x00);
		LcdWriteData(0x00);
		LcdWriteData(0x00);
		LcdWriteData(0x00);
		LcdWriteData(0x00);
		LcdWriteData(0x00);
		LcdWriteData(0x00);
		LcdWriteData(0x00);
		LcdWriteData(0x00);
		LcdWriteData(0x00);
		LcdWriteData(0x00);
		LcdWriteData(0x00);

		LcdWriteReg(0xE0);
		LcdWriteData(0x16);
		LcdWriteData(0x00);
		LcdWriteData(0x1C);
		LcdWriteData(0x00);
		LcdWriteData(0x21);
		LcdWriteData(0x00);
		LcdWriteData(0x36);
		LcdWriteData(0x00);
		LcdWriteData(0x46);
		LcdWriteData(0x00);
		LcdWriteData(0x52);
		LcdWriteData(0x00);
		LcdWriteData(0x64);
		LcdWriteData(0x00);
		LcdWriteData(0x7A);
		LcdWriteData(0x00);
		LcdWriteData(0x8B);
		LcdWriteData(0x00);
		LcdWriteData(0x99);
		LcdWriteData(0x00);
		LcdWriteData(0xA8);
		LcdWriteData(0x00);
		LcdWriteData(0xB9);
		LcdWriteData(0x00);
		LcdWriteData(0xC4);
		LcdWriteData(0x00);
		LcdWriteData(0xCA);
		LcdWriteData(0x00);
		LcdWriteData(0xD2);
		LcdWriteData(0x00);
		LcdWriteData(0xD9);
		LcdWriteData(0x00);
		LcdWriteData(0xE0);
		LcdWriteData(0x00);
		LcdWriteData(0xF3);
		LcdWriteData(0x00);

		LcdWriteReg(0xE1);
		LcdWriteData(0x16);
		LcdWriteData(0x00);
		LcdWriteData(0x1C);
		LcdWriteData(0x00);
		LcdWriteData(0x22);
		LcdWriteData(0x00);
		LcdWriteData(0x36);
		LcdWriteData(0x00);
		LcdWriteData(0x45);
		LcdWriteData(0x00);
		LcdWriteData(0x52);
		LcdWriteData(0x00);
		LcdWriteData(0x64);
		LcdWriteData(0x00);
		LcdWriteData(0x7A);
		LcdWriteData(0x00);
		LcdWriteData(0x8B);
		LcdWriteData(0x00);
		LcdWriteData(0x99);
		LcdWriteData(0x00);
		LcdWriteData(0xA8);
		LcdWriteData(0x00);
		LcdWriteData(0xB9);
		LcdWriteData(0x00);
		LcdWriteData(0xC4);
		LcdWriteData(0x00);
		LcdWriteData(0xCA);
		LcdWriteData(0x00);
		LcdWriteData(0xD2);
		LcdWriteData(0x00);
		LcdWriteData(0xD8);
		LcdWriteData(0x00);
		LcdWriteData(0xE0);
		LcdWriteData(0x00);
		LcdWriteData(0xF3);
		LcdWriteData(0x00);

		LcdWriteReg(0xE2);
		LcdWriteData(0x05);
		LcdWriteData(0x00);
		LcdWriteData(0x0B);
		LcdWriteData(0x00);
		LcdWriteData(0x1B);
		LcdWriteData(0x00);
		LcdWriteData(0x34);
		LcdWriteData(0x00);
		LcdWriteData(0x44);
		LcdWriteData(0x00);
		LcdWriteData(0x4F);
		LcdWriteData(0x00);
		LcdWriteData(0x61);
		LcdWriteData(0x00);
		LcdWriteData(0x79);
		LcdWriteData(0x00);
		LcdWriteData(0x88);
		LcdWriteData(0x00);
		LcdWriteData(0x97);
		LcdWriteData(0x00);
		LcdWriteData(0xA6);
		LcdWriteData(0x00);
		LcdWriteData(0xB7);
		LcdWriteData(0x00);
		LcdWriteData(0xC2);
		LcdWriteData(0x00);
		LcdWriteData(0xC7);
		LcdWriteData(0x00);
		LcdWriteData(0xD1);
		LcdWriteData(0x00);
		LcdWriteData(0xD6);
		LcdWriteData(0x00);
		LcdWriteData(0xDD);
		LcdWriteData(0x00);
		LcdWriteData(0xF3);
		LcdWriteData(0x00);
		LcdWriteReg(0xE3);
		LcdWriteData(0x05);
		LcdWriteData(0x00);
		LcdWriteData(0xA);
		LcdWriteData(0x00);
		LcdWriteData(0x1C);
		LcdWriteData(0x00);
		LcdWriteData(0x33);
		LcdWriteData(0x00);
		LcdWriteData(0x44);
		LcdWriteData(0x00);
		LcdWriteData(0x50);
		LcdWriteData(0x00);
		LcdWriteData(0x62);
		LcdWriteData(0x00);
		LcdWriteData(0x78);
		LcdWriteData(0x00);
		LcdWriteData(0x88);
		LcdWriteData(0x00);
		LcdWriteData(0x97);
		LcdWriteData(0x00);
		LcdWriteData(0xA6);
		LcdWriteData(0x00);
		LcdWriteData(0xB7);
		LcdWriteData(0x00);
		LcdWriteData(0xC2);
		LcdWriteData(0x00);
		LcdWriteData(0xC7);
		LcdWriteData(0x00);
		LcdWriteData(0xD1);
		LcdWriteData(0x00);
		LcdWriteData(0xD5);
		LcdWriteData(0x00);
		LcdWriteData(0xDD);
		LcdWriteData(0x00);
		LcdWriteData(0xF3);
		LcdWriteData(0x00);

		LcdWriteReg(0xE4);
		LcdWriteData(0x01);
		LcdWriteData(0x00);
		LcdWriteData(0x01);
		LcdWriteData(0x00);
		LcdWriteData(0x02);
		LcdWriteData(0x00);
		LcdWriteData(0x2A);
		LcdWriteData(0x00);
		LcdWriteData(0x3C);
		LcdWriteData(0x00);
		LcdWriteData(0x4B);
		LcdWriteData(0x00);
		LcdWriteData(0x5D);
		LcdWriteData(0x00);
		LcdWriteData(0x74);
		LcdWriteData(0x00);
		LcdWriteData(0x84);
		LcdWriteData(0x00);
		LcdWriteData(0x93);
		LcdWriteData(0x00);
		LcdWriteData(0xA2);
		LcdWriteData(0x00);
		LcdWriteData(0xB3);
		LcdWriteData(0x00);
		LcdWriteData(0xBE);
		LcdWriteData(0x00);
		LcdWriteData(0xC4);
		LcdWriteData(0x00);
		LcdWriteData(0xCD);
		LcdWriteData(0x00);
		LcdWriteData(0xD3);
		LcdWriteData(0x00);
		LcdWriteData(0xDD);
		LcdWriteData(0x00);
		LcdWriteData(0xF3);
		LcdWriteData(0x00);
		LcdWriteReg(0xE5);
		LcdWriteData(0x00);
		LcdWriteData(0x00);
		LcdWriteData(0x00);
		LcdWriteData(0x00);
		LcdWriteData(0x02);
		LcdWriteData(0x00);
		LcdWriteData(0x29);
		LcdWriteData(0x00);
		LcdWriteData(0x3C);
		LcdWriteData(0x00);
		LcdWriteData(0x4B);
		LcdWriteData(0x00);
		LcdWriteData(0x5D);
		LcdWriteData(0x00);
		LcdWriteData(0x74);
		LcdWriteData(0x00);
		LcdWriteData(0x84);
		LcdWriteData(0x00);
		LcdWriteData(0x93);
		LcdWriteData(0x00);
		LcdWriteData(0xA2);
		LcdWriteData(0x00);
		LcdWriteData(0xB3);
		LcdWriteData(0x00);
		LcdWriteData(0xBE);
		LcdWriteData(0x00);
		LcdWriteData(0xC4);
		LcdWriteData(0x00);
		LcdWriteData(0xCD);
		LcdWriteData(0x00);
		LcdWriteData(0xD3);
		LcdWriteData(0x00);
		LcdWriteData(0xDC);
		LcdWriteData(0x00);
		LcdWriteData(0xF3);
		LcdWriteData(0x00);

		LcdWriteReg(0xE6);
		LcdWriteData(0x11);
		LcdWriteData(0x00);
		LcdWriteData(0x34);
		LcdWriteData(0x00);
		LcdWriteData(0x56);
		LcdWriteData(0x00);
		LcdWriteData(0x76);
		LcdWriteData(0x00);
		LcdWriteData(0x77);
		LcdWriteData(0x00);
		LcdWriteData(0x66);
		LcdWriteData(0x00);
		LcdWriteData(0x88);
		LcdWriteData(0x00);
		LcdWriteData(0x99);
		LcdWriteData(0x00);
		LcdWriteData(0xBB);
		LcdWriteData(0x00);
		LcdWriteData(0x99);
		LcdWriteData(0x00);
		LcdWriteData(0x66);
		LcdWriteData(0x00);
		LcdWriteData(0x55);
		LcdWriteData(0x00);
		LcdWriteData(0x55);
		LcdWriteData(0x00);
		LcdWriteData(0x45);
		LcdWriteData(0x00);
		LcdWriteData(0x43);
		LcdWriteData(0x00);
		LcdWriteData(0x44);
		LcdWriteData(0x00);

		LcdWriteReg(0xE7);
		LcdWriteData(0x32);
		LcdWriteData(0x00);
		LcdWriteData(0x55);
		LcdWriteData(0x00);
		LcdWriteData(0x76);
		LcdWriteData(0x00);
		LcdWriteData(0x66);
		LcdWriteData(0x00);
		LcdWriteData(0x67);
		LcdWriteData(0x00);
		LcdWriteData(0x67);
		LcdWriteData(0x00);
		LcdWriteData(0x87);
		LcdWriteData(0x00);
		LcdWriteData(0x99);
		LcdWriteData(0x00);
		LcdWriteData(0xBB);
		LcdWriteData(0x00);
		LcdWriteData(0x99);
		LcdWriteData(0x00);
		LcdWriteData(0x77);
		LcdWriteData(0x00);
		LcdWriteData(0x44);
		LcdWriteData(0x00);
		LcdWriteData(0x56);
		LcdWriteData(0x00);
		LcdWriteData(0x23); 
		LcdWriteData(0x00);
		LcdWriteData(0x33);
		LcdWriteData(0x00);
		LcdWriteData(0x45);
		LcdWriteData(0x00);

		LcdWriteReg(0xE8);
		LcdWriteData(0x00);
		LcdWriteData(0x00);
		LcdWriteData(0x99);
		LcdWriteData(0x00);
		LcdWriteData(0x87);
		LcdWriteData(0x00);
		LcdWriteData(0x88);
		LcdWriteData(0x00);
		LcdWriteData(0x77);
		LcdWriteData(0x00);
		LcdWriteData(0x66);
		LcdWriteData(0x00);
		LcdWriteData(0x88);
		LcdWriteData(0x00);
		LcdWriteData(0xAA);
		LcdWriteData(0x00);
		LcdWriteData(0xBB);
		LcdWriteData(0x00);
		LcdWriteData(0x99);
		LcdWriteData(0x00);
		LcdWriteData(0x66);
		LcdWriteData(0x00);
		LcdWriteData(0x55);
		LcdWriteData(0x00);
		LcdWriteData(0x55);
		LcdWriteData(0x00);
		LcdWriteData(0x44);
		LcdWriteData(0x00);
		LcdWriteData(0x44);
		LcdWriteData(0x00);
		LcdWriteData(0x55);
		LcdWriteData(0x00);

		LcdWriteReg(0xE9);
		LcdWriteData(0xAA);
		LcdWriteData(0x00);
		LcdWriteData(0x00);
		LcdWriteData(0x00);

		LcdWriteReg(0x00);
		LcdWriteData(0xAA);

		LcdWriteReg(0xCF);
		LcdWriteData(0x00);
		LcdWriteData(0x00);
		LcdWriteData(0x00);
		LcdWriteData(0x00);
		LcdWriteData(0x00);
		LcdWriteData(0x00);
		LcdWriteData(0x00);
		LcdWriteData(0x00);
		LcdWriteData(0x00);
		LcdWriteData(0x00);
		LcdWriteData(0x00);
		LcdWriteData(0x00);
		LcdWriteData(0x00);
		LcdWriteData(0x00);
		LcdWriteData(0x00);
		LcdWriteData(0x00);
		LcdWriteData(0x00);

		LcdWriteReg(0xF0);
		LcdWriteData(0x00);
		LcdWriteData(0x50);
		LcdWriteData(0x00);
		LcdWriteData(0x00);
		LcdWriteData(0x00);

		LcdWriteReg(0xF3);
		LcdWriteData(0x00);

		LcdWriteReg(0xF9);
		LcdWriteData(0x06);
		LcdWriteData(0x10);
		LcdWriteData(0x29);
		LcdWriteData(0x00);

		LcdWriteReg(0x3A);
		LcdWriteData(0x55);	//66

		LcdWriteReg(0x11);
		DelayMs(100);
		LcdWriteReg(0x29);
		LcdWriteReg(0x35);
		LcdWriteData(0x00);

		LcdWriteReg(0x51);
		LcdWriteData(0xFF);
		LcdWriteReg(0x53);
		LcdWriteData(0x2C);
		LcdWriteReg(0x55);
		LcdWriteData(0x82);
		LcdWriteReg(0x2c);
			
		lcddev.width=320;
		lcddev.height=480;
		
		LCD_LED=1;				//点亮背光
} 
poYBAGDYdXCAWkKMAAAAK8RNs4s030.png

4.3 LCD相关的绘图函数代码示例

/*
函数功能:画点函数
参    数:x,y坐标
          color颜色值
*/
void LcdDrawPoint(u16 x,u16 y,u16 color)
{
	LcdSetCursor(x,y);		  //设置光标位置 
	LcdWriteRAM_Prepare();	//开始写入GRAM
	LCD_RAM=color; 
}


/*
函数功能:清屏函数
参    数:color颜色值
*/
void LcdClear(u16 color)
{
	u32 i;
	LcdSetCursor(0,0);		  //设置光标位置 
	LcdWriteRAM_Prepare();	//开始写入GRAM
	for(i=0;i<320*480;i++)LCD_RAM=color; 
}


/*
函数功能:显示一个指定大小的汉字
参    数:
					x,y :汉字的坐标
					font:取模数据
					size:字体大小尺寸
*/
void LcdShowFont(u16 x,u16 y,u8 *dzk,u8 size)
{
	u8 temp,t,t1;
	u16 x0=x;	
  u8 csize=(size/8+((size%8)?1:0))*(size);//得到字体一个字符对应点阵集所占的字节数  
	    for(t=0;t0)incx=1; //设置单步方向 
	else if(delta_x==0)incx=0;//垂直线 
	else {incx=-1;delta_x=-delta_x;} 
	if(delta_y>0)incy=1; 
	else if(delta_y==0)incy=0;//水平线 
	else{incy=-1;delta_y=-delta_y;} 
	if( delta_x>delta_y)distance=delta_x; //选取基本增量坐标轴 
	else distance=delta_y; 
	for(t=0;t<=distance+1;t++ )//画线输出 
	{  
		LcdDrawPoint(uRow,uCol,color);//画点 
		xerr+=delta_x ; 
		yerr+=delta_y ; 
		if(xerr>distance) 
		{ 
			xerr-=distance; 
			uRow+=incx; 
		} 
		if(yerr>distance) 
		{ 
			yerr-=distance; 
			uCol+=incy; 
		} 
	}  
}  


/*
函数功能:任意角度画直线 
参    数:
					w  :线段的长度
					len:半径
					c  :颜色
					x,y:坐标
*/
#include 
void LcdDrawAngleLine(u32 x,u32 y,float du,u32 len,u32 w,u16 c)
{
  int i;
	int x0,y0;
	float k=du*(3.1415926535/180);	
	for(i=len-w;i高位在前
				//画点LCD的像素是16位的,所以需要将2个8位像素合成16位像素。
				 p+=2;
			}
	 }
}

;i++)>;t++)>
poYBAGDYdXCAWkKMAAAAK8RNs4s030.png

五、按键驱动代码示例

拼图的时候,通过4个按键控制上下左右方向的,按键的代码如下:

功能    :按键的初始化
硬件连接:
KEY1:PA0  --按下是高电平
KEY2:PE2  --按下是低电平
KEY3:PE3  --按下是低电平
KEY4:PE4  --按下是低电平
返回值:无
*/
void KeyInit(void)
{
	 /*1. 开时钟*/
	 RCC->APB2ENR|=1<<2; //PA
	 RCC->APB2ENR|=1<<6; //PE 
	
	 /*2. 配置GPIO口模式*/
	 GPIOA->CRL&=0xFFFFFFF0;
	 GPIOA->CRL|=0x00000008; //上下拉输入模式
	 
	 GPIOE->CRL&=0xFFF000FF;
	 GPIOE->CRL|=0x00088800; //上下拉输入模式
	
	 /*3. 上下拉控制*/
	 //GPIOA->ODR&=~(1<<0); //PA0下拉
	 GPIOE->ODR|=1<<2;
	 GPIOE->ODR|=1<<3;
	 GPIOE->ODR|=1<<4;
}

/*
功  能:获取按键值
返回值:
    1表示按键1按下
		2表示按键2按下
		3表示按键3按下
		4表示按键4按下
    0表示没有按键按下
实现单次按下模式(上一次按下的按键没有松开,就不做检测)和连续按下的模式。

模式:
   mode=1; //表示单击
   mode=0; //表示连续
*/
u8 GetKeyValue(u32 mode)
{
	 static int stat=0; //保存上一次按键的状态
	 if(mode==0)stat=0;
	
	 if((KEY1||KEY2==0||KEY3==0||KEY4==0) && stat==0)
	 {
		  DelayMs(20); //延时消抖
		  stat=1;      //表示上次有按键按下
		  if(KEY1)return 1;
		  if(KEY2==0)return 2;
		  if(KEY3==0)return 3;
		  if(KEY4==0)return 4;
	 }
	 else
	 {
		  if(KEY1==0&&KEY2&&KEY3&&KEY4)stat=0; //判断所有的按键是否已经松开
	 }
	 return 0;
}
poYBAGDYdXCAWkKMAAAAK8RNs4s030.png

六、拼图的逻辑代码

分隔图片: 这里是按照320*240---2.8寸计算的

//           0,0         60,0         120,0         180,0
//           0,80        60,80        120, 80        180,80
//           0,160       60,160,      120, 160,      180,160
//           0,240,      60,240       120, 240       180,240
//显示位置起始坐标。
u32 record[16][2];

int pic[16][2]=      
{//第i张图片的屏幕所在位置。
  {60,160},{180,80},{0,80},
 {180,0},{180,160},{180,240},
 {0,160},{120,160},{60,80},{120,0},
 {120,240},{120,80},{0,0},{60,240},{60,0},{0,240}}; 
poYBAGDYdXCAWkKMAAAAK8RNs4s030.png

循环里处理图片移动逻辑:

while(1)
	{
			//【1】产生随机图片ID号:0--15,   4号图片用作空白图片。
 //ID号和图片首地址固定。图片所在的位置不固定。  
		picture_production(tu32,pic);
		while(1)
		{
			  key=GetKeyValue(1);
       if(key==1)      //右移动
        {if(record[4][0]>0)              //180,160
          {
            number=lookingfor_blankblock(record[4][0]-60,record[4][1],tu32);
            show_picture_(record[4][0],record[4][1],tu32,number);//空白左边图覆盖空白
            show_picture_(record[4][0]-60,record[4][1],tu32,4);  //出现新空白
            k=record[number][0];
            record[number][0]=record[4][0];
            record[4][0]=k;
           
                                
          }
        }
        if(key==3)    //左移动
        {if(record[4][0]<180)              //180,160
          {
            number=lookingfor_blankblock(record[4][0]+60,record[4][1],tu32);
            show_picture_(record[4][0],record[4][1],tu32,number);//空白左边图覆盖空白
            show_picture_(record[4][0]+60,record[4][1],tu32,4);  //出现新空白
            k=record[number][0];
            record[number][0]=record[4][0];
            record[4][0]=k;
            
            
                                //新空白横坐标
          }
        }
        if(key==2)    //下移动
        {if(record[4][1]>0)  
           { 
            number=lookingfor_blankblock(record[4][0],record[4][1]-80,tu32);
            show_picture_(record[4][0],record[4][1],tu32,number);//空白左边图覆盖空白
            show_picture_(record[4][0],record[4][1]-80,tu32,4);  //出现新空白
            k=record[number][1];
            record[number][1]=record[4][1];
            record[4][1]=k;
           }
        }
        
        if(key==4)    //上移动
        {
          if(record[4][1]<240)
            {
            number=lookingfor_blankblock(record[4][0],record[4][1]+80,tu32);
            show_picture_(record[4][0],record[4][1],tu32,number);//空白左边图覆盖空白
            show_picture_(record[4][0],record[4][1]+80,tu32,4);  //出现新空白
            k=record[number][1];
            record[number][1]=record[4][1];
            record[4][1]=k;
            }
        }
      }
	}
poYBAGDYdXCAWkKMAAAAK8RNs4s030.png

完整工程代码下载地址:https://download.csdn.net/download/xiaolong1126626497/20706318

审核编辑:符乾江

声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉
  • STM32
    +关注

    关注

    2270

    文章

    10900

    浏览量

    355911
  • 代码
    +关注

    关注

    30

    文章

    4787

    浏览量

    68589
收藏 人收藏

    评论

    相关推荐

    STM32项目分享:机智云智慧农业系统

    01—项目简介1.功能详解基于STM32的机智云智慧农业系统。功能如下:温湿度采集:使用DHT11温湿度传感器采集环境温湿度土壤湿度
    的头像 发表于 12-19 18:40 152次阅读
    <b class='flag-5'>STM32</b>项目分享:机智云智慧农业系统

    OPPO发布“风驰游戏内核”,优化游戏体验

    在近日举行的一加游戏大会活动中,OPPO正式推出了其独家自研的芯片级游戏技术——“风驰游戏内核”。OPPO首席产品官刘作虎明确表示,游戏体验将成为OPPO在性能赛道上的重要战略方向,而
    的头像 发表于 12-13 09:46 247次阅读

    FPGA打砖块小游戏设计思路

      交流问题   Q :FPGA打砖块小游戏,如何基于FPGA用verilog语言在Vivado平台上写打砖块小游戏,最好能用到PS2与VGA。 A :以下是一个基于 FPGA  Verilog
    的头像 发表于 12-09 16:57 191次阅读

    STM32项目分享:智能台灯系统

    01—项目简介1.功能详解基于STM32智能台灯系统。功能如下:亮度控制:按键控制照明灯的亮度(5挡亮度)计时功能:按键启动计时,累
    的头像 发表于 11-29 01:02 573次阅读
    <b class='flag-5'>STM32</b>项目分享:智能台灯系统

    详解MySQL多实例部署

    详解MySQL多实例部署
    的头像 发表于 11-11 11:10 239次阅读

    补齐拼图后,高通终于猛推“三端归一”

    高通自研CPU架构Oryon的“开枝散叶”。在去年首次应用于高通AI PC芯片骁龙X Elite之后,Oryon CPU走进了此次发布的一款手机芯片平台和两款汽车芯片平台。加上高通自研的Adreno GPU和Hexagon NPU,高通补齐了自研SoC的最后一块拼图,也统一了PC、手机、汽车三条产
    的头像 发表于 10-25 10:00 201次阅读
    补齐<b class='flag-5'>拼图</b>后,高通终于猛推“三端归一”

    STM32项目分享:智能门禁锁系统

    01—项目简介1.功能详解基于STM32智能门禁锁系统。功能如下:按键解锁:系统通过4×4矩阵键盘模块输入密码进入主菜单门禁刷卡:通
    的头像 发表于 09-12 08:04 1662次阅读
    <b class='flag-5'>STM32</b>项目分享:智能门禁锁系统

    STM32项目分享:智能大棚/智慧农业系统

    01—项目简介1.功能详解基于STM32的智慧农业系统。功能如下:温湿度采集:使用DHT11温湿度传感器采集环境温湿度土壤湿度采集:
    的头像 发表于 08-30 12:15 1032次阅读
    <b class='flag-5'>STM32</b>项目分享:智能大棚/智慧农业系统

    基于 FPGA 的飞机大战游戏系统设计

    第一部分 设计概述1.1 设计目的我们设计了一款基于 FPGA 的SEA开发板 的飞机大战游戏。飞机大战游戏是一款休闲益智类游戏,既简单又耐玩。在初始界面,我们有开始游戏、重新开始、
    发表于 07-24 20:03

    芯片应用:国产HMI芯片应用于4.3寸拼图

    产品,而非传统的玩具。这不仅反映了新生代父母对于启蒙教育、家庭教育的重视,也显示了他们对于亲子关系培养的关注。 为适应市场需求,启明智显将国产工业级HMI芯片Model3C应用于4.3寸拼图机中,寓教于乐,帮助孩子启蒙。   这款拼图
    的头像 发表于 06-04 14:40 413次阅读

    STM32—启动脚本详解

    我从Keil和STM32CubeIDE开始我的嵌入式开发,然后慢慢过渡到只使用文本编辑器而没有任何集成开发环境(IDE)。我一直使用这些IDE提供的启动文件。在这篇文章中,我将解释如何用C++编写
    发表于 05-31 14:17 1次下载

    HarmonyOS开发实战:【亲子拼图游戏

    编程语言编写的一个分布式益智拼图游戏,可以两台设备同时开启一局拼图游戏,每次点击九宫格内的图片,都会同步更新两台设备的图片位置
    的头像 发表于 04-16 17:00 582次阅读
    HarmonyOS开发实战:【亲子<b class='flag-5'>拼图游戏</b>】

    FPGA实现的“俄罗斯方块”游戏系统设计

    本项目主要在FPGA上实现了一个经典小游戏“俄罗斯方块”。本项目基本解决方案是,使用Xilinx Zynq系列开发板 ZedBoard 作为平台,实现主控模块,通过VGA接口来控制屏幕进行显示。
    发表于 03-28 10:41 1988次阅读
    FPGA实现的“俄罗斯方块”<b class='flag-5'>游戏</b>系统设计

    请问stm32H743II usb HOST如何识别双遥杆游戏手柄?

    stm32H743II usb HOST 如何识别 双遥杆游戏手柄?北通usb游戏手柄插到PC上显示是XBOX 360手柄,手柄上传为14个字节数据,分别为0-7两个遥杆的XY,8-9为手柄油门
    发表于 03-15 07:52

    DCDC原理详解

    电子发烧友网站提供《DCDC原理详解.pptx》资料免费下载
    发表于 03-05 17:18 97次下载