|
楼主 |
发表于 2009-4-20 17:49:45
|
显示全部楼层
/****************************************************************************
**文件名称:tftdrv.c
**功能:2.4寸320x240,65K色液晶的基本操作,硬件相关操作
**配置信息:主控芯片:LPC2148
** 晶振:11.0592MHZ
** 颜色说明:以一个十六位的数存放一个点的颜色, RGB三种颜色位数分别是5、6、5位
即:最高五位为红色灰阶.中间六位为绿色灰阶.最低五位为蓝色灰阶
例如: 0XF800 纯红色
0X07E0 纯绿色
0X001F 纯蓝色
**作者:
**日期:2008.12.22
**备注:硬件相关性强,移植请注意
****************************************************************************/
/*******************************************************************
**管脚连接说明
**D0--D7:P1.16--P1.23
**RS命令/数据:P0.17
**CS片选:P0.18
**WR写:P0.20
**RD读:P0.21
**RST复位:P0.22
*******************************************************************/
#include"config.h"
/* 输出数据宏定义 */
#define OutData(dat) FIO1CLR = 0xFF<<16; FIO1SET = (dat&0xff)<<16
/*定义RS控制*/
#define TFT_RS 0
#define SRS() FIO0SET=1<<TFT_RS
#define CRS() FIO0CLR=1<<TFT_RS
/*定义CS控制*/
#define TFT_CS 1
#define SCS() FIO0SET = 1<<TFT_CS
#define CCS() FIO0CLR = 1<<TFT_CS
/*定义WR控制*/
#define TFT_WR 20
#define SWR() FIO0SET=1<<TFT_WR
#define CWR() FIO0CLR=1<<TFT_WR
/*定义RD控制*/
#define TFT_RD 21
#define SRD() FIO0SET=1<<TFT_RD
#define CRD() FIO0CLR=1<<TFT_RD
/*定义RST控制*/
#define TFT_RST 22
#define SRST() FIO0SET = 1<<TFT_RST
#define CRST() FIO0CLR = 1<<TFT_RST
const uint16 color[8]={0xf800,0x07e0,0x001f,0xffe0,0x0020,0xffff,0x07ff,0xf81f}; //8 colore
//红、绿、深蓝、黄、深绿、白、淡绿、粉
/************************************************************************************************
* 名称:void Port_Ini(void)
* 功能:管脚初始化
* 入口参数:无
* 出口参数:无
***********************************************************************/
void Port_Ini(void)
{
(*((volatile unsigned long *) 0xE01FC1A0)) = 0x03;
FIO0MASK = 0x00;
FIO1MASK = 0x00;
PINSEL0 &=~(3<<0);
PINSEL0 &=~(3<<2);
PINSEL0 &=~(3<<4);
PINSEL1 &=~(3<<10);
PINSEL1 &=~(3<<12);
FIO1DIR=0x00FF0000; //设置管脚输入输出
FIO0DIR |=1<<TFT_RS|
1<<TFT_CS|
1<<TFT_WR|
1<<TFT_RD|
1<<TFT_RST;
FIO0SET |=1<<TFT_RS|
1<<TFT_CS|
1<<TFT_WR|
1<<TFT_RD|
1<<TFT_RST;
FIO1SET=0x00FF0000;
}
/*********************************************************************************
* 名称:void Delay(uint32 time)
* 功能:软件延时函数。用于TFT显示输出时序控制。
* 入口参数:time
* 出口参数:无
**********************************************************************************/
void Delay (uint32 time)
{
while(time--);
}
/*********************************************************************************
* 名称:void Delay_us(uint16 time)
* 功能:us级软件延时函数。用于TFT显示输出时序控制。
* 入口参数:i
* 出口参数:无
**********************************************************************************/
extern void Delay_us(uint16 i) //delay function of us
{
uint16 j;
for(;i>0;i--)
for(j=20;j>0;j--);
}
/*********************************************************************************
* 名称:void Delay_ms(uint16 i)
* 功能:ms级软件延时函数。用于TFT显示输出时序控制。
* 入口参数:i
* 出口参数:无
**********************************************************************************/
void Delay_ms(uint16 i) //delay funtion of ms
{
uint16 j;
for(;i>0;i--)
for(j=9000;j>0;j--)
;
}
/***********************************************************************
* 名称:void TFT_WData(uint8 data)
* 功能:写数据子程序
* 入口参数:data 要写入TFT的数据
*出口参数:无
***********************************************************************/
#define TFT_WData(data) SRS();\
CCS();\
OutData(data);\
CWR();\
SWR();\
SCS();\
CRS()
/*
void TFT_WData(uint8 data)
{
SRS(); //data
CCS(); //select
OutData(data); //out data
CWR(); //write
SWR();
SCS(); //unselect
CRS();
}
*/
/***********************************************************************
* 名称:void TFT_WReg(uint8 adr)
* 功能:写寄存器地址
* 入口参数:adr 要写的地址
*出口参数:无
***********************************************************************/
#define TFT_WReg(adr) CRS();\
CCS();\
OutData(adr);\
CWR();\
SWR();\
SCS();\
CRS()
/*
void TFT_WReg(uint8 adr)
{
CRS(); //command
CCS(); //select
OutData(adr); //out data
CWR(); //write
SWR();
SCS(); //unselect
CRS();
}
*/
/***********************************************************************
* 名称:void TFT_WCmd(uint8 dbw,uint8 index,uint16 cmd)
* 功能:写数据子程序
* 入口参数:dbw: the style of command 1:16 bit 0:8bit
index: number of register
cmd: command
*出口参数:无
***********************************************************************/
#define TFT_WCmd(dbw,index,cmd) TFT_WReg(index);\
if(dbw)\
{TFT_WData(cmd>>8);}\
TFT_WData(cmd&0xFF)
/*
void TFT_WCmd(uint8 dbw,uint8 index,uint16 cmd)
{
TFT_WReg(index); //write thr address of register
if(dbw) //16 bits command
{
TFT_WData(cmd>>8); //high 8 bits
}
TFT_WData(cmd&0xFF); //low 8 bits
}
*/
/***********************************************************************
* 名称:void TFT_WData16(uint16 data)
* 功能:写16位数据子程序
* 入口参数:data 16 bits data
*出口参数:无
***********************************************************************/
#define TFT_WData16(data) TFT_WData(data>>8);\
TFT_WData(data&0xff)
/*
void TFT_WData16(uint16 data) //写16位数据函数
{
TFT_WData(data>>8); //高8位数据
TFT_WData(data&0xff); //低8位数据
}
*/
/***********************************************************************
* 名称:void TFT_Clear(uint16 x,uint16 y,uint16 len,uint16 wid,uint16 colour)
* 功能:clear screen with a special colour
* 入口参数:x:start 0--240
y:start 0--320
len:length of clear area
wid:width of clear area
colour:colour
*出口参数:无
*caution:distinguish x and y,len and width
***********************************************************************/
extern void TFT_Clear(uint8 x,uint16 y,uint16 len,uint16 wid,uint16 colour)
{
uint16 mid;
uint32 n,temp;
TFT_WCmd(0,0x02,x); //start point's x,the x coordinate<=239,the type is 8 bits
TFT_WCmd(1,0x03,y); //start point's y,the y coordinate<=319,the type is 16 bits
mid=x+len-1; //config the parameter
if(mid>239)mid=239;
TFT_WCmd(0,0x04,mid); //set end point's x coordinate
mid=y+wid-1;
if(mid>319)mid=319;
TFT_WCmd(1,0x05,mid); //set end point's y coordinate
TFT_WReg(0x0e); //set register
temp=len*wid;
for(n=0;n<temp;n++) //the clear area display white
{
TFT_WData16(colour);
}
}
/***********************************************************************
* 名称:void TFT_int(void)
* 功能:initialize
* 入口参数:no
*出口参数:无
***********************************************************************/
extern void TFT_int(void)
{
CRST(); //LCD复位
Delay_ms(400);
SRST();
Delay_ms(10);
TFT_WReg(0xa1);
TFT_WData(0x00);
TFT_WReg(0xa0);
TFT_WData(0x00);
TFT_WCmd(0,0x70,0xc8); //对亮度有影响
Delay_ms(2);
TFT_WCmd(0,0x72,0xa3);
TFT_WCmd(0,0x73,0x04);
TFT_WCmd(0,0x75,0x45);
TFT_WCmd(0,0x76,0x23);
TFT_WCmd(0,0x77,0x08);
TFT_WCmd(0,0x78,0x08);
TFT_WCmd(0,0x79,0x00);
TFT_WCmd(0,0x7f,0xf0);
TFT_WCmd(0,0x71,0x81);
Delay_ms(2);
TFT_WCmd(0,0x0d,0x23);
TFT_WCmd(0,0x11,0x00);
TFT_WCmd(0,0x12,0x00);
TFT_WCmd(0,0x21,0x37);
TFT_WCmd(0,0x22,0x02);
TFT_WCmd(0,0x23,0x24);
TFT_WCmd(0,0x24,0x13);
TFT_WCmd(0,0x25,0x0a);
TFT_WCmd(0,0x26,0x82);
TFT_WCmd(0,0x27,0x01);
TFT_WCmd(0,0x1e,0x25);
TFT_WCmd(0,0x1f,0x59); //有影响
Delay_ms(2);
//GAMMA设置
TFT_WCmd(1,0x30,0x0777);
TFT_WCmd(1,0x31,0x0444);
TFT_WCmd(1,0x32,0x0555);
TFT_WCmd(1,0x33,0x0444);
TFT_WCmd(1,0x34,0x0333);
TFT_WCmd(1,0x35,0x0333);
TFT_WCmd(1,0x36,0x0333);
TFT_WCmd(1,0x37,0x0333);
TFT_WCmd(1,0x38,0x0444);
TFT_WCmd(1,0x39,0x0555);
TFT_WCmd(1,0x3a,0x0666);
TFT_WCmd(1,0x3b,0x0666);
TFT_WCmd(1,0x3c,0x0777);
TFT_WCmd(1,0x3d,0x0777);
TFT_WCmd(1,0x3e,0x0777);
TFT_WCmd(1,0x3f,0x0777);
TFT_WCmd(1,0x40,0x0777);
TFT_WCmd(1,0x41,0x0777);
TFT_WCmd(1,0x42,0x0777);
TFT_WCmd(1,0x43,0x0777);
TFT_WCmd(1,0x44,0x0777);
TFT_WCmd(1,0x45,0x0777);
TFT_WCmd(1,0x46,0x0777);
TFT_WCmd(1,0x47,0x0777);
TFT_WCmd(1,0x48,0x0777);
TFT_WCmd(1,0x49,0x0777);
TFT_WCmd(1,0x4a,0x0777);
TFT_WCmd(1,0x4b,0x0777);
TFT_WCmd(1,0x4c,0x0777);
TFT_WCmd(1,0x4d,0x0666);
TFT_WCmd(1,0x4e,0x0666);
TFT_WCmd(1,0x4f,0x0666);
TFT_WCmd(0,0x00,0x04); //扫描方向控制
TFT_WCmd(0,0x01,0x07); //扫描方向控制
TFT_WCmd(0,0x02,0x00); //设置X坐标点
TFT_WCmd(1,0x03,0x0000); //设置Y坐标点
//面板大小设置
TFT_WCmd(0,0x04,0xef); //结束列数(0~239)
TFT_WCmd(1,0x05,0x013f); //结束行数(0~319)
//窗口设置
TFT_WCmd(0,0x06,0x00); //水平起始位置
TFT_WCmd(1,0x07,0x0000); //垂直起始位置
TFT_WCmd(0,0x08,0xef); //水平结束位置
TFT_WCmd(1,0x09,0x013f); //垂直结束位置
TFT_WCmd(0,0x0a,0x00);
TFT_WCmd(0,0x0b,0x00);
TFT_WCmd(0,0x0c,0x00);
TFT_WCmd(0,0x14,0x00);
TFT_WCmd(0,0x15,0x00);
TFT_WCmd(0,0x16,0x00);
TFT_WCmd(1,0x17,0x01ff);
TFT_WCmd(1,0x18,0x01ff);
TFT_WCmd(0,0x13,0x00);
TFT_WCmd(1,0x19,0x01ff);
TFT_WCmd(1,0x1b,0x01ff);
TFT_WCmd(1,0x1c,0x01ff);
TFT_WCmd(1,0x1a,0x01ff);
TFT_WCmd(0,0x1d,0x0e); //无影响
TFT_WCmd(0,0x10,0x06); //开显示
TFT_WReg(0x0E);
TFT_Clear(0,0,240,320,0x0000); //清屏
}
/***********************************************************************
* 名称: void TFT_test(void)
* 功能:test function ,display colour bar
* 入口参数:no
*出口参数:无
***********************************************************************/
extern void TFT_test(void)
{
uint16 temp,num;
uint8 c;
//TFT_WCmd(0,0x00,0x04);
//TFT_WCmd(0,0x01,0x07);
//TFT_WCmd(0,0x02,0);
//TFT_WCmd(1,0x03,0);
//TFT_WCmd(0,0x04,0xEF);
//TFT_WCmd(1,0x05,0x013F);
//TFT_WCmd(0,0x06,0x00);
//TFT_WCmd(1,0x07,0x0000);
//TFT_WCmd(0,0x08,0xEF);
//TFT_WCmd(1,0x09,0x013F);
//TFT_WCmd(0,0x03,0);
//TFT_WCmd(1,0x02,0);
TFT_WReg(0x0E);
for(c=0;c<8;c++)
{
temp=color[c]; //8种颜色
for(num=40*240;num>0;num--)
{
TFT_WData16(temp);
}
}
}
/***********************************************************************
* 名称: void GUI_Point(uint8 x,uint16 y,uint16 colour)
* 功能:display in special coordinate
* 入口参数x: x coordinate 0--239
y: y coordinate 0--319
*出口参数:无
***********************************************************************/
/*
#define GUI_Point( x, y, colour) TFT_WCmd(0,0x02,x);\
TFT_WCmd(1,0x03,y);\
x=x+1;\
if(x>239)x=239;\
TFT_WCmd(0,0x04,x);\
y=y+1;\
if(y>319)y=319;\
TFT_WCmd(1,0x05,y);\
TFT_WReg(0x0e);\
TFT_WData16(colour)
*/
void GUI_Point(uint8 x,uint16 y,uint16 colour)
{
TFT_WCmd(0,0x02,x); //start point's x,the x coordinate<=239,the type is 8 bits
TFT_WCmd(1,0x03,y); //start point's y,the y coordinate<=319,the type is 16 bits
x=x+1;
if(x>239)x=239;
TFT_WCmd(0,0x04,x); //set end point's x coordinate
y=y+1;
if(y>319)y=319;
TFT_WCmd(1,0x05,y); //set end point's y coordinate
TFT_WReg(0x0e); //set register
TFT_WData16(colour);
}
/****************************************************************************
* 名称:void GUI_HLine(uint8 x0, uint8 x1,uint8 y)
* 功能:画水平线。
* 入口参数:x0 水平线起点所在列的位置
* x1 水平线终点所在列的位置
* y 水平线起点所在行的位置
* 出口参数:无
****************************************************************************/
void GUI_HLine(uint8 x0, uint8 x1,uint16 y,uint16 colour)
{
uint8 bak;
if(x0>x1) // 对x0、x1大小进行排列,以便画图
{
bak = x1;
x1 = x0;
x0 = bak;
}
do
{
GUI_Point(x0, y,colour); // 逐点显示,描出垂直线
x0++;
}while(x1>=x0);
}
/****************************************************************************
* 名称:void GUI_VLine(uint16 y0, uint16 y1,uint8 x,uint16 colour)
* 功能:画垂直线。
* 入口参数:y0 垂直线起点所在行的位置
* y1 水平线终点所在行的位置
* x 水平线起点所在列的位置
* 出口参数:无
****************************************************************************/
void GUI_VLine(uint16 y0, uint16 y1,uint8 x,uint16 colour)
{
uint16 bak;
if(y0>y1) // 对y0、y1大小进行排列,以便画图
{
bak = y1;
y1 = y0;
y0 = bak;
}
do
{
GUI_Point(x, y0,colour); // 逐点显示,描出垂直线
y0++;
}while(y1>=y0);
}
/****************************************************************************
* 名称:void TFT_Colorfyl(uint16 colour)
* 功能:彩条全屏
* 入口参数:uint16 colour:颜色
* 出口参数:无
****************************************************************************/
void TFT_Colorful(uint16 colour)
{
uint16 i;
for(i=0;i<320;i++)
{
TFT_Clear(0,i,240,1,colour);
colour-=1234;
}
} |
|