(2)创建lcd.c,并添加以下代码。
#include "lcd.h"
#include "font.h"
#include "delay.h"
void LCD_WriteReg( u16 LCD_Reg, u16 LCD_RegValue )
{
LCD->LCD_REG = LCD_Reg ; //写入寄存器序号
LCD->LCD_RAM = LCD_RegValue ; //写入数据
}
_lcd_dev lcddev ; //管理LCD重要参数
void LCD_SetCursor( u16 Xpos, u16 Ypos )
{
LCD_WriteReg( lcddev.setxcmd, Xpos>>8 ) ;
LCD_WriteReg( lcddev.setxcmd+1, Xpos&0xFF ) ;
LCD_WriteReg( lcddev.setycmd, Ypos>>8 ) ;
LCD_WriteReg( lcddev.setycmd+1, Ypos&0xFF ) ;
}
void LCD_Display_Dir()
{
u16 temp;
lcddev.wramcmd = 0x2C00 ;
lcddev.setxcmd = 0x2A00 ;
lcddev.setycmd = 0x2B00 ;
lcddev.width = 480 ;
lcddev.height = 800 ;
//交换X,Y
if( lcddev.width>lcddev.height )
{
temp = lcddev.width ;
lcddev.width = lcddev.height ;
lcddev.height = temp ;
}
//设置扫描方向
LCD_WriteReg( 0x3600, 0 ) ;
LCD_WriteReg( lcddev.setxcmd, 0 ) ;
LCD_WriteReg( lcddev.setxcmd+1, 0 ) ;
LCD_WriteReg( lcddev.setxcmd+2, ( lcddev.width-1 )>>8 ) ;
LCD_WriteReg( lcddev.setxcmd+3, ( lcddev.width-1 )&0xFF ) ;
LCD_WriteReg( lcddev.setycmd, 0 ) ;
LCD_WriteReg( lcddev.setycmd+1, 0 ) ;
LCD_WriteReg( lcddev.setycmd+2, ( lcddev.height-1 )>>8 ) ;
LCD_WriteReg( lcddev.setycmd+3, ( lcddev.height-1 )&0xFF ) ;
}
void LCD_Init()
{
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 ;
FSMC_Bank1->BTCR[ 6 ] = 0x00000000 ;
FSMC_Bank1->BTCR[ 7 ] = 0x00000000 ;
FSMC_Bank1E->BWTR[ 6 ] = 0x00000000 ;
FSMC_Bank1->BTCR[ 6 ] |= 1<<12 ; //存储器写使能
FSMC_Bank1->BTCR[ 6 ] |= 1<<14 ; //读写使用不同的时序
FSMC_Bank1->BTCR[ 6 ] |= 1<<4 ; //存储器数据宽度为16bit
FSMC_Bank1->BTCR[ 7 ] |= 0<<28 ; //模式A
FSMC_Bank1->BTCR[ 7 ] |= 1<<0 ; //地址建立时间为2个HCLK 1/36M=27ns
FSMC_Bank1->BTCR[ 7 ] |= 0xF<<8 ; //数据保存时间为16个HCLK
FSMC_Bank1E->BWTR[ 6 ] |= 0<<28 ; //模式A
FSMC_Bank1E->BWTR[ 6 ] |= 0<<0 ; //地址建立时间为1个HCLK
FSMC_Bank1E->BWTR[ 6 ] |= 3<<8 ; //数据保存时间为4个HCLK
FSMC_Bank1->BTCR[ 6 ] |= 1<<0 ; //使能BANK1区域4
delay_ms( 50 ) ; //delay 50 ms
LCD_WriteReg( 0xF000, 0x55 ) ;
LCD_WriteReg( 0xF001, 0xAA ) ;
LCD_WriteReg( 0xF002, 0x52 ) ;
LCD_WriteReg( 0xF003, 0x08 ) ;
LCD_WriteReg( 0xF004, 0x01 ) ;
//AVDD Set AVDD 5.2V
LCD_WriteReg( 0xB000, 0x0D ) ;
LCD_WriteReg( 0xB001, 0x0D ) ;
LCD_WriteReg( 0xB002, 0x0D ) ;
//AVDD ratio
LCD_WriteReg( 0xB600, 0x34 ) ;
LCD_WriteReg( 0xB601, 0x34 ) ;
LCD_WriteReg( 0xB602, 0x34 ) ;
//AVEE -5.2V
LCD_WriteReg( 0xB100, 0x0D ) ;
LCD_WriteReg( 0xB101, 0x0D ) ;
LCD_WriteReg( 0xB102, 0x0D ) ;
//AVEE ratio
LCD_WriteReg( 0xB700, 0x34 ) ;
LCD_WriteReg( 0xB701, 0x34 ) ;
LCD_WriteReg( 0xB702, 0x34 ) ;
//VCL -2.5V
LCD_WriteReg( 0xB200, 0x00 ) ;
LCD_WriteReg( 0xB201, 0x00 ) ;
LCD_WriteReg( 0xB202, 0x00 ) ;
//VCL ratio
LCD_WriteReg( 0xB800, 0x24 ) ;
LCD_WriteReg( 0xB801, 0x24 ) ;
LCD_WriteReg( 0xB802, 0x24 ) ;
//VGH 15V
LCD_WriteReg( 0xBF00, 0x01 ) ;
LCD_WriteReg( 0xB300, 0x0F ) ;
LCD_WriteReg( 0xB301, 0x0F ) ;
LCD_WriteReg( 0xB302, 0x0F ) ;
//VGH ratio
LCD_WriteReg( 0xB900, 0x34 ) ;
LCD_WriteReg( 0xB901, 0x34 ) ;
LCD_WriteReg( 0xB902, 0x34 ) ;
//VGL_REG -10V
LCD_WriteReg( 0xB500, 0x08 ) ;
LCD_WriteReg( 0xB501, 0x08 ) ;
LCD_WriteReg( 0xB502, 0x08 ) ;
LCD_WriteReg( 0xC200, 0x03 ) ;
//VGLX ratio
LCD_WriteReg( 0xBA00, 0x24 ) ;
LCD_WriteReg( 0xBA01, 0x24 ) ;
LCD_WriteReg( 0xBA02, 0x24 ) ;
//VGMP/VGSP 4.5V/0V
LCD_WriteReg( 0xBC00, 0x00 ) ;
LCD_WriteReg( 0xBC01, 0x78 ) ;
LCD_WriteReg( 0xBC02, 0x00 ) ;
//VGMN/VGSN -4.5V/0V
LCD_WriteReg( 0xBD00, 0x00 ) ;
LCD_WriteReg( 0xBD01, 0x78 ) ;
LCD_WriteReg( 0xBD02, 0x00 ) ;
//VCOM
LCD_WriteReg( 0xBE00, 0x00 ) ;
LCD_WriteReg( 0xBE01, 0x64 ) ;
//Gamma Setting
LCD_WriteReg( 0xD100, 0x00 ) ;
LCD_WriteReg( 0xD101, 0x33 ) ;
LCD_WriteReg( 0xD102, 0x00 ) ;
LCD_WriteReg( 0xD103, 0x34 ) ;
LCD_WriteReg( 0xD104, 0x00 ) ;
LCD_WriteReg( 0xD105, 0x3A ) ;
LCD_WriteReg( 0xD106, 0x00 ) ;
LCD_WriteReg( 0xD107, 0x4A ) ;
LCD_WriteReg( 0xD108, 0x00 ) ;
LCD_WriteReg( 0xD109, 0x5C ) ;
LCD_WriteReg( 0xD10A, 0x00 ) ;
LCD_WriteReg( 0xD10B, 0x81 ) ;
LCD_WriteReg( 0xD10C, 0x00 ) ;
LCD_WriteReg( 0xD10D, 0xA6 ) ;
LCD_WriteReg( 0xD10E, 0x00 ) ;
LCD_WriteReg( 0xD10F, 0xE5 ) ;
LCD_WriteReg( 0xD110, 0x01 ) ;
LCD_WriteReg( 0xD111, 0x13 ) ;
LCD_WriteReg( 0xD112, 0x01 ) ;
LCD_WriteReg( 0xD113, 0x54 ) ;
LCD_WriteReg( 0xD114, 0x01 ) ;
LCD_WriteReg( 0xD115, 0x82 ) ;
LCD_WriteReg( 0xD116, 0x01 ) ;
LCD_WriteReg( 0xD117, 0xCA ) ;
LCD_WriteReg( 0xD118, 0x02 ) ;
LCD_WriteReg( 0xD119, 0x00 ) ;
LCD_WriteReg( 0xD11A, 0x02 ) ;
LCD_WriteReg( 0xD11B, 0x01 ) ;
LCD_WriteReg( 0xD11C, 0x02 ) ;
LCD_WriteReg( 0xD11D, 0x34 ) ;
LCD_WriteReg( 0xD11E, 0x02 ) ;
LCD_WriteReg( 0xD11F, 0x67 ) ;
LCD_WriteReg( 0xD120, 0x02 ) ;
LCD_WriteReg( 0xD121, 0x84 ) ;
LCD_WriteReg( 0xD122, 0x02 ) ;
LCD_WriteReg( 0xD123, 0xA4 ) ;
LCD_WriteReg( 0xD124, 0x02 ) ;
LCD_WriteReg( 0xD125, 0xB7 ) ;
LCD_WriteReg( 0xD126, 0x02 ) ;
LCD_WriteReg( 0xD127, 0xCF ) ;
LCD_WriteReg( 0xD128, 0x02 ) ;
LCD_WriteReg( 0xD129, 0xDE ) ;
LCD_WriteReg( 0xD12A, 0x02 ) ;
LCD_WriteReg( 0xD12B, 0xF2 ) ;
LCD_WriteReg( 0xD12C, 0x02 ) ;
LCD_WriteReg( 0xD12D, 0xFE ) ;
LCD_WriteReg( 0xD12E, 0x03 ) ;
LCD_WriteReg( 0xD12F, 0x10 ) ;
LCD_WriteReg( 0xD130, 0x03 ) ;
LCD_WriteReg( 0xD131, 0x33 ) ;
LCD_WriteReg( 0xD132, 0x03 ) ;
LCD_WriteReg( 0xD133, 0x6D ) ;
LCD_WriteReg( 0xD200, 0x00 ) ;
LCD_WriteReg( 0xD201, 0x33 ) ;
LCD_WriteReg( 0xD202, 0x00 ) ;
LCD_WriteReg( 0xD203, 0x34 ) ;
LCD_WriteReg( 0xD204, 0x00 ) ;
LCD_WriteReg( 0xD205, 0x3A ) ;
LCD_WriteReg( 0xD206, 0x00 ) ;
LCD_WriteReg( 0xD207, 0x4A ) ;
LCD_WriteReg( 0xD208, 0x00 ) ;
LCD_WriteReg( 0xD209, 0x5C ) ;
LCD_WriteReg( 0xD20A, 0x00 ) ;
LCD_WriteReg( 0xD20B, 0x81 ) ;
LCD_WriteReg( 0xD20C, 0x00 ) ;
LCD_WriteReg( 0xD20D, 0xA6 ) ;
LCD_WriteReg( 0xD20E, 0x00 ) ;
LCD_WriteReg( 0xD20F, 0xE5 ) ;
LCD_WriteReg( 0xD210, 0x01 ) ;
LCD_WriteReg( 0xD211, 0x13 ) ;
LCD_WriteReg( 0xD212, 0x01 ) ;
LCD_WriteReg( 0xD213, 0x54 ) ;
LCD_WriteReg( 0xD214, 0x01 ) ;
LCD_WriteReg( 0xD215, 0x82 ) ;
LCD_WriteReg( 0xD216, 0x01 ) ;
LCD_WriteReg( 0xD217, 0xCA ) ;
LCD_WriteReg( 0xD218, 0x02 ) ;
LCD_WriteReg( 0xD219, 0x00 ) ;
LCD_WriteReg( 0xD21A, 0x02 ) ;
LCD_WriteReg( 0xD21B, 0x01 ) ;
LCD_WriteReg( 0xD21C, 0x02 ) ;
LCD_WriteReg( 0xD21D, 0x34 ) ;
LCD_WriteReg( 0xD21E, 0x02 ) ;
LCD_WriteReg( 0xD21F, 0x67 ) ;
LCD_WriteReg( 0xD220, 0x02 ) ;
LCD_WriteReg( 0xD221, 0x84 ) ;
LCD_WriteReg( 0xD222, 0x02 ) ;
LCD_WriteReg( 0xD223, 0xA4 ) ;
LCD_WriteReg( 0xD224, 0x02 ) ;
LCD_WriteReg( 0xD225, 0xB7 ) ;
LCD_WriteReg( 0xD226, 0x02 ) ;
LCD_WriteReg( 0xD227, 0xCF ) ;
LCD_WriteReg( 0xD228, 0x02 ) ;
LCD_WriteReg( 0xD229, 0xDE ) ;
LCD_WriteReg( 0xD22A, 0x02 ) ;
LCD_WriteReg( 0xD22B, 0xF2 ) ;
LCD_WriteReg( 0xD22C, 0x02 ) ;
LCD_WriteReg( 0xD22D, 0xFE ) ;
LCD_WriteReg( 0xD22E, 0x03 ) ;
LCD_WriteReg( 0xD22F, 0x10 ) ;
LCD_WriteReg( 0xD230, 0x03 ) ;
LCD_WriteReg( 0xD231, 0x33 ) ;
LCD_WriteReg( 0xD232, 0x03 ) ;