LCD_Driver.h
#ifndef _LCD_DRIVER_H_
#define _LCD_DRIVER_H_
#include "stm32f4xx.h"
#include "sys.h"
#include "delay.h"
#define USE_HORIZONTAL 0
#define USE_HARDWARE_SPI 0
#define LCD_X_SIZE 240
#define LCD_Y_SIZE 320
#if USE_HORIZONTAL
#define X_MAX_PIXEL LCD_Y_SIZE
#define Y_MAX_PIXEL LCD_X_SIZE
#else
#define X_MAX_PIXEL LCD_X_SIZE
#define Y_MAX_PIXEL LCD_Y_SIZE
#endif
#define RED 0xf800
#define GREEN 0x07e0
#define BLUE 0x001f
#define WHITE 0xffff
#define BLACK 0x0000
#define YELLOW 0xFFE0
#define GRAY0 0xEF7D
#define GRAY1 0x8410
#define GRAY2 0x4208
#define LCD_SCL_PORT GPIOE
#define LCD_SCL_PIN GPIO_Pin_4
#define LCD_SCL_CLK_ENA() {RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOE ,ENABLE);}
#define LCD_SDA_PORT GPIOE
#define LCD_SDA_PIN GPIO_Pin_3
#define LCD_SDA_CLK_ENA() {RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOE ,ENABLE);}
#define LCD_RS_PORT GPIOE
#define LCD_RS_PIN GPIO_Pin_2
#define LCD_RS_CLK_ENA() {RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOE ,ENABLE);}
#define LCD_RST_PORT GPIOE
#define LCD_RST_PIN GPIO_Pin_0
#define LCD_RST_CLK_ENA() {RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOE ,ENABLE);}
#define LCD_CS_PORT GPIOE
#define LCD_CS_PIN GPIO_Pin_1
#define LCD_CS_CLK_ENA() {RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOE ,ENABLE);}
#define LCD_SCL_SET() {GPIO_SetBits(LCD_SCL_PORT,LCD_SCL_PIN);}
#define LCD_SDA_SET() {GPIO_SetBits(LCD_SDA_PORT,LCD_SDA_PIN);}
#define LCD_RS_SET() {GPIO_SetBits(LCD_RS_PORT,LCD_RS_PIN);}
#define LCD_RST_SET() {GPIO_SetBits(LCD_RST_PORT,LCD_RST_PIN);}
#define LCD_CS_SET() {GPIO_SetBits(LCD_CS_PORT,LCD_CS_PIN);}
#define LCD_SCL_CLR() {GPIO_ResetBits(LCD_SCL_PORT,LCD_SCL_PIN); }
#define LCD_SDA_CLR() { GPIO_ResetBits(LCD_SDA_PORT,LCD_SDA_PIN);}
#define LCD_RS_CLR() { GPIO_ResetBits(LCD_RS_PORT,LCD_RS_PIN); }
#define LCD_RST_CLR() { GPIO_ResetBits(LCD_RST_PORT,LCD_RST_PIN); }
#define LCD_CS_CLR() { GPIO_ResetBits(LCD_CS_PORT,LCD_CS_PIN);}
void LCD_GPIO_Init(void);
void Lcd_WriteIndex(u8 Index);
void Lcd_WriteData(u8 Data);
void Lcd_WriteReg(u8 Index,u8 Data);
u16 Lcd_ReadReg(u8 LCD_Reg);
void Lcd_Reset(void);
void Lcd_Init(void);
void Lcd_Clear(u16 Color);
void Lcd_SetXY(u16 x,u16 y);
void Gui_DrawPoint(u16 x,u16 y,u16 Data);
unsigned int Lcd_ReadPoint(u16 x,u16 y);
void Lcd_SetRegion(u16 xStar, u16 yStar,u16 xEnd,u16 yEnd);
void Lcd_WriteData_16Bit(u16 Data);
void LCD_ShowCharAsc16(u8 x,u8 y,u16 fc, u16 bc,u8 chr);
void LCD_ShowStringAsc16(u8 x,u8 y,u16 fc, u16 bc,u8 *chr);
void LCD_ShowNumAsc16(u8 x,u8 y,u16 fc, u16 bc,u16 num,u8 len);
void LCD_ShowCharAsc32(u16 x,u16 y,u16 fc, u16 bc,u8 chr);
void LCD_ShowStringAsc32(u16 x,u16 y,u16 fc, u16 bc,u8 *chr);
void LCD_ShowNumAsc32(u16 x,u16 y,u16 fc, u16 bc,u16 num,u8 len);
void LCD_ShowFloatAsc32(u16 x,u16 y,u16 fc, u16 bc,float num,u8 len);
void Gui_DrawFont_Num96(u16 x, u16 y, u16 fc, u16 bc, u16 num);
#endif
LCD_Driver.c
#include "Lcd_Driver.h"
#include "font.h"
#include "stdio.h"
void LCD_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
LCD_SCL_CLK_ENA();
GPIO_InitStructure.GPIO_Pin = LCD_SCL_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(LCD_SCL_PORT, &GPIO_InitStructure);
LCD_SDA_CLK_ENA();
GPIO_InitStructure.GPIO_Pin = LCD_SDA_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(LCD_SDA_PORT, &GPIO_InitStructure);
LCD_RS_CLK_ENA();
GPIO_InitStructure.GPIO_Pin = LCD_RS_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(LCD_RS_PORT, &GPIO_InitStructure);
LCD_RST_CLK_ENA();
GPIO_InitStructure.GPIO_Pin = LCD_RST_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(LCD_RST_PORT, &GPIO_InitStructure);
LCD_CS_CLK_ENA();
GPIO_InitStructure.GPIO_Pin = LCD_CS_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(LCD_CS_PORT, &GPIO_InitStructure);
}
void SPIv_WriteData(unsigned char Data)
{
unsigned char i=0;
for(i=8;i>0;i--)
{
if(Data&0x80)
LCD_SDA_SET();
if((Data&0x80)==0)
LCD_SDA_CLR();
LCD_SCL_CLR();
LCD_SCL_SET();
Data<<=1;
}
}
void Lcd_WriteIndex(u8 Index)
{
LCD_CS_CLR();
LCD_RS_CLR();
SPIv_WriteData(Index);
LCD_CS_SET();
}
void Lcd_WriteData(u8 Data)
{
LCD_CS_CLR();
LCD_RS_SET();
SPIv_WriteData(Data);
LCD_CS_SET();
}
void LCD_WriteReg(u8 Index,u16 Data)
{
Lcd_WriteIndex(Index);
Lcd_WriteData_16Bit(Data);
}
void Lcd_WriteData_16Bit(u16 Data)
{
Lcd_WriteData(Data>>8);
Lcd_WriteData(Data);
}
void Lcd_Reset(void)
{
LCD_RST_CLR();
Delay_ms(100);
LCD_RST_SET();
Delay_ms(50);
}
void Lcd_Init(void)
{
LCD_GPIO_Init();
Lcd_Reset();
Lcd_WriteIndex(0x11);
Lcd_WriteData(0x00);
Lcd_WriteIndex(0xCF);
Lcd_WriteData(0X00);
Lcd_WriteData(0XC1);
Lcd_WriteData(0X30);
Lcd_WriteIndex(0xED);
Lcd_WriteData(0X64);
Lcd_WriteData(0X03);
Lcd_WriteData(0X12);
Lcd_WriteData(0X81);
Lcd_WriteIndex(0xE8);
Lcd_WriteData(0X85);
Lcd_WriteData(0X11);
Lcd_WriteData(0X78);
Lcd_WriteIndex(0xF6);
Lcd_WriteData(0X01);
Lcd_WriteData(0X30);
Lcd_WriteData(0X00);
Lcd_WriteIndex(0xCB);
Lcd_WriteData(0X39);
Lcd_WriteData(0X2C);
Lcd_WriteData(0X00);
Lcd_WriteData(0X34);
Lcd_WriteData(0X05);
Lcd_WriteIndex(0xF7);
Lcd_WriteData(0X20);
Lcd_WriteIndex(0xEA);
Lcd_WriteData(0X00);
Lcd_WriteData(0X00);
Lcd_WriteIndex(0xC0);
Lcd_WriteData(0X20);
Lcd_WriteIndex(0xC1);
Lcd_WriteData(0X11);
Lcd_WriteIndex(0xC5);
Lcd_WriteData(0X31);
Lcd_WriteData(0X3C);
Lcd_WriteIndex(0xC7);
Lcd_WriteData(0XA9);
Lcd_WriteIndex(0x3A);
Lcd_WriteData(0X55);
Lcd_WriteIndex(0x36);
#if USE_HORIZONTAL
Lcd_WriteData(0xE8);
#else
Lcd_WriteData(0x48);
#endif
Lcd_WriteIndex(0xB1);
Lcd_WriteData(0X00);
Lcd_WriteData(0X18);
Lcd_WriteIndex(0xB4);
Lcd_WriteData(0X00);
Lcd_WriteData(0X00);
Lcd_WriteIndex(0xF2);
Lcd_WriteData(0X00);
Lcd_WriteIndex(0x26);
Lcd_WriteData(0X01);
Lcd_WriteIndex(0xE0);
Lcd_WriteData(0X0F);
Lcd_WriteData(0X17);
Lcd_WriteData(0X14);
Lcd_WriteData(0X09);
Lcd_WriteData(0X0C);
Lcd_WriteData(0X06);
Lcd_WriteData(0X43);
Lcd_WriteData(0X75);
Lcd_WriteData(0X36);
Lcd_WriteData(0X08);
Lcd_WriteData(0X13);
Lcd_WriteData(0X05);
Lcd_WriteData(0X10);
Lcd_WriteData(0X0B);
Lcd_WriteData(0X08);
Lcd_WriteIndex(0xE1);
Lcd_WriteData(0X00);
Lcd_WriteData(0X1F);
Lcd_WriteData(0X23);
Lcd_WriteData(0X03);
Lcd_WriteData(0X0E);
Lcd_WriteData(0X04);
Lcd_WriteData(0X39);
Lcd_WriteData(0X25);
Lcd_WriteData(0X4D);
Lcd_WriteData(0X06);
Lcd_WriteData(0X0D);
Lcd_WriteData(0X0B);
Lcd_WriteData(0X33);
Lcd_WriteData(0X37);
Lcd_WriteData(0X0F);
Lcd_WriteIndex(0x29);
}
void Lcd_SetXY(u16 Xpos, u16 Ypos)
{
Lcd_WriteIndex(0x2A);
Lcd_WriteData_16Bit(Xpos);
Lcd_WriteIndex(0x2B);
Lcd_WriteData_16Bit(Ypos);
Lcd_WriteIndex(0x2c);
}
void Lcd_SetRegion(u16 xStar, u16 yStar,u16 xEnd,u16 yEnd)
{
Lcd_WriteIndex(0x2A);
Lcd_WriteData_16Bit(xStar);
Lcd_WriteData_16Bit(xEnd);
Lcd_WriteIndex(0x2B);
Lcd_WriteData_16Bit(yStar);
Lcd_WriteData_16Bit(yEnd);
Lcd_WriteIndex(0x2c);
}
void Gui_DrawPoint(u16 x,u16 y,u16 Data)
{
Lcd_SetXY(x,y);
Lcd_WriteData_16Bit(Data);
}
void Lcd_Clear(u16 Color)
{
unsigned int i;
Lcd_SetRegion(0,0,X_MAX_PIXEL-1,Y_MAX_PIXEL-1);
LCD_CS_CLR();
LCD_RS_SET();
for(i=0;i<X_MAX_PIXEL*Y_MAX_PIXEL;i++)
{
SPIv_WriteData(Color>>8);
SPIv_WriteData(Color);
}
LCD_CS_SET();
}
u16 Lcd_pow(u8 m,u8 n)
{
u16 result=1;
while(n--)result*=m;
return result;
}
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);
}
void LCD_ShowCharAsc16(u8 x,u8 y,u16 fc, u16 bc,u8 chr)
{
unsigned char i,j,k,c;
for(i=0;i<16;i++)
{
for(j=0;j<1;j++)
{
c=*(font816+(chr-' ')*16*1+i*1+j);
for (k=0;k<8;k++)
{
if(c&(0x80>>k)) Gui_DrawPoint(x+j*8+k,y+i,fc);
else {
if (fc!=bc) Gui_DrawPoint(x+j*8+k,y+i,bc);
}
}
}
}
}
u16 LCD_pow(u8 m,u8 n)
{
u16 result=1;
while(n--)result*=m;
return result;
}
void LCD_ShowNumAsc16(u8 x,u8 y,u16 fc, u16 bc,u16 num,u8 len)
{
u8 t,temp;
u8 enshow=0;
for(t=0;t<len;t++)
{
temp=(num/LCD_pow(10,len-t-1))%10;
if(enshow==0&&t<(len-1))
{
if(temp==0)
{
LCD_ShowCharAsc16(x+9*t,y,fc,bc,' ');
continue;
}else enshow=1;
}
LCD_ShowCharAsc16(x+9*t,y,fc,bc,temp+'0');
}
}
void LCD_ShowStringAsc16(u8 x,u8 y,u16 fc, u16 bc,u8 *chr)
{
unsigned char j=0;
while (chr[j]!='\0')
{ LCD_ShowCharAsc16(x,y,fc,bc,chr[j]);
x+=8;
if(x>232){x=0;y+=16;}
j++;
}
}
void LCD_ShowCharAsc32(u16 x,u16 y,u16 fc, u16 bc,u8 chr)
{
unsigned char i,j,k,c;
for(i=0;i<32;i++)
{
for(j=0;j<2;j++)
{
c=*(font1632+(chr-' ')*32*2+i*2+j);
for (k=0;k<8;k++)
{
if(c&(0x80>>k)) Gui_DrawPoint(x+j*8+k,y+i,fc);
else {
if (fc!=bc) Gui_DrawPoint(x+j*8+k,y+i,bc);
}
}
}
}
}
void LCD_ShowNumAsc32(u16 x,u16 y,u16 fc, u16 bc,u16 num,u8 len)
{
u8 t,temp;
u8 enshow=0;
for(t=0;t<len;t++)
{
temp=(num/LCD_pow(10,len-t-1))%10;
if(enshow==0&&t<(len-1))
{
if(temp==0)
{
LCD_ShowCharAsc32(x+17*t,y,fc,bc,' ');
continue;
}else enshow=1;
}
LCD_ShowCharAsc32(x+17*t,y,fc,bc,temp+'0');
}
}
void LCD_ShowStringAsc32(u16 x,u16 y,u16 fc, u16 bc,u8 *chr)
{
unsigned char j=0;
while (chr[j]!='\0')
{
LCD_ShowCharAsc32(x,y,fc,bc,chr[j]);
x+=16;
if(x>232){x=0;y+=32;}
j++;
}
}
void LCD_ShowFloatAsc32(u16 x,u16 y,u16 fc, u16 bc,float num,u8 len)
{
char disbuftemp[9];
switch(len)
{
case 3: sprintf(disbuftemp,"%.3f",num);break;
case 2: sprintf(disbuftemp,"%.2f",num);break;
case 1: sprintf(disbuftemp,"%.1f",num);break;
default: sprintf(disbuftemp,"%.3f",num);break;
}
LCD_ShowStringAsc32(x,y,fc,bc,disbuftemp);
}
void Gui_DrawFont_Num96(u16 x, u16 y, u16 fc, u16 bc, u16 num)
{
unsigned char i,j,k,c;
for(i=0;i<96;i++)
{
for(j=0;j<6;j++)
{
c=*(zi96+num*96*6+i*6+j);
for (k=0;k<8;k++)
{
if(c&(0x80>>k)) Gui_DrawPoint(x+j*8+k,y+i,fc);
else {
if (fc!=bc) Gui_DrawPoint(x+j*8+k,y+i,bc);
}
}
}
}
}
void Color_Test(void)
{
u8 i=1;
Lcd_Clear(GRAY0);
Delay_ms(1200);
while(i--)
{
Lcd_Clear(WHITE); Delay_ms(500);
Lcd_Clear(BLACK); Delay_ms(500);
Lcd_Clear(RED); Delay_ms(500);
Lcd_Clear(GREEN); Delay_ms(500);
Lcd_Clear(BLUE); Delay_ms(500);
}
}