我想要一段stm32f103zet6驱动tftlcd显示屏的HAL库代码
时间: 2023-09-19 07:05:00 浏览: 284
STM32F103ZET6单片机FSMC接口-TFTLCD显示实验软件例程源码.zip
5星 · 资源好评率100%
好的,以下是一段针对STM32F103ZET6驱动TFTLCD显示屏的HAL库代码,供您参考:
```c
#include "stm32f1xx_hal.h"
#define LCD_WIDTH 240
#define LCD_HEIGHT 320
#define LCD_REG (*(volatile uint16_t *)0x60000000)
#define LCD_RAM (*(volatile uint16_t *)0x60020000)
#define LCD_CS_LOW() HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, GPIO_PIN_RESET)
#define LCD_CS_HIGH() HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, GPIO_PIN_SET)
#define LCD_DC_LOW() HAL_GPIO_WritePin(GPIOB, GPIO_PIN_13, GPIO_PIN_RESET)
#define LCD_DC_HIGH() HAL_GPIO_WritePin(GPIOB, GPIO_PIN_13, GPIO_PIN_SET)
#define LCD_RST_LOW() HAL_GPIO_WritePin(GPIOB, GPIO_PIN_14, GPIO_PIN_RESET)
#define LCD_RST_HIGH() HAL_GPIO_WritePin(GPIOB, GPIO_PIN_14, GPIO_PIN_SET)
static void LCD_WriteReg(uint16_t Reg);
static void LCD_WriteData(uint16_t Data);
static void LCD_WriteRegData(uint16_t Reg, uint16_t Data);
void LCD_Init(void)
{
LCD_RST_LOW();
HAL_Delay(50);
LCD_RST_HIGH();
HAL_Delay(50);
LCD_CS_LOW();
/* Display off */
LCD_WriteRegData(0x28, 0x0000);
/* Power control */
LCD_WriteRegData(0xCB, 0x39);
LCD_WriteRegData(0xCF, 0x00);
LCD_WriteRegData(0xE8, 0x3C);
LCD_WriteRegData(0xEA, 0x00);
LCD_WriteRegData(0xED, 0xC1);
/* VCM control */
LCD_WriteRegData(0xC0, 0x1B);
LCD_WriteRegData(0xC1, 0x01);
LCD_WriteRegData(0xC5, 0x30);
LCD_WriteRegData(0xC7, 0xB7);
/* Memory access control */
LCD_WriteRegData(0x36, 0x48);
/* Pixel format */
LCD_WriteRegData(0x3A, 0x55);
/* Frame rate control */
LCD_WriteRegData(0xB1, 0x00);
LCD_WriteRegData(0xB6, 0x0A);
LCD_WriteRegData(0xB7, 0x02);
/* Gamma */
LCD_WriteRegData(0xF2, 0x00);
LCD_WriteRegData(0x26, 0x01);
LCD_WriteRegData(0xE0, 0x1F);
LCD_WriteRegData(0xE1, 0x18);
LCD_WriteRegData(0xE2, 0x1B);
LCD_WriteRegData(0xE3, 0x01);
LCD_WriteRegData(0xE4, 0x11);
LCD_WriteRegData(0xE5, 0x0F);
LCD_WriteRegData(0xE6, 0x35);
LCD_WriteRegData(0xE7, 0x31);
LCD_WriteRegData(0xB0, 0x00);
/* Display on */
LCD_WriteRegData(0x29, 0x0000);
LCD_CS_HIGH();
}
void LCD_SetWindow(uint16_t X, uint16_t Y, uint16_t Width, uint16_t Height)
{
LCD_CS_LOW();
// Set column address
LCD_WriteReg(0x2a);
LCD_WriteData(X >> 8);
LCD_WriteData(X & 0xFF);
LCD_WriteData((X + Width - 1) >> 8);
LCD_WriteData((X + Width - 1) & 0xFF);
// Set page address
LCD_WriteReg(0x2b);
LCD_WriteData(Y >> 8);
LCD_WriteData(Y & 0xFF);
LCD_WriteData((Y + Height - 1) >> 8);
LCD_WriteData((Y + Height - 1) & 0xFF);
// Write memory start
LCD_WriteReg(0x2c);
}
void LCD_WritePixel(uint16_t Color)
{
LCD_WriteData(Color >> 8);
LCD_WriteData(Color & 0xFF);
}
static void LCD_WriteReg(uint16_t Reg)
{
LCD_DC_LOW();
LCD_WriteData(Reg);
}
static void LCD_WriteData(uint16_t Data)
{
HAL_SPI_Transmit(&hspi1, &Data, 1, HAL_MAX_DELAY);
}
static void LCD_WriteRegData(uint16_t Reg, uint16_t Data)
{
LCD_WriteReg(Reg);
LCD_WriteData(Data);
}
```
希望这份代码能够对您有所帮助。
阅读全文