赞
踩
目录
1.8寸液晶屏 LCD SPI串口显示屏模块 TFT彩屏
本博客选用的单片机型号:STM32F103C8T6
该款单片机应用就多,相对性价比较高。
由于液晶模块采用的是SPI通信,但这里均使用模拟SPI的方式进行通信,实现液晶显示(后面博客会使用STM32自带的SPI功能实现数据传输)。
TFT真彩屏 液晶模块 | STM32F103C8T6 |
GND | 电源地 |
VCC | 接5V或3.3v电源 |
SCL | PA6(SCL) |
SDA | PA7(SDA) |
RES | PB0 |
DC | PB1 |
CS | PB10 |
BL | PB11 |
因为液晶模块主要用于显示功能,而且是模拟SPI通信,这里的所以的IO口均设置成推挽输出模式。引脚接线是依据需要修改的,但需要大家修改完全,不然不能成功。
标准库的开发环境:
HAL库开发环境:
STMCUBEMX、KEIL5
由于工程文件较多,只给出部分核心代码,供大家参考,博文最后会有全套代码给大家。
main.c
- #include "stm32f10x.h"
- #include "delay.h"
- #include "QDTFT_demo.h"
- #include "Lcd_Driver.h"
- #include "GUI.h"
- #include "stdio.h"
- #include "string.h"
-
-
- int main(void)
- {
- SystemInit(); //System init.
- delay_init(72);//Delay init.
- Lcd_Init();
- LCD_BL_SET;//通过IO控制背光亮
-
- while(1)
- {
- QDTFT_Test_Demo();
- }
-
- }

