#include "LCD2.h"
void LCD2_GPIO_Init()
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOD | RCC_APB2Periph_AFIO, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //口线翻转速度为50MHz
GPIO_Init(GPIOB, &GPIO_InitStructure);
//8位数据输出
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //口线翻转速度为50MHz
GPIO_Init(GPIOD, &GPIO_InitStructure);
}
//////////////////////////////////////////////////////////////////
//最底层数据传输函数
//////////////////////////////////////////////////////////////////
//写命令
void Write_Cmd(unsigned char DH,unsigned char DL)
{
LCD2_CS=0;
LCD2_RS=0;
DataPort=DH;
LCD2_RW=0;
LCD2_RW=1;
DataPort=DL;
LCD2_RW=0;
LCD2_RW=1;
LCD2_CS=1;
}
//写数据 双8位
void Write_Data(unsigned char DH,unsigned char DL)
{
LCD2_CS=0;
LCD2_RS=1;
DataPort=DH;
LCD2_RW=0;
LCD2_RW=1;
DataPort=DL;
LCD2_RW=0;
LCD2_RW=1;
LCD2_CS=1;
}
//写数据 双8位
void Write_Data2(unsigned char DH,unsigned char DL)
{
DataPort=DH;
LCD2_RW=0;
LCD2_RW=1;
DataPort=DL;
LCD2_RW=0;
LCD2_RW=1;
}
//////////////////////////////////////////////////////////////////
//调用上面最底层实现稍高层写命令和数据函数
//////////////////////////////////////////////////////////////////
/*----------------------------------------------------------------
写命令、写数据
输入参数:x 需要输入的命令 16位
y 需要输入的数据 16位
----------------------------------------------------------------*/
void Write_Cmd_Data (unsigned char x,unsigned int y)
{
unsigned char m,n;
m=y>>8;
n=y;
Write_Cmd(0x00,x);
Write_Data(m,n);
}
/*----------------------------------------------------------------
写16位数据
----------------------------------------------------------------*/
void Write_Data_U16(unsigned int y)
{
unsigned char m,n;
m=y>>8;
n=y;
Write_Data2(m,n);
}
/*----------------------------------------------------------------
液晶初始化
----------------------------------------------------------------*/
void LCD2_Init(void)
{
LCD2_CS=1;
DELAY_MS(5);
LCD2_RES=0;
DELAY_MS(5);
LCD2_RES=1;
DELAY_MS(50);
Write_Cmd_Data(0x0001,0x0100);
Write_Cmd_Data(0x0002,0x0700);
Write_Cmd_Data(0x0003,0x1030);
Write_Cmd_Data(0x0004,0x0000);
Write_Cmd_Data(0x0008,0x0207);
Write_Cmd_Data(0x0009,0x0000);
Write_Cmd_Data(0x000A,0x0000);
Write_Cmd_Data(0x000C,0x0000);
Write_Cmd_Data(0x000D,0x0000);
Write_Cmd_Data(0x000F,0x0000);
//power on sequence VGHVGL
Write_Cmd_Data(0x0010,0x0000);
Write_Cmd_Data(0x0011,0x0007);
Write_Cmd_Data(0x0012,0x0000);
Write_Cmd_Data(0x0013,0x0000);
//vgh
Write_Cmd_Data(0x0010,0x1290);
Write_Cmd_Data(0x0011,0x0227);
//DELAY_MS(100);
//vregiout
Write_Cmd_Data(0x0012,0x001d); //0x001b
//DELAY_MS(100);
//vom amplitude
Write_Cmd_Data(0x0013,0x1500);
//DELAY_MS(100);
//vom H
Write_Cmd_Data(0x0029,0x0018);
Write_Cmd_Data(0x002B,0x000D);
//gamma
Write_Cmd_Data(0x0030,0x0004);
Write_Cmd_Data(0x0031,0x0307);
Write_Cmd_Data(0x0032,0x0002);// 0006
Write_Cmd_Data(0x0035,0x0206);
Write_Cmd_Data(0x0036,0x0408);
Write_Cmd_Data(0x0037,0x0507);
Write_Cmd_Data(0x0038,0x0204);//0200
Write_Cmd_Data(0x0039,0x0707);
Write_Cmd_Data(0x003C,0x0405);// 0504
Write_Cmd_Data(0x003D,0x0F02);
//ram
Write_Cmd_Data(0x0050,0x0000);
Write_Cmd_Data(0x0051,0x00EF);
Write_Cmd_Data(0x0052,0x0000);
Write_Cmd_Data(0x0053,0x013F);
Write_Cmd_Data(0x0060,0xA700);
Write_Cmd_Data(0x0061,0x0001);
Write_Cmd_Data(0x006A,0x0000);
//
Write_Cmd_Data(0x0080,0x0000);
Write_Cmd_Data(0x0081,0x0000);
Write_Cmd_Data(0x0082,0x0000);
Write_Cmd_Data(0x0083,0x0000);
Write_Cmd_Data(0x0084,0x0000);
Write_Cmd_Data(0x0085,0x0000);
//
Write_Cmd_Data(0x0090,0x0010);
Write_Cmd_Data(0x0092,0x0600);
Write_Cmd_Data(0x0093,0x0003);
Write_Cmd_Data(0x0095,0x0110);
Write_Cmd_Data(0x0097,0x0000);
Write_Cmd_Data(0x0098,0x0000);
Write_Cmd_Data(0x0007,0x0133);
// Write_Cmd_Data(0x0022);//
}
/*----------------------------------------------------------------
设置坐标
----------------------------------------------------------------*/
/*----------------------------------------------------------------
全局变量
----------------------------------------------------------------*/
#define WINDOW_XADDR_START 0x0050 // Horizontal Start Address Set
#define WINDOW_XADDR_END 0x0051 // Horizontal End Address Set
#define WINDOW_YADDR_START 0x0052 // Vertical Start Address Set
#define WINDOW_YADDR_END 0x0053 // Vertical End Address Set
#define GRAM_XADDR 0x0020 // GRAM Horizontal Address Set
#define GRAM_YADDR 0x0021 // GRAM Vertical Address Set
#define GRAMWR 0x0022 // memory write
void LCD_SetPos(unsigned int x0,unsigned int x1,unsigned int y0,unsigned int y1)
{
Write_Cmd_Data(WINDOW_XADDR_START,x0);
Write_Cmd_Data(WINDOW_XADDR_END,x1);
Write_Cmd_Data(WINDOW_YADDR_START,y0);
Write_Cmd_Data(WINDOW_YADDR_END,y1);
Write_Cmd_Data(GRAM_XADDR,x0);
Write_Cmd_Data(GRAM_YADDR,y0);
Write_Cmd (0x00,0x22);//LCD_WriteCMD(GRAMWR);
}
/*----------------------------------------------------------------
显示RGB颜色
输入参数:x0,y0 起始坐标
x1,y1 结束坐标
Color 背景颜色
----------------------------------------------------------------*/
void Show_RGB (unsigned int x0,unsigned int x1,unsigned int y0,unsigned int y1,unsigned int Color)
{
unsigned int i,j;
LCD_SetPos(x0,x1,y0,y1);
LCD2_CS=0;
LCD2_RS=1;
// for (i=y0;i<=y1;i++)
// {
// for (j=x0;j<=x1;j++)
// Write_Data_U16(Color);
// }
for (i=0;i<=(y1-y0+1)*(x1-x0+1);i+=32)
{
Write_Data_U16(Color);
Write_Data_U16(Color);
Write_Data_U16(Color);
Write_Data_U16(Color);
Write_Data_U16(Color);
Write_Data_U16(Color);
Write_Data_U16(Color);
Write_Data_U16(Color);
Write_Data_U16(Color);
Write_Data_U16(Color);
Write_Data_U16(Color);
Write_Data_U16(Color);
Write_Data_U16(Color);
Write_Data_U16(Color);
Write_Data_U16(Color);
Write_Data_U16(Color);
Write_Data_U16(Color);
Write_Data_U16(Color);
Write_Data_U16(Color);
Write_Data_U16(Color);
Write_Data_U16(Color);
Write_Data_U16(Color);
Write_Data_U16(Color);
Write_Data_U16(Color);
Write_Data_U16(Color);
Write_Data_U16(Color);
Write_Data_U16(Color);
Write_Data_U16(Color);
Write_Data_U16(Color);
Write_Data_U16(Color);
Write_Data_U16(Color);
Write_Data_U16(Color);
}
LCD2_CS=1;
}
void Delay_ms(u16 time)
{
u16 i=0;
while(time--)
{
i=12000;
while(i--);
}
}