st7789v2 240x320驱动
时间: 2023-10-30 21:10:07 浏览: 187
ST7789V2是一款240x320分辨率的TFT液晶驱动IC。以下是一个简单的C语言示例代码,可以用于驱动ST7789V2:
```c
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include "st7789v2.h"
#define DELAY(ms) \
do { \
volatile uint32_t n = (SystemCoreClock / 1000) * (ms); \
while (n--) \
__NOP(); \
} while (0)
#define SPI_TIMEOUT 5000
static void spi_write(uint8_t data)
{
while (!(SPI2->SR & SPI_SR_TXE))
;
*(volatile uint8_t *)&SPI2->DR = data;
while (!(SPI2->SR & SPI_SR_RXNE))
;
while (SPI2->SR & SPI_SR_BSY)
;
(void)SPI2->DR;
}
static void spi_write16(uint16_t data)
{
spi_write(data >> 8);
spi_write(data & 0xFF);
}
static void spi_write_command(uint8_t cmd)
{
GPIOB->BSRR = GPIO_BSRR_BR1; // DC low
spi_write(cmd);
GPIOB->BSRR = GPIO_BSRR_BS1; // DC high
}
static void spi_write_data(uint8_t data)
{
spi_write(data);
}
static void spi_write_data16(uint16_t data)
{
GPIOB->BSRR = GPIO_BSRR_BS1; // DC high
spi_write16(data);
}
void st7789v2_init(void)
{
uint8_t data;
RCC->AHBENR |= RCC_AHBENR_GPIOBEN;
RCC->APB1ENR |= RCC_APB1ENR_SPI2EN;
// PB12 = SPI2_NSS
GPIOB->MODER |= GPIO_MODER_MODER12_0;
// PB13 = SPI2_SCK, PB14 = SPI2_MISO, PB15 = SPI2_MOSI
GPIOB->MODER |= GPIO_MODER_MODER13_1 | GPIO_MODER_MODER14_1 | GPIO_MODER_MODER15_1;
GPIOB->AFR[1] |= (5 << (1 * 4)) | (5 << (2 * 4)) | (5 << (3 * 4));
SPI2->CR1 = SPI_CR1_SSM | SPI_CR1_SSI | SPI_CR1_MSTR | SPI_CR1_BR_0 | SPI_CR1_BR_1;
SPI2->CR2 = SPI_CR2_DS_2 | SPI_CR2_DS_1 | SPI_CR2_DS_0;
SPI2->CR1 |= SPI_CR1_SPE;
// Reset display
GPIOB->BSRR = GPIO_BSRR_BR0; // Reset low
DELAY(50);
GPIOB->BSRR = GPIO_BSRR_BS0; // Reset high
DELAY(50);
// Initialize display
spi_write_command(0x11); // Exit sleep mode
DELAY(120);
spi_write_command(0x3A); // Set pixel format
spi_write_data(0x55);
spi_write_command(0xB2); // Set display mode
spi_write_data(0x0C);
spi_write_data(0x0C);
spi_write_data(0x00);
spi_write_data(0x33);
spi_write_data(0x33);
spi_write_command(0xB7); // Set vertical scroll
spi_write_data(0x35);
spi_write_command(0xBB); // Set display inversion
spi_write_data(0x2B);
spi_write_command(0xC0); // Set power control
spi_write_data(0x2C);
spi_write_command(0xC2); // Set VCOM
spi_write_data(0x01);
spi_write_data(0xFF);
spi_write_command(0xC3); // Set VDV and VRH command enable
spi_write_data(0x11);
spi_write_command(0xC4); // Set VDV and VRH values
spi_write_data(0x20);
spi_write_data(0x00);
spi_write_command(0xC6); // Set frame rate
spi_write_data(0x0F);
spi_write_command(0xD0); // Set panel driving
spi_write_data(0xA4);
spi_write_data(0xA1);
spi_write_command(0xE0); // Set gamma curve
spi_write_data(0xD0);
spi_write_data(0x08);
spi_write_data(0x11);
spi_write_data(0x08);
spi_write_data(0x0C);
spi_write_data(0x15);
spi_write_data(0x39);
spi_write_data(0x33);
spi_write_data(0x50);
spi_write_data(0x36);
spi_write_data(0x13);
spi_write_data(0x14);
spi_write_data(0x29);
spi_write_data(0x2D);
spi_write_command(0xE1); // Set gamma curve
spi_write_data(0xD0);
spi_write_data(0x08);
spi_write_data(0x10);
spi_write_data(0x08);
spi_write_data(0x06);
spi_write_data(0x06);
spi_write_data(0x39);
spi_write_data(0x44);
spi_write_data(0x51);
spi_write_data(0x0B);
spi_write_data(0x16);
spi_write_data(0x14);
spi_write_data(0x2F);
spi_write_data(0x31);
spi_write_command(0x29); // Display on
}
void st7789v2_set_window(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1)
{
spi_write_command(0x2A); // Set column address
spi_write_data16(x0);
spi_write_data16(x1);
spi_write_command(0x2B); // Set page address
spi_write_data16(y0);
spi_write_data16(y1);
spi_write_command(0x2C); // Memory write
}
void st7789v2_write_pixel(uint16_t color)
{
spi_write_data16(color);
}
void st7789v2_fill_screen(uint16_t color)
{
uint16_t x, y;
st7789v2_set_window(0, 0, 239, 319);
for (y = 0; y < 320; y++) {
for (x = 0; x < 240; x++) {
st7789v2_write_pixel(color);
}
}
}
```
该代码假设SPI2接口已经初始化,并且PB0和PB1用于复位和DC控制引脚。要使用该代码,只需调用`st7789v2_init()`来初始化液晶屏幕。要在屏幕上绘制像素,首先必须调用`st7789v2_set_window()`设置要绘制像素的窗口,然后使用`st7789v2_write_pixel()`写入像素值。要在屏幕上绘制图形,可以使用`st7789v2_fill_screen()`函数填充整个屏幕。
阅读全文