📄 ili9481.c
字号:
/******************** (C) COPYRIGHT 2007 STMicroelectronics ********************
* File Name : lcd.c
* Author : MCD Application Team
* Version : V1.1
* Date : 11/26/2007
* Description : This file includes the LCD driver for AM-240320LTNQW00H
* (LCD_HX8312) and AM-240320L8TNQW00H (LCD_ILI9320)
* Liquid Crystal Display Module of STM3210B-EVAL board.
********************************************************************************
* THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
* CONTENT OF SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*******************************************************************************/
/* Includes ------------------------------------------------------------------*/
#include "ili9481.h"
#include "stm32f10x.h"
//#include "GBK2312.h"
#include "ascii.h"
#include "delay.h"
unsigned int Txt_Color = WHITE,Back_Ground =BLUE ;
//#define START_BYTE 0x70
//#define SET_INDEX 0x00
//#define READ_STATUS 0x01
//#define WRITE_REG 0x02
//#define READ_REG 0x03
#define LCD_Data_Addr ((uint32_t)0x60030000)
#define LCD_Reg_Addr ((uint32_t)0x60000000)
//#define LCD_WriteRAM_Ready() {GPIOD->BSRR=GPIO_Pin_7;}
/*typedef struct
{
vu16 LCD_REG;
vu16 LCD_RAM;
} LCD_TypeDef;
#define LCD_BASE (u32)(0x60000000)
#define LCD ((LCD_TypeDef *) LCD_BASE)
*/
//#define LCD_nRST GPIOC->BRR=GPIO_Pin_12
//#define LCD_nCS GPIOD->BRR=GPIO_Pin_7
//#define LCD_nRS GPIOD->BRR=GPIO_Pin_11
//#define LCD_nWR GPIOD->BRR=GPIO_Pin_5
//#define LCD_nRD GPIOD->BRR=GPIO_Pin_4
/*******************************************************************************
* Function Name : LCD_CtrlLinesConfig
* Description : Configures LCD control lines in Output Push-Pull mode.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void TFT_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_FSMC, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC |
RCC_APB2Periph_AFIO|RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE , ENABLE);
/*===========GPIO For the LCD_Bus========================*/
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_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
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_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOE, &GPIO_InitStructure);
/*===========GPIO For the LCD_RESET=======================*/
/**
* PC12 -> LCD_RESET
*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 ;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 ;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOC, &GPIO_InitStructure);
/*===========GPIO For the Control========================*/
/**
* PD13 -> Light
* PD4 -> RD , PD5 -> WE
* PD7 -> CS , PD11 -> RS
*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 |GPIO_Pin_5 | GPIO_Pin_7 | GPIO_Pin_11;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIOD->BSRR = GPIO_Pin_13; //打开背光LED
}
void FSMC_LCD_Init(void)
{
FSMC_NORSRAMInitTypeDef FSMC_NORSRAMInitStructure;
FSMC_NORSRAMTimingInitTypeDef FSMC_TimingInitStructure;
FSMC_TimingInitStructure.FSMC_AddressSetupTime =0;
FSMC_TimingInitStructure.FSMC_AddressHoldTime = 0;
FSMC_TimingInitStructure.FSMC_DataSetupTime = 1;
FSMC_TimingInitStructure.FSMC_BusTurnAroundDuration = 0;
FSMC_TimingInitStructure.FSMC_CLKDivision = 0;
FSMC_TimingInitStructure.FSMC_DataLatency = 0;
FSMC_TimingInitStructure.FSMC_AccessMode = FSMC_AccessMode_A;
FSMC_NORSRAMInitStructure.FSMC_Bank = FSMC_Bank1_NORSRAM1;
FSMC_NORSRAMInitStructure.FSMC_DataAddressMux = FSMC_DataAddressMux_Disable;
FSMC_NORSRAMInitStructure.FSMC_MemoryType = FSMC_MemoryType_SRAM;
FSMC_NORSRAMInitStructure.FSMC_MemoryDataWidth = FSMC_MemoryDataWidth_16b;
FSMC_NORSRAMInitStructure.FSMC_BurstAccessMode = FSMC_BurstAccessMode_Disable;
FSMC_NORSRAMInitStructure.FSMC_WaitSignalPolarity = FSMC_WaitSignalPolarity_Low;
FSMC_NORSRAMInitStructure.FSMC_WrapMode = FSMC_WrapMode_Disable;
FSMC_NORSRAMInitStructure.FSMC_WaitSignalActive = FSMC_WaitSignalActive_BeforeWaitState;
FSMC_NORSRAMInitStructure.FSMC_WriteOperation = FSMC_WriteOperation_Enable;
FSMC_NORSRAMInitStructure.FSMC_WaitSignal = FSMC_WaitSignal_Disable;
FSMC_NORSRAMInitStructure.FSMC_ExtendedMode = FSMC_ExtendedMode_Disable;
FSMC_NORSRAMInitStructure.FSMC_WriteBurst = FSMC_WriteBurst_Disable;
FSMC_NORSRAMInitStructure.FSMC_ReadWriteTimingStruct = &FSMC_TimingInitStructure;
FSMC_NORSRAMInitStructure.FSMC_WriteTimingStruct = &FSMC_TimingInitStructure;
FSMC_NORSRAMInit(&FSMC_NORSRAMInitStructure);
FSMC_NORSRAMCmd(FSMC_Bank1_NORSRAM1, ENABLE);
}
/*---------------写数据到GRAM----------------*/
void TFT_WriteData(unsigned int val)
{
*(volatile uint16_t *)(LCD_Data_Addr) = val;
// LCD->LCD_RAM= val;
}
/*---------------写索引寄存器----------------*/
void TFT_WriteIndex(unsigned int index)
{
*(volatile uint16_t *)(LCD_Reg_Addr) = index;
//LCD->LCD_REG= index;
}
/*******************************************************************************
* Function Name : LCD_WriteReg
* Description : Writes to the selected LCD register.
* Input : - LCD_Reg: address of the selected register.
* - LCD_RegValue: value to write to the selected register.
* Output : None
* Return : None
*******************************************************************************/
void TFT_WriteRegister(unsigned int LCD_Reg, unsigned int LCD_RegValue)
{
*(volatile uint16_t *) (LCD_Reg_Addr) = LCD_Reg;
*(volatile uint16_t *) (LCD_Data_Addr) = LCD_RegValue;
//LCD->LCD_RAM= LCD_RegValue;
//LCD->LCD_REG = LCD_Reg;
}
/*---------------写数据准备-------------------*/
void TFT_WriteRAM_Prepare(void)
{
TFT_WriteIndex(R2C);
}
u16 TFT_ReadReg(u8 LCD_Reg)
{
*(volatile uint16_t *) (LCD_Reg_Addr) = LCD_Reg;
return (*(volatile uint16_t *) (LCD_Data_Addr) );
/* Write 16-bit Index (then Read Reg) */
//LCD->LCD_REG = LCD_Reg;
/* Read 16-bit Reg */
//return (LCD->LCD_RAM);
}
/*******************************************************************************
* Function Name : LCD_ReadRAM
* Description : Reads the LCD RAM.
* Input : None
* Output : None
* Return : LCD RAM Value.
*******************************************************************************/
u16 TFT_ReadRAM(void)
{
*(volatile uint16_t *) (LCD_Reg_Addr) =R2C;
return (*(volatile uint16_t *) (LCD_Data_Addr) );
//LCD->LCD_REG =R22;
//return LCD->LCD_RAM;
}
void TFT_Delay(vu32 nCount)
{
int i;
for(i=0;i<20000;i++)
for(; nCount != 0; nCount--);
}
void TFT_Init_Config(void)
{
/* Configure the LCD Control pins --------------------------------------------*/
TFT_GPIO_Config();
GPIOC->BRR = GPIO_Pin_12;
Delay(0x5FFFF);
GPIOC->BSRR = GPIO_Pin_12;
Delay(0x5FFFF);
//************* Start Initial Sequence **********//
TFT_WriteIndex(R11);//exit sleep mode
//TFT_Delay(1000000);
TFT_WriteIndex(R13);//Enter_normal_mode
TFT_WriteIndex(0X00D0);
TFT_WriteData(0x0007);
TFT_WriteData(0x0040); //41
TFT_WriteData(0x001c);//1e
TFT_WriteIndex(0x00D1);
TFT_WriteData(0x0000);
TFT_WriteData(0x0018);
TFT_WriteData(0x001d);
TFT_WriteIndex(0x00D2);
TFT_WriteData(0x0001);
TFT_WriteData(0x0001);//11
TFT_WriteIndex(0x00C0);
TFT_WriteData(0x0000);
TFT_WriteData(0x003B);
TFT_WriteData(0x0000);
TFT_WriteData(0x0002);
TFT_WriteData(0x0011);
TFT_WriteIndex(0x00C1);
TFT_WriteData(0x0010);
TFT_WriteData(0x000B);
TFT_WriteData(0x0088);
TFT_WriteIndex(0x00C5);
TFT_WriteData(0x0000);
TFT_WriteIndex(0x00C8);
TFT_WriteData(0x0000);
TFT_WriteData(0x0030); //30
TFT_WriteData(0x0036);
TFT_WriteData(0x0045);
TFT_WriteData(0x0004);
TFT_WriteData(0x0016);
TFT_WriteData(0x0037);
TFT_WriteData(0x0075);
TFT_WriteData(0x0077);
TFT_WriteData(0x0054);
TFT_WriteData(0x000f);
TFT_WriteData(0x0000);
TFT_WriteIndex(0x00E4);
TFT_WriteData(0x00A0);
TFT_WriteIndex(0x00F0);
TFT_WriteData(0x0001);
TFT_WriteIndex(0x00F3);
TFT_WriteData(0x0040);
TFT_WriteData(0x000A);
TFT_WriteIndex(0x00F7);
TFT_WriteData(0x0080);
TFT_WriteIndex(0x0036);
TFT_WriteData(0x000a);
TFT_WriteIndex(0x003A);
TFT_WriteData(0x0055);
TFT_WriteIndex(0x002A);
TFT_WriteData(0x0000);
TFT_WriteData(0x0000);
TFT_WriteData(0x0001);
TFT_WriteData(0x003F);
TFT_WriteIndex(0x002B);
TFT_WriteData(0x0000);
TFT_WriteData(0x0000);
TFT_WriteData(0x0001);
TFT_WriteData(0x00df);
TFT_Delay(1200000);
TFT_WriteIndex(0x0029);
TFT_WriteIndex(0x002C);
}
void lcd_set_cursor(unsigned int row, unsigned int column)
{
/*TFT_WriteRegister(0x002B, row); //v垂直方向
TFT_WriteRegister(0x002A, column); //h水平方向
TFT_WriteIndex(0x002C);
*/
TFT_WriteIndex(R2A);
TFT_WriteData(column>>8);
TFT_WriteData(RFF&column);
TFT_WriteData(R01);
TFT_WriteData(R3F);
TFT_WriteIndex(R2B);
TFT_WriteData(row>>8);
TFT_WriteData(RFF&row);
TFT_WriteData(R01);
TFT_WriteData(RDF);
TFT_WriteIndex(R2C);
}
/****************************************************************************
* 名 称:void TFT_SetCursor(u16 x,u16 y)
* 功 能:设置屏幕座标
* 入口参数:x 行座标
* y 列座标
* 出口参数:无
* 说 明:
* 调用方法:TFT_SetCursor(10,10);
****************************************************************************/
void TFT_SetCursor(u16 x,u16 y)
{
TFT_WriteIndex(R2A);
TFT_WriteData(x>>8);
TFT_WriteData(RFF&x);
TFT_WriteData(R01);
TFT_WriteData(R3F);
TFT_WriteIndex(R2B);
TFT_WriteData(y>>8);
TFT_WriteData(RFF&y);
TFT_WriteData(R01);
TFT_WriteData(RDF);
}
void TFTSetXY(u16 x,u16 y)
{
TFT_SetCursor(x,y);
TFT_WriteIndex(R2C);
}
/****************************************************************************
* 名 称:void ili9320_SetPoint(u16 x,u16 y,u16 point)
* 功 能:在指定座标画点
* 入口参数:x 行座标
* y 列座标
* point 点的颜色
* 出口参数:无
* 说 明:
* 调用方法:ili9320_SetPoint(10,10,0x0fe0);
****************************************************************************/
void TFT_SetPoint(u16 x,u16 y,u16 point)
{
TFT_SetCursor(x,y);
TFT_WriteIndex(R2C);
TFT_WriteData(point);
}
/****************************************************************************
* 名 称:u16 ili9320_BGR2RGB(u16 c)
* 功 能:RRRRRGGGGGGBBBBB 改为 BBBBBGGGGGGRRRRR 格式
* 入口参数:c BRG 颜色值
* 出口参数:RGB 颜色值
* 说 明:内部函数调用
* 调用方法:
****************************************************************************/
u16 TFT_BGR2RGB(u16 c)
{
u16 r, g, b, rgb;
b = (c>>0) & R1F;
g = (c>>5) & R3F;
r = (c>>11) & R1F;
rgb = (b<<11) + (g<<5) + (r<<0);
return( rgb );
}
/****************************************************************************
* 名 称:void ili9320_Clear(u16 dat)
* 功 能:将屏幕填充成指定的颜色,如清屏,则填充 0xffff
* 入口参数:dat 填充值
* 出口参数:无
* 说 明:
* 调用方法:ili9320_Clear(0xffff);
****************************************************************************/
void TFT_Clear(u16 dat)
{
u32 i,j;
TFTSetXY(0,0);
TFT_WriteIndex(R2C);
for(i=480;i;i--)
{
for(j=320;j;j--)
{
TFT_WriteData(dat);
}
}
}
/****************************************************************************
* 名 称:u16 ili9320_GetPoint(u16 x,u16 y)
* 功 能:获取指定座标的颜色值
* 入口参数:x 行座标
* y 列座标
* 出口参数:当前座标颜色值
* 说 明:
* 调用方法:i=ili9320_GetPoint(10,10);
****************************************************************************/
u16 TFT_GetPoint(u16 x,u16 y)
{
TFT_SetCursor(x,y);
return (TFT_BGR2RGB(TFT_ReadRAM()));
}
/****************************************************************************
* 名 称:void ili9320_BackLight(u8 status)
* 功 能:开、关液晶背光
* 入口参数:status 1:背光开 0:背光关
* 出口参数:无
* 说 明:
* 调用方法:ili9320_BackLight(1);
****************************************************************************/
void TFT_BackLight(u8 status)
{
if ( status >= 1 )
{
GPIOD->BSRR = GPIO_Pin_13;
}
else
{
GPIOD->BRR = GPIO_Pin_13;
}
}
/****************************************************************************
* 名 称:void ili9320_DrawPicture(u16 StartX,u16 StartY,u16 EndX,u16 EndY,u16 *pic)
* 功 能:在指定座标范围显示一副图片
* 入口参数:StartX 行起始座标
* StartY 列起始座标
* EndX 行结束座标
* EndY 列结束座标
pic 图片头指针
* 出口参数:无
* 说 明:图片取模格式为水平扫描,16位颜色模式
* 调用方法:ili9320_DrawPicture(0,0,100,100,(u16*)demo);
****************************************************************************//*
void TFT_DrawPicture(u16 StartX,u16 StartY,u16 EndX,u16 EndY,u8 const *pic)
{
u32 i;
u16 j,k;
TFTSetXY(StartX,StartY);
TFT_WriteRAM_Prepare();
TFT_WriteIndex(R2C);
for (i=0;i<(EndX*EndY);i++)
{
j=*pic++;
TFT_WriteData((*pic<<8)|j);
*pic++;
}
}
*/
/**************************************************************
** 函数名:TFTShowBmp
** 功能:画BMP
** 注意事项:规定点阵BUF是先横后竖的排列
***************************************************************/
void TFT_ShowBmp(u16 x_star,u16 y_star,u16 length,u16 height,u8 *buf)
{
u16 i=0,length_null=0;
u16 j=0,k;
//CHECK_LOC(x_star,y_star);
//如果超出屏幕范围,则计算超出的长度
if(x_star+length>MAX_X)
{
length_null=x_star+length-MAX_X;
}
//如果超出屏幕范围,则计算可用的高度
if(y_star+height>MAX_Y)
{
height-=y_star+height-MAX_Y;
}
TFT_WriteIndex(R2C);
for(j=0;j<height;j++)
{
TFTSetXY(x_star,y_star);//Y0会跟随变化,所以要放到循环中执行
for(i=0;i<length;i++)
{
k=*buf++;
TFT_WriteData((*buf<<8)|k);
*buf++;
}
y_star++;
buf+=length_null;//偏移多出的那段BMP 跳过去
}
}
void TFTShowBmp(u16 x_star,u16 y_star,u16 length,u16 height,u16 *buf)
{
u16 i=0,length_null=0;
u16 j=0;
//CHECK_LOC(x_star,y_star);
//如果超出屏幕范围,则计算超出的长度
if(x_star+length>MAX_X)
{
length_null=x_star+length-MAX_X;
}
//如果超出屏幕范围,则计算可用的高度
if(y_star+height>MAX_Y)
{
height-=y_star+height-MAX_Y;
}
for(j=0;j<height;j++)
{
TFTSetXY(x_star,y_star);//Y0会跟随变化,所以要放到循环中执行
for(i=0;i<length;i++)
{
TFT_WriteData(*buf++);
}
y_star++;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -