高级会员
- 积分
- 918
- 威望
- 534
- 贡献
- 252
- 兑换币
- 119
- 注册时间
- 2011-11-6
- 在线时间
- 66 小时
|
头文件
#ifndef TFT_H_
#define TFT_H_
//1 头文件
#include "common.h"
#include "gpio.h"
//2 宏定义
/* LCD Control pins */
#define LCD_DATA_PORT PORTA //TFT数据使用的端口
#define LCD_CtrlPin_NCS 0 /* B 0 */
#define LCD_CtrlPin_RS 1 /* B 1 */
#define LCD_CtrlPin_NWE 2 /* B 2 */
#define LCD_CtrlPin_NOE 3 /* B 3 */
#define LCD_CtrlPin_RST 4 /* B 4 */
#define LCD_CtrlPin_BACKLIGHT 5 /* B 5 */
#define LCD_Ctrl_PORT PORTB //TFT控制使用的端口
#define LCD_CS_Low() gpio_ctrl(LCD_Ctrl_PORT,LCD_CtrlPin_NCS,0)
#define LCD_CS_High() gpio_ctrl(LCD_Ctrl_PORT,LCD_CtrlPin_NCS,1)
#define LCD_RS_Low() gpio_ctrl(LCD_Ctrl_PORT,LCD_CtrlPin_RS,0)
#define LCD_RS_High() gpio_ctrl(LCD_Ctrl_PORT,LCD_CtrlPin_RS,1)
#define LCD_WR_Low() gpio_ctrl(LCD_Ctrl_PORT,LCD_CtrlPin_NWE,0)
#define LCD_WR_High() gpio_ctrl(LCD_Ctrl_PORT,LCD_CtrlPin_NWE,1)
#define LCD_RD_Low() gpio_ctrl(LCD_Ctrl_PORT,LCD_CtrlPin_NOE,0)
#define LCD_RD_High() gpio_ctrl(LCD_Ctrl_PORT,LCD_CtrlPin_NOE,1)
#define LCD_RST_Low() gpio_ctrl(LCD_Ctrl_PORT,LCD_CtrlPin_RST,0)
#define LCD_RST_High() gpio_ctrl(LCD_Ctrl_PORT,LCD_CtrlPin_RST,1)
/* LCD color */
#define White 0xFFFF
#define Black 0x0000
#define Grey 0xF7DE
#define Blue 0x001F
#define Blue2 0x051F
#define Red 0xF800
#define Magenta 0xF81F
#define Green 0x07E0
#define Cyan 0x7FFF
#define Yellow 0xFFE0
#define LCD_DATAUSE_PORT(data) GPIO_PDOR_REG(PORTA)=(data&0xffff) //还有缺陷,这样赋值A口其他脚不好用了
//void delay(uint16 y);
void delay(uint32 z);
void delay_us(uint32 z);
void LCD_Write_CMD(uint16 iadd);
void LCD_Write_Data(uint16 idat);
void LCD_WriteReg(uint16 iadd,uint16 iidata);
void LCD_SET_XY(uint16 x,uint16 y);
void LCD_Clear (uint16 color);
void LCD_init(void);
#endif
文件
//-------------------------------------------------------------------------*
// 文件名: tft.c *
// 说 明: tft初始化函数文件 *
//-------------------------------------------------------------------------*
#include "tft.h" //tft初始化函数文件
void delay(uint32 z)
{
uint32 x;
for(x=0;x<z;x++);
}
void delay_us(uint32 z)
{
while(z)
{
z--;
}
}
/*********************************************************************************
** 函数名称: void LCD_Write_CMD(uint16 iadd).
** 功能描述: LCD写指令.
** 输入参数: uint16 iadd 寄存器地址.
** 输出参数: None.
** 返回参数: None.
**********************************************************************************/
void LCD_Write_CMD(uint16 iadd)
{
LCD_CS_Low();
LCD_RS_Low();
LCD_DATAUSE_PORT(iadd);
LCD_WR_Low();
delay(1);//180ns
LCD_WR_High();
LCD_CS_High();
}/**/
/*********************************************************************************
** 函数名称: void LCD_Write_Data(uint16 idat).
** 功能描述: LCD写数据.
** 输入参数: uint16 idat 16为数据.
** 输出参数: None.
** 返回参数: None.
**********************************************************************************/
void LCD_Write_Data(uint16 idat)
{
LCD_CS_Low();
LCD_RS_High();
LCD_DATAUSE_PORT(idat);
LCD_WR_Low();
delay(1);//180ns
LCD_WR_High();
LCD_CS_High();
}
/*********************************************************************************
** 函数名称: void LCD_Write_Reg(INT16U iadd, INT16U iidata).
** 功能描述: 对lcd寄存器,写命令和数据.
** 输入参数: INT16U iadd, INT16U iidata.
** 输出参数: None.
** 返回参数: None.
**********************************************************************************/
void LCD_WriteReg(uint16 iadd,uint16 iidata)
{
LCD_Write_CMD(iadd);
LCD_Write_Data(iidata);
}
/*********************************************************************************
** 函数名称: void LCD_SET_XY(uint16 Xpos,uint16 Ypos).
** 功能描述: 设置光标函数.
** 输入参数: uint16 Xpos,uint16 Ypos.
** 输出参数: None.
** 返回参数: None.
**********************************************************************************/
void LCD_SET_XY(uint16 Xpos,uint16 Ypos)
{
/*
px,py 是物理坐标, x,y是虚拟坐标
转换公式:
py = 399 - x;
px = y;
*/
LCD_WriteReg(0x0200, Ypos); /* px */
LCD_WriteReg(0x0201, 399 - Xpos); /* py */
//LCD_WriteReg(0x200,y);
//LCD_WriteReg(0x201,x);
//LCD_Write_CMD(0x0202);
}
/*********************************************************************************
** 函数名称: void LCD_Clear (INT16U color).
** 功能描述: LCD指定颜色清屏.
** 输入参数: INT16U color 清屏颜色.
** 输出参数: None.
** 返回参数: None.
**********************************************************************************/
void LCD_Clear (uint16 color)
{
uint8 x;
uint16 y;
LCD_SET_XY(0,0);
for(x=0;x<240;x++)//200
{
for(y=0;y<400;y++)//300
{
LCD_WriteReg(0x202, color);
}
}
}
//---------------------------------- ---------------------------------------*
//函数名: LCD_init *
//功 能: 初始化LCD控制端口 *
//参 数: 无 *
//返 回: 无 *
//说 明: 无 *
//-------------------------------------------------------------------------*
void LCD_init(void)
{
gpio_init(LCD_Ctrl_PORT,LCD_CtrlPin_NCS,1,1);//初始化控制引脚
gpio_init(LCD_Ctrl_PORT,LCD_CtrlPin_RS,1,1);
gpio_init(LCD_Ctrl_PORT,LCD_CtrlPin_NWE,1,1);
gpio_init(LCD_Ctrl_PORT,LCD_CtrlPin_NOE,1,1);
gpio_init(LCD_Ctrl_PORT,LCD_CtrlPin_RST,1,1);
gpio_init(LCD_Ctrl_PORT,LCD_CtrlPin_BACKLIGHT,1,0);
gpio_ctrl(LCD_Ctrl_PORT,LCD_CtrlPin_BACKLIGHT,1);/* 背光控制 0:关 1:开 */
gpio_ctrl(LCD_Ctrl_PORT,LCD_CtrlPin_NOE,1);
delay(500); //11.3us
LCD_RST_Low();
delay(500); //11.3us
LCD_RST_High();
delay(500); //11.3us
LCD_CS_Low();
delay(500); //11.3us
// 初始化LCD,写LCD寄存器进行配置
LCD_WriteReg(0x0000, 0x0000);
LCD_WriteReg(0x0001, 0x0100);
LCD_WriteReg(0x0002, 0x0100);
LCD_WriteReg(0x0003, 0x1018); // 0x1018 1030
LCD_WriteReg(0x0008, 0x0808);
LCD_WriteReg(0x0009, 0x0001);
LCD_WriteReg(0x000B, 0x0010);
LCD_WriteReg(0x000C, 0x0000);
LCD_WriteReg(0x000F, 0x0000);
LCD_WriteReg(0x0007, 0x0001);
LCD_WriteReg(0x0010, 0x0013);
LCD_WriteReg(0x0011, 0x0501);
LCD_WriteReg(0x0012, 0x0300);
LCD_WriteReg(0x0020, 0x021E);
LCD_WriteReg(0x0021, 0x0202);
LCD_WriteReg(0x0090, 0x8000);
LCD_WriteReg(0x0100, 0x17B0);
LCD_WriteReg(0x0101, 0x0147);
LCD_WriteReg(0x0102, 0x0135);
LCD_WriteReg(0x0103, 0x0700);
LCD_WriteReg(0x0107, 0x0000);
LCD_WriteReg(0x0110, 0x0001);
LCD_WriteReg(0x0210, 0x0000);
LCD_WriteReg(0x0211, 0x00EF);
LCD_WriteReg(0x0212, 0x0000);
LCD_WriteReg(0x0213, 0x018F);
LCD_WriteReg(0x0280, 0x0000);
LCD_WriteReg(0x0281, 0x0004);
LCD_WriteReg(0x0282, 0x0000);
LCD_WriteReg(0x0300, 0x0101);
LCD_WriteReg(0x0301, 0x0B2C);
LCD_WriteReg(0x0302, 0x1030);
LCD_WriteReg(0x0303, 0x3010);
LCD_WriteReg(0x0304, 0x2C0B);
LCD_WriteReg(0x0305, 0x0101);
LCD_WriteReg(0x0306, 0x0807);
LCD_WriteReg(0x0307, 0x0708);
LCD_WriteReg(0x0308, 0x0107);
LCD_WriteReg(0x0309, 0x0105);
LCD_WriteReg(0x030A, 0x0F04);
LCD_WriteReg(0x030B, 0x0F00);
LCD_WriteReg(0x030C, 0x000F);
LCD_WriteReg(0x030D, 0x040F);
LCD_WriteReg(0x030E, 0x0300);
LCD_WriteReg(0x030F, 0x0701);
LCD_WriteReg(0x0400, 0x3500);
LCD_WriteReg(0x0401, 0x0001);
LCD_WriteReg(0x0404, 0x0000);
LCD_WriteReg(0x0500, 0x0000);
LCD_WriteReg(0x0501, 0x0000);
LCD_WriteReg(0x0502, 0x0000);
LCD_WriteReg(0x0503, 0x0000);
LCD_WriteReg(0x0504, 0x0000);
LCD_WriteReg(0x0505, 0x0000);
LCD_WriteReg(0x0600, 0x0000);
LCD_WriteReg(0x0606, 0x0000);
LCD_WriteReg(0x06F0, 0x0000);
LCD_WriteReg(0x07F0, 0x5420);
LCD_WriteReg(0x07DE, 0x0000);
LCD_WriteReg(0x07F2, 0x00DF);
LCD_WriteReg(0x07F3, 0x0810);
LCD_WriteReg(0x07F4, 0x0077);
LCD_WriteReg(0x07F5, 0x0021);
LCD_WriteReg(0x07F0, 0x0000);
LCD_WriteReg(0x0007, 0x0173);
// 设置显示窗口 WINDOWS
LCD_WriteReg(0x0210, 0); // 水平起始地址
LCD_WriteReg(0x0211, 239); // 水平结束坐标
LCD_WriteReg(0x0212, 0); // 垂直起始地址
LCD_WriteReg(0x0213, 399); // 垂直结束地址
}
主函数
//头文件
#include "includes.h"
//全局变量声明
//主函数
void main(void)
{
DisableInterrupts; //禁止总中断
pllinit180M();
LCD_init();
while(1)
{
LCD_Clear(Red);
}
} |
|