Lcd_Driver.c
- //
- //本程序只供学习使用,未经作者许可,不得用于其它任何用途
- //中景园电子
- //店铺地址:http://shop73023976.taobao.com/?spm=2013.1.0.0.M4PqC2
- //
- // 文 件 名 : main.c
- // 版 本 号 : v2.0
- // 作 者 : HuangKai
- // 生成日期 : 2014-0101
- // 最近修改 :
- // 功能描述 : 1.8寸LCD 4接口演示例程(STM32系列)
- /******************************************************************************
- //本程序适用与STM32F103C8
- // GND 电源地
- // VCC 接5V或3.3v电源
- // SCL 接PA6(SCL)
- // SDA 接PA7(SDA)
- // RES 接PB0
- // DC 接PB1
- // CS 接PB10
- // BL 接PB11
- *******************************************************************************/
- // 修改历史 :
- // 日 期 :
- // 作 者 : HuangKai
- // 修改内容 : 创建文件
- //版权所有,盗版必究。
- //Copyright(C) 中景园电子2014/3/16
- //All rights reserved
- //******************************************************************************/
-
-
- #include "stm32f10x.h"
- #include "Lcd_Driver.h"
- #include "LCD_Config.h"
- #include "delay.h"
-
- //液晶IO初始化配置
- void LCD_GPIO_Init(void)
- {
-
- GPIO_InitTypeDef GPIO_InitStructure;
-
- RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB ,ENABLE);
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0| GPIO_Pin_1| GPIO_Pin_10| GPIO_Pin_11;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
-
-
- RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA ,ENABLE);
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6| GPIO_Pin_7;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- }
- //向SPI总线传输一个8位数据
- void SPI_WriteData(u8 Data)
- {
- unsigned char i=0;
- for(i=8;i>0;i--)
- {
- if(Data&0x80)
- LCD_SDA_SET; //输出数据
- else LCD_SDA_CLR;
-
- LCD_SCL_CLR;
- LCD_SCL_SET;
- Data<<=1;
- }
- }
-
- //向液晶屏写一个8位指令
- void Lcd_WriteIndex(u8 Index)
- {
- //SPI 写命令时序开始
- LCD_CS_CLR;
- LCD_DC_CLR;
- SPI_WriteData(Index);
- LCD_CS_SET;
- }
-
- //向液晶屏写一个8位数据
- void Lcd_WriteData(u8 Data)
- {
- LCD_CS_CLR;
- LCD_DC_SET;
- SPI_WriteData(Data);
- LCD_CS_SET;
- }
- //向液晶屏写一个16位数据
- void LCD_WriteData_16Bit(u16 Data)
- {
- LCD_CS_CLR;
- LCD_DC_SET;
- SPI_WriteData(Data>>8); //写入高8位数据
- SPI_WriteData(Data); //写入低8位数据
- LCD_CS_SET;
- }
-
- void Lcd_WriteReg(u8 Index,u8 Data)
- {
- Lcd_WriteIndex(Index);
- Lcd_WriteData(Data);
- }
-
- void Lcd_Reset(void)
- {
- LCD_RST_CLR;
- delay_ms(100);
- LCD_RST_SET;
- delay_ms(50);
- }
-
- //LCD Init For 1.44Inch LCD Panel with ST7735R.
- void Lcd_Init(void)
- {
- LCD_GPIO_Init();
- Lcd_Reset(); //Reset before LCD Init.
-
- //LCD Init For 1.44Inch LCD Panel with ST7735R.
- Lcd_WriteIndex(0x11);//Sleep exit
- delay_ms (120);
-
- //ST7735R Frame Rate
- Lcd_WriteIndex(0xB1);
- Lcd_WriteData(0x01);
- Lcd_WriteData(0x2C);
- Lcd_WriteData(0x2D);
-
- Lcd_WriteIndex(0xB2);
- Lcd_WriteData(0x01);
- Lcd_WriteData(0x2C);
- Lcd_WriteData(0x2D);
-
- Lcd_WriteIndex(0xB3);
- Lcd_WriteData(0x01);
- Lcd_WriteData(0x2C);
- Lcd_WriteData(0x2D);
- Lcd_WriteData(0x01);
- Lcd_WriteData(0x2C);
- Lcd_WriteData(0x2D);
-
- Lcd_WriteIndex(0xB4); //Column inversion
- Lcd_WriteData(0x07);
-
- //ST7735R Power Sequence
- Lcd_WriteIndex(0xC0);
- Lcd_WriteData(0xA2);
- Lcd_WriteData(0x02);
- Lcd_WriteData(0x84);
- Lcd_WriteIndex(0xC1);
- Lcd_WriteData(0xC5);
-
- Lcd_WriteIndex(0xC2);
- Lcd_WriteData(0x0A);
- Lcd_WriteData(0x00);
-
- Lcd_WriteIndex(0xC3);
- Lcd_WriteData(0x8A);
- Lcd_WriteData(0x2A);
- Lcd_WriteIndex(0xC4);
- Lcd_WriteData(0x8A);
- Lcd_WriteData(0xEE);
-
- Lcd_WriteIndex(0xC5); //VCOM
- Lcd_WriteData(0x0E);
-
- Lcd_WriteIndex(0x36); //MX, MY, RGB mode
- Lcd_WriteData(0xC0);
-
- //ST7735R Gamma Sequence
- Lcd_WriteIndex(0xe0);
- Lcd_WriteData(0x0f);
- Lcd_WriteData(0x1a);
- Lcd_WriteData(0x0f);
- Lcd_WriteData(0x18);
- Lcd_WriteData(0x2f);
- Lcd_WriteData(0x28);
- Lcd_WriteData(0x20);
- Lcd_WriteData(0x22);
- Lcd_WriteData(0x1f);
- Lcd_WriteData(0x1b);
- Lcd_WriteData(0x23);
- Lcd_WriteData(0x37);
- Lcd_WriteData(0x00);
- Lcd_WriteData(0x07);
- Lcd_WriteData(0x02);
- Lcd_WriteData(0x10);
-
- Lcd_WriteIndex(0xe1);
- Lcd_WriteData(0x0f);
- Lcd_WriteData(0x1b);
- Lcd_WriteData(0x0f);
- Lcd_WriteData(0x17);
- Lcd_WriteData(0x33);
- Lcd_WriteData(0x2c);
- Lcd_WriteData(0x29);
- Lcd_WriteData(0x2e);
- Lcd_WriteData(0x30);
- Lcd_WriteData(0x30);
- Lcd_WriteData(0x39);
- Lcd_WriteData(0x3f);
- Lcd_WriteData(0x00);
- Lcd_WriteData(0x07);
- Lcd_WriteData(0x03);
- Lcd_WriteData(0x10);
-
- Lcd_WriteIndex(0x2a);
- Lcd_WriteData(0x00);
- Lcd_WriteData(0x00);
- Lcd_WriteData(0x00);
- Lcd_WriteData(0x7f);
-
- Lcd_WriteIndex(0x2b);
- Lcd_WriteData(0x00);
- Lcd_WriteData(0x00);
- Lcd_WriteData(0x00);
- Lcd_WriteData(0x9f);
-
- Lcd_WriteIndex(0xF0); //Enable test command
- Lcd_WriteData(0x01);
- Lcd_WriteIndex(0xF6); //Disable ram power save mode
- Lcd_WriteData(0x00);
-
- Lcd_WriteIndex(0x3A); //65k mode
- Lcd_WriteData(0x05);
-
-
- Lcd_WriteIndex(0x29);//Display on
- }
-
-
- /*************************************************
- 函数名:LCD_Set_Region
- 功能:设置lcd显示区域,在此区域写点数据自动换行
- 入口参数:xy起点和终点
- 返回值:无
- *************************************************/
- void Lcd_SetRegion(u16 x_start,u16 y_start,u16 x_end,u16 y_end)
- {
- Lcd_WriteIndex(0x2a);
- Lcd_WriteData(0x00);
- Lcd_WriteData(x_start+2);
- Lcd_WriteData(0x00);
- Lcd_WriteData(x_end+2);
-
- Lcd_WriteIndex(0x2b);
- Lcd_WriteData(0x00);
- Lcd_WriteData(y_start+1);
- Lcd_WriteData(0x00);
- Lcd_WriteData(y_end+1);
-
- Lcd_WriteIndex(0x2c);
-
- }
-
- /*************************************************
- 函数名:LCD_Set_XY
- 功能:设置lcd显示起始点
- 入口参数:xy坐标
- 返回值:无
- *************************************************/
- void Lcd_SetXY(u16 x,u16 y)
- {
- Lcd_SetRegion(x,y,x,y);
- }
-
-
- /*************************************************
- 函数名:LCD_DrawPoint
- 功能:画一个点
- 入口参数:无
- 返回值:无
- *************************************************/
- void Gui_DrawPoint(u16 x,u16 y,u16 Data)
- {
- Lcd_SetRegion(x,y,x+1,y+1);
- LCD_WriteData_16Bit(Data);
-
- }
-
- /*****************************************
- 函数功能:读TFT某一点的颜色
- 出口参数:color 点颜色值
- ******************************************/
- unsigned int Lcd_ReadPoint(u16 x,u16 y)
- {
- unsigned int Data;
- Lcd_SetXY(x,y);
-
- //Lcd_ReadData();//丢掉无用字节
- //Data=Lcd_ReadData();
- Lcd_WriteData(Data);
- return Data;
- }
- /*************************************************
- 函数名:Lcd_Clear
- 功能:全屏清屏函数
- 入口参数:填充颜色COLOR
- 返回值:无
- *************************************************/
- void Lcd_Clear(u16 Color)
- {
- unsigned int i,m;
- Lcd_SetRegion(0,0,X_MAX_PIXEL-1,Y_MAX_PIXEL-1);
- Lcd_WriteIndex(0x2C);
- for(i=0;i<X_MAX_PIXEL;i++)
- {
- for(m=0;m<Y_MAX_PIXEL;m++)
- {
- LCD_WriteData_16Bit(Color);
- }
- }
- }
-

