?
一、環境介紹
LCD顯示屏:正點原子的TFT 3.5寸顯示屏
二、運行效果
?
?
?
三、功能介紹
因為代碼原來是在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編程原理。
?
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; //點亮背光
}
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++)>
五、按鍵驅動代碼示例
拼圖的時候,通過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;
}
六、拼圖的邏輯代碼
分隔圖片: 這里是按照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}};
循環里處理圖片移動邏輯:
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;
}
}
}
}
完整工程代碼下載地址:https://download.csdn.net/download/xiaolong1126626497/20706318
審核編輯:符乾江
-
STM32
+關注
關注
2270文章
10895瀏覽量
355750 -
代碼
+關注
關注
30文章
4779瀏覽量
68525
發布評論請先 登錄
相關推薦
評論