小弟写的程序是这样的
#include "STC12C5A.h"
#include "intrins.h"
#define u8 unsigned char
#define u16 unsigned short
#define u32 unsigned long
//定义颜色
#define WHITE 0xFFFF
#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define MAGENTA 0xF81F
#define GREEN 0x07E0
#define CYAN 0x7FFF
#define YELLOW 0xFFE0
#define ORANGE 0x05bf
#define PINK 0xFB56//粉红
#define PURPLE 0xF8FF//紫色
#define BLUEVIOLET 0X895C//紫罗蓝
#define SKYBLUE 0X867F //天蓝
#define BROWN 0XBC40 //棕色
#define BRRED 0XFC07 //棕红色
#define GRAY 0X8430 //灰色
#define LGRAY 0XC618 //浅灰色
//定义接口
***it TFT_RS=P1^0; //寄存器选择:0:写命令;1:写数据
***it TFT_RD=P1^1; //读使能,低电平有效
***it BLED=P1^2; //背光使能脚,高电平有效
***it INT=P1^3; //触摸中断脚
***it LE=P1^4; //锁存时钟脚????????????????????????????????????????
***it F_CS=P1^5; //dataflash 片选
***it SD_CS=P1^6; //SD卡片选
***it TFT_CS=P0^0; //屏片选,低电平有效??????????
***it TFT_WR=P0^1; //写使能,低电平有效
***it TFT_RES=P0^2; //复位脚,低电平有效
***it MISO=P0^3; //SPI主入从出
***it MOSI=P0^4; //SPI主出从入
***it SCLK=P0^5; //SPI时钟
***it TP_CS=P0^6; //触摸芯片片选
//us延时
void delay_us(unsigned int t)
{
while(t--)
{
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
}
}
//延时函数(24M晶振下延时1ms)
void delay_ms(unsigned int time)
{
// unsigned int t;
for(;time>0;time--)
{
/*t = 1500;
while(t--); */
delay_us(1000);
}
}
//////////////////////////////////////////////////////////
void DataToWrite(u16 dat)
{
LE=1;
P2=dat;
LE=0;
delay_ms(2);
P2=dat>>8;
delay_ms(2);
}
//写指令
void TFT_WriteCmd(u16 cmd)
{
TFT_CS=0; //CS=0???????????????????????????????
TFT_RS=0; //RS=0,写指令
DataToWrite(cmd);
TFT_WR=0; //WR为低电平时允许写
TFT_WR=1; //WR为高时禁止写
TFT_CS=1; //?????????????????????????????????
}
//写数据
void TFT_WriteDat(u16 dat)
{
TFT_CS=0; //CS=0???????????????????????????????
TFT_RS=1; //RS=0,写数据
DataToWrite(dat);
TFT_WR=0; //WR为低电平时允许写
TFT_WR=1; //WR为高时禁止写
TFT_CS=1; //???????????????????????????????????
}
//写寄存器
void TFT_WriteReg(u16 addr,u16 dat)
{
TFT_WriteCmd(addr);
TFT_WriteDat(dat);
}
//开始写
void TFT_WriteStart(void)
{
TFT_CS = 0;
TFT_RS = 0;
DataToWrite(0x0022);
TFT_WR = 0;
TFT_WR = 1;
TFT_RS = 1;
}
//写结束
void TFT_WriteEnd(void)
{
TFT_CS = 1;
}
//写数据
void TFT_WriteData(unsigned int val)
{
DataToWrite(val);
TFT_WR = 0;
TFT_WR = 1;
}
//设置区域
void TFT_SetRam(u16 startx,u16 starty,u16 endx,u16 endy)
{
TFT_WriteReg(0x0050,startx);
TFT_WriteReg(0x0051,endx);
TFT_WriteReg(0x0052,starty);
TFT_WriteReg(0x0053,endy);
TFT_WriteReg(0x0020,startx); //GRAM AC
TFT_WriteReg(0x0021,starty);
//TFT_WriteReg(0x0016,0x00C8);在初始化时已经写过,不必再写
//TFT_WriteCmd(0x0022); 可以不用吗?
}
//清屏
void TFT_Clear(u16 Color)
{
u32 temp;
TFT_SetRam(0,0,239,319); //设置全屏区域
//TFT_WriteStart();
TFT_CS=0;
TFT_RS=0;
DataToWrite(0x0022);
TFT_WR=0;
TFT_WR=1;
TFT_RS = 1;
for (temp = 0; temp < 76800; temp++) //填充颜色
{
DataToWrite(Color);
TFT_WR=0;
TFT_WR=1;
}
TFT_CS = 1;
}
//TFT初始化
void TFT_Init(void)
{
P2=0xff; //输出高
P0=0xff;
BLED = 1; //打开背光
TFT_RES = 0; //复位液晶
delay_ms(100); //RST脚持续100ms低电平
TFT_RES = 1;
TFT_WriteReg(0x0001,0x0100);
TFT_WriteReg(0x0002,0x0700);
TFT_WriteReg(0x0003,0x1030);
TFT_WriteReg(0x0004,0x0000);
TFT_WriteReg(0x0008,0x0207);
TFT_WriteReg(0x0009,0x0000);
TFT_WriteReg(0x000A,0x0000);
TFT_WriteReg(0x000C,0x0000);
TFT_WriteReg(0x000D,0x0000);
TFT_WriteReg(0x000F,0x0000);
//power on sequence VGHVGL
TFT_WriteReg(0x0010,0x0000);
TFT_WriteReg(0x0011,0x0007);
TFT_WriteReg(0x0012,0x0000);
TFT_WriteReg(0x0013,0x0000);
//vgh
delay_ms(50);
TFT_WriteReg(0x0010,0x1290);
TFT_WriteReg(0x0011,0x0227);
delay_ms(50);
//vregiout
TFT_WriteReg(0x0012,0x001d); //0x001b
delay_ms(50);
//vom amplitude
TFT_WriteReg(0x0013,0x1500);
delay_ms(50);
//vom H
TFT_WriteReg(0x0029,0x0018);
TFT_WriteReg(0x002B,0x000D);
delay_ms(50);
//gamma
TFT_WriteReg(0x0030,0x0004);
TFT_WriteReg(0x0031,0x0307);
TFT_WriteReg(0x0032,0x0002);// 0006
TFT_WriteReg(0x0035,0x0206);
TFT_WriteReg(0x0036,0x0408);
TFT_WriteReg(0x0037,0x0507);
TFT_WriteReg(0x0038,0x0204);//0200
TFT_WriteReg(0x0039,0x0707);
TFT_WriteReg(0x003C,0x0405);// 0504
TFT_WriteReg(0x003D,0x0F02);
//ram
TFT_WriteReg(0x0050,0x0000);
TFT_WriteReg(0x0051,0x00EF);
TFT_WriteReg(0x0052,0x0000);
TFT_WriteReg(0x0053,0x013F);
TFT_WriteReg(0x0060,0xA700);
TFT_WriteReg(0x0061,0x0001);
TFT_WriteReg(0x006A,0x0000);
//
TFT_WriteReg(0x0080,0x0000);
TFT_WriteReg(0x0081,0x0000);
TFT_WriteReg(0x0082,0x0000);
TFT_WriteReg(0x0083,0x0000);
TFT_WriteReg(0x0084,0x0000);
TFT_WriteReg(0x0085,0x0000);
//
TFT_WriteReg(0x0090,0x0010);
TFT_WriteReg(0x0092,0x0600);
TFT_WriteReg(0x0093,0x0003);
TFT_WriteReg(0x0095,0x0110);
TFT_WriteReg(0x0097,0x0000);
TFT_WriteReg(0x0098,0x0000);
TFT_WriteReg(0x0007,0x0133);
//TFT_WriteReg(0x0003,0x1008);//控制扫描方向
// TFT_WriteReg(0x0022);//
TFT_Clear(BLUE);
}
void main()
{
TFT_Init();
TFT_Clear(0x001F);
}