实现效果:
CubeMX配置引脚为output,然后修改别名,如下:
时钟配置为32MHz,其它其实也可以,无所谓的。
main.c
- int main(void)
- {
- /* USER CODE BEGIN 1 */
-
- /* USER CODE END 1 */
-
- /* MCU Configuration--------------------------------------------------------*/
-
- /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
- HAL_Init();
-
- /* USER CODE BEGIN Init */
-
- /* USER CODE END Init */
-
- /* Configure the system clock */
- SystemClock_Config();
-
- /* USER CODE BEGIN SysInit */
-
- /* USER CODE END SysInit */
-
- /* Initialize all configured peripherals */
- MX_GPIO_Init();
- /* USER CODE BEGIN 2 */
- Lcd_Init();
- HAL_GPIO_WritePin(GPIOB, BL_Pin, GPIO_PIN_SET);//通过IO控制背光亮
- /* USER CODE END 2 */
-
- /* Infinite loop */
- /* USER CODE BEGIN WHILE */
- while (1)
- {
- /* USER CODE END WHILE */
-
- /* USER CODE BEGIN 3 */
- QDTFT_Test_Demo();
- }
- /* USER CODE END 3 */
- }

lcddriver.c
-
- #include "main.h"
-
-
- #define X_MAX_PIXEL 128
- #define Y_MAX_PIXEL 160
-
-
- //向SPI总线传输一个8位数据
- void SPI_WriteData(uint8_t Data)
- {
- unsigned char i=0;
- for(i=8;i>0;i--)
- {
- if(Data&0x80)
- HAL_GPIO_WritePin(GPIOA, SDA_Pin, GPIO_PIN_SET); //输出数据
- else HAL_GPIO_WritePin(GPIOA, SDA_Pin, GPIO_PIN_RESET);
-
- HAL_GPIO_WritePin(GPIOA, SCL_Pin, GPIO_PIN_RESET);
- HAL_GPIO_WritePin(GPIOA, SCL_Pin, GPIO_PIN_SET);
- Data<<=1;
- }
- }
-
- //向液晶屏写一个8位指令
- void Lcd_WriteIndex(uint8_t Index)
- {
- //SPI 写命令时序开始
- HAL_GPIO_WritePin(CS_GPIO_Port, CS_Pin, GPIO_PIN_RESET);
- HAL_GPIO_WritePin(DC_GPIO_Port, DC_Pin, GPIO_PIN_RESET);
- SPI_WriteData(Index);
-
- HAL_GPIO_WritePin(CS_GPIO_Port, CS_Pin, GPIO_PIN_SET);
- }
-
- //向液晶屏写一个8位数据
- void Lcd_WriteData(uint8_t Data)
- {
- HAL_GPIO_WritePin(CS_GPIO_Port, CS_Pin, GPIO_PIN_RESET);
- HAL_GPIO_WritePin(DC_GPIO_Port, DC_Pin, GPIO_PIN_SET);
- SPI_WriteData(Data);
-
- HAL_GPIO_WritePin(CS_GPIO_Port, CS_Pin, GPIO_PIN_SET);
- }
-
- //向液晶屏写一个16位数据
- void LCD_WriteData_16Bit(uint16_t Data)
- {
- uint8_t temp = (uint8_t)(Data>>8);
- HAL_GPIO_WritePin(CS_GPIO_Port, CS_Pin, GPIO_PIN_RESET);
- HAL_GPIO_WritePin(DC_GPIO_Port, DC_Pin, GPIO_PIN_SET);
-
- SPI_WriteData(temp); //写入高8位数据
- temp = (uint8_t)(Data);
- SPI_WriteData(temp); //写入低8位数据
- HAL_GPIO_WritePin(CS_GPIO_Port, CS_Pin, GPIO_PIN_SET);
- }
-
-
- void Lcd_WriteReg(uint8_t Index,uint8_t Data)
- {
- Lcd_WriteIndex(Index);
- Lcd_WriteData(Data);
- }
-
- void Lcd_Reset(void)
- {
- HAL_GPIO_WritePin(RES_GPIO_Port, RES_Pin, GPIO_PIN_RESET);
- HAL_Delay(100);
- HAL_GPIO_WritePin(RES_GPIO_Port, RES_Pin, GPIO_PIN_SET);
- HAL_Delay(50);
- }
-
- //LCD Init For 1.44Inch LCD Panel with ST7735R.
- void Lcd_Init(void)
- {
- Lcd_Reset(); //Reset before LCD Init.
-
- //LCD Init For 1.44Inch LCD Panel with ST7735R.
- Lcd_WriteIndex(0x11);//Sleep exit
- HAL_Delay(120);
-
- //ST7735R Frame Rate
- Lcd_WriteIndex(0xB1);
- Lcd_WriteData(0x01);
- Lcd_WriteData(0x2C);
- Lcd_WriteData(0x2D);
-
- Lcd_WriteIndex(0xB2);
- Lcd_WriteData(0x01);
- Lcd_WriteData(0x2C);
- Lcd_WriteData(0x2D);
-
- Lcd_WriteIndex(0xB3);
- Lcd_WriteData(0x01);
- Lcd_WriteData(0x2C);
- Lcd_WriteData(0x2D);
- Lcd_WriteData(0x01);
- Lcd_WriteData(0x2C);
- Lcd_WriteData(0x2D);
-
- Lcd_WriteIndex(0xB4); //Column inversion
- Lcd_WriteData(0x07);
-
- //ST7735R Power Sequence
- Lcd_WriteIndex(0xC0);
- Lcd_WriteData(0xA2);
- Lcd_WriteData(0x02);
- Lcd_WriteData(0x84);
- Lcd_WriteIndex(0xC1);
- Lcd_WriteData(0xC5);
-
- Lcd_WriteIndex(0xC2);
- Lcd_WriteData(0x0A);
- Lcd_WriteData(0x00);
-
- Lcd_WriteIndex(0xC3);
- Lcd_WriteData(0x8A);
- Lcd_WriteData(0x2A);
- Lcd_WriteIndex(0xC4);
- Lcd_WriteData(0x8A);
- Lcd_WriteData(0xEE);
-
- Lcd_WriteIndex(0xC5); //VCOM
- Lcd_WriteData(0x0E);
-
- Lcd_WriteIndex(0x36); //MX, MY, RGB mode
- Lcd_WriteData(0xC0);
-
- //ST7735R Gamma Sequence
- Lcd_WriteIndex(0xe0);
- Lcd_WriteData(0x0f);
- Lcd_WriteData(0x1a);
- Lcd_WriteData(0x0f);
- Lcd_WriteData(0x18);
- Lcd_WriteData(0x2f);
- Lcd_WriteData(0x28);
- Lcd_WriteData(0x20);
- Lcd_WriteData(0x22);
- Lcd_WriteData(0x1f);
- Lcd_WriteData(0x1b);
- Lcd_WriteData(0x23);
- Lcd_WriteData(0x37);
- Lcd_WriteData(0x00);
- Lcd_WriteData(0x07);
- Lcd_WriteData(0x02);
- Lcd_WriteData(0x10);
-
- Lcd_WriteIndex(0xe1);
- Lcd_WriteData(0x0f);
- Lcd_WriteData(0x1b);
- Lcd_WriteData(0x0f);
- Lcd_WriteData(0x17);
- Lcd_WriteData(0x33);
- Lcd_WriteData(0x2c);
- Lcd_WriteData(0x29);
- Lcd_WriteData(0x2e);
- Lcd_WriteData(0x30);
- Lcd_WriteData(0x30);
- Lcd_WriteData(0x39);
- Lcd_WriteData(0x3f);
- Lcd_WriteData(0x00);
- Lcd_WriteData(0x07);
- Lcd_WriteData(0x03);
- Lcd_WriteData(0x10);
-
- Lcd_WriteIndex(0x2a);
- Lcd_WriteData(0x00);
- Lcd_WriteData(0x00);
- Lcd_WriteData(0x00);
- Lcd_WriteData(0x7f);
-
- Lcd_WriteIndex(0x2b);
- Lcd_WriteData(0x00);
- Lcd_WriteData(0x00);
- Lcd_WriteData(0x00);
- Lcd_WriteData(0x9f);
-
- Lcd_WriteIndex(0xF0); //Enable test command
- Lcd_WriteData(0x01);
- Lcd_WriteIndex(0xF6); //Disable ram power save mode
- Lcd_WriteData(0x00);
-
- Lcd_WriteIndex(0x3A); //65k mode
- Lcd_WriteData(0x05);
-
-
- Lcd_WriteIndex(0x29);//Display on
- }
-
- /*************************************************
- 函数名:LCD_Set_Region
- 功能:设置lcd显示区域,在此区域写点数据自动换行
- 入口参数:xy起点和终点
- 返回值:无
- *************************************************/
- void Lcd_SetRegion(uint16_t x_start,uint16_t y_start,uint16_t x_end,uint16_t y_end)
- {
- Lcd_WriteIndex(0x2a);
- Lcd_WriteData(0x00);
- Lcd_WriteData(x_start+2);
- Lcd_WriteData(0x00);
- Lcd_WriteData(x_end+2);
-
- Lcd_WriteIndex(0x2b);
- Lcd_WriteData(0x00);
- Lcd_WriteData(y_start+1);
- Lcd_WriteData(0x00);
- Lcd_WriteData(y_end+1);
-
- Lcd_WriteIndex(0x2c);
-
- }
-
- /*************************************************
- 函数名:LCD_Set_XY
- 功能:设置lcd显示起始点
- 入口参数:xy坐标
- 返回值:无
- *************************************************/
- void Lcd_SetXY(uint16_t x,uint16_t y)
- {
- Lcd_SetRegion(x,y,x,y);
- }
-
-
- /*************************************************
- 函数名:LCD_DrawPoint
- 功能:画一个点
- 入口参数:无
- 返回值:无
- *************************************************/
- void Gui_DrawPoint(uint16_t x,uint16_t y,uint16_t Data)
- {
- Lcd_SetRegion(x,y,x+1,y+1);
- LCD_WriteData_16Bit(Data);
-
- }
-
- /*****************************************
- 函数功能:读TFT某一点的颜色
- 出口参数:color 点颜色值
- ******************************************/
- unsigned int Lcd_ReadPoint(uint16_t x,uint16_t y)
- {
- unsigned int Data;
- Lcd_SetXY(x,y);
-
- //Lcd_ReadData();//丢掉无用字节
- //Data=Lcd_ReadData();
- Lcd_WriteData(Data);
- return Data;
- }
- /*************************************************
- 函数名:Lcd_Clear
- 功能:全屏清屏函数
- 入口参数:填充颜色COLOR
- 返回值:无
- *************************************************/
- void Lcd_Clear(uint16_t Color)
- {
- unsigned int i,m;
- Lcd_SetRegion(0,0,X_MAX_PIXEL-1,Y_MAX_PIXEL-1);
- Lcd_WriteIndex(0x2C);
- for(i=0;i<X_MAX_PIXEL;i++)
- for(m=0;m<Y_MAX_PIXEL;m++)
- {
- LCD_WriteData_16Bit(Color);
- }
- }

实现效果:
实现效果与标准库实现效果一致。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。