智能车制作
标题:
写了个TFT触摸屏,spfd5420的。
[打印本页]
作者:
aa330233789
时间:
2012-10-3 17:56
标题:
写了个TFT触摸屏,spfd5420的。
头文件
#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);
}
}
作者:
Move123456
时间:
2012-10-4 13:30
哈啊哈哈哈。今早以上论坛就看见楼主的触摸屏的图片。再看见这个就好起进来,吼吼,原来是楼主啊
作者:
linping9656
时间:
2012-10-4 17:47
楼主厉害加无私
作者:
maitianshouwei
时间:
2013-2-16 15:31
膜拜先!我也想玩一下tft屏,眼下正好有一个,但是不知从何入手,看了楼主的代码感觉可以试试
作者:
dlyt03
时间:
2013-2-16 20:31
强悍!!
作者:
东成西就
时间:
2013-11-2 22:33
这种就触摸屏?????你的触摸程序在哪里,拿来忽悠小朋友的吧
欢迎光临 智能车制作 (http://111.231.132.190/)
Powered by Discuz! X3.2