📄 tft_lcd.c
字号:
if (x2 >= x1)
{
dx = x2 - x1;
}
else
{
dx = x1 - x2;
}
/* dy = abs ( y2 - y1 ); */
if (y2 >= y1)
{
dy = y2 - y1;
}
else
{
dy = y1 - y2;
}
if ( dx < dy ) /*如果dy为计长方向,则交换纵横坐标。*/
{
uint16_t temp;
iTag = 1 ;
temp = x1; x1 = y1; y1 = temp;
temp = x2; x2 = y2; y2 = temp;
temp = dx; dx = dy; dy = temp;
}
tx = x2 > x1 ? 1 : -1 ; /*确定是增1还是减1*/
ty = y2 > y1 ? 1 : -1 ;
x = x1 ;
y = y1 ;
inc1 = 2 * dy ;
inc2 = 2 * ( dy - dx );
d = inc1 - dx ;
while ( x != x2 ) /*循环画点*/
{
if ( d < 0 )
{
d += inc1 ;
}
else
{
y += ty ;
d += inc2 ;
}
if ( iTag )
{
LCD_PutPixel ( y , x , c ) ;
}
else
{
LCD_PutPixel ( x , y , c ) ;
}
x += tx ;
}
return;
}
/*******************************************************************************
* 函数名: LCD_DrawLine
* 参 数: Xpos :X坐标
* YPos :Y坐标
* Length :长度
* Direction :方向(Horizontal,Vertical)
* 返 回: 无
* 功 能: 在LCD上画1条线, 只支持水平线和垂直线
*/
void LCD_DrawLine(uint16_t Xpos, uint16_t Ypos, uint16_t Length, uint8_t Direction)
{
uint32_t i = 0;
/* 设置线的起始坐标 */
LCD_SetCursor(Xpos, Ypos);
if (Direction == Horizontal) /* 水平绘制 */
{
LCD_WriteRAM_Prepare();
for (i = 0; i < Length; i++)
{
LCD_WriteRAM(s_TextColor);
}
}
else /* 垂直绘制 */
{
for (i = 0; i < Length; i++)
{
LCD_WriteRAM_Prepare();
LCD_WriteRAM(s_TextColor);
Ypos++;
LCD_SetCursor(Xpos, Ypos);
}
}
}
/*******************************************************************************
* 函数名: LCD_DrawPoints
* 参 数: _x : x坐标数组
* _y : y坐标数组
* _Size : 坐标点个数
* _Color :颜色
* 返 回: 无
* 功 能: 在LCD上画一组点
*/
void LCD_DrawPoints(uint16_t *x, uint16_t *y, uint16_t Size, uint16_t Color)
{
#if 1
uint16_t i;
for (i = 0 ; i < Size - 1; i++)
{
LCD_BresenhamLine(x[i], y[i], x[i + 1], y[i + 1], Color);
}
#else
uint16_t i;
for (i = 0 ; i < Size; i++)
{
LCD_PutPixel(x[i], y[i], Color);
}
#endif
}
/*******************************************************************************
* 函数名: LCD_DrawRect
* 参 数: Xpos :X坐标
* YPos :Y坐标
* Height :高度
* Width :宽度
* 返 回: 无
* 功 能: 在LCD上画一个矩形框
*/
void LCD_DrawRect(uint16_t Xpos, uint16_t Ypos, uint8_t Height, uint16_t Width)
{
/*
---------------->---
|(Xpos,Ypos) |
V V Height
| |
---------------->---
Width
*/
LCD_DrawLine(Xpos, Ypos, Width, Horizontal); /* 顶 */
LCD_DrawLine(Xpos, Ypos + Height, Width, Horizontal); /* 底 */
LCD_DrawLine(Xpos, Ypos, Height, Vertical); /* 左 */
LCD_DrawLine(Xpos + Width, Ypos, Height + 1, Vertical); /* 右 */
}
/*******************************************************************************
* 函数名: LCD_DrawCircle
* 参 数: Xpos :X坐标
* Radius :圆的半径
* 返 回: 无
* 功 能: 在LCD上画一个圆
*/
void LCD_DrawCircle(uint16_t Xpos, uint16_t Ypos, uint16_t Radius)
{
int32_t D; /* Decision Variable */
uint32_t CurX; /* 当前 X 值 */
uint32_t CurY; /* 当前 Y 值 */
D = 3 - (Radius << 1);
CurX = 0;
CurY = Radius;
while (CurX <= CurY)
{
LCD_SetCursor(Xpos + CurX, Ypos + CurY);
LCD_WriteRAM_Prepare(); /* Prepare to write GRAM */
LCD_WriteRAM(s_TextColor);
LCD_SetCursor(Xpos + CurX, Ypos - CurY);
LCD_WriteRAM_Prepare(); /* Prepare to write GRAM */
LCD_WriteRAM(s_TextColor);
LCD_SetCursor(Xpos - CurX, Ypos + CurY);
LCD_WriteRAM_Prepare(); /* Prepare to write GRAM */
LCD_WriteRAM(s_TextColor);
LCD_SetCursor(Xpos - CurX, Ypos - CurY);
LCD_WriteRAM_Prepare(); /* Prepare to write GRAM */
LCD_WriteRAM(s_TextColor);
LCD_SetCursor(Xpos + CurY, Ypos + CurX);
LCD_WriteRAM_Prepare(); /* Prepare to write GRAM */
LCD_WriteRAM(s_TextColor);
LCD_SetCursor(Xpos + CurY, Ypos - CurX);
LCD_WriteRAM_Prepare(); /* Prepare to write GRAM */
LCD_WriteRAM(s_TextColor);
LCD_SetCursor(Xpos - CurY, Ypos + CurX);
LCD_WriteRAM_Prepare(); /* Prepare to write GRAM */
LCD_WriteRAM(s_TextColor);
LCD_SetCursor(Xpos - CurY, Ypos - CurX);
LCD_WriteRAM_Prepare(); /* Prepare to write GRAM */
LCD_WriteRAM(s_TextColor);
if (D < 0)
{
D += (CurX << 2) + 6;
}
else
{
D += ((CurX - CurY) << 2) + 10;
CurY--;
}
CurX++;
}
}
/*******************************************************************************
* 函数名: LCD_DrawMonoPict
* 参 数: Pict : 图片点阵指针
* 返 回: 无
* 功 能: 在LCD上画一个单色图片
*/
void LCD_DrawMonoPict(const uint32_t *Pict)
{
uint32_t index = 0, i = 0;
LCD_SetCursor(0, 400);
LCD_WriteRAM_Prepare(); /* Prepare to write GRAM */
for (index = 0; index < 2400; index++)
{
for (i = 0; i < 32; i++)
{
if ((Pict[index] & (1 << i)) == 0x00)
{
LCD_WriteRAM(s_BackColor);
}
else
{
LCD_WriteRAM(s_TextColor);
}
}
}
}
/*******************************************************************************
* 函数名: LCD_WriteBMP
* 参 数: ptr : 图片点阵指针
* 返 回: 无
* 功 能: 在LCD上显示一个BMP位图
*/
void LCD_WriteBMP(const uint16_t *ptr)
{
uint32_t index = 0;
const uint16_t *p;
/* 修改扫描方向 */
/* Set GRAM write direction and BGR = 1 */
/* I/D=00 (Horizontal : decrement, Vertical : decrement) */
/* AM=1 (address is updated in vertical writing direction) */
//LCD_WriteReg(R3, 0x1008);
LCD_WriteRAM_Prepare();
p = ptr;
for (index = 0; index < 400 * 240; index++)
{
/*
armfly : 进行优化, 函数就地展开
LCD_WriteRAM(ptr[index]);
此处可考虑用DMA操作
*/
LCD->LCD_RAM = *p++;
}
/* 还原扫描方向 */
/* Set GRAM write direction and BGR = 1 */
/* I/D = 01 (Horizontal : increment, Vertical : decrement) */
/* AM = 1 (address is updated in vertical writing direction) */
//LCD_WriteReg(R3, 0x1018);
}
/*******************************************************************************
* 函数名: LCD_WriteReg
* 参 数: LCD_Reg :寄存器地址; LCD_RegValue : 寄存器值
* 返 回: 无
* 功 能: 修改LCD控制器的寄存器的值
*/
void LCD_WriteReg(__IO uint16_t LCD_Reg, uint16_t LCD_RegValue)
{
/* Write 16-bit Index, then Write Reg */
LCD->LCD_REG = LCD_Reg;
/* Write 16-bit Reg */
LCD->LCD_RAM = LCD_RegValue;
}
/*******************************************************************************
* 函数名: LCD_ReadReg
* 参 数: LCD_Reg :寄存器地址
* 返 回: 寄存器的值
* 功 能: 读LCD控制器的寄存器的值
*/
uint16_t LCD_ReadReg(__IO uint16_t LCD_Reg)
{
/* Write 16-bit Index (then Read Reg) */
LCD->LCD_REG = LCD_Reg;
/* Read 16-bit Reg */
return (LCD->LCD_RAM);
}
/*******************************************************************************
* 函数名: LCD_WriteRAM_Prepare
* 参 数: 无
* 返 回: 无
* 功 能: 写显存前的准备,即设置显存寄存器地址。
*/
void LCD_WriteRAM_Prepare(void)
{
LCD->LCD_REG = 0x202;
}
/*******************************************************************************
* 函数名: LCD_WriteRAM
* 参 数: RGB_Code : 颜色代码
* 返 回: 无
* 功 能: 写显存,显存地址自动增加。适用于连续写。
*/
void LCD_WriteRAM(uint16_t RGB_Code)
{
/* Write 16-bit GRAM Reg */
LCD->LCD_RAM = RGB_Code;
}
/*******************************************************************************
* 函数名: LCD_WriteRAM1
* 参 数: RGB_Code : 颜色代码
* 返 回: 无
* 功 能: 写显存,显存地址自动增加。适用于写单个像素。
*/
void LCD_WriteRAM1(uint16_t RGB_Code)
{
LCD->LCD_REG = 0x202;
/* Write 16-bit GRAM Reg */
LCD->LCD_RAM = RGB_Code;
}
/*******************************************************************************
* 函数名: LCD_ReadRAM
* 参 数: 无
* 返 回: 显存数据
* 功 能: 读显存,地址自动增加
*/
uint16_t LCD_ReadRAM(void)
{
#if 0
/* Write 16-bit Index (then Read Reg) */
LCD->LCD_REG = 0x202; /* Select GRAM Reg */
#endif
/* Read 16-bit Reg */
return LCD->LCD_RAM;
}
/*******************************************************************************
* 函数名: LCD_DisplayOn
* 参 数: 无
* 返 回: 无
* 功 能: 打开显示
*/
void LCD_DisplayOn(void)
{
/* Display On */
LCD_WriteReg(R7, 0x0173); /* 262K color and display ON */
}
/*******************************************************************************
* 函数名: LCD_DisplayOff
* 参 数: 无
* 返 回: 无
* 功 能: 关闭显示
*/
void LCD_DisplayOff(void)
{
/* Display Off */
LCD_WriteReg(R7, 0x0);
}
/*******************************************************************************
* 函数名: LCD_CtrlLinesConfig
* 参 数: 无
* 返 回: 无
* 功 能: 配置LCD控制口线,FSMC管脚设置为复用功能
*/
static void LCD_CtrlLinesConfig(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* 使能 FSMC, GPIOD, GPIOE, GPIOF, GPIOG 和 AFIO 时钟 */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_FSMC, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE |
RCC_APB2Periph_GPIOF | RCC_APB2Periph_GPIOG |
RCC_APB2Periph_AFIO, ENABLE);
/* 设置 PD.00(D2), PD.01(D3), PD.04(NOE), PD.05(NWE), PD.08(D13), PD.09(D14),
PD.10(D15), PD.14(D0), PD.15(D1) 为复用推挽输出 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_4 | GPIO_Pin_5 |
GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_14 |
GPIO_Pin_15; // | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOD, &GPIO_InitStructure);
/* 设置 PE.07(D4), PE.08(D5), PE.09(D6), PE.10(D7), PE.11(D8), PE.12(D9), PE.13(D10),
PE.14(D11), PE.15(D12) 为复用推挽输出 */
/* PE3,PE4 用于A19, A20, STM32F103ZE-EK(REV 2.0)必须使能 */
/* PE5,PE6 用于A19, A20, STM32F103ZE-EK(REV 2.0)必须使能 */
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_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6;
GPIO_Init(GPIOE, &GPIO_InitStructure);
/* 设置 PF.00(A0 (RS)) 为复用推挽输出 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_Init(GPIOF, &GPIO_InitStructure);
/* 设置 PG.12(NE4 (LCD/CS)) 为复用推挽输出 - CE3(LCD /CS) */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_Init(GPIOG, &GPIO_InitStructure);
}
/*******************************************************************************
* 函数名: LCD_FSMCConfig
* 参 数: 无
* 返 回: 无
* 功 能: 配置FSMC并口访问时序
*/
static void LCD_FSMCConfig(void)
{
FSMC_NORSRAMInitTypeDef FSMC_NORSRAMInitStructure;
FSMC_NORSRAMTimingInitTypeDef FSMC_NORSRAMTimingInitStructure;
/*-- FSMC Configuration ------------------------------------------------------*/
/*----------------------- SRAM Bank 4 ----------------------------------------*/
/* FSMC_Bank1_NORSRAM4 configuration */
FSMC_NORSRAMTimingInitStructure.FSMC_AddressSetupTime = 1;
FSMC_NORSRAMTimingInitStructure.FSMC_AddressHoldTime = 0;
FSMC_NORSRAMTimingInitStructure.FSMC_DataSetupTime = 2;
FSMC_NORSRAMTimingInitStructure.FSMC_BusTurnAroundDuration = 0;
FSMC_NORSRAMTimingInitStructure.FSMC_CLKDivision = 0;
FSMC_NORSRAMTimingInitStructure.FSMC_DataLatency = 0;
FSMC_NORSRAMTimingInitStructure.FSMC_AccessMode = FSMC_AccessMode_B;
/* Color LCD configuration ------------------------------------
LCD configured as follow:
- Data/Address MUX = Disable
- Memory Type = SRAM
- Data Width = 16bit
- Write Operation = Enable
- Extended Mode = Enable
- Asynchronous Wait = Disable */
FSMC_NORSRAMInitStructure.FSMC_Bank = FSMC_Bank1_NORSRAM4;
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_NORSRAMTimingInitStructure;
FSMC_NORSRAMInitStructure.FSMC_WriteTimingStruct = &FSMC_NORSRAMTimingInitStructure;
FSMC_NORSRAMInit(&FSMC_NORSRAMInitStructure);
/* - BANK 3 (of NOR/SRAM Bank 0~3) is enabled */
FSMC_NORSRAMCmd(FSMC_Bank1_NORSRAM4, ENABLE);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -