AT32F413芯片用ST7789驱动1.3寸分辨率240×240代码
时间: 2024-03-11 10:47:02 浏览: 239
以下是使用AT32F413芯片控制ST7789驱动1.3寸240x240分辨率显示屏的代码示例:
```c
#include "at32f4xx.h"
#include "at32_board.h"
#include "stdio.h"
#define TFT_CS_Pin GPIO_Pin_4
#define TFT_CS_Port GPIOA
#define TFT_RST_Pin GPIO_Pin_5
#define TFT_RST_Port GPIOA
#define TFT_DC_Pin GPIO_Pin_6
#define TFT_DC_Port GPIOA
#define TFT_WIDTH 240
#define TFT_HEIGHT 240
#define TFT_CMD 0
#define TFT_DATA 1
void delay_us(uint32_t us)
{
uint32_t i,j;
for(i=0;i<us;i++)
for(j=0;j<15;j++);
}
void spi_write(uint8_t data)
{
while(SPI_GetFlagStatus(SPI1, SPI_FLAG_TXBE) == RESET);
SPI_SendData(SPI1, data);
while(SPI_GetFlagStatus(SPI1, SPI_FLAG_RXBNE) == RESET);
SPI_ReceiveData(SPI1);
}
void tft_write_cmd(uint8_t cmd)
{
GPIO_ResetBits(TFT_DC_Port, TFT_DC_Pin);
spi_write(cmd);
}
void tft_write_data(uint8_t data)
{
GPIO_SetBits(TFT_DC_Port, TFT_DC_Pin);
spi_write(data);
}
void tft_init()
{
GPIO_ResetBits(TFT_RST_Port, TFT_RST_Pin);
delay_us(50000);
GPIO_SetBits(TFT_RST_Port, TFT_RST_Pin);
delay_us(50000);
tft_write_cmd(0x11); // sleep out
delay_us(120000);
tft_write_cmd(0xB1);
tft_write_data(0x01);
tft_write_data(0x2C);
tft_write_data(0x2D);
tft_write_cmd(0xB2);
tft_write_data(0x01);
tft_write_data(0x2C);
tft_write_data(0x2D);
tft_write_cmd(0xB3);
tft_write_data(0x01);
tft_write_data(0x2C);
tft_write_data(0x2D);
tft_write_data(0x01);
tft_write_data(0x2C);
tft_write_data(0x2D);
tft_write_cmd(0xB4); // column inversion
tft_write_data(0x07);
tft_write_cmd(0xC0); // power control
tft_write_data(0xA2);
tft_write_data(0x02);
tft_write_data(0x84);
tft_write_cmd(0xC1); // power control
tft_write_data(0xC5);
tft_write_cmd(0xC2); // power control
tft_write_data(0x0A);
tft_write_data(0x00);
tft_write_cmd(0xC3); // power control
tft_write_data(0x8A);
tft_write_data(0x2A);
tft_write_cmd(0xC4);
tft_write_data(0x8A);
tft_write_data(0xEE);
tft_write_cmd(0xC5); // vcom control
tft_write_data(0x0E);
tft_write_cmd(0x36); // memory access
tft_write_data(0xC8);
tft_write_cmd(0x3A);
tft_write_data(0x05);
tft_write_cmd(0x20); // display inversion off
tft_write_cmd(0x13); // normal display mode
tft_write_cmd(0x29); // display on
}
void tft_set_addr_window(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1)
{
tft_write_cmd(0x2A);
tft_write_data(x0 >> 8);
tft_write_data(x0 & 0xFF);
tft_write_data(x1 >> 8);
tft_write_data(x1 & 0xFF);
tft_write_cmd(0x2B);
tft_write_data(y0 >> 8);
tft_write_data(y0 & 0xFF);
tft_write_data(y1 >> 8);
tft_write_data(y1 & 0xFF);
tft_write_cmd(0x2C);
}
void tft_fill_screen(uint16_t color)
{
uint16_t x, y;
tft_set_addr_window(0, 0, TFT_WIDTH - 1, TFT_HEIGHT - 1);
for (y = 0; y < TFT_HEIGHT; ++y) {
for (x = 0; x < TFT_WIDTH; ++x) {
tft_write_data(color >> 8);
tft_write_data(color & 0xFF);
}
}
}
void tft_draw_pixel(uint16_t x, uint16_t y, uint16_t color)
{
tft_set_addr_window(x, y, x, y);
tft_write_data(color >> 8);
tft_write_data(color & 0xFF);
}
void tft_draw_line(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, uint16_t color)
{
int32_t dx = abs(x1 - x0), sx = x0 < x1 ? 1 : -1;
int32_t dy = abs(y1 - y0), sy = y0 < y1 ? 1 : -1;
int32_t err = (dx > dy ? dx : -dy) / 2;
while (1) {
tft_draw_pixel(x0, y0, color);
if (x0 == x1 && y0 == y1) break;
int32_t e2 = err;
if (e2 > -dx) { err -= dy; x0 += sx; }
if (e2 < dy) { err += dx; y0 += sy; }
}
}
void tft_draw_rect(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint16_t color)
{
tft_draw_line(x, y, x + w - 1, y, color);
tft_draw_line(x, y, x, y + h - 1, color);
tft_draw_line(x + w - 1, y, x + w - 1, y + h - 1, color);
tft_draw_line(x, y + h - 1, x + w - 1, y + h - 1, color);
}
void tft_fill_rect(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint16_t color)
{
uint16_t i, j;
tft_set_addr_window(x, y, x + w - 1, y + h - 1);
for (i = 0; i < w; ++i) {
for (j = 0; j < h; ++j) {
tft_write_data(color >> 8);
tft_write_data(color & 0xFF);
}
}
}
void tft_draw_circle(int16_t x0, int16_t y0, int16_t r, uint16_t color)
{
int16_t x = -r, y = 0, err = 2 - 2 * r;
do {
tft_draw_pixel(x0 - x, y0 + y, color);
tft_draw_pixel(x0 - y, y0 - x, color);
tft_draw_pixel(x0 + x, y0 - y, color);
tft_draw_pixel(x0 + y, y0 + x, color);
r = err;
if (r > x) err += ++x * 2 + 1;
if (r <= y) err += ++y * 2 + 1;
} while (x < 0);
}
void tft_fill_circle(int16_t x0, int16_t y0, int16_t r, uint16_t color)
{
int16_t x = -r, y = 0, err = 2 - 2 * r;
do {
tft_draw_line(x0 - x, y0 - y, x0 - x, y0 + y, color);
tft_draw_line(x0 - y, y0 - x, x0 - y, y0 + x, color);
tft_draw_line(x0 + x, y0 - y, x0 + x, y0 + y, color);
tft_draw_line(x0 + y, y0 - x, x0 + y, y0 + x, color);
r = err;
if (r > x) err += ++x * 2 + 1;
if (r <= y) err += ++y * 2 + 1;
} while (x < 0);
}
int main(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
GPIO_InitStructure.GPIO_Pin = TFT_CS_Pin | TFT_DC_Pin;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = TFT_RST_Pin;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_SetBits(TFT_CS_Port, TFT_CS_Pin);
GPIO_ResetBits(TFT_DC_Port, TFT_DC_Pin);
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_Init(SPI1, &SPI_InitStructure);
SPI_Cmd(SPI1, ENABLE);
tft_init();
tft_fill_screen(0x0000); // 填充黑色
tft_draw_circle(120, 120, 60, 0xF800); // 画圆
tft_draw_rect(20, 20, 200, 200, 0x07E0); // 画矩形
tft_fill_rect(60, 60, 120, 120, 0x001F); // 填充矩形
while (1);
return 0;
}
```
这个代码使用了AT32F413芯片的SPI1模块来控制ST7789驱动的显示屏,并提供了一些简单的图形绘制函数。需要注意的是,不同的驱动芯片和显示屏规格可能需要不同的初始化代码和操作方式,具体请参考相关的技术文档和示例代码。
阅读全文