#include<reg51.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
#define Type_LCD_Command 0
#define Type_LCD_Data 1
#define Data P0
sbit LCD_RS=P2^5;
sbit LCD_CS=P2^6;
sbit LCD_RST=P2^7;
sbit LCD_RD=P3^2;
sbit LCD_WR=P3^3;
uint Colors[]={0x0000,0x001f,0xf800,0x07e0,0xffff,0x07ff,0xffe0,0xf81f};
//黑、深蓝、红、绿、白、天蓝、黄、紫
void delayms(uint z)//延时1ms
{
uint x,y;
for(x=0;x<z;x++)
for(y=0;y<110;y++);
}
void LCD_Write(uchar type,uchar dat)//写命令(type=0)、数据(type=1)
{
LCD_CS=0;//可以理解成片选
LCD_RS=type;
LCD_WR=0;
Data=dat;//上升沿读走Data
LCD_WR=1;
LCD_CS=1;
}
void LCD_Color_Byte(uint dat)//写颜色字节
{
LCD_CS=0;
LCD_RS=1;
LCD_WR=0;
Data=(uchar)dat;//低八位,强制类型转换
LCD_WR=1;
LCD_WR=0;
Data=(uchar)(dat>>8);//高八位,强制类型转换
LCD_WR=1;
LCD_CS=1;
}
void Reg_Write(uchar reg,uchar dat)//写寄存器,reg为地址,dat为数据
{
LCD_Write(Type_LCD_Command,reg);
LCD_Write(Type_LCD_Data,dat);
}
void LCD_SetRamAdd(uchar xStart,uchar xEnd,uchar yStart,uchar yEnd)//设置当前窗口,具体设置对照芯片资料
{
Reg_Write(0x09,xStart);
Reg_Write(0x10,yStart);
Reg_Write(0x11,xEnd);
Reg_Write(0x12,yEnd);
Reg_Write(0x18,xStart);
Reg_Write(0x19,yStart);
LCD_Write(Type_LCD_Command,0x22);
}
void LCD_Init()//TFT初始化,具体设置对照芯片资料
{
Reg_Write(0x01,0x02);
Reg_Write(0x02,0x12);
Reg_Write(0x03,0x00);
Reg_Write(0x04,0x10);
LCD_SetRamAdd(0,127, 0,159);
delayms(10);
LCD_Color_Byte(0xffff);
Reg_Write(0x05,0x08);
Reg_Write(0x07,0x7f);
Reg_Write(0x08,0x17);
Reg_Write(0x09,0x00);
Reg_Write(0x10,0x00);
Reg_Write(0x11,0x83);
Reg_Write(0x12,0x9f);
Reg_Write(0x17,0x00);
Reg_Write(0x18,0x00);
Reg_Write(0x19,0x00);
Reg_Write(0x06,0xc7);
delayms(10);
}
void LCD_Clear(uchar n)//清屏,以某种颜色填充整个屏幕
{
uint num;
LCD_SetRamAdd(0,127,0,159);
for(num=160*128;num>0;num--)
{
LCD_Color_Byte(Colors[n]);
}
}
void Color_Bar()//显示8个彩条
{
uint i,j;
for(i=0;i<8;i++)
{
LCD_SetRamAdd(0+i*16,15+i*16,0,159);
for(j=160*16;j>0;j--)
{
LCD_Color_Byte(Colors[i]);
}
}
}
void main()
{
int i;
P2=0xff;
P0=0xff;
LCD_Init();
Color_Bar();
while(1);
}