#include "lcd.h"
#include "font.h"
//画笔颜色,背景颜色
u16 POINT_COLOR = 0x0000,BACK_COLOR = 0xFFFF;
u16 DeviceCode;
#if LCD_FAST_IO==1 //快速IO
//写寄存器函数
void LCD_WR_REG(u8 data)
{
LCD_RS_CLR;//写地址
LCD_CS_CLR;
DATAOUT(data);
LCD_WR_CLR;
LCD_WR_SET;
LCD_CS_SET;
}
#else//正常IO
//写寄存器函数
void LCD_WR_REG(u8 data)
{
LCD_RS=0;//写地址
LCD_CS=0;
DATAOUT(data);
LCD_WR=0;
LCD_WR=1;
LCD_CS=1;
}
#endif
//写寄存器
void LCD_WriteReg(u8 LCD_Reg, u16 LCD_RegValue)
{
LCD_WR_REG(LCD_Reg);
LCD_WR_DATA(LCD_RegValue);
}
//读寄存器
u16 LCD_ReadReg(u8 LCD_Reg)
{
u16 t;
LCD_WR_REG(LCD_Reg); //写入要读的寄存器号
GPIOB->CRL=0X88888888; //PB0-7 上拉输入
GPIOB->CRH=0X88888888; //PB8-15 上拉输入
GPIOB->ODR=0XFFFF; //全部输出高
#if LCD_FAST_IO==1 //快速IO
LCD_RS_SET;
LCD_CS_CLR;
//读取数据(读寄存器时,并不需要读2次)
LCD_RD_CLR;
delay_us(5);//FOR 8989,延时5us
LCD_RD_SET;
t=DATAIN;
LCD_CS_SET;
#else
LCD_RS=1;
LCD_CS=0;
//读取数据(读寄存器时,并不需要读2次)
LCD_RD=0;
LCD_RD=1;
t=DATAIN;
LCD_CS=1;
#endif
GPIOB->CRL=0X33333333; //PB0-7 上拉输出
GPIOB->CRH=0X33333333; //PB8-15 上拉输出
GPIOB->ODR=0XFFFF; //全部输出高
return t;
}
//开始写GRAM
void LCD_WriteRAM_Prepare(void)
{
LCD_WR_REG(R34);
}
//LCD写GRAM
void LCD_WriteRAM(u16 RGB_Code)
{
LCD_WR_DATA(RGB_Code);//写十六位GRAM
}
//从ILI93xx读出的数据为GBR格式,而我们写入的时候为RGB格式。
//通过该函数转换
//c:GBR格式的颜色值
//返回值:RGB格式的颜色值
u16 LCD_BGR2RGB(u16 c)
{
u16 r,g,b,rgb;
b=(c>>0)&0x1f;
g=(c>>5)&0x3f;
r=(c>>11)&0x1f;
rgb=(b<<11)+(g<<5)+(r<<0);
return(rgb);
}
//读取个某点的颜色值
//x:0~239
//y:0~319
//返回值:此点的颜色
u16 LCD_ReadPoint(u16 x,u16 y)
{
u16 t;
if(x>=LCD_W||y>=LCD_H)return 0;//超过了范围,直接返回
LCD_SetCursor(x,y);
LCD_WR_REG(R34); //选择GRAM地址
GPIOB->CRL=0X88888888; //PB0-7 上拉输入
GPIOB->CRH=0X88888888; //PB8-15 上拉输入
GPIOB->ODR=0XFFFF; //全部输出高
#if LCD_FAST_IO==1 //快速IO
LCD_RS_SET;
LCD_CS_CLR;
//读取数据(读GRAM时,需要读2次)
LCD_RD_CLR;
LCD_RD_SET;
delay_us(2);//FOR 9320,延时2us
//dummy READ
LCD_RD_CLR;
delay_us(2);//FOR 8989,延时2us
LCD_RD_SET;
t=DATAIN;
LCD_CS_SET;
#else
LCD_RS=1;
LCD_CS=0;
//读取数据(读GRAM时,需要读2次)
LCD_RD=0;
LCD_RD=1;
//dummy READ
LCD_RD=0;
LCD_RD=1;
t=DATAIN;
LCD_CS=1;
#endif
GPIOB->CRL=0X33333333; //PB0-7 上拉输出
GPIOB->CRH=0X33333333; //PB8-15 上拉输出
GPIOB->ODR=0XFFFF; //全部输出高
if(DeviceCode==0X4531||DeviceCode==0X8989||DeviceCode==0XB505)return t;//这几种IC直接返回颜色值
else return LCD_BGR2RGB(t);
}
//LCD开启显示
void LCD_DisplayOn(void)
{
LCD_WriteReg(R7, 0x0173); //26万色显示开启
}
//LCD关闭显示
void LCD_DisplayOff(void)
{
LCD_WriteReg(R7, 0x0);//关闭显示
}
//设置光标位置
//Xpos:横坐标
//Ypos:纵坐标
__inline void LCD_SetCursor(u16 Xpos, u16 Ypos)
{
#if USE_HORIZONTAL==1
if(DeviceCode==0X8989)
{
LCD_WriteReg(0X4E, Ypos);
LCD_WriteReg(0X4F, 319-Xpos);
}else
{
LCD_WriteReg(R32, Ypos);
LCD_WriteReg(R33, 319-Xpos);
}
#else
if(DeviceCode==0X8989)
{
LCD_WriteReg(0X4E, Xpos);
LCD_WriteReg(0X4F, Ypos);
}else
{
LCD_WriteReg(R32, Xpos);
LCD_WriteReg(R33, Ypos);
}
#endif
}
//画点
//x:0~239
//y:0~319
//POINT_COLOR:此点的颜色
void LCD_DrawPoint(u16 x,u16 y)
{
LCD_SetCursor(x,y);//设置光标位置
LCD_WR_REG(R34);//开始写入GRAM
LCD_WR_DATA(POINT_COLOR);
}
//初始化lcd
//该初始化函数可以初始化各种ILI93XX液晶,但是其他函数是基于ILI9320的!!!
//在其他型号的驱动芯片上没有测试!
void LCD_Init(void)
{
RCC->APB2ENR|=1<<3;//先使能外设PORTB时钟
RCC->APB2ENR|=1<<4;//先使能外设PORTC时钟
RCC->APB2ENR|=1<<0; //开启辅助时钟
JTAG_Set(SWD_ENABLE); //开启SWD
//PORTC6~10复用推挽输出
GPIOC->CRH&=0XFFFFF000;
GPIOC->CRH|=0X00000333;
GPIOC->CRL&=0X00FFFFFF;
GPIOC->CRL|=0X33000000;
GPIOC->ODR|=0X07C0;
//PORTB 推挽输出
GPIOB->CRH=0X33333333;
GPIOB->CRL=0X33333333;
GPIOB->ODR=0XFFFF;
delay_ms(50); // delay 50 ms
LCD_WriteReg(0x0000,0x0001);
delay_ms(50); // delay 50 ms
DeviceCode = LCD_ReadReg(0x0000);
printf(" LCD ID:%x\n",DeviceCode); //打印LCD ID
if(DeviceCode==0x9325||DeviceCode==0x9328)//ILI9325
{
LCD_WriteReg(0x00e7,0x0010);
LCD_WriteReg(0x0000,0x0001);//开启内部时钟
LCD_WriteReg(0x0001,0x0100);
LCD_WriteReg(0x0002,0x0700);//电源开启
//LCD_WriteReg(0x0003,(1<<3)|(1<<4) ); //65K RGB
//DRIVE TABLE(寄存器 03H)
//BIT3=AM BIT4:5=ID0:1
//AM ID0 ID1 FUNCATION
// 0 0 0 R->L D->U
// 1 0 0 D->U R->L
// 0 1 0 L->R D->U
// 1 1 0 D->U L->R
// 0 0 1 R->L U->D
// 1 0 1 U->D R->L
// 0 1 1 L->R U->D 正常就用这个.
// 1 1 1 U->D L->R
LCD_WriteReg(0x0003,(1<<12)|(3<<4)|(0<<3) );//65K
LCD_WriteReg(0x0004,0x0000);
LCD_WriteReg(0x0008,0x0207);
LCD_WriteReg(0x0009,0x0000);
LCD_WriteReg(0x000a,0x0000);//display setting
LCD_WriteReg(0x000c,0x0001);//display setting
LCD_WriteReg(0x000d,0x0000);//0f3c
LCD_WriteReg(0x000f,0x0000);
//电源配置
LCD_WriteReg(0x0010,0x0000);
LCD_WriteReg(0x0011,0x0007);
LCD_WriteReg(0x0012,0x0000);
LCD_WriteReg(0x0013,0x0000);
delay_ms(50);
LCD_WriteReg(0x0010,0x1590);
LCD_WriteReg(0x0011,0x0227);
delay_ms(50);
LCD_WriteReg(0x0012,0x009c);
delay_ms(50);
LCD_WriteReg(0x0013,0x1900);
LCD_WriteReg(0x0029,0x0023);
LCD_WriteReg(0x002b,0x000e);
delay_ms(50);
LCD_WriteReg(0x0020,0x0000);
LCD_WriteReg(0x0021,0x013f);
delay_ms(50);
//伽马校正
LCD_WriteReg(0x0030,0x0007);
LCD_WriteReg(0x0031,0x0707);
LCD_WriteReg(0x0032,0x0006);
LCD_WriteReg(0x0035,0x0704);
LCD_WriteReg(0x0036,0x1f04);
LCD_WriteReg(0x0037,0x0004);
LCD_WriteReg(0x0038,0x0000);
LCD_WriteReg(0x0039,0x0706);
LCD_WriteReg(0x003c,0x0701);
LCD_WriteReg(0x003d,0x000f);
delay_ms(50);
LCD_WriteReg(0x0050,0x0000); //水平GRAM起始位置
LCD_WriteReg(0x0051,0x00ef); //水平GRAM终止位置
LCD_WriteReg(0x0052,0x0000); //垂直GRAM起始位置
LCD_WriteReg(0x0053,0x013f); //垂直GRAM终止位置
LCD_WriteReg(0x0060,0xa700);
LCD_WriteReg(0x0061,0x0001);
LCD_WriteReg(0x006a,0x0000);
LCD_WriteReg(0x0080,0x0000);
LCD_WriteReg(0x0081,0x0000);
LCD_WriteReg(0x0082,0x0000);
LCD_WriteReg(0x0083,0x0000);
LCD_WriteReg(0x0084,0x0000);
LCD_WriteReg(0x0085,0x0000);
LCD_WriteReg(0x0090,0x0010);
LCD_WriteReg(0x0092,0x0000);
LCD_WriteReg(0x0093,0x0003);
LCD_WriteReg(0x0095,0x0110);
LCD_WriteReg(0x0097,0x0000);
LCD_WriteReg(0x0098,0x0000);
//开启显示设置
LCD_WriteReg(0x0007,0x0133);
LCD_WriteReg(0x0020,0x0000);
LCD_WriteReg(0x0021,0x013f);
}else if(DeviceCode==0x9320||DeviceCode==0x9300)
{
LCD_WriteReg(0x00,0