色哟哟视频在线观看-色哟哟视频在线-色哟哟欧美15最新在线-色哟哟免费在线观看-国产l精品国产亚洲区在线观看-国产l精品国产亚洲区久久

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

    文章

    10895

    瀏覽量

    355750
  • 代碼
    +關注

    關注

    30

    文章

    4779

    瀏覽量

    68525
收藏 人收藏

    評論

    相關推薦

    STM32項目分享:機智云智慧農業系統

    01—項目簡介1.功能詳解基于STM32的機智云智慧農業系統。功能如下:溫濕度采集:使用DHT11溫濕度傳感器采集環境溫濕度土壤濕度
    的頭像 發表于 12-19 18:40 ?91次閱讀
    <b class='flag-5'>STM32</b>項目分享:機智云智慧農業系統

    OPPO發布“風馳游戲內核”,優化游戲體驗

    在近日舉行的一加游戲大會活動中,OPPO正式推出了其獨家自研的芯片級游戲技術——“風馳游戲內核”。OPPO首席產品官劉作虎明確表示,游戲體驗將成為OPPO在性能賽道上的重要戰略方向,而
    的頭像 發表于 12-13 09:46 ?227次閱讀

    FPGA打磚塊小游戲設計思路

    ? 交流問題 ? Q :FPGA打磚塊小游戲,如何基于FPGA用verilog語言在Vivado平臺上寫打磚塊小游戲,最好能用到PS2與VGA。 A :以下是一個基于 FPGA? Verilog
    的頭像 發表于 12-09 16:57 ?161次閱讀

    STM32項目分享:智能臺燈系統

    01—項目簡介1.功能詳解基于STM32智能臺燈系統。功能如下:亮度控制:按鍵控制照明燈的亮度(5擋亮度)計時功能:按鍵啟動計時,累
    的頭像 發表于 11-29 01:02 ?489次閱讀
    <b class='flag-5'>STM32</b>項目分享:智能臺燈系統

    詳解MySQL多實例部署

    詳解MySQL多實例部署
    的頭像 發表于 11-11 11:10 ?225次閱讀

    補齊拼圖后,高通終于猛推“三端歸一”

    高通自研CPU架構Oryon的“開枝散葉”。在去年首次應用于高通AI PC芯片驍龍X Elite之后,Oryon CPU走進了此次發布的一款手機芯片平臺和兩款汽車芯片平臺。加上高通自研的Adreno GPU和Hexagon NPU,高通補齊了自研SoC的最后一塊拼圖,也統一了PC、手機、汽車三條產
    的頭像 發表于 10-25 10:00 ?196次閱讀
    補齊<b class='flag-5'>拼圖</b>后,高通終于猛推“三端歸一”

    STM32項目分享:智能門禁鎖系統

    01—項目簡介1.功能詳解基于STM32智能門禁鎖系統。功能如下:按鍵解鎖:系統通過4×4矩陣鍵盤模塊輸入密碼進入主菜單門禁刷卡:通
    的頭像 發表于 09-12 08:04 ?1578次閱讀
    <b class='flag-5'>STM32</b>項目分享:智能門禁鎖系統

    STM32項目分享:智能大棚/智慧農業系統

    01—項目簡介1.功能詳解基于STM32的智慧農業系統。功能如下:溫濕度采集:使用DHT11溫濕度傳感器采集環境溫濕度土壤濕度采集:
    的頭像 發表于 08-30 12:15 ?993次閱讀
    <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 ?402次閱讀

    STM32—啟動腳本詳解

    我從Keil和STM32CubeIDE開始我的嵌入式開發,然后慢慢過渡到只使用文本編輯器而沒有任何集成開發環境(IDE)。我一直使用這些IDE提供的啟動文件。在這篇文章中,我將解釋如何用C++編寫
    發表于 05-31 14:17 ?1次下載

    HarmonyOS開發實戰:【親子拼圖游戲

    編程語言編寫的一個分布式益智拼圖游戲,可以兩臺設備同時開啟一局拼圖游戲,每次點擊九宮格內的圖片,都會同步更新兩臺設備的圖片位置
    的頭像 發表于 04-16 17:00 ?580次閱讀
    HarmonyOS開發實戰:【親子<b class='flag-5'>拼圖游戲</b>】

    FPGA實現的“俄羅斯方塊”游戲系統設計

    本項目主要在FPGA上實現了一個經典小游戲“俄羅斯方塊”。本項目基本解決方案是,使用Xilinx Zynq系列開發板 ZedBoard 作為平臺,實現主控模塊,通過VGA接口來控制屏幕進行顯示。
    發表于 03-28 10:41 ?1971次閱讀
    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次下載
    主站蜘蛛池模板: 亚洲娇小性色xxxx| 九九热这里只有精品视频免费| 白嫩美女直冒白浆| 97视频在线播放| 爱爱好爽好大好紧视频| 国产成人精品男人的天堂网站| 国产精品单位女同事在线| 好嗨哟在线看片免费| 久久免费看少妇高潮A片JA| 欧美动物交ideos| 熟妇内谢69XXXXXA片| 亚洲精品在线影院| 97在线视频免费| 俄罗斯15一16处交| 国拍自产精品福利区| 毛片内射久久久一区| 日日摸天天添天天添无码蜜臀| 亚洲高清一区二区三区电影| 在镜头里被CAO翻了H| YELLOW在线观看高清视频免费 | 国产成人免费视频| 精品动漫国产亚洲AV在线观看| 两个人的视频hd全免费 | 男人插女人动态图| 色欲国产麻豆一精品一AV一免费 | 亚洲国产精品无码中文字满 | 中文字幕久久熟女人妻AV免费| RUN AWAY无删减全集动漫| 国产人妻午夜无码AV天堂| 老师真棒无遮瑕版漫画免费| 日韩精品AV一区二区三区| 亚洲无AV在线中文字幕| www.色小姐| 娇妻归来在线观看免费完整版电影| 木凡的天空在线收听| 午夜阳光影院在线观看视频| 5566精品资源在线播放| 国产福利秒拍weipai.ee| 美女被日出水| 亚洲 中文 自拍 无码| ⅹxx日本护土|