#include "lcd.h"
#include "stdlib.h"
#include "font.h"
#include "usart.h"
//2.4/2.8寸TFT液晶驱动(ILI9328/ILI9325/RM68021/ILI9320 Verision)
//STM32-SS开发板
//TFTLCD 驱动代码
//正点原子@ALIENTEK,ZHOUMI
//2010/6/5
//画笔颜色,背景颜色
u16 POINT_COLOR = 0x0000,BACK_COLOR = 0xFFFF;
u16 DeviceCode;
//写寄存器函数
void LCD_WR_REG(u8 data)
{
LCD_RS=0;//写地址
LCD_CS=0;
DATAOUT(data);
LCD_WR=0;
LCD_WR=1;
LCD_CS=1;
}
//写寄存器
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); //写入要读的寄存器号
GPIOE->CRL=0X88888888; //PE0-7 上拉输入
GPIOE->CRH=0X88888888; //PE8-15 上拉输入
GPIOE->ODR=0XFFFF; //全部输出高
LCD_RS=1;
LCD_CS=0;
//读取数据(读寄存器时,并不需要读2次)
LCD_RD=0;
LCD_RD=1;
t=DATAIN;
LCD_CS=1;
GPIOE->CRL=0X33333333; //PE0-7 上拉输出
GPIOE->CRH=0X33333333; //PE8-15 上拉输出
GPIOE->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地址
GPIOE->CRL=0X88888888; //PE0-7 上拉输入
GPIOE->CRH=0X88888888; //PE8-15 上拉输入
GPIOE->ODR=0XFFFF; //全部输出高
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;
GPIOE->CRL=0X33333333; //PE0-7 上拉输出
GPIOE->CRH=0X33333333; //PE8-15 上拉输出
GPIOE->ODR=0XFFFF; //全部输出高
if(DeviceCode==0X4531)return t;//4531驱动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);//关闭显示
}
//LCD延时函数 10MS
void Delay (u32 nCount)
{
volatile int i;
for (i=0;i<nCount*100;i++);
}
//设置光标位置
//Xpos:横坐标
//Ypos:纵坐标
__inline void LCD_SetCursor(u8 Xpos, u16 Ypos)
{
LCD_WriteReg(R32, Xpos);
LCD_WriteReg(R33, Ypos);
}
//画点
//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<<6;//先使能外设PORTE时钟,数据口PE
RCC->APB2ENR|=1<<4;//先使能外设PORTC时钟,控制线
//RCC->APB2ENR|=1<<0; //开启辅助时钟
//AFIO->MAPR&=~(7<<24); //关闭JTAG,保留SWD
//AFIO->MAPR|=2<<24; //关闭JTAG,保留SWD
//PORTC0 1 2 3 5 复用推挽输出
GPIOC->CRL&=0XFF0F0000;
GPIOC->CRL|=0X00303333;
GPIOC->ODR|=0X0002F;
//PORTE 推挽输出
GPIOE->CRH=0X33333333;
GPIOE->CRL=0X33333333;
GPIOE->ODR=0XFFFF;
Delay(5); // delay 50 ms
LCD_WriteReg(0x0000,0x0001);
Delay(5); // delay 50 ms
DeviceCode = LCD_ReadReg(0x0000);
printf("ID:%d\n",DeviceCode);
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 (0x0003,(1<<12)|(3<<4)|(0<<3) )
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(5);
LCD_WriteReg(0x0010,0x1590);
LCD_WriteReg(0x0011,0x0227);
Delay(5);
LCD_WriteReg(0x0012,0x009c);
Delay(5);
LCD_WriteReg(0x0013,0x1900);
LCD_WriteReg(0x0029,0x0023);
LCD_WriteReg(0x002b,0x000e);
Delay(5);
LCD_WriteReg(0x0020,0x0000);
LCD_WriteReg(0x0021,0x013f);
Delay(5);
//伽马校正
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(5);
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,0x0000);
LCD_WriteReg(0x01,0x0100); //Driver Output Contral.
LCD_WriteReg(0x02,0x0700); //LCD Driver Waveform Contral.
LCD_WriteReg(0x03,0x1030);//Entry Mode Set.
//LCD_WriteReg(0x03,0x1018); //Entry Mode Set.
LCD_WriteReg(0x04,0x0000); //Scalling Contral.
LCD_WriteReg(0x08,0x0202); //Display Contral 2.(0x0207)
LCD_WriteReg(0x09,0x0000); //Display Contral 3.(0x0000)
LCD_WriteReg(0x0a,0x0000); //Frame Cycle Contal.(0x0000)
LCD_WriteReg(0x0c,(1<<0)); //Extern Display Interface Contral 1.(0x0000)
LCD_WriteReg(0x0d,0x0000); //Frame Maker Position.
LCD_WriteReg(0x0f,0x0000); //Extern Display Interface Contral 2.
Delay(5);
LCD_WriteReg(0x07,0x0101); //Display Contral.
Delay(5);
LCD_WriteReg(0x10,(1<<12)|(0<<8)|(1<<7)|(1<<6)|(0<<4)); //Po
评论0