lcd_c
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
#include "lcd.h"
extern unsigned char Asc2_1608[1520];
static int BACK_COLOR=0x0000, POINT_COLOR=0xF800;
void LCD_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStruct;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO|RCC_APB2Periph_GPIOB,ENABLE);
RCC_APB1PeriphClockCmd (RCC_APB1Periph_SPI2, ENABLE);//使用SPI2
GPIO_InitStructure.GPIO_Pin = LCD_DOC|LCD_RST;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = LCD_LED;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = LCD_CS;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = LCD_SCK|LCD_SDI;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = LCD_SDO;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOB, &GPIO_InitStructure);
Reset_LCD_LED();
Set_LCD_DOC();
Set_LCD_RST();
Set_LCD_CS();
SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;//波特率
SPI_InitStruct.SPI_CPHA = SPI_CPHA_2Edge;
SPI_InitStruct.SPI_CPOL = SPI_CPOL_High;
SPI_InitStruct.SPI_CRCPolynomial = 7;
SPI_InitStruct.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStruct.SPI_Direction = SPI_Direction_1Line_Tx ;
SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStruct.SPI_Mode = SPI_Mode_Master;
SPI_InitStruct.SPI_NSS = SPI_NSS_Soft;
SPI_Init(SPI2, &SPI_InitStruct);
SPI_Cmd(SPI2, ENABLE);
Reset_LCD_RST();
Delay_Ms(200);
Set_LCD_RST();
Delay_Ms(200);
Reset_LCD_CS();
LCD_Writ_Reg(0xCB);
LCD_Writ_Data(0x39);
LCD_Writ_Data(0x2C);
LCD_Writ_Data(0x00);
LCD_Writ_Data(0x34);
LCD_Writ_Data(0x02);
LCD_Writ_Reg(0xCF);
LCD_Writ_Data(0x00);
LCD_Writ_Data(0XC1);
LCD_Writ_Data(0X30);
LCD_Writ_Reg(0xE8);
LCD_Writ_Data(0x85);
LCD_Writ_Data(0x00);
LCD_Writ_Data(0x78);
LCD_Writ_Reg(0xEA);
LCD_Writ_Data(0x00);
LCD_Writ_Data(0x00);
LCD_Writ_Reg(0xED);
LCD_Writ_Data(0x64);
LCD_Writ_Data(0x03);
LCD_Writ_Data(0X12);
LCD_Writ_Data(0X81);
LCD_Writ_Reg(0xF7);
LCD_Writ_Data(0x20);
LCD_Writ_Reg(0xC0);//Power control
LCD_Writ_Data(0x23);//VRH[5:0]
LCD_Writ_Reg(0xC1);//Power control
LCD_Writ_Data(0x10);//SAP[2:0];BT[3:0]
LCD_Writ_Reg(0xC5);//VCM control
LCD_Writ_Data(0x3e);//对比度调节
LCD_Writ_Data(0x28);
LCD_Writ_Reg(0xC7);//VCM control2
LCD_Writ_Data(0x86);//--
LCD_Writ_Reg(0x36);//Memory Access Control
LCD_Writ_Data(0xE8);//C8 //48 68竖屏//28 E8 横屏
LCD_Writ_Reg(0x3A);
LCD_Writ_Data(0x55);
LCD_Writ_Reg(0xB1);
LCD_Writ_Data(0x00);
LCD_Writ_Data(0x18);
LCD_Writ_Reg(0xB6);//Display Function Control
LCD_Writ_Data(0x08);
LCD_Writ_Data(0x82);
LCD_Writ_Data(0x27);
LCD_Writ_Reg(0xF2);//3Gamma Function Disable
LCD_Writ_Data(0x00);
LCD_Writ_Reg(0x26);//Gamma curve selected
LCD_Writ_Data(0x01);
LCD_Writ_Reg(0xE0);//Set Gamma
LCD_Writ_Data(0x0F);
LCD_Writ_Data(0x31);
LCD_Writ_Data(0x2B);
LCD_Writ_Data(0x0C);
LCD_Writ_Data(0x0E);
LCD_Writ_Data(0x08);
LCD_Writ_Data(0x4E);
LCD_Writ_Data(0xF1);
LCD_Writ_Data(0x37);
LCD_Writ_Data(0x07);
LCD_Writ_Data(0x10);
LCD_Writ_Data(0x03);
LCD_Writ_Data(0x0E);
LCD_Writ_Data(0x09);
LCD_Writ_Data(0x00);
LCD_Writ_Reg(0XE1);//Set Gamma
LCD_Writ_Data(0x00);
LCD_Writ_Data(0x0E);
LCD_Writ_Data(0x14);
LCD_Writ_Data(0x03);
LCD_Writ_Data(0x11);
LCD_Writ_Data(0x07);
LCD_Writ_Data(0x31);
LCD_Writ_Data(0xC1);
LCD_Writ_Data(0x48);
LCD_Writ_Data(0x08);
LCD_Writ_Data(0x0F);
LCD_Writ_Data(0x0C);
LCD_Writ_Data(0x31);
LCD_Writ_Data(0x36);
LCD_Writ_Data(0x0F);
LCD_Writ_Reg(0x11);//Exit Sleep
Delay_Ms(200);
LCD_Writ_Reg(0x29);//Display on
LCD_Writ_Reg(0x2c);
LCD_Clear(0x0000);
}
void LCD_Writ_Byte(char Writ_Byte)
{
while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET);//等待待发送数据寄存器已空
SPI_I2S_SendData(SPI2, Writ_Byte);
/*
unsigned char Byte_X;
for(Byte_X=0;Byte_X<8;Byte_X++)
{
if(Writ_Byte&0x80)
{
Set_LCD_SDI();
}
else
{
Reset_LCD_SDI();
}
Reset_LCD_SCK();
Writ_Byte=Writ_Byte<<1;
Set_LCD_SCK();
}
*/
}
void LCD_Writ_Reg(char Writ_Reg)
{
Reset_LCD_DOC();
LCD_Writ_Byte(Writ_Reg);
}
void LCD_Writ_Data(char Writ_Data)
{
Set_LCD_DOC();
LCD_Writ_Byte(Writ_Data);
}
void LCD_Writ_LData(int Writ_LData)
{
LCD_Writ_Data(Writ_LData>>8);
LCD_Writ_Data(Writ_LData);
}
void Addr_Set(unsigned int Set_X, unsigned int Set_Y, unsigned int Set_Xi, unsigned int Set_Yi)
{
LCD_Writ_Reg(0x2A);
LCD_Writ_LData(Set_X);
LCD_Writ_LData(Set_Xi);
LCD_Writ_Reg(0x2B);
LCD_Writ_LData(Set_Y);
LCD_Writ_LData(Set_Yi);
LCD_Writ_Reg(0x2C);
}
void LCD_Clear(u16 Clear_Color)
{
u16 Clear_W,Clear_H;
Addr_Set(0,0,LCD_W-1,LCD_H-1);
for(Clear_W=0;Clear_W
for(Clear_H=0;Clear_H
LCD_Writ_LData(Clear_Color);
}
}
}
void LCD_Draw_Point(u16 Point_X, u16 Point_Y)
{
Addr_Set(Point_X, Point_Y, Point_X, Point_Y);//设置光标位置
LCD_Writ_LData(POINT_COLOR);
}
void LCD_Show_Char(u16 Char_X, u16 Char_Y, u8 Char_Num, u8 Char_Mode)
{
u8 Char_Temp;
u8 Point_H,Point_W;
u16 Char_X0=Char_X;
u16 Color_Temp=POINT_COLOR;
if(Char_X>LCD_W-16||Char_Y>LCD_H-16) return;
Char_Num=Char_Num-' ';
Addr_Set(Char_
X, Char_Y, Char_X+8-1, Char_Y+16-1);
if(!Char_Mode)//非叠加方式
{
for(Point_H=0;Point_H<16;Point_H++)
{
Char_Temp=Asc2_1608[(u16)Char_Num*16+Point_H]; //调用1608字体
for(Point_W=0;Point_W<8;Point_W++)
{
if(Char_Temp&0x01) POINT_COLOR=Color_Temp;
else POINT_COLOR=BACK_COLOR;
LCD_Writ_LData(POINT_COLOR);
Char_Temp>>=1;
Char_X++;
}
Char_X=Char_X0;
Char_Y++;
}
}
else//叠加方式
{
for(Point_H=0;Point_H<16;Point_H++)
{
Char_Temp=Asc2_1608[(u16)Char_Num*16+Point_H]; //调用1608字体
for(Point_W=0;Point_W<8;Point_W++)
{
if(Char_Temp&0x01)LCD_Draw_Point(Char_X+Point_W,Char_Y+Point_H);//画一个点
Char_Temp>>=1;
}
}
}
POINT_COLOR=Color_Temp;
}
u32 Num_Con(u8 Con_N,u8 Con_L)
{
u32 Con_Result=1;
while(Con_L--) Con_Result*=Con_N;
return Con_Result;
}
void LCD_Show_Num(u16 Num_X,u16 Num_Y,u32 Num_Val,u8 Num_Len)
{
u8 Len_B,B_Result;
u8 End_Show=0;
Num_Val=(u16)Num_Val;
for(Len_B=0;Len_B
B_Result=(Num_Val/Num_Con(10,Num_Len-Len_B-1))%10;
if(End_Show==0&&Len_B<(Num_Len-1))
{
if(B_Result==0)
{
LCD_Show_Char(Num_X+8*Len_B, Num_Y, '0', 0);
continue;
}
else End_Show=1;
}
LCD_Show_Char(Num_X+8*Len_B,Num_Y,B_Result+48,0);
}
}
void LCD_Show_Str(u16 str_x, u16 str_y, const u8 buff[])
{
while(*buff!='\0')
{
if(str_x>LCD_W-16)
{str_x=0;str_y+=16;}
if(str_y>LCD_H-16)
{str_y=str_x=0;}
LCD_Show_Char(str_x,str_y,*buff,0);
str_x+=8;
buff++;
}
}