?
一、環(huán)境介紹
LCD顯示屏:正點(diǎn)原子的TFT 3.5寸顯示屏
二、運(yùn)行效果
?
?
?
三、功能介紹
因?yàn)榇a原來是在2.8寸屏上設(shè)計(jì)的,但是2.8寸屏摔碎了,現(xiàn)在手上只有塊3.5寸的LCD屏。就把代碼移植到3.5寸屏上顯示了,目前運(yùn)行的效果可以看上面的效果圖。
拼圖游戲在小時(shí)候還是經(jīng)常玩,玩法也比較簡單,這里就使用STM32設(shè)計(jì)一個(gè)拼圖小游戲分享出來一起學(xué)習(xí)。
目前游戲是通過開發(fā)板上4按鍵進(jìn)行控制圖片的移動,完成最終的拼圖效果,如果想要在上面擴(kuò)充其他游戲功能都還是比較容易。
當(dāng)然,設(shè)計(jì)編寫這個(gè)款小游戲不僅僅是為了玩游戲這么簡單,主要是為了通過設(shè)計(jì)游戲的過程學(xué)習(xí)STM32編程、LCD的編程、按鍵、圖片等等很多編程知識。
做拼圖這款游戲主要的硬件就是LCD顯示屏和幾個(gè)按鍵,沒有其他外設(shè),如果自己有其他STM32最小系統(tǒng)板有LCD顯示屏的話,完全可以把代碼移植過去運(yùn)行。當(dāng)前的代碼兼容正點(diǎn)原子戰(zhàn)艦開發(fā)板,可以直接在正點(diǎn)原子戰(zhàn)艦直接編譯下載運(yùn)行。其他的開發(fā)板上運(yùn)行也是一樣的道理,底層替換自己LCD畫點(diǎn)函數(shù)即可。
目前的拼圖的游戲工程里圖片是提前通過取模軟件取好模之后把數(shù)據(jù)放在工程里的,如果想要動態(tài)更加靈活的切換拼圖的圖片,可以加上SD卡和FATFS文件系統(tǒng),把圖片放在SD卡,通過按鍵選擇指定圖片切割進(jìn)行拼圖。
代碼分為3個(gè)部分:
1. LCD屏驅(qū)動代碼部分
2. 按鍵驅(qū)動代碼部分
3. 拼圖的邏輯代碼部分
四、LCD屏驅(qū)動代碼
4.1 LCD介紹
這款LCD顯示屏的驅(qū)動芯片型號是NT35310,時(shí)序支持8080時(shí)序,普通的MCU可以采用IO模擬8080時(shí)序驅(qū)動,有硬件接口的可以采用硬件接口驅(qū)動。STM32F103ZET6的FSMC是可以輸出8080時(shí)序,為了提高刷屏速度,這里代碼就采用FSMC接口來驅(qū)動LCD屏。照著LCD的手冊指令集,自己編寫一遍LCD的初始化代碼,可以深入學(xué)習(xí)一遍LCD編程原理。
?
4.2 LCD顯示屏的初始化代碼示例
/*
函數(shù)功能:寫寄存器函數(shù)
參 數(shù):regval:寄存器值
*/
void LcdWriteReg(u16 regval)
{
LCD_REG=regval;//寫入要寫的寄存器序號
}
/*
函數(shù)功能:寫LCD數(shù)據(jù)
函數(shù)參數(shù):data:要寫入的值
*/
void LcdWriteData(u16 data)
{
LCD_RAM=data;
}
/*
函數(shù)功能:讀LCD數(shù)據(jù)
返 回 值:讀到的值
*/
u16 LcdReadData(void)
{
vu16 ram; //防止被優(yōu)化
ram=LCD_RAM;
return ram;
}
/*
函數(shù)功能:讀寄存器
函數(shù)參數(shù):LCD_Reg:寄存器地址
返 回 值:讀到的數(shù)據(jù)
*/
u16 LcdReadReg(u16 LCD_Reg)
{
LcdWriteReg(LCD_Reg); //寫入要讀的寄存器序號
DelayUs(5);
return LcdReadData(); //返回讀到的值
}
/*
函數(shù)功能:開始寫GRAM
*/
void LcdWriteRAM_Prepare(void)
{
LCD_REG=0X2C;
}
/*
函數(shù)功能:LCD寫GRAM
函數(shù)參數(shù):RGB_Code:顏色值
*/
void LcdWriteRAM(u16 RGB_Code)
{
LCD_RAM = RGB_Code;//寫十六位GRAM
}
/*
函數(shù)功能:設(shè)置光標(biāo)位置
函數(shù)參數(shù):
Xpos:橫坐標(biāo)
Ypos:縱坐標(biāo)
*/
void LcdSetCursor(u16 Xpos, u16 Ypos)
{
LcdWriteReg(0X2A);
LcdWriteData(Xpos>>8);
LcdWriteData(Xpos&0XFF);
LcdWriteReg(0X2B);
LcdWriteData(Ypos>>8);
LcdWriteData(Ypos&0XFF);
}
/*
功能:讀取個(gè)某點(diǎn)的顏色值
參數(shù):x,y:坐標(biāo)
返回值:此點(diǎn)的顏色
*/
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(); //實(shí)際坐標(biāo)顏色
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時(shí)鐘
RCC->APB2ENR|=1<<3; //使能PORTB時(shí)鐘
RCC->APB2ENR|=1<<5; //使能PORTD時(shí)鐘
RCC->APB2ENR|=1<<6; //使能PORTE時(shí)鐘
RCC->APB2ENR|=1<<8; //使能PORTG時(shí)鐘
GPIOB->CRL&=0XFFFFFFF0; //PB0 推挽輸出 背光
GPIOB->CRL|=0X00000003;
//PORTD復(fù)用推挽輸出
GPIOD->CRH&=0X00FFF000;
GPIOD->CRH|=0XBB000BBB;
GPIOD->CRL&=0XFF00FF00;
GPIOD->CRL|=0X00BB00BB;
//PORTE復(fù)用推挽輸出
GPIOE->CRH&=0X00000000;
GPIOE->CRH|=0XBBBBBBBB;
GPIOE->CRL&=0X0FFFFFFF;
GPIOE->CRL|=0XB0000000;
//PORTG12復(fù)用推挽輸出
GPIOG->CRH&=0XFFF0FFFF;
GPIOG->CRH|=0X000B0000;
GPIOG->CRL&=0XFFFFFFF0;//PG0->RS
GPIOG->CRL|=0X0000000B;
//寄存器清零
//bank1有NE1~4,每一個(gè)有一個(gè)BCR+TCR,所以總共八個(gè)寄存器。
//這里我們使用NE4 ,也就對應(yīng)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; //讀寫使用不同的時(shí)序
FSMC_Bank1->BTCR[6]|=1<<4; //存儲器數(shù)據(jù)寬度為16bit
//操作BTR寄存器
//讀時(shí)序控制寄存器
FSMC_Bank1->BTCR[7]|=0<<28; //模式A
FSMC_Bank1->BTCR[7]|=1<<0; //地址建立時(shí)間(ADDSET)為2個(gè)HCLK 1/36M=27ns(實(shí)際>200ns)
//因?yàn)橐壕?qū)動IC的讀數(shù)據(jù)的時(shí)候,速度不能太快,尤其對1289這個(gè)IC。
FSMC_Bank1->BTCR[7]|=0XF<<8; //數(shù)據(jù)保存時(shí)間為16個(gè)HCLK
//寫時(shí)序控制寄存器
FSMC_Bank1E->BWTR[6]|=0<<28; //模式A
FSMC_Bank1E->BWTR[6]|=0<<0; //地址建立時(shí)間(ADDSET)為1個(gè)HCLK
//4個(gè)HCLK(HCLK=72M)因?yàn)橐壕?qū)動IC的寫信號脈寬,最少也得50ns。72M/4=24M=55ns
FSMC_Bank1E->BWTR[6]|=3<<8; //數(shù)據(jù)保存時(shí)間為4個(gè)HCLK
//使能BANK1,區(qū)域4
FSMC_Bank1->BTCR[6]|=1<<0; //使能BANK1,區(qū)域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; //點(diǎn)亮背光
}
4.3 LCD相關(guān)的繪圖函數(shù)代碼示例
/*
函數(shù)功能:畫點(diǎn)函數(shù)
參 數(shù):x,y坐標(biāo)
color顏色值
*/
void LcdDrawPoint(u16 x,u16 y,u16 color)
{
LcdSetCursor(x,y); //設(shè)置光標(biāo)位置
LcdWriteRAM_Prepare(); //開始寫入GRAM
LCD_RAM=color;
}
/*
函數(shù)功能:清屏函數(shù)
參 數(shù):color顏色值
*/
void LcdClear(u16 color)
{
u32 i;
LcdSetCursor(0,0); //設(shè)置光標(biāo)位置
LcdWriteRAM_Prepare(); //開始寫入GRAM
for(i=0;i<320*480;i++)LCD_RAM=color;
}
/*
函數(shù)功能:顯示一個(gè)指定大小的漢字
參 數(shù):
x,y :漢字的坐標(biāo)
font:取模數(shù)據(jù)
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);//得到字體一個(gè)字符對應(yīng)點(diǎn)陣集所占的字節(jié)數(shù)
for(t=0;t0)incx=1; //設(shè)置單步方向
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; //選取基本增量坐標(biāo)軸
else distance=delta_y;
for(t=0;t<=distance+1;t++ )//畫線輸出
{
LcdDrawPoint(uRow,uCol,color);//畫點(diǎn)
xerr+=delta_x ;
yerr+=delta_y ;
if(xerr>distance)
{
xerr-=distance;
uRow+=incx;
}
if(yerr>distance)
{
yerr-=distance;
uCol+=incy;
}
}
}
/*
函數(shù)功能:任意角度畫直線
參 數(shù):
w :線段的長度
len:半徑
c :顏色
x,y:坐標(biāo)
*/
#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高位在前
//畫點(diǎn)LCD的像素是16位的,所以需要將2個(gè)8位像素合成16位像素。
p+=2;
}
}
}
;i++)>;t++)>
五、按鍵驅(qū)動代碼示例
拼圖的時(shí)候,通過4個(gè)按鍵控制上下左右方向的,按鍵的代碼如下:
功能 :按鍵的初始化
硬件連接:
KEY1:PA0 --按下是高電平
KEY2:PE2 --按下是低電平
KEY3:PE3 --按下是低電平
KEY4:PE4 --按下是低電平
返回值:無
*/
void KeyInit(void)
{
/*1. 開時(shí)鐘*/
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表示沒有按鍵按下
實(shí)現(xiàn)單次按下模式(上一次按下的按鍵沒有松開,就不做檢測)和連續(xù)按下的模式。
模式:
mode=1; //表示單擊
mode=0; //表示連續(xù)
*/
u8 GetKeyValue(u32 mode)
{
static int stat=0; //保存上一次按鍵的狀態(tài)
if(mode==0)stat=0;
if((KEY1||KEY2==0||KEY3==0||KEY4==0) && stat==0)
{
DelayMs(20); //延時(shí)消抖
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; //判斷所有的按鍵是否已經(jīng)松開
}
return 0;
}
六、拼圖的邏輯代碼
分隔圖片: 這里是按照320*240---2.8寸計(jì)算的
// 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
//顯示位置起始坐標(biāo)。
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}};
循環(huán)里處理圖片移動邏輯:
while(1)
{
//【1】產(chǎn)生隨機(jī)圖片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); //出現(xiàn)新空白
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); //出現(xiàn)新空白
k=record[number][0];
record[number][0]=record[4][0];
record[4][0]=k;
//新空白橫坐標(biāo)
}
}
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); //出現(xiàn)新空白
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); //出現(xiàn)新空白
k=record[number][1];
record[number][1]=record[4][1];
record[4][1]=k;
}
}
}
}
完整工程代碼下載地址:https://download.csdn.net/download/xiaolong1126626497/20706318
審核編輯:符乾江
-
STM32
+關(guān)注
關(guān)注
2266文章
10871瀏覽量
354791 -
代碼
+關(guān)注
關(guān)注
30文章
4747瀏覽量
68348
發(fā)布評論請先 登錄
相關(guān)推薦
評論