本帖最后由 流云舒语 于 2015-5-14 16:02 编辑
/*
*接线:相对应的数据线,D0~D15: PD 14 15 0 1,PE 7 8 9 10 11 12 13 14 15,PD 8 9 10
* CS - PG12 ; RS - PF0 ; WR - PD5 ; RD - PD4 ; LED_A(背光) - PA1;
*
*
* RS :控制寄存器/数据寄存器选择(低电平选择控制寄存器)
* CS :片选信号,低电平有效
* WR : 写信号,低电平有效
* RD : 读信号,低电平有效
******************************************************************************
*/
/* ----------------------------------------------------------------------------*/
#include "TFT9325.h"
uint16_t GPIO_16;
void Delay(__IO uint32_t nCount) //简单的延时函数
{
for(; nCount != 0; nCount--);
}
/*****************初始化TFT引脚******************/
/**
* @brief 初始化驱动TFT驱动芯片所需要的引脚
* @param
* @retval
*/
void TFT_GPIO_Config()
{
/*定义一个GPIO_InitTypeDef类型的结构体*/
GPIO_InitTypeDef GPIO_InitStructure;
/*开启GPIOBEFG的外设时钟*/
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOD|RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOF|RCC_APB2Periph_GPIOE|RCC_APB2Periph_GPIOG, ENABLE);
//初始化D4 D5 为读 和 写
/*选择要控制的GPIOB引脚*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5;
/*设置引脚模式为通用推挽输出*/
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
/*设置引脚速率为50MHz */
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
/*调用库函数,初始化GPIO*/
GPIO_Init(GPIOD, &GPIO_InitStructure);
/* 配置LCD背光控制管脚*/
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//初始化 G12 为片选信号
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOG, &GPIO_InitStructure);
//初始化 F0 为复位信号
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOF, &GPIO_InitStructure);
/* 配置相对应的数据线,FSMC-D0~D15: PD 14 15 0 1,PE 7 8 9 10 11 12 13 14 15,PD 8 9 10*/
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_8 | GPIO_Pin_9 |
GPIO_Pin_10 | GPIO_Pin_14 | GPIO_Pin_15 ;
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 |
GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 |
GPIO_Pin_15;
GPIO_Init(GPIOE, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_14;
GPIO_Init(GPIOG, &GPIO_InitStructure);
// GPIO_SetBits(GPIOG,GPIO_Pin_14);
// GPIO_16=0x8001;
// GPIO_PIN_SET();
}
void TFT_Config(void)
{
/* 开背光 */
LED_A_SET;
CS_SET;
Delay(0x0f);
Write_cmd_data(0x0001,0x0100);//源扫描设置ss=0;输出方向的移动方向是从s1到s720
Write_cmd_data(0x0002,0x0700);
Write_cmd_data(0x0003,0x1030);
Write_cmd_data(0x0004,0x0000);
Write_cmd_data(0x0008,0x0207);
Write_cmd_data(0x0009,0x0000);
Write_cmd_data(0x0010,0x1290);
Write_cmd_data(0x0011,0x0227);
Write_cmd_data(0x0012,0x001d);
Write_cmd_data(0x0013,0x1500);
Write_cmd_data(0x0029,0x0018);
Write_cmd_data(0x002B,0x000D);
//伽马校正
Write_cmd_data(0x0030,0x0004);
Write_cmd_data(0x0031,0x0307);
Write_cmd_data(0x0032,0x0002);//0006
Write_cmd_data(0x0035,0x0206);
Write_cmd_data(0x0036,0x0408);
Write_cmd_data(0x0037,0x0507);
Write_cmd_data(0x0038,0x0204);//0200
Write_cmd_data(0x0039,0x0707);
Write_cmd_data(0x003C,0x0405);//0504
Write_cmd_data(0x003D,0x0F02);
//窗口设置
Write_cmd_data(0x0050,0x0000);//水平起始位置
Write_cmd_data(0x0051,0x00EF);//水平终止位置
Write_cmd_data(0x0052,0x0000);//垂直起始位置
Write_cmd_data(0x0053,0x013F);//垂直终止位置
Write_cmd_data(0x0060,0xA700);//门扫描设置,GS=1:从G320扫描到G1,320线
Write_cmd_data(0x0061,0x0001);
Write_cmd_data(0x006A,0x0000);
//
Write_cmd_data(0x0080,0x0000);
Write_cmd_data(0x0081,0x0000);
Write_cmd_data(0x0082,0x0000);
Write_cmd_data(0x0083,0x0000);
Write_cmd_data(0x0084,0x0000);
Write_cmd_data(0x0085,0x0000);
//
Write_cmd_data(0x0090,0x0010);
Write_cmd_data(0x0092,0x0600);
Write_cmd_data(0x0093,0x0003);
Write_cmd_data(0x0095,0x0110);
Write_cmd_data(0x0097,0x0000);
Write_cmd_data(0x0098,0x0000);
Write_cmd_data(0x0007,0x0133);
}
//===============================================================
//设置窗口
//入口参数: x0 y0,为显示窗口左上角坐标;x1,y1为显示窗口右下角坐标。
//出口参数: 无
//说明:该函数设置TFT模块的显示窗口。
void TFT_SetWindow(unsigned char x0,unsigned int y0,unsigned char x1,unsigned int y1)
{
Write_cmd_data(WINDOW_XADDR_START,x0);//水平起始位置
Write_cmd_data(WINDOW_XADDR_END,x1);//水平终止位置
Write_cmd_data(WINDOW_YADDR_START,y0);//垂直起始位置
Write_cmd_data(WINDOW_YADDR_END,y1);//垂直终止位置
Write_cmd_data(GRAM_XADDR,x0);//设置X坐标位置
Write_cmd_data(GRAM_YADDR,y0);//设置Y坐标位置
Write_cmd(0x0022);//LCD_WriteCMD(GRAMWR);
}
//===============================================================
//设定当前显示坐标
//入口参数: x,y为当前显示坐标。
//出口参数: 无
//说明:本函数实际上是LCD_setwindow函数的一个特例,显示窗口为最小,仅是一个像素点。
void TFT_Setxy(unsigned char x,unsigned int y)
{
TFT_SetWindow(x,y,x,y);
}
/*****************写命令******************/
/**
* @brief 向芯片内写入命令
* @param
* @retval
*/
void Write_cmd(uint16_t cmd)
{
CS_CLR;
Delay(0xFF);
RS_CLR;
Delay(0xFF);
GPIO_16=cmd;
Delay(0xFF);
GPIO_PIN_SET();
Delay(0xFF);
WR_CLR;
Delay(0xFF);
WR_SET;
Delay(0xFF);
CS_SET;
}
/*****************写数据******************/
/**
* @brief 向芯片内写入数据
* @param
* @retval
*/
void Write_data(uint16_t dat)
{
CS_CLR;
Delay(0xFF);
RS_SET;
Delay(0xFF);
GPIO_16=dat;
Delay(0xFF);
GPIO_PIN_SET();
Delay(0xFF);
WR_CLR;
Delay(0xFF);
WR_SET;
Delay(0xFF);
CS_SET;
Delay(0xFF);
}
/*****************写命令数据******************/
/**
* @brief 向芯片内写入数据
* @param
* @retval
*/
void Write_cmd_data(uint16_t cmd, uint16_t dat)
{
Write_cmd(cmd);
Delay(0xFF);
Write_data(dat);
Delay(0xFF);
}
/*****************把16个引脚转换成一个16位二进制数******************/
/**
* @brief 把16个IO口统一赋值
* @param
* @retval
*/
void GPIO_PIN_SET(void)
{
uint16_t tmp;
tmp=GPIO_16;
//低0
if((tmp)&&0x0001)
{
GPIO_SetBits(GPIOD,GPIO_Pin_15);
Delay(0XFF);
}
else
{
GPIO_ResetBits(GPIOD,GPIO_Pin_15);
Delay(0XFF);
}
//1
if((tmp=tmp>>1)&&0x0001)
{
GPIO_SetBits(GPIOD,GPIO_Pin_1);
Delay(0XFF);
}
else
{
GPIO_ResetBits(GPIOD,GPIO_Pin_1);
Delay(0XFF);
}
//2
if((tmp=tmp>>1)&&0x0001)
{
GPIO_SetBits(GPIOE,GPIO_Pin_8);
Delay(0XFF);
}
else
{
GPIO_ResetBits(GPIOE,GPIO_Pin_8);
Delay(0XFF);
}
//3
if((tmp=tmp>>1)&&0x0001)
{
GPIO_SetBits(GPIOE,GPIO_Pin_10);
Delay(0XFF);
}
else
{
GPIO_ResetBits(GPIOE,GPIO_Pin_10);
Delay(0XFF);
}
//4
if((tmp=tmp>>1)&&0x0001)
{
GPIO_SetBits(GPIOE,GPIO_Pin_12);
Delay(0XFF);
}
else
{
GPIO_ResetBits(GPIOE,GPIO_Pin_12);
Delay(0XFF);
}
//5
if((tmp=tmp>>1)&&0x0001)
{
GPIO_SetBits(GPIOE,GPIO_Pin_14);
Delay(0XFF);
}
else
{
GPIO_ResetBits(GPIOE,GPIO_Pin_14);
Delay(0XFF);
}
//6
if((tmp=tmp>>1)&&0x0001)
{
GPIO_SetBits(GPIOD,GPIO_Pin_8);
Delay(0XFF);
}
else
{
GPIO_ResetBits(GPIOD,GPIO_Pin_8);
Delay(0XFF);
}
//7
if((tmp=tmp>>1)&&0x0001)
{
GPIO_SetBits(GPIOD,GPIO_Pin_10);
Delay(0XFF);
}
else
{
GPIO_ResetBits(GPIOD,GPIO_Pin_10);
Delay(0XFF);
}
//8
if((tmp=tmp>>1)&&0x0001)
{
GPIO_SetBits(GPIOD,GPIO_Pin_14);
Delay(0XFF);
}
else
{
GPIO_ResetBits(GPIOD,GPIO_Pin_14);
Delay(0XFF);
}
//9
if((tmp=tmp>>1)&&0x0001)
{
GPIO_SetBits(GPIOD,GPIO_Pin_0);
Delay(0XFF);
}
else
{
GPIO_ResetBits(GPIOD,GPIO_Pin_0);
Delay(0XFF);
}
//10
if((tmp=tmp>>1)&&0x0001)
{
GPIO_SetBits(GPIOE,GPIO_Pin_7);
Delay(0XFF);
}
else
{
GPIO_ResetBits(GPIOE,GPIO_Pin_7);
Delay(0XFF);
}
//11
if((tmp=tmp>>1)&&0x0001)
{
GPIO_SetBits(GPIOE,GPIO_Pin_9);
Delay(0XFF);
}
else
{
GPIO_ResetBits(GPIOE,GPIO_Pin_9);
Delay(0XFF);
}
//12
if((tmp=tmp>>1)&&0x0001)
{
GPIO_SetBits(GPIOE,GPIO_Pin_11);
Delay(0XFF);
}
else
{
GPIO_ResetBits(GPIOE,GPIO_Pin_11);
Delay(0XFF);
}
//13
if((tmp=tmp>>1)&&0x0001)
{
GPIO_SetBits(GPIOE,GPIO_Pin_13);
Delay(0XFF);
}
else
{
GPIO_ResetBits(GPIOE,GPIO_Pin_13);
Delay(0XFF);
}
//14
if((tmp=tmp>>1)&&0x0001)
{
GPIO_SetBits(GPIOE,GPIO_Pin_15);
Delay(0XFF);
}
else
{
GPIO_ResetBits(GPIOE,GPIO_Pin_15);
Delay(0XFF);
}
//15
if((tmp=tmp>>1)&&0x0001)
{
GPIO_SetBits(GPIOD,GPIO_Pin_9);
Delay(0XFF);
}
else
{
GPIO_ResetBits(GPIOD,GPIO_Pin_9);
Delay(0XFF);
}
}
/************************************
清屏
入口参数: b_color是背景颜色。
出口参数: 无
说明:使用背景颜色清除TFT模块屏幕的全部显示内容。在调用這个函数是无效
*************************************/
void GUI_clearscreen(uint16_t b_color)
{
uint16_t i,j;
TFT_SetWindow(0,0,240,320);
for (i=0;i<320;i++)
{
for(j=0;j<240;j++)
{
Write_data(b_color);
}
}GPIO_SetBits(GPIOG,GPIO_Pin_14);
}
example_study_TFT.part01.rar
2015-5-14 15:59 上传
点击文件名下载附件 |
|