用C语言写一段ST7789V的初始化驱动代码
时间: 2023-05-30 13:02:33 浏览: 562
以下是一段ST7789V的初始化驱动代码,基于STM32F4xx系列MCU的HAL库编写:
```c
#include "stm32f4xx_hal.h"
#define ST7789V_WIDTH 240
#define ST7789V_HEIGHT 320
#define ST7789V_RST_PIN GPIO_PIN_0
#define ST7789V_RST_PORT GPIOA
#define ST7789V_DC_PIN GPIO_PIN_1
#define ST7789V_DC_PORT GPIOA
#define ST7789V_CS_PIN GPIO_PIN_2
#define ST7789V_CS_PORT GPIOA
#define ST7789V_CMD 0
#define ST7789V_DATA 1
static SPI_HandleTypeDef hspi;
void ST7789V_WriteCommand(uint8_t cmd)
{
HAL_GPIO_WritePin(ST7789V_DC_PORT, ST7789V_DC_PIN, ST7789V_CMD);
HAL_GPIO_WritePin(ST7789V_CS_PORT, ST7789V_CS_PIN, GPIO_PIN_RESET);
HAL_SPI_Transmit(&hspi, &cmd, 1, 100);
HAL_GPIO_WritePin(ST7789V_CS_PORT, ST7789V_CS_PIN, GPIO_PIN_SET);
}
void ST7789V_WriteData(uint8_t* data, uint32_t size)
{
HAL_GPIO_WritePin(ST7789V_DC_PORT, ST7789V_DC_PIN, ST7789V_DATA);
HAL_GPIO_WritePin(ST7789V_CS_PORT, ST7789V_CS_PIN, GPIO_PIN_RESET);
HAL_SPI_Transmit(&hspi, data, size, 100);
HAL_GPIO_WritePin(ST7789V_CS_PORT, ST7789V_CS_PIN, GPIO_PIN_SET);
}
void ST7789V_Reset(void)
{
HAL_GPIO_WritePin(ST7789V_RST_PORT, ST7789V_RST_PIN, GPIO_PIN_RESET);
HAL_Delay(100);
HAL_GPIO_WritePin(ST7789V_RST_PORT, ST7789V_RST_PIN, GPIO_PIN_SET);
HAL_Delay(100);
}
void ST7789V_Init(void)
{
ST7789V_Reset();
ST7789V_WriteCommand(0x11); // Sleep Out
HAL_Delay(120);
ST7789V_WriteCommand(0x36); // Memory Data Access Control
uint8_t data = 0x00;
ST7789V_WriteData(&data, 1);
ST7789V_WriteCommand(0x3A); // Interface Pixel Format
data = 0x55;
ST7789V_WriteData(&data, 1);
ST7789V_WriteCommand(0xB2); // Porch Setting
uint8_t pdata[4] = {0x0C, 0x0C, 0x00, 0x33};
ST7789V_WriteData(pdata, 4);
ST7789V_WriteCommand(0xB7); // Gate Control
data = 0x35;
ST7789V_WriteData(&data, 1);
ST7789V_WriteCommand(0xBB); // VCOM Setting
data = 0x19;
ST7789V_WriteData(&data, 1);
ST7789V_WriteCommand(0xC0); // LCM Control
data = 0x2C;
ST7789V_WriteData(&data, 1);
ST7789V_WriteCommand(0xC2); // VDV and VRH Command Enable
pdata[0] = 0x01;
pdata[1] = 0xFF;
pdata[2] = 0xFF;
ST7789V_WriteData(pdata, 3);
ST7789V_WriteCommand(0xC3); // VRH Set
data = 0x11;
ST7789V_WriteData(&data, 1);
ST7789V_WriteCommand(0xC4); // VDV Set
pdata[0] = 0x20;
pdata[1] = 0x00;
ST7789V_WriteData(pdata, 2);
ST7789V_WriteCommand(0xC6); // Frame Rate Control
data = 0x0F;
ST7789V_WriteData(&data, 1);
ST7789V_WriteCommand(0xD0); // Power Control 1
pdata[0] = 0xA4;
pdata[1] = 0xA1;
ST7789V_WriteData(pdata, 2);
ST7789V_WriteCommand(0xE0); // Positive Gamma Correction
pdata[0] = 0xD0;
pdata[1] = 0x08;
pdata[2] = 0x11;
pdata[3] = 0x08;
pdata[4] = 0x0C;
pdata[5] = 0x15;
pdata[6] = 0x39;
pdata[7] = 0x33;
pdata[8] = 0x50;
pdata[9] = 0x36;
pdata[10] = 0x13;
pdata[11] = 0x14;
ST7789V_WriteData(pdata, 12);
ST7789V_WriteCommand(0xE1); // Negative Gamma Correction
pdata[0] = 0xD0;
pdata[1] = 0x08;
pdata[2] = 0x10;
pdata[3] = 0x08;
pdata[4] = 0x06;
pdata[5] = 0x06;
pdata[6] = 0x39;
pdata[7] = 0x44;
pdata[8] = 0x51;
pdata[9] = 0x0B;
pdata[10] = 0x16;
pdata[11] = 0x14;
ST7789V_WriteData(pdata, 12);
ST7789V_WriteCommand(0x29); // Display On
}
```