Files
LonganTFReader/component/screen/Source/lcd.c
T
2026-10-11 21:48:59 +08:00

2060 lines
26 KiB
C

#include "../Include/lcd.h"
#include "gd32vf103_gpio.h"
#include "gd32vf103_spi.h"
static uint16_t background_color; //背景色
static const uint8_t asc2_1608[1520] = {
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x08,
0x08,
0x08,
0x08,
0x08,
0x08,
0x08,
0x00,
0x00,
0x18,
0x18,
0x00,
0x00,
0x00,
0x48,
0x6C,
0x24,
0x12,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x24,
0x24,
0x24,
0x7F,
0x12,
0x12,
0x12,
0x7F,
0x12,
0x12,
0x12,
0x00,
0x00,
0x00,
0x00,
0x08,
0x1C,
0x2A,
0x2A,
0x0A,
0x0C,
0x18,
0x28,
0x28,
0x2A,
0x2A,
0x1C,
0x08,
0x08,
0x00,
0x00,
0x00,
0x22,
0x25,
0x15,
0x15,
0x15,
0x2A,
0x58,
0x54,
0x54,
0x54,
0x22,
0x00,
0x00,
0x00,
0x00,
0x00,
0x0C,
0x12,
0x12,
0x12,
0x0A,
0x76,
0x25,
0x29,
0x11,
0x91,
0x6E,
0x00,
0x00,
0x00,
0x06,
0x06,
0x04,
0x03,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x40,
0x20,
0x10,
0x10,
0x08,
0x08,
0x08,
0x08,
0x08,
0x08,
0x10,
0x10,
0x20,
0x40,
0x00,
0x00,
0x02,
0x04,
0x08,
0x08,
0x10,
0x10,
0x10,
0x10,
0x10,
0x10,
0x08,
0x08,
0x04,
0x02,
0x00,
0x00,
0x00,
0x00,
0x00,
0x08,
0x08,
0x6B,
0x1C,
0x1C,
0x6B,
0x08,
0x08,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x08,
0x08,
0x08,
0x08,
0x7F,
0x08,
0x08,
0x08,
0x08,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x06,
0x06,
0x04,
0x03,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0xFE,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x06,
0x06,
0x00,
0x00,
0x00,
0x00,
0x80,
0x40,
0x40,
0x20,
0x20,
0x10,
0x10,
0x08,
0x08,
0x04,
0x04,
0x02,
0x02,
0x00,
0x00,
0x00,
0x00,
0x18,
0x24,
0x42,
0x42,
0x42,
0x42,
0x42,
0x42,
0x42,
0x24,
0x18,
0x00,
0x00,
0x00,
0x00,
0x00,
0x08,
0x0E,
0x08,
0x08,
0x08,
0x08,
0x08,
0x08,
0x08,
0x08,
0x3E,
0x00,
0x00,
0x00,
0x00,
0x00,
0x3C,
0x42,
0x42,
0x42,
0x20,
0x20,
0x10,
0x08,
0x04,
0x42,
0x7E,
0x00,
0x00,
0x00,
0x00,
0x00,
0x3C,
0x42,
0x42,
0x20,
0x18,
0x20,
0x40,
0x40,
0x42,
0x22,
0x1C,
0x00,
0x00,
0x00,
0x00,
0x00,
0x20,
0x30,
0x28,
0x24,
0x24,
0x22,
0x22,
0x7E,
0x20,
0x20,
0x78,
0x00,
0x00,
0x00,
0x00,
0x00,
0x7E,
0x02,
0x02,
0x02,
0x1A,
0x26,
0x40,
0x40,
0x42,
0x22,
0x1C,
0x00,
0x00,
0x00,
0x00,
0x00,
0x38,
0x24,
0x02,
0x02,
0x1A,
0x26,
0x42,
0x42,
0x42,
0x24,
0x18,
0x00,
0x00,
0x00,
0x00,
0x00,
0x7E,
0x22,
0x22,
0x10,
0x10,
0x08,
0x08,
0x08,
0x08,
0x08,
0x08,
0x00,
0x00,
0x00,
0x00,
0x00,
0x3C,
0x42,
0x42,
0x42,
0x24,
0x18,
0x24,
0x42,
0x42,
0x42,
0x3C,
0x00,
0x00,
0x00,
0x00,
0x00,
0x18,
0x24,
0x42,
0x42,
0x42,
0x64,
0x58,
0x40,
0x40,
0x24,
0x1C,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x18,
0x18,
0x00,
0x00,
0x00,
0x00,
0x18,
0x18,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x08,
0x00,
0x00,
0x00,
0x00,
0x00,
0x08,
0x08,
0x04,
0x00,
0x00,
0x00,
0x40,
0x20,
0x10,
0x08,
0x04,
0x02,
0x04,
0x08,
0x10,
0x20,
0x40,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x7F,
0x00,
0x00,
0x00,
0x7F,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x02,
0x04,
0x08,
0x10,
0x20,
0x40,
0x20,
0x10,
0x08,
0x04,
0x02,
0x00,
0x00,
0x00,
0x00,
0x00,
0x3C,
0x42,
0x42,
0x46,
0x40,
0x20,
0x10,
0x10,
0x00,
0x18,
0x18,
0x00,
0x00,
0x00,
0x00,
0x00,
0x1C,
0x22,
0x5A,
0x55,
0x55,
0x55,
0x55,
0x2D,
0x42,
0x22,
0x1C,
0x00,
0x00,
0x00,
0x00,
0x00,
0x08,
0x08,
0x18,
0x14,
0x14,
0x24,
0x3C,
0x22,
0x42,
0x42,
0xE7,
0x00,
0x00,
0x00,
0x00,
0x00,
0x1F,
0x22,
0x22,
0x22,
0x1E,
0x22,
0x42,
0x42,
0x42,
0x22,
0x1F,
0x00,
0x00,
0x00,
0x00,
0x00,
0x7C,
0x42,
0x42,
0x01,
0x01,
0x01,
0x01,
0x01,
0x42,
0x22,
0x1C,
0x00,
0x00,
0x00,
0x00,
0x00,
0x1F,
0x22,
0x42,
0x42,
0x42,
0x42,
0x42,
0x42,
0x42,
0x22,
0x1F,
0x00,
0x00,
0x00,
0x00,
0x00,
0x3F,
0x42,
0x12,
0x12,
0x1E,
0x12,
0x12,
0x02,
0x42,
0x42,
0x3F,
0x00,
0x00,
0x00,
0x00,
0x00,
0x3F,
0x42,
0x12,
0x12,
0x1E,
0x12,
0x12,
0x02,
0x02,
0x02,
0x07,
0x00,
0x00,
0x00,
0x00,
0x00,
0x3C,
0x22,
0x22,
0x01,
0x01,
0x01,
0x71,
0x21,
0x22,
0x22,
0x1C,
0x00,
0x00,
0x00,
0x00,
0x00,
0xE7,
0x42,
0x42,
0x42,
0x42,
0x7E,
0x42,
0x42,
0x42,
0x42,
0xE7,
0x00,
0x00,
0x00,
0x00,
0x00,
0x3E,
0x08,
0x08,
0x08,
0x08,
0x08,
0x08,
0x08,
0x08,
0x08,
0x3E,
0x00,
0x00,
0x00,
0x00,
0x00,
0x7C,
0x10,
0x10,
0x10,
0x10,
0x10,
0x10,
0x10,
0x10,
0x10,
0x10,
0x11,
0x0F,
0x00,
0x00,
0x00,
0x77,
0x22,
0x12,
0x0A,
0x0E,
0x0A,
0x12,
0x12,
0x22,
0x22,
0x77,
0x00,
0x00,
0x00,
0x00,
0x00,
0x07,
0x02,
0x02,
0x02,
0x02,
0x02,
0x02,
0x02,
0x02,
0x42,
0x7F,
0x00,
0x00,
0x00,
0x00,
0x00,
0x77,
0x36,
0x36,
0x36,
0x36,
0x2A,
0x2A,
0x2A,
0x2A,
0x2A,
0x6B,
0x00,
0x00,
0x00,
0x00,
0x00,
0xE3,
0x46,
0x46,
0x4A,
0x4A,
0x52,
0x52,
0x52,
0x62,
0x62,
0x47,
0x00,
0x00,
0x00,
0x00,
0x00,
0x1C,
0x22,
0x41,
0x41,
0x41,
0x41,
0x41,
0x41,
0x41,
0x22,
0x1C,
0x00,
0x00,
0x00,
0x00,
0x00,
0x3F,
0x42,
0x42,
0x42,
0x42,
0x3E,
0x02,
0x02,
0x02,
0x02,
0x07,
0x00,
0x00,
0x00,
0x00,
0x00,
0x1C,
0x22,
0x41,
0x41,
0x41,
0x41,
0x41,
0x4D,
0x53,
0x32,
0x1C,
0x60,
0x00,
0x00,
0x00,
0x00,
0x3F,
0x42,
0x42,
0x42,
0x3E,
0x12,
0x12,
0x22,
0x22,
0x42,
0xC7,
0x00,
0x00,
0x00,
0x00,
0x00,
0x7C,
0x42,
0x42,
0x02,
0x04,
0x18,
0x20,
0x40,
0x42,
0x42,
0x3E,
0x00,
0x00,
0x00,
0x00,
0x00,
0x7F,
0x49,
0x08,
0x08,
0x08,
0x08,
0x08,
0x08,
0x08,
0x08,
0x1C,
0x00,
0x00,
0x00,
0x00,
0x00,
0xE7,
0x42,
0x42,
0x42,
0x42,
0x42,
0x42,
0x42,
0x42,
0x42,
0x3C,
0x00,
0x00,
0x00,
0x00,
0x00,
0xE7,
0x42,
0x42,
0x22,
0x24,
0x24,
0x14,
0x14,
0x18,
0x08,
0x08,
0x00,
0x00,
0x00,
0x00,
0x00,
0x6B,
0x49,
0x49,
0x49,
0x49,
0x55,
0x55,
0x36,
0x22,
0x22,
0x22,
0x00,
0x00,
0x00,
0x00,
0x00,
0xE7,
0x42,
0x24,
0x24,
0x18,
0x18,
0x18,
0x24,
0x24,
0x42,
0xE7,
0x00,
0x00,
0x00,
0x00,
0x00,
0x77,
0x22,
0x22,
0x14,
0x14,
0x08,
0x08,
0x08,
0x08,
0x08,
0x1C,
0x00,
0x00,
0x00,
0x00,
0x00,
0x7E,
0x21,
0x20,
0x10,
0x10,
0x08,
0x04,
0x04,
0x42,
0x42,
0x3F,
0x00,
0x00,
0x00,
0x78,
0x08,
0x08,
0x08,
0x08,
0x08,
0x08,
0x08,
0x08,
0x08,
0x08,
0x08,
0x08,
0x78,
0x00,
0x00,
0x00,
0x02,
0x02,
0x04,
0x04,
0x08,
0x08,
0x08,
0x10,
0x10,
0x20,
0x20,
0x20,
0x40,
0x40,
0x00,
0x1E,
0x10,
0x10,
0x10,
0x10,
0x10,
0x10,
0x10,
0x10,
0x10,
0x10,
0x10,
0x10,
0x1E,
0x00,
0x00,
0x38,
0x44,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0xFF,
0x00,
0x06,
0x08,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x3C,
0x42,
0x78,
0x44,
0x42,
0x42,
0xFC,
0x00,
0x00,
0x00,
0x00,
0x00,
0x03,
0x02,
0x02,
0x02,
0x1A,
0x26,
0x42,
0x42,
0x42,
0x26,
0x1A,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x38,
0x44,
0x02,
0x02,
0x02,
0x44,
0x38,
0x00,
0x00,
0x00,
0x00,
0x00,
0x60,
0x40,
0x40,
0x40,
0x78,
0x44,
0x42,
0x42,
0x42,
0x64,
0xD8,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x3C,
0x42,
0x7E,
0x02,
0x02,
0x42,
0x3C,
0x00,
0x00,
0x00,
0x00,
0x00,
0xF0,
0x88,
0x08,
0x08,
0x7E,
0x08,
0x08,
0x08,
0x08,
0x08,
0x3E,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x7C,
0x22,
0x22,
0x1C,
0x02,
0x3C,
0x42,
0x42,
0x3C,
0x00,
0x00,
0x00,
0x03,
0x02,
0x02,
0x02,
0x3A,
0x46,
0x42,
0x42,
0x42,
0x42,
0xE7,
0x00,
0x00,
0x00,
0x00,
0x00,
0x0C,
0x0C,
0x00,
0x00,
0x0E,
0x08,
0x08,
0x08,
0x08,
0x08,
0x3E,
0x00,
0x00,
0x00,
0x00,
0x00,
0x30,
0x30,
0x00,
0x00,
0x38,
0x20,
0x20,
0x20,
0x20,
0x20,
0x20,
0x22,
0x1E,
0x00,
0x00,
0x00,
0x03,
0x02,
0x02,
0x02,
0x72,
0x12,
0x0A,
0x16,
0x12,
0x22,
0x77,
0x00,
0x00,
0x00,
0x00,
0x00,
0x0E,
0x08,
0x08,
0x08,
0x08,
0x08,
0x08,
0x08,
0x08,
0x08,
0x3E,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x7F,
0x92,
0x92,
0x92,
0x92,
0x92,
0xB7,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x3B,
0x46,
0x42,
0x42,
0x42,
0x42,
0xE7,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x3C,
0x42,
0x42,
0x42,
0x42,
0x42,
0x3C,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x1B,
0x26,
0x42,
0x42,
0x42,
0x22,
0x1E,
0x02,
0x07,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x78,
0x44,
0x42,
0x42,
0x42,
0x44,
0x78,
0x40,
0xE0,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x77,
0x4C,
0x04,
0x04,
0x04,
0x04,
0x1F,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x7C,
0x42,
0x02,
0x3C,
0x40,
0x42,
0x3E,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x08,
0x08,
0x3E,
0x08,
0x08,
0x08,
0x08,
0x08,
0x30,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x63,
0x42,
0x42,
0x42,
0x42,
0x62,
0xDC,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0xE7,
0x42,
0x24,
0x24,
0x14,
0x08,
0x08,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0xEB,
0x49,
0x49,
0x55,
0x55,
0x22,
0x22,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x76,
0x24,
0x18,
0x18,
0x18,
0x24,
0x6E,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0xE7,
0x42,
0x24,
0x24,
0x14,
0x18,
0x08,
0x08,
0x07,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x7E,
0x22,
0x10,
0x08,
0x08,
0x44,
0x7E,
0x00,
0x00,
0x00,
0xC0,
0x20,
0x20,
0x20,
0x20,
0x20,
0x10,
0x20,
0x20,
0x20,
0x20,
0x20,
0x20,
0xC0,
0x00,
0x10,
0x10,
0x10,
0x10,
0x10,
0x10,
0x10,
0x10,
0x10,
0x10,
0x10,
0x10,
0x10,
0x10,
0x10,
0x10,
0x00,
0x06,
0x08,
0x08,
0x08,
0x08,
0x08,
0x10,
0x08,
0x08,
0x08,
0x08,
0x08,
0x08,
0x06,
0x00,
0x0C,
0x32,
0xC2,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
};
/******************************************************************************
函数说明:LCD串行数据写入函数
入口数据:dat 要写入的串行数据
返回值: 无
******************************************************************************/
void LCD_Writ_Bus(uint8_t dat)
{
OLED_CS_Clr();
while (RESET == spi_i2s_flag_get(SPI0, SPI_FLAG_TBE))
;
spi_i2s_data_transmit(SPI0, dat);
while (RESET == spi_i2s_flag_get(SPI0, SPI_FLAG_RBNE))
;
spi_i2s_data_receive(SPI0);
OLED_CS_Set();
}
/******************************************************************************
函数说明:LCD写入数据
入口数据:dat 写入的数据
返回值: 无
******************************************************************************/
void LCD_WR_DATA8(uint8_t dat)
{
OLED_DC_Set(); //写数据
LCD_Writ_Bus(dat);
}
/******************************************************************************
函数说明:LCD写入数据
入口数据:dat 写入的数据
返回值: 无
******************************************************************************/
void LCD_WR_DATA(uint16_t dat)
{
OLED_DC_Set(); //写数据
LCD_Writ_Bus(dat >> 8);
LCD_Writ_Bus(dat);
}
/******************************************************************************
函数说明:LCD写入命令
入口数据:dat 写入的命令
返回值: 无
******************************************************************************/
void LCD_WR_REG(uint8_t dat)
{
OLED_DC_Clr(); //写命令
LCD_Writ_Bus(dat);
}
/******************************************************************************
函数说明:设置起始和结束地址
入口数据:x1,x2 设置列的起始和结束地址
y1,y2 设置行的起始和结束地址
返回值: 无
******************************************************************************/
void LCD_Address_Set(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2)
{
if (USE_HORIZONTAL == 0)
{
LCD_WR_REG(0x2a); //列地址设置
LCD_WR_DATA(x1 + 26);
LCD_WR_DATA(x2 + 26);
LCD_WR_REG(0x2b); //行地址设置
LCD_WR_DATA(y1 + 1);
LCD_WR_DATA(y2 + 1);
LCD_WR_REG(0x2c); //储存器写
}
else if (USE_HORIZONTAL == 1)
{
LCD_WR_REG(0x2a); //列地址设置
LCD_WR_DATA(x1 + 26);
LCD_WR_DATA(x2 + 26);
LCD_WR_REG(0x2b); //行地址设置
LCD_WR_DATA(y1 + 1);
LCD_WR_DATA(y2 + 1);
LCD_WR_REG(0x2c); //储存器写
}
else if (USE_HORIZONTAL == 2)
{
LCD_WR_REG(0x2a); //列地址设置
LCD_WR_DATA(x1 + 1);
LCD_WR_DATA(x2 + 1);
LCD_WR_REG(0x2b); //行地址设置
LCD_WR_DATA(y1 + 26);
LCD_WR_DATA(y2 + 26);
LCD_WR_REG(0x2c); //储存器写
}
else
{
LCD_WR_REG(0x2a); //列地址设置
LCD_WR_DATA(x1 + 1);
LCD_WR_DATA(x2 + 1);
LCD_WR_REG(0x2b); //行地址设置
LCD_WR_DATA(y1 + 26);
LCD_WR_DATA(y2 + 26);
LCD_WR_REG(0x2c); //储存器写
}
}
void spi_config(void)
{
spi_parameter_struct spi_init_struct;
/* deinitilize SPI and the parameters */
spi_struct_para_init(&spi_init_struct);
/* SPI0 parameter config */
spi_init_struct.trans_mode = SPI_TRANSMODE_FULLDUPLEX;
spi_init_struct.device_mode = SPI_MASTER;
spi_init_struct.frame_size = SPI_FRAMESIZE_8BIT;
spi_init_struct.clock_polarity_phase = SPI_CK_PL_HIGH_PH_2EDGE;
spi_init_struct.nss = SPI_NSS_SOFT;
spi_init_struct.prescale = SPI_PSC_8;
spi_init_struct.endian = SPI_ENDIAN_MSB;
spi_init(SPI0, &spi_init_struct);
spi_crc_polynomial_set(SPI0, 7);
spi_enable(SPI0);
}
/******************************************************************************
函数说明:LCD初始化函数
入口数据:无
返回值: 无
******************************************************************************/
void LCD_Init(void)
{
rcu_periph_clock_enable(RCU_GPIOA);
rcu_periph_clock_enable(RCU_GPIOB);
rcu_periph_clock_enable(RCU_AF);
rcu_periph_clock_enable(RCU_SPI0);
/* SPI0 GPIO config: NSS/PA4, SCK/PA5, MOSI/PA7 */
gpio_init(GPIOA, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7);
gpio_init(GPIOB, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_2);
spi_config();
gpio_init(GPIOB, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_0 | GPIO_PIN_1);
gpio_bit_reset(GPIOB, GPIO_PIN_0 | GPIO_PIN_1);
OLED_RST_Clr();
delay_1ms(200);
OLED_RST_Set();
delay_1ms(20);
OLED_BLK_Set();
LCD_WR_REG(0x11); // turn off sleep mode
delay_1ms(100);
LCD_WR_REG(0x21); // display inversion mode
LCD_WR_REG(0xB1); // Set the frame frequency of the full colors normal mode
// Frame rate=fosc/((RTNA x 2 + 40) x (LINE + FPA + BPA +2))
// fosc = 850kHz
LCD_WR_DATA8(0x05); // RTNA
LCD_WR_DATA8(0x3A); // FPA
LCD_WR_DATA8(0x3A); // BPA
LCD_WR_REG(0xB2); // Set the frame frequency of the Idle mode
// Frame rate=fosc/((RTNB x 2 + 40) x (LINE + FPB + BPB +2))
// fosc = 850kHz
LCD_WR_DATA8(0x05); // RTNB
LCD_WR_DATA8(0x3A); // FPB
LCD_WR_DATA8(0x3A); // BPB
LCD_WR_REG(0xB3); // Set the frame frequency of the Partial mode/ full colors
LCD_WR_DATA8(0x05);
LCD_WR_DATA8(0x3A);
LCD_WR_DATA8(0x3A);
LCD_WR_DATA8(0x05);
LCD_WR_DATA8(0x3A);
LCD_WR_DATA8(0x3A);
LCD_WR_REG(0xB4);
LCD_WR_DATA8(0x03);
LCD_WR_REG(0xC0);
LCD_WR_DATA8(0x62);
LCD_WR_DATA8(0x02);
LCD_WR_DATA8(0x04);
LCD_WR_REG(0xC1);
LCD_WR_DATA8(0xC0);
LCD_WR_REG(0xC2);
LCD_WR_DATA8(0x0D);
LCD_WR_DATA8(0x00);
LCD_WR_REG(0xC3);
LCD_WR_DATA8(0x8D);
LCD_WR_DATA8(0x6A);
LCD_WR_REG(0xC4);
LCD_WR_DATA8(0x8D);
LCD_WR_DATA8(0xEE);
LCD_WR_REG(0xC5); /*VCOM*/
LCD_WR_DATA8(0x0E);
LCD_WR_REG(0xE0);
LCD_WR_DATA8(0x10);
LCD_WR_DATA8(0x0E);
LCD_WR_DATA8(0x02);
LCD_WR_DATA8(0x03);
LCD_WR_DATA8(0x0E);
LCD_WR_DATA8(0x07);
LCD_WR_DATA8(0x02);
LCD_WR_DATA8(0x07);
LCD_WR_DATA8(0x0A);
LCD_WR_DATA8(0x12);
LCD_WR_DATA8(0x27);
LCD_WR_DATA8(0x37);
LCD_WR_DATA8(0x00);
LCD_WR_DATA8(0x0D);
LCD_WR_DATA8(0x0E);
LCD_WR_DATA8(0x10);
LCD_WR_REG(0xE1);
LCD_WR_DATA8(0x10);
LCD_WR_DATA8(0x0E);
LCD_WR_DATA8(0x03);
LCD_WR_DATA8(0x03);
LCD_WR_DATA8(0x0F);
LCD_WR_DATA8(0x06);
LCD_WR_DATA8(0x02);
LCD_WR_DATA8(0x08);
LCD_WR_DATA8(0x0A);
LCD_WR_DATA8(0x13);
LCD_WR_DATA8(0x26);
LCD_WR_DATA8(0x36);
LCD_WR_DATA8(0x00);
LCD_WR_DATA8(0x0D);
LCD_WR_DATA8(0x0E);
LCD_WR_DATA8(0x10);
LCD_WR_REG(0x3A); // define the format of RGB picture data
LCD_WR_DATA8(0x05); // 16-bit/pixel
LCD_WR_REG(0x36);
if (USE_HORIZONTAL == 0)
LCD_WR_DATA8(0x08);
else if (USE_HORIZONTAL == 1)
LCD_WR_DATA8(0xC8);
else if (USE_HORIZONTAL == 2)
LCD_WR_DATA8(0x78);
else
LCD_WR_DATA8(0xA8);
LCD_WR_REG(0x29); // Display On
}
/******************************************************************************
函数说明:LCD清屏函数
入口数据:无
返回值: 无
******************************************************************************/
void LCD_Clear(uint16_t Color)
{
uint16_t i, j;
LCD_Address_Set(0, 0, LCD_W - 1, LCD_H - 1);
for (i = 0; i < LCD_W; i++)
{
for (j = 0; j < LCD_H; j++)
{
LCD_WR_DATA(Color);
}
}
}
void LCD_SetBackgroundColor(uint16_t color)
{
background_color = color;
}
void LCD_DrawPoint(uint16_t x, uint16_t y, uint16_t color)
{
LCD_Address_Set(x, y, x, y); //设置光标位置
LCD_WR_DATA(color);
}
/******************************************************************************
函数说明:在指定区域填充颜色
入口数据:xsta,ysta 起始坐标
xend,yend 终止坐标
返回值: 无
******************************************************************************/
void LCD_Fill(uint16_t xsta, uint16_t ysta, uint16_t xend, uint16_t yend, uint16_t color)
{
uint16_t i, j;
LCD_Address_Set(xsta, ysta, xend, yend); //设置光标位置
for (i = ysta; i <= yend; i++)
{
for (j = xsta; j <= xend; j++)
LCD_WR_DATA(color); //设置光标位置
}
}
/******************************************************************************
函数说明:画线
入口数据:x1,y1 起始坐标
x2,y2 终止坐标
返回值: 无
******************************************************************************/
void LCD_DrawLine(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t color)
{
uint16_t t;
int xerr = 0, yerr = 0, delta_x, delta_y, distance;
int incx, incy, uRow, uCol;
delta_x = x2 - x1; //计算坐标增量
delta_y = y2 - y1;
uRow = x1; //画线起点坐标
uCol = y1;
if (delta_x > 0)
incx = 1; //设置单步方向
else if (delta_x == 0)
incx = 0; //垂直线
else
{
incx = -1;
delta_x = -delta_x;
}
if (delta_y > 0)
incy = 1;
else if (delta_y == 0)
incy = 0; //水平线
else
{
incy = -1;
delta_y = -delta_x;
}
if (delta_x > delta_y)
distance = delta_x; //选取基本增量坐标轴
else
distance = delta_y;
for (t = 0; t < distance + 1; t++)
{
LCD_DrawPoint(uRow, uCol, color); //画点
xerr += delta_x;
yerr += delta_y;
if (xerr > distance)
{
xerr -= distance;
uRow += incx;
}
if (yerr > distance)
{
yerr -= distance;
uCol += incy;
}
}
}
/******************************************************************************
函数说明:画矩形
入口数据:x1,y1 起始坐标
x2,y2 终止坐标
返回值: 无
******************************************************************************/
void LCD_DrawRectangle(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t color)
{
LCD_DrawLine(x1, y1, x2, y1, color);
LCD_DrawLine(x1, y1, x1, y2, color);
LCD_DrawLine(x1, y2, x2, y2, color);
LCD_DrawLine(x2, y1, x2, y2, color);
}
/******************************************************************************
函数说明:画圆
入口数据:x0,y0 圆心坐标
r 半径
返回值: 无
******************************************************************************/
void LCD_DrawCircle(uint16_t x0, uint16_t y0, uint8_t r, uint16_t color)
{
int a, b;
// int di;
a = 0;
b = r;
while (a <= b)
{
LCD_DrawPoint(x0 - b, y0 - a, color); //3
LCD_DrawPoint(x0 + b, y0 - a, color); //0
LCD_DrawPoint(x0 - a, y0 + b, color); //1
LCD_DrawPoint(x0 - a, y0 - b, color); //2
LCD_DrawPoint(x0 + b, y0 + a, color); //4
LCD_DrawPoint(x0 + a, y0 - b, color); //5
LCD_DrawPoint(x0 + a, y0 + b, color); //6
LCD_DrawPoint(x0 - b, y0 + a, color); //7
a++;
if ((a * a + b * b) > (r * r)) //判断要画的点是否过远
{
b--;
}
}
}
/******************************************************************************
函数说明:显示字符
入口数据:x,y 起点坐标
num 要显示的字符
mode 1叠加方式 0非叠加方式
返回值: 无
******************************************************************************/
void LCD_ShowChar(uint16_t x, uint16_t y, uint8_t num, uint8_t mode, uint16_t color)
{
uint8_t temp;
uint8_t pos, t;
uint16_t x0 = x;
if (x > LCD_W - 16 || y > LCD_H - 16)
return; //设置窗口
num = num - ' '; //得到偏移后的值
LCD_Address_Set(x, y, x + 8 - 1, y + 16 - 1); //设置光标位置
if (!mode) //非叠加方式
{
for (pos = 0; pos < 16; pos++)
{
temp = asc2_1608[(uint16_t)num * 16 + pos]; //调用1608字体
for (t = 0; t < 8; t++)
{
if (temp & 0x01)
LCD_WR_DATA(color);
else
LCD_WR_DATA(background_color);
temp >>= 1;
x++;
}
x = x0;
y++;
}
}
else //叠加方式
{
for (pos = 0; pos < 16; pos++)
{
temp = asc2_1608[(uint16_t)num * 16 + pos]; //调用1608字体
for (t = 0; t < 8; t++)
{
if (temp & 0x01)
LCD_DrawPoint(x + t, y + pos, color); //画一个点
temp >>= 1;
}
}
}
}
/******************************************************************************
函数说明:显示字符串
入口数据:x,y 起点坐标
*p 字符串起始地址
返回值: 无
******************************************************************************/
void LCD_ShowString(uint16_t x, uint16_t y, const uint8_t *p, uint16_t color)
{
while (*p != '\0')
{
if (x > LCD_W - 16)
{
x = 0;
y += 16;
}
if (y > LCD_H - 16)
{
y = x = 0;
LCD_Clear(RED);
}
LCD_ShowChar(x, y, *p, 0, color);
x += 8;
p++;
}
}
/******************************************************************************
函数说明:显示数字
入口数据:m底数,n指数
返回值: 无
******************************************************************************/
static uint32_t mypow(uint8_t m, uint8_t n)
{
uint32_t result = 1;
while (n--)
result *= m;
return result;
}
/******************************************************************************
函数说明:显示小数
入口数据:x,y 起点坐标
num 要显示的小数
len 要显示的数字个数
返回值: 无
******************************************************************************/
void LCD_ShowFloat(uint16_t x, uint16_t y, float num, uint8_t len, uint16_t color)
{
uint8_t t, temp;
// uint8_t enshow=0;
uint16_t num1;
num1 = num * 100;
for (t = 0; t < len; t++)
{
temp = (num1 / mypow(10, len - t - 1)) % 10;
if (t == (len - 2))
{
LCD_ShowChar(x + 8 * (len - 2), y, '.', 0, color);
t++;
len += 1;
}
LCD_ShowChar(x + 8 * t, y, temp + 48, 0, color);
}
}
/******************************************************************************
函数说明:显示图片
入口数据:x1,y1起点坐标,x2,y2终点坐标
返回值: 无
******************************************************************************/
void LCD_ShowImage(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, const uint8_t *const image, size_t image_byte_count)
{
size_t rect_bytes = (x2 - x1 + 1) * (y2 - y1 + 1) * 2;
size_t write_bytes = image_byte_count > rect_bytes ? rect_bytes : image_byte_count;
LCD_Address_Set(x1, y1, x2, y2);
for (size_t i = 0; i < write_bytes; i++)
{
LCD_WR_DATA8(image[i]);
}
}