stm32 st7789 驱动代码
时间: 2023-08-03 19:07:36 浏览: 277
以下是基于STM32的ST7789驱动代码示例(使用SPI通信):
首先,需要在代码中包含SPI和GPIO库的头文件:
```c
#include "stm32f4xx_spi.h"
#include "stm32f4xx_gpio.h"
```
然后定义SPI接口和GPIO引脚,这里以SPI1和GPIOB为例:
```c
#define LCD_SPI SPI1
#define LCD_GPIO GPIOB
#define LCD_DC_PIN GPIO_Pin_0
#define LCD_CS_PIN GPIO_Pin_1
#define LCD_RST_PIN GPIO_Pin_2
```
接着,初始化SPI和GPIO:
```c
void LCD_SPI_Init(void)
{
SPI_InitTypeDef SPI_InitStruct;
GPIO_InitTypeDef GPIO_InitStruct;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
// 配置SPI1的SCK、MISO、MOSI引脚
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOB, &GPIO_InitStruct);
// 将SCK、MISO、MOSI引脚映射到SPI1
GPIO_PinAFConfig(GPIOB, GPIO_PinSource3, GPIO_AF_SPI1);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource4, GPIO_AF_SPI1);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource5, GPIO_AF_SPI1);
// 配置片选、复位、数据/命令引脚
GPIO_InitStruct.GPIO_Pin = LCD_DC_PIN | LCD_CS_PIN | LCD_RST_PIN;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(LCD_GPIO, &GPIO_InitStruct);
// 初始化SPI1
SPI_InitStruct.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStruct.SPI_Mode = SPI_Mode_Master;
SPI_InitStruct.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStruct.SPI_CPOL = SPI_CPOL_Low;
SPI_InitStruct.SPI_CPHA = SPI_CPHA_1Edge;
SPI_InitStruct.SPI_NSS = SPI_NSS_Soft;
SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;
SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStruct.SPI_CRCPolynomial = 7; // CRC校验多项式,不使用CRC校验时可以不设置
SPI_Init(LCD_SPI, &SPI_InitStruct);
// 使能SPI1
SPI_Cmd(LCD_SPI, ENABLE);
// 初始化LCD复位引脚
GPIO_SetBits(LCD_GPIO, LCD_RST_PIN);
DelayMs(5);
GPIO_ResetBits(LCD_GPIO, LCD_RST_PIN);
DelayMs(20);
GPIO_SetBits(LCD_GPIO, LCD_RST_PIN);
DelayMs(150);
}
```
其中,`DelayMs`是一个自定义的函数,用于延迟一定的时间,可以根据实际情况自行实现。
接下来,定义发送数据和发送命令的函数:
```c
void LCD_SPI_SendData(uint8_t data)
{
while (SPI_I2S_GetFlagStatus(LCD_SPI, SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData(LCD_SPI, data);
while (SPI_I2S_GetFlagStatus(LCD_SPI, SPI_I2S_FLAG_RXNE) == RESET);
SPI_I2S_ReceiveData(LCD_SPI);
}
void LCD_SendCommand(uint8_t cmd)
{
GPIO_ResetBits(LCD_GPIO, LCD_DC_PIN);
GPIO_ResetBits(LCD_GPIO, LCD_CS_PIN);
LCD_SPI_SendData(cmd);
GPIO_SetBits(LCD_GPIO, LCD_CS_PIN);
}
void LCD_SendData(uint8_t data)
{
GPIO_SetBits(LCD_GPIO, LCD_DC_PIN);
GPIO_ResetBits(LCD_GPIO, LCD_CS_PIN);
LCD_SPI_SendData(data);
GPIO_SetBits(LCD_GPIO, LCD_CS_PIN);
}
```
这里的`LCD_SendCommand`和`LCD_SendData`函数分别用于发送命令和数据,通过设置和清除`DC`引脚的电平来区分。
最后,就可以通过这些函数来操作ST7789液晶屏了,例如初始化屏幕:
```c
void LCD_Init(void)
{
LCD_SPI_Init();
LCD_SendCommand(0x11); // sleep out
DelayMs(120);
LCD_SendCommand(0x36); // MADCTL (Memory Access Control)
LCD_SendData(0x00); // row address order: top to bottom
LCD_SendData(0x00); // column address order: left to right
LCD_SendData(0xC0); // RGB color filter panel, vertical refresh order: top to bottom
LCD_SendCommand(0x3A); // COLMOD (Interface Pixel Format)
LCD_SendData(0x05); // 16-bit RGB, 65K colors
LCD_SendCommand(0xB2); // PORCTRL (PORch control)
LCD_SendData(0x0C); // VREG1OUT voltage = 4.1V
LCD_SendData(0x0C); // VDV (VCOM Dynamic Driving Voltage) = VREG1OUT x 0.75
LCD_SendData(0x00); // VCM (VCOM voltage) = VREG1OUT x 0.75
LCD_SendData(0x33); // VDVAC (VDVAC amplitude) = VREG1OUT x 1.6
LCD_SendCommand(0xB7); // GCTRL (Gate Control)
LCD_SendData(0x35); // VGH (Gate High voltage) = VREG1OUT x 3.0
LCD_SendData(0x35); // VGL (Gate Low voltage) = -VREG1OUT x 3.0
LCD_SendCommand(0xBB); // VCOMS (VCOM setting)
LCD_SendData(0x1E); // VCOMS = -1.025V
LCD_SendCommand(0xC0); // LCMCTRL (LCM Control)
LCD_SendData(0x2C); // LCD Driving waveform control: no inversion
LCD_SendData(0x2D); // Source output level: VCOMH - 1.05V
LCD_SendData(0x07); // LCD display line number: 320
LCD_SendCommand(0xC2); // VDVVRHEN (VDV and VRH Command Enable)
LCD_SendData(0x01); // enable VDV and VRH registers
LCD_SendCommand(0xC3); // VRHS (VRH Set)
LCD_SendData(0x11); // VAP (VCOM alternating amplitude) = VREG1OUT x 0.9
LCD_SendData(0x02); // VDV (VCOM Dynamic Driving Voltage) = VREG1OUT x 0.75
LCD_SendCommand(0xC4); // VDVS (VDV Set)
LCD_SendData(0x20); // VDV (VCOM Dynamic Driving Voltage)
LCD_SendCommand(0xC6); // FRCTRL2 (Frame Rate control in normal mode)
LCD_SendData(0x0F); // 60Hz, inversion off
LCD_SendCommand(0xD0); // PWCTRL1 (Power Control 1)
LCD_SendData(0xA4); // AVDD = VREG1OUT x 2.4
LCD_SendData(0xA1); // AVEE = -VREG1OUT x 2.4
LCD_SendData(0x00); // VCL = VREG1OUT x 1.8
LCD_SendCommand(0xE0); // PGAMCTRL (Positive Gamma Control)
LCD_SendData(0xD0);
LCD_SendData(0x08);
LCD_SendData(0x11);
LCD_SendData(0x08);
LCD_SendData(0x0C);
LCD_SendData(0x15);
LCD_SendData(0x39);
LCD_SendData(0x33);
LCD_SendData(0x50);
LCD_SendData(0x36);
LCD_SendData(0x13);
LCD_SendData(0x14);
LCD_SendData(0x29);
LCD_SendData(0x2d);
LCD_SendData(0x2A);
LCD_SendCommand(0xE1); // NGAMCTRL (Negative Gamma Control)
LCD_SendData(0xD0);
LCD_SendData(0x08);
LCD_SendData(0x10);
LCD_SendData(0x08);
LCD_SendData(0x06);
LCD_SendData(0x06);
LCD_SendData(0x39);
LCD_SendData(0x44);
LCD_SendData(0x51);
LCD_SendData(0x0B);
LCD_SendData(0x16);
LCD_SendData(0x14);
LCD_SendData(0x2F);
LCD_SendData(0x31);
LCD_SendData(0x2A);
LCD_SendCommand(0x29); // display on
}
```
这里的初始化代码可以根据实际情况进行修改,具体的命令和参数可以参考ST7789的数据手册。
阅读全文