?? tft.c
字號:
//**********************************************************************************************
//* 文 件 名 : tft.c
//*
//* 文件描述 : tft0x84*0x84 LCD 驅(qū)動
//*
//* 作 者 : knight
//* 版 本 : V0.01
//* 編 譯 器 : IAR EWARM 5.30
//* 日 期 : 2009-10-18
//* 所需支持代碼:
//**********************************************************************************************
#include "tft.h"
#include "img.h"
/**********************************************************************
* 函數(shù) TFT_Y_Dec ---Y軸遞增方向
* 輸入?yún)?shù) data --- 1 byte 數(shù)據(jù)
* 輸出參數(shù) 無
* 返回值 無
**********************************************************************/
void
TFT_Y_Direction(u8 Up_or_Down)
{
if(Up_or_Down==Up)
{
tftWriteReg(0x01,0x030f);
}
else
{
tftWriteReg(0x01,0x010f);
}
}
/**********************************************************************
* 函數(shù) PutDataToIO --- 把數(shù)據(jù)放入TFTLCD數(shù)據(jù)口
* 輸入?yún)?shù) data --- 1 byte 數(shù)據(jù)
* 輸出參數(shù) 無
* 返回值 無
**********************************************************************/
void
PutDataToIO(unsigned char data)
{
u16 PortVal;
PortVal = GPIO_ReadOutputData(TFT_DATA_PORT);
PortVal = (PortVal&0xff00) | data;
GPIO_Write(TFT_DATA_PORT,PortVal);
}
/**********************************************************************
* 函數(shù) tftWriteCmd --- 寫命令
* 輸入?yún)?shù) command --- 命令
* 輸出參數(shù) 無
* 返回值 無
**********************************************************************/
void
tftWriteCmd(unsigned char command)
{
TFT_RS_L;
TFT_CS_L;
PutDataToIO(0x00);
TFT_WR_L;
TFT_WR_H;
PutDataToIO(command);
TFT_WR_L;
TFT_WR_H;
TFT_CS_H;
}
/**********************************************************************
* 函數(shù) tftWriteData8 --- 寫入 1 byte 數(shù)據(jù)
* 輸入?yún)?shù) data --- 寫入的數(shù)據(jù)
* 輸出參數(shù) 無
* 返回值 無
**********************************************************************/
void
tftWriteData8(unsigned char data)
{
TFT_RS_H;
TFT_CS_L;
PutDataToIO(data);
TFT_WR_L;
TFT_WR_H;
TFT_CS_H;
}
/**********************************************************************
* 函數(shù) tftWriteData16 --- 寫入 2 byte 數(shù)據(jù)
* 輸入?yún)?shù) data_h ---寫入的數(shù)據(jù) 高位
* data_l--- 寫入的數(shù)據(jù) 低位
* 輸出參數(shù) 無
* 返回值 無
**********************************************************************/
void
tftWriteData16(unsigned char data_h,unsigned char data_l)
{
TFT_RS_H;
TFT_CS_L;
PutDataToIO(data_h);
TFT_WR_L;
TFT_WR_H;
PutDataToIO(data_l);
TFT_WR_L;
TFT_WR_H;
TFT_CS_H;
}
/**********************************************************************
* 函數(shù) tftWriteReg --- 寫入寄存器 16bits 數(shù)據(jù)
* 輸入?yún)?shù) reg --- 寄存器地址
* data16 --- 寫入的數(shù)據(jù)
* 輸出參數(shù) 無
* 返回值 無
**********************************************************************/
void
tftWriteReg(unsigned char reg,unsigned int data16)
{
unsigned char dataH,dataL;
dataH=data16/256;
dataL=data16&0xff;
tftWriteCmd(reg);
tftWriteData16(dataH,dataL);
}
/**********************************************************************
* 函數(shù) tftInit --- 初始化TFTLCD屏
* 輸入?yún)?shù) 無
* 輸出參數(shù) 無
* 返回值 無
**********************************************************************/
///---------------------------------------------------------------------
/// 初始化后 TFTLCD 坐標
///
/// Y|
/// |
/// |
/// |
/// 0---------------------------- X
///---------------------------------------------------------------------
void
tftInit(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
//--開啟GPIO時鐘
RCC_APB2PeriphClockCmd(TFT_GPIO_PORTS, ENABLE);
//設(shè)置 RS 引腳為輸出 push-pull 模式
GPIO_InitStructure.GPIO_Pin = TFT_RS;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(TFT_RS_PORT, &GPIO_InitStructure);
//設(shè)置 CS RST WR RD 引腳為輸出 push-pull 模式
GPIO_InitStructure.GPIO_Pin = TFT_CS|TFT_RST|TFT_WR|TFT_RD;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(TFT_CS_PORT, &GPIO_InitStructure);
//設(shè)置 data 引腳為輸出 push-pull 模式
GPIO_InitStructure.GPIO_Pin = TFT_DATA;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(TFT_DATA_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOD, &GPIO_InitStructure);
//初始化操作GPIO
TFT_RST_L;
TFT_CS_H;
TFT_RS_H;
TFT_WR_H;
TFT_RD_H;
Delay_Ms(100);
TFT_RST_H;
Delay_Ms(100);
///初始化設(shè)置寄存器
tftWriteReg(0x00,0x0001);
Delay_Ms(50);
tftWriteReg(0x12,0x0109);
tftWriteReg(0x13,0x0e1d);
tftWriteReg(0x14,0x6224);
tftWriteReg(0x10,0x2004);
Delay_Ms(50);
tftWriteReg(0x13,0x0e5d);
Delay_Ms(50);
tftWriteReg(0x01,0x010f);
//TFT_Y_Direction(Down);
tftWriteReg(0x02,0x0700);
tftWriteReg(0x03,0x1030);
tftWriteReg(0x07,0x0000);
tftWriteReg(0x08,0x0202);
tftWriteReg(0x0b,0x0001);
tftWriteReg(0x0c,0x0000);
tftWriteReg(0x40,0x0000);
tftWriteReg(0x42,0x8300);
tftWriteReg(0x43,0x0000);
tftWriteReg(0x44,0x8304);
tftWriteReg(0x45,0x7f00);
tftWriteReg(0x25,0x0002);
tftWriteReg(0x26,0x0002);
tftWriteReg(0x30,0x0000);
tftWriteReg(0x31,0x0102);
tftWriteReg(0x32,0x0001);
tftWriteReg(0x33,0x0202);
tftWriteReg(0x34,0x0707);
tftWriteReg(0x35,0x0707);
tftWriteReg(0x36,0x0707);
tftWriteReg(0x37,0x0202);
tftWriteReg(0x07,0x0005);
Delay_Ms(50);
tftWriteReg(0x07,0x0015);
Delay_Ms(50);
tftWriteReg(0x21,0x0004);
Delay_Ms(50);
tftWriteReg(0x21,0x0004);
tftWriteReg(0x44,0x8304);
tftWriteReg(0x45,0x7f00);
tftWriteCmd(0x22);
tftWriteReg(0x10,0x0000);
Delay_Ms(50);
tftWriteReg(0x03,0x1030);
Delay_Ms(50);
tftWriteReg(0x10,0x2004);
Delay_Ms(50);
tftWriteReg(0x13,0x0e5d);
Delay_Ms(50);
tftWriteReg(0x07,0x0017);
Delay_Ms(50);
tftWriteCmd(0x22);
///開背光
GPIO_SetBits(TFT_LED_PWM_PORT,TFT_LED_PWM ) ;
}
/**********************************************************************
* 函數(shù) PosXY --- 定義起始光標位置
* 輸入?yún)?shù) X,Y 坐標
* 輸出參數(shù) 無
* 返回值 無
**********************************************************************/
void
PosXY(unsigned char X,unsigned char Y)
{
u16 Pos;
Pos= Y*0x100 + X;
tftWriteReg(TFTCMD_XYPos,Pos);
}
/**********************************************************************
* 函數(shù) SetWindowRange --- 設(shè)置當前TFT操作窗口范圍
* 輸入?yún)?shù) X,Y --- 窗口起點坐標
* XRange,YRange --- 窗口的寬和高
* 輸出參數(shù) 無
* 返回值 無
**********************************************************************/
void
SetWindowRange(u8 X,u8 Y,u8 XRange,u8 YRange)
{
u16 Pos;
PosXY(X,Y);
Pos=(X + XRange-1)*0x100 + X;
tftWriteReg(TFTCMD_XRange,Pos);
Pos=(Y + YRange-1)*0x100 + Y;
tftWriteReg(TFTCMD_YRange,Pos);
}
/**********************************************************************
* 函數(shù) ClrTFT --- 清屏 把當前窗口填充為一種顏色
* 輸入?yún)?shù) R,G,B --- RGB顏色
* 輸出參數(shù) 無
* 返回值 無
**********************************************************************/
void
ClrTFT(u8 RGB_H,u8 RGB_L,s16 len)
{
s16 n;
n=len;
if (n<0) n=TFTScreenWidth * TFTScreenHeight;
tftWriteCmd(0x22);
while(n>0)
{
tftWriteData16(RGB_H,RGB_L);
n--;
}
}
/**********************************************************************
* 函數(shù) WriteBufToTFT_16 --- 直接寫B(tài)MP16緩存
* 輸入?yún)?shù) _buf --- 緩存地址
* buflen --- 寫緩存長度
* 輸出參數(shù) 無
* 返回值 無
**********************************************************************/
void
WriteBufToTFT_16(const u16* _buf,u16 buflen)
{
u16 i=0;
u8 dataH,dataL;
tftWriteCmd(0x22);
while ( (*_buf!=0 || buflen<MaxBufLen) && (i<buflen) )
{
dataH=(*_buf)/0x100;
dataL=(*_buf)&0xff;
tftWriteData16(dataH,dataL);
i++;
_buf++;
}
}
/**********************************************************************
* 函數(shù) WriteBufToTFT_8 --- 直接寫B(tài)MP8緩存
* 輸入?yún)?shù) _buf --- 緩存地址
* buflen --- 寫緩存長度
* 輸出參數(shù) 無
* 返回值 無
**********************************************************************/
void
WriteBufToTFT_8(const u8* _buf,u16 buflen)
{
u16 i=0;
tftWriteCmd(0x22);
while ( (*_buf!=0 || buflen<MaxBufLen) && (i<buflen) )
{
tftWriteData8(*_buf);
i++;
_buf++;
}
}
/**********************************************************************
* 函數(shù) tftTest --- 測試TFTLCD
* 輸入?yún)?shù) 無
* 輸出參數(shù) 無
* 返回值 無
**********************************************************************/
void
tftTest(void)
{
///-----清屏
SetWindowRange(0,0,TFTScreenWidth,TFTScreenHeight);
ClrTFT(0,0,-1);
Delay_Ms(100);
#if 0
///-----測試 1-1
SetWindowRange(0,0,TFTScreenWidth,TFTScreenHeight);
ClrTFT(0xf8,0);
Delay_Ms(1000);
///-----測試 1-2
SetWindowRange(0,0,TFTScreenWidth,TFTScreenHeight);
ClrTFT(0x07,0xe0);
Delay_Ms(1000);
///-----測試 1-3
SetWindowRange(0,0,TFTScreenWidth,TFTScreenHeight);
ClrTFT(0,0x1f);
Delay_Ms(1000);
#endif
#if 1
///-----清屏幕
SetWindowRange(0,0,TFTScreenWidth,TFTScreenHeight);
ClrTFT(0,0,-1);
///-----測試 2
///-----測試 2-1
SetWindowRange(0,0,TFTScreenWidth,TFTScreenHeight/3);
ClrTFT(0xf8,0,-1);
Delay_Ms(500);
///-----測試 2-2
SetWindowRange(0,TFTScreenHeight/3,TFTScreenWidth,TFTScreenHeight/3);
ClrTFT(0x07,0xe0,-1);
Delay_Ms(500);
///-----測試 2-3
SetWindowRange(0,TFTScreenHeight*2/3,TFTScreenWidth,TFTScreenHeight/3);
ClrTFT(0,0x1f,-1);
Delay_Ms(500);
#endif
#if 1
///-----測試 3
///-----測試 3-1
///-------------------------------------------------------------------------------
SetWindowRange(0,0,TFTScreenWidth,TFTScreenHeight); ///為什么需要加這一句代碼?????????
///-------------------------------------------------------------------------------
SetWindowRange(20,20,50,50);
ClrTFT(0x07,0xff,-1);
Delay_Ms(500);
///-----測試 3-2
SetWindowRange(40,40,50,50);
ClrTFT(0xf8,0x1f,-1);
Delay_Ms(500);
///-----測試 3-3
SetWindowRange(60,60,50,50);
ClrTFT(0xff,0xE0,-1);
Delay_Ms(500);
#endif
#if 0
///------測試 4-1
u8 Xpos,Ypos,Xlen,Ylen,j;
Xlen=0x60;
Ylen=0x60;
//清屏幕
SetWindowRange(0,0,TFTScreenWidth,TFTScreenHeight);
ClrTFT(0xBE,0x5F);
for(j=0;j<0x14;j++)
{
Xpos=j;
Ypos=4+j;
SetWindowRange( Xpos,Ypos,Xlen,Ylen );
tftWriteCmd(0x22);
WriteBufToTFT_8(_LOGO,(Xlen * Ylen*2) );
Delay_Ms(100);
}
Delay_Ms(1000);
#endif
}
//////////////////////////////////////end of TFTLCD Drive///////////////////////////////////////////
/*
void tftReadCmd(unsigned char command)
{
TFT_RS_L;
//Delay_nms(1);
//GPIO_WriteBit(TFT_DATA_PORT,TFT_DATA,0x00);
PutDataToIO(command);
TFT_CS_L;
TFT_RD_L;
Delay_nms(1);
TFT_CS_H;
TFT_RD_H;
//Delay_nms(1);
//TFT_RS_H;
//GPIO_WriteBit(TFT_DATA_PORT,TFT_DATA,command);
PutDataToIO(0x00);
TFT_CS_L;
TFT_RD_L;
Delay_nms(1);
TFT_CS_H;
TFT_RD_H;
//Delay_nms(1);
}
u16 tftReadData16()
{
u16 data_h,data_l;
TFT_RS_H;
//Delay_nms(1);
TFT_CS_L;
TFT_RD_L;
Delay_nms(1);
TFT_CS_H;
TFT_RD_H;
data_h = GPIO_ReadOutputData(TFT_DATA_PORT)&0xff00;
Delay_nms(1);
TFT_CS_L;
TFT_RD_L;
Delay_nms(1);
TFT_CS_H;
TFT_RD_H;
data_l = (GPIO_ReadOutputData(TFT_DATA_PORT)&0xff00)>>8;
Delay_nms(1);
return (data_h+data_l);
}
*/
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -