利用彩屏顯示漢字、字符和字符串,需要先完成彩屏的驅(qū)動(dòng)程序,然后在驅(qū)動(dòng)程序的基礎(chǔ)上再編寫應(yīng)用程序。
本文引用地址:http://m.butianyuan.cn/article/201611/322858.htm彩屏的驅(qū)動(dòng)程序如果寫好的話,就可以一直使用了,精力主要集中在應(yīng)用程序的編寫就可以了,但是移植的話,要移植驅(qū)動(dòng)程序。其實(shí)移植也只是改變那些很底層的靠近處理器的那部分代碼。
代碼如下,注釋比較詳細(xì),看懂了基本就可以了。
驅(qū)動(dòng)程序的頭文件LCD_driver.h如下:
#defineucharunsignedchar
#defineuintunsignedint
#defineBus_16//16位數(shù)據(jù)模式,如果使用8位模式,請(qǐng)注釋此語(yǔ)句,如果使用16位模式,請(qǐng)打開(kāi)此句
#defineLCD_DataPortHP1//高8位數(shù)據(jù)口,8位模式下只使用高8位
#defineLCD_DataPortLP0//低8位數(shù)據(jù)口,8位模式下低8位可以不接線
#defineLCD_WRGPIO_Pin_2//WR引腳定義P2^5
#defineLCD_RSGPIO_Pin_1//RS引腳定義P2^6
#defineLCD_CSGPIO_Pin_0//CS引腳定義P2^7
#defineLCD_RSTGPIO_Pin_11//RST引腳定義P3^3
#defineLCD_RDGPIO_Pin_8//RD引腳定義P3^2
#defineLCD_SIZE_X240//彩屏的分辨率為240*320
#defineLCD_SIZE_Y320
externuintcolors[];
voidLCD_Init(void);//LCD初始化函數(shù)
voidLCD_ShowString(uintx,uinty,uintFor_color,uintBk_color,char*p);//寫字符串函數(shù)
voidLCD_ShowChar(uintx,uinty,uintFor_color,uintBk_color,charch);//寫字符函數(shù)
voidChineseChar(uintx,uinty,intsize,uintFor_color,uintBk_color,charc);//寫漢字函數(shù)
voiddelayms(intcount);
//voidLCD_Writ_Bus(charVH,charVL);//8位模式下的總線寫函數(shù)
voidLCD_Writ_Bus(uintVH,uintVL);//16位模式下的總線寫函數(shù)
voidLCD_Write_COM(charVH,charVL);//寫命令函數(shù)
voidLCD_Write_DATA(charVH,charVL);//寫數(shù)據(jù)函數(shù)
voidPant(charVH,charVL);//涂滿彩屏函數(shù)
voidAddress_set(unsignedintx1,unsignedinty1,unsignedintx2,unsignedinty2);//設(shè)置地址范圍函數(shù)
驅(qū)動(dòng)程序Lcd.c文件如下:
#include"stm32f10x_lib.h"
#include"LCD_driver.h"
#include"chinese.h"
#include"char.h"
#defineLCD_rest(x)x?GPIO_SetBits(GPIOA,LCD_RST):GPIO_ResetBits(GPIOA,LCD_RST)//自己定義位操作函數(shù)
#defineLCD_rd(x)x?GPIO_SetBits(GPIOA,LCD_RD):GPIO_ResetBits(GPIOA,LCD_RD)
#defineLCD_rs(x)x?GPIO_SetBits(GPIOB,LCD_RS):GPIO_ResetBits(GPIOB,LCD_RS)
#defineLCD_wr(x)x?GPIO_SetBits(GPIOB,LCD_WR):GPIO_ResetBits(GPIOB,LCD_WR)
#defineLCD_cs(x)x?GPIO_SetBits(GPIOB,LCD_CS):GPIO_ResetBits(GPIOB,LCD_CS)
uintcolors[]=
{
0xf800,0x07e0,0x001f,0xffe0,0x0000,0x07ff,0xf81f,0xffff//顏色數(shù)據(jù)是16位的
};
voidLCD_Init(void)//lcd初始化
{
LCD_rest(1);
delayms(5);
LCD_rest(0);
delayms(5);
LCD_rest(1);
delayms(5);
LCD_cs(0);//打開(kāi)片選使能
//
LCD_Write_COM(0x00,0xE5);LCD_Write_DATA(0x78,0xF0);//setSRAMinternaltiming
LCD_Write_COM(0x00,0x01);LCD_Write_DATA(0x01,0x00);//setSSandSMbit
LCD_Write_COM(0x00,0x02);LCD_Write_DATA(0x07,0x00);//set1lineinversion
LCD_Write_COM(0x00,0x03);LCD_Write_DATA(0x10,0x30);//setGRAMwritedirectionandBGR=1.
LCD_Write_COM(0x00,0x04);LCD_Write_DATA(0x00,0x00);//Resizeregister
LCD_Write_COM(0x00,0x08);LCD_Write_DATA(0x02,0x07);//setthebackporchandfrontporch
LCD_Write_COM(0x00,0x09);LCD_Write_DATA(0x00,0x00);//setnon-displayarearefreshcycleISC[3:0]
LCD_Write_COM(0x00,0x0A);LCD_Write_DATA(0x00,0x00);//FMARKfunction
LCD_Write_COM(0x00,0x0C);LCD_Write_DATA(0x00,0x00);//RGBinterfacesetting
LCD_Write_COM(0x00,0x0D);LCD_Write_DATA(0x00,0x00);//FramemarkerPosition
LCD_Write_COM(0x00,0x0F);LCD_Write_DATA(0x00,0x00);//RGBinterfacepolarity
//
LCD_Write_COM(0x00,0x10);LCD_Write_DATA(0x00,0x00);//SAP,BT[3:0],AP,DSTB,SLP,STB
LCD_Write_COM(0x00,0x11);LCD_Write_DATA(0x00,0x07);//DC1[2:0],DC0[2:0],VC[2:0]
LCD_Write_COM(0x00,0x12);LCD_Write_DATA(0x00,0x00);//VREG1OUTvoltage
LCD_Write_COM(0x00,0x13);LCD_Write_DATA(0x00,0x00);//VDV[4:0]forVCOMamplitude
LCD_Write_COM(0x00,0x07);LCD_Write_DATA(0x00,0x01);
delayms(50);//Dis-chargecapacitorpowervoltage
LCD_Write_COM(0x00,0x10);LCD_Write_DATA(0x10,0x90);//1490//SAP,BT[3:0],AP,DSTB,SLP,STB
LCD_Write_COM(0x00,0x11);LCD_Write_DATA(0x02,0x27);//DC1[2:0],DC0[2:0],VC[2:0]
delayms(50);//Delay50ms
LCD_Write_COM(0x00,0x12);LCD_Write_DATA(0x00,0x1F);//001C//Internalreferencevoltage=Vci;
delayms(50);//Delay50ms
LCD_Write_COM(0x00,0x13);LCD_Write_DATA(0x15,0x00);//0x1000//1400SetVDV[4:0]forVCOMamplitude1A00
LCD_Write_COM(0x00,0x29);LCD_Write_DATA(0x00,0x27);//0x0012//001aSetVCM[5:0]forVCOMH//0x00250034
LCD_Write_COM(0x00,0x2B);LCD_Write_DATA(0x00,0x0D);//SetFrameRate000C
delayms(50);//Delay50ms
LCD_Write_COM(0x00,0x20);LCD_Write_DATA(0x00,0x00);//GRAMhorizontalAddress
LCD_Write_COM(0x00,0x21);LCD_Write_DATA(0x00,0x00);//GRAMVerticalAddress
//-----------AdjusttheGammaCurve----------//
LCD_Write_COM(0x00,0x30);LCD_Write_DATA(0x00,0x00);
LCD_Write_COM(0x00,0x31);LCD_Write_DATA(0x07,0x07);
LCD_Write_COM(0x00,0x32);LCD_Write_DATA(0x03,0x07);
LCD_Write_COM(0x00,0x35);LCD_Write_DATA(0x02,0x00);
LCD_Write_COM(0x00,0x36);LCD_Write_DATA(0x00,0x08);//0207
LCD_Write_COM(0x00,0x37);LCD_Write_DATA(0x00,0x04);//0306
LCD_Write_COM(0x00,0x38);LCD_Write_DATA(0x00,0x00);//0102
LCD_Write_COM(0x00,0x39);LCD_Write_DATA(0x07,0x07);//0707
LCD_Write_COM(0x00,0x3C);LCD_Write_DATA(0x00,0x02);//0702
LCD_Write_COM(0x00,0x3D);LCD_Write_DATA(0x1D,0x04);//1604
//------------------SetGRAMarea---------------//
LCD_Write_COM(0x00,0x50);LCD_Write_DATA(0x00,0x00);//HorizontalGRAMStartAddress
LCD_Write_COM(0x00,0x51);LCD_Write_DATA(0x00,0xEF);//HorizontalGRAMEndAddress
LCD_Write_COM(0x00,0x52);LCD_Write_DATA(0x00,0x00);//VerticalGRAMStartAddress
LCD_Write_COM(0x00,0x53);LCD_Write_DATA(0x01,0x3F);//VerticalGRAMStartAddress
LCD_Write_COM(0x00,0x60);LCD_Write_DATA(0xA7,0x00);//GateScanLine
LCD_Write_COM(0x00,0x61);LCD_Write_DATA(0x00,0x01);//NDL,VLE,REV
LCD_Write_COM(0x00,0x6A);LCD_Write_DATA(0x00,0x00);//setscrollingline
//--------------PartialDisplayControl---------//
LCD_Write_COM(0x00,0x80);LCD_Write_DATA(0x00,0x00);
LCD_Write_COM(0x00,0x81);LCD_Write_DATA(0x00,0x00);
LCD_Write_COM(0x00,0x82);LCD_Write_DATA(0x00,0x00);
LCD_Write_COM(0x00,0x83);LCD_Write_DATA(0x00,0x00);
LCD_Write_COM(0x00,0x84);LCD_Write_DATA(0x00,0x00);
LCD_Write_COM(0x00,0x85);LCD_Write_DATA(0x00,0x00);
//--------------PanelControl-------------------//
LCD_Write_COM(0x00,0x90);LCD_Write_DATA(0x00,0x10);
LCD_Write_COM(0x00,0x92);LCD_Write_DATA(0x06,0x00);
LCD_Write_COM(0x00,0x07);LCD_Write_DATA(0x01,0x33);//262KcoloranddisplayON
LCD_cs(1);//關(guān)閉片選使能
}
voiddelayms(intcount)//
{
inti,j;
for(i=0;ifor(j=0;j<100;j++);
}
#ifdefBus_16//條件編譯-16位數(shù)據(jù)模式
//并行寫入數(shù)據(jù)把數(shù)據(jù)分為高8位VH和低8位VL
voidLCD_Writ_Bus(uintVH,uintVL)//并行數(shù)據(jù)寫入函數(shù)
{
//LCD_DataPortH=VH;//高位P1口
//屏蔽高8位將數(shù)據(jù)送到PA0-PA7
GPIOA->BSRR=VH&0x00ff;
GPIOA->BRR=(~VH)&0x00ff;
//GPIO_SetBits(GPIOA,VH&0x00ff);
//GPIO_ResetBits(GPIOA,(~VH&0x00ff));
//LCD_DataPortL=VL;//低位P0口
//屏蔽低8位將數(shù)據(jù)送到PB8-PB15
GPIOB->BSRR=(VL)<<8&0xff00;
GPIOB->BRR=((~VL)<<8)&0xff00;
LCD_wr(0);
LCD_wr(1);
}
評(píng)論