【瑞萨RA MCU创意氛围赛速通】瑞萨RA6M5单片机之SPI屏幕显示测试
使用软件SPI
实现字符串、数字显示,为先一步模块数据显示打下基础
1.SPI协议简介
SPI协议是由摩托罗拉公司提出的通讯协议(SerialPeripheralInterface),即串行外围设备接口,是一种高速全双工的通信总线。它被广泛地使用在ADC、LCD等设备与MCU间,要求通讯速率较高的场合。
1.1SPI物理层
SPI通讯设备之间的常用连接方式见图
SPI通讯使用3条总线及片选线,3条总线分别为SCK、MOSI、MISO,片选线为,它们的作用介绍如下:
(1)(SlaveSelect):从设备选择信号线,常称为片选信号线,也称为NSS、CS,以下用NSS表示。
当有多个SPI从设备与SPI主机相连时,设备的其它信号线SCK、MOSI及MISO同时并联到相同的SPI总线上,即无论有多少个从设备,都共同只使用这3条总线;而每个从设备都有独立的这一条NSS信号线,本信号线独占主机的一个引脚,即有多少个从设备,就有多少条片选信号线。I2C协议中通过设备地址来寻址、选中总线上的某个设备并与其进行通讯;而SPI协议中没有设备地址,它使用NSS信号线来寻址,当主机要选择从设备时,把该从设备的NSS信号线设置为低电平,该从设备即被选中,即片选有效,接着主机开始与被选中的从设备进行SPI通讯。所以SPI通讯以NSS线置低电平为开始信号,以NSS线被拉高作为结束信号。
(2)SCK(SerialClock):时钟信号线,用于通讯数据同步。它由通讯主机产生,决定了通讯的速率,不同的设备支持的最高时钟频率不一样,如RA6M5的SPI时钟频率最大为fpclkA/2,两个设备之间通讯时,通讯速率受限于低速设备。
(3)MOSI(MasterOutput,SlaveInput):主设备输出/从设备输入引脚。主机的数据从这条信号线输出,从机由这条信号线读入主机发送的数据,即这条线上数据的方向为主机到从机。
(4)MISO(MasterInput,,SlaveOutput):主设备输入/从设备输出引脚。主机从这条信线读入数据,从机的数据由这条信号线输出到主机,即在这条线上数据的方向为从机到主机。
1.2协议层
与I2C的类似,SPI协议定义了通讯的起始和停止信号、数据有效性、时钟同步等环节。
1.2.1SPI基本通讯过程
NSS、SCK、MOSI信号都由主机控制产生,而MISO的信号由从机产生,主机通过该信号线读取从机的数据。MOSI与MISO的信号只在NSS为低电平的时候才有效,在SCK的每个时钟周期MOSI和MISO传输一位数据。
1.2.2数据有效性
SPI使用MOSI及MISO信号线来传输数据,使用SCK信号线进行数据同步。MOSI及MISO数据线在SCK的每个时钟周期传输一位数据,且数据输入输出是同时进行的。数据传输时,MSB 先行或LSB先行并没有作硬性规定,但要保证两个SPI通讯设备之间使用同样的协定。即大端传输和小端传输,一般情况下都会使用大端传输。
1.2.3CPOL/CPHA及通讯模式
CPOL/CPHA代表的是时钟的极性和相位,时钟极性CPOL是指SPI通讯设备处于空闲状态时,SCK信号线的电平信号(即SPI通讯开始前、NSS线为高电平时SCK的状态)。CPOL=0时,SCK在空闲状态时为低电平,CPOL=1时,则相反。时钟相位CPHA是指数据的采样的时刻,当CPHA=0时,MOSI或MISO数据线上的信号将会在SCK时钟线的“奇数边沿”被采样。当CPHA=1时,数据线在SCK的“偶数边沿”采样。
当然,对CPHA我们可以换一种方式理解,即当CPHA=0时,代表在第一个边沿采样,CPHA=1时,代表在第二个边沿采样。
2.ST7735芯片简介
2.1ST7735芯片简介
ST7735S是一款用于262k彩色、图形型TFT-LCD的单片机控制器/驱动程序。这个芯片能够直接连接到外部并接受串行外设接口(SPI), 8位/9位/16位/18位并行接口。显示数据可存储在132 x 162 x 18位片上显示数据RAM中。它可以在没有外部操作时钟的情况下执行显示数据RAM读/写操作,以尽量减少功耗。此外,由于驱动液晶所需的集成电源电路,可以用更少的元件制作显示系统。
2.2ST7735芯片特点
带有RAM片上显示数据RAM(即帧存储器)的单片TFT-LCD控制器/驱动程序。
LCD驱动输出电路:1.源输出:132个RGB通道;2.门输出:162通道;3.共电极输出。
支持正色-黑色和正色-白色LC。
软件可编程的颜色深度模式。
部分窗口移动和数据滚动。
3.硬件连接
P0.2——MOSI
P0.1——CLK
P0.7——CS
P0.6——DC
P0.3——RST
P0.8——BL
使用的是软件SPI,所以引脚配置Mode选择Output mode(Initial High)
主程序展示:
4.效果展示以及代码
hal_entry.c代码如下(字库代码有删减,需要请留言,因为字库代码太多了,写上去文章发不出来):
#include "hal_data.h"
/*
* 作者:PrairieOne
* csdn:PrairieOne
* 邮箱:prairieone1024@163.com
* 嵌入式技术交流群:826251093
*/
FSP_CPP_HEADER
void R_BSP_WarmStart(bsp_warm_start_event_t event);
FSP_CPP_FOOTER
/* 用户头文件包含 */
#include "led/bsp_led.h"
#include "led/ST7735.h"
/*******************************************************************************************************************//**
* main() is generated by the RA Configuration editor and is used to generate threads if an RTOS is used. This function
* is called by main() when no RTOS is used.
**********************************************************************************************************************/
void hal_entry(void)
{
/* TODO: add your own code here */
LED_Init(); // LED 初始化
XTFT_Init_GPIO();//LCD引脚初始化
LCD_Init();//LCD——SPI初始化
ST7735_FillScreen(ST7735_BLACK);
while(1)
{
// ST7735_DrawString(0, 0, "CSDN", Font_7x10, ST7735_RED, ST7735_BLACK);
//ST7735_DrawString(0, 32, "CSDN", Font_16x26, ST7735_BLUE, ST7735_BLACK);
ST7735_DrawString(0, 0, "RA6M5", Font_11x18, ST7735_GREEN, ST7735_BLACK);
ST7735_DrawString(0, 16, "PrairieOne", Font_11x18, ST7735_GREEN, ST7735_BLACK);
LCD_ShowIntNum(0, 32,123, Font_11x18, ST7735_GREEN, ST7735_BLACK);
LED1_ON; // LED1亮
LED2_ON; // LED2亮
LED3_ON; // LED3亮
R_BSP_SoftwareDelay(1, BSP_DELAY_UNITS_SECONDS); //延时1秒
LED1_OFF; // LED1灭
LED2_OFF; // LED2灭
LED3_OFF; // LED3灭
R_BSP_SoftwareDelay(1, BSP_DELAY_UNITS_SECONDS); //延时1秒
}
#if BSP_TZ_SECURE_BUILD
/* Enter non-secure code */
R_BSP_NonSecureEnter();
#endif
}
/*******************************************************************************************************************//**
* This function is called at various points during the startup process. This implementation uses the event that is
* called right before main() to set up the pins.
*
* @param[in] event Where at in the start up process the code is currently at
**********************************************************************************************************************/
void R_BSP_WarmStart(bsp_warm_start_event_t event)
{
if (BSP_WARM_START_RESET == event)
{
#if BSP_FEATURE_FLASH_LP_VERSION != 0
/* Enable reading from data flash. */
R_FACI_LP->DFLCTL = 1U;
/* Would normally have to wait tDSTOP(6us) for data flash recovery. Placing the enable here, before clock and
* C runtime initialization, should negate the need for a delay since the initialization will typically take more than 6us. */
#endif
}
if (BSP_WARM_START_POST_C == event)
{
/* C runtime environment and system clocks are setup. */
/* Configure pins. */
R_IOPORT_Open (&g_ioport_ctrl, g_ioport.p_cfg);
}
}
#if BSP_TZ_SECURE_BUILD
BSP_CMSE_NONSECURE_ENTRY void template_nonsecure_callable ();
/* Trustzone Secure Projects require at least one nonsecure callable function in order to build (Remove this if it is not required to build). */
BSP_CMSE_NONSECURE_ENTRY void template_nonsecure_callable ()
{
}
#endif
ST7735.c代码如下:
#include "bsp_led.h"
#include "ST7735.h"
#include "stdlib.h"
#include "string.h"
uint16_t scr_width;
uint16_t scr_height;
void ST7735_DrawString(uint16_t x, uint16_t y, const char *str, FontDef font, uint16_t color, uint16_t bgcolor);
/******************************************************************************
函数说明:LCD引脚配置
入口数据:无
返回值: 无
******************************************************************************/
void XTFT_Init_GPIO(void)
{
TFT_BL_H();// R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_04_PIN_00, BSP_IO_LEVEL_HIGH) //SET
TFT_CS_H();// R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_04_PIN_00, BSP_IO_LEVEL_HIGH)
TFT_DC_D();// R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_04_PIN_00, BSP_IO_LEVEL_HIGH)
TFT_RES_H();// R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_04_PIN_00, BSP_IO_LEVEL_HIGH)
TFT_SCL_H();// R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_04_PIN_00, BSP_IO_LEVEL_HIGH)
TFT_MOSI_H();// R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_04_PIN_00, BSP_IO_LEVEL_HIGH)
}
/******************************************************************************
函数说明:LCD串行数据写入函数
入口数据:dat 要写入的串行数据
返回值: 无
******************************************************************************/
void SendDataSPI(unsigned char dat)
{
unsigned char i;
for(i=0; i<8; i++)
{
if( (dat&0x80)!=0 )
OLED_MOSI_SET();
else
OLED_MOSI_CLR();
dat <<= 1;
OLED_CLK_CLR();
OLED_CLK_SET();
}
}
void WriteComm(unsigned int i) //DC=0
{
OLED_CS_Clr();
OLED_DC_Clr();
SendDataSPI(i);
OLED_CS_Set();
}
/******************************************************************************
函数说明:LCD写入数据
入口数据:dat 写入的数据
返回值: 无
******************************************************************************/
void WriteData(unsigned int i) //DC=1
{
OLED_CS_Clr();
OLED_DC_Set();
SendDataSPI(i);
OLED_CS_Set();
}
void WriteDispData(unsigned char DataH,unsigned char DataL)
{
//OLED_CS_Clr();
//OLED_DC_Set();
SendDataSPI(DataH);
SendDataSPI(DataL);
//OLED_CS_Set();
}
/******************************************************************************
函数说明:LCD初始化
入口数据:
返回值: 无
******************************************************************************/
void LCD_Init(void)
{
OLED_RST_Set();
R_BSP_SoftwareDelay(100, BSP_DELAY_UNITS_MILLISECONDS);
OLED_RST_Clr();
R_BSP_SoftwareDelay(100, BSP_DELAY_UNITS_MILLISECONDS);
OLED_RST_Set();
R_BSP_SoftwareDelay(200, BSP_DELAY_UNITS_MILLISECONDS);
WriteComm(0x11); //Exit Sleep
R_BSP_SoftwareDelay(120, BSP_DELAY_UNITS_MILLISECONDS);
WriteComm(0xB1);
WriteData(0x05);//0a
WriteData(0x3c);//14
WriteData(0x3c);
WriteComm(0xB2);
WriteData(0x05);
WriteData(0x3c);
WriteData(0x3c);
WriteComm(0xB3);
WriteData(0x05);
WriteData(0x3c);
WriteData(0x3c);
WriteData(0x05);
WriteData(0x3c);
WriteData(0x3c);
WriteComm(0xB4);
WriteData(0x03);
WriteComm(0xC0); //Set VRH1[4:0] & VC[2:0] for VCI1 & GVDD
WriteData(0x28);//
WriteData(0x08);
WriteData(0x04);//
WriteComm(0xC1); //Set BT[2:0] for AVDD & VCL & VGH & VGL
WriteData(0xC0);
WriteComm(0xC2); //Set VMH[6:0] & VML[6:0] for VOMH & VCOML
WriteData(0x0D);//54h
WriteData(0x00);//33h
WriteComm(0xC3);
WriteData(0x8D);
WriteData(0x2A);
WriteComm(0xC4);
WriteData(0x8D);
WriteData(0xEE);
WriteComm(0xC5);
WriteData(0x1A);
WriteComm(0x36); //MX,MY,RGB MODE
WriteData(0x08);
WriteComm(0xe0);
WriteData(0x04);//2c
WriteData(0x22);
WriteData(0x07);
WriteData(0x0A);
WriteData(0x2E);
WriteData(0x30);
WriteData(0x25);
WriteData(0x2A);
WriteData(0x28);
WriteData(0x26);
WriteData(0x2E);
WriteData(0x3A);
WriteData(0x00);
WriteData(0x01);
WriteData(0x03);
WriteData(0x03);
WriteComm(0xe1);
WriteData(0x04);
WriteData(0x16);
WriteData(0x06);
WriteData(0x06);
WriteData(0x0D);
WriteData(0x2D);
WriteData(0x26);
WriteData(0x23);
WriteData(0x27);
WriteData(0x27);
WriteData(0x25);
WriteData(0x2D);
WriteData(0x3B);
WriteData(0x00);
WriteData(0x01);
WriteData(0x04);
WriteData(0x13);
WriteComm(0x3A);
WriteData(0x05);
WriteComm(0x29); // Display On
R_BSP_SoftwareDelay(20, BSP_DELAY_UNITS_MILLISECONDS);
}
void BlockWrite(unsigned int Xstart,unsigned int Xend,unsigned int Ystart,unsigned int Yend)
{
WriteComm(0x2A);
WriteData(Xstart>>8);
WriteData(Xstart+2);
WriteData(Xend>>8);
WriteData(Xend+2);
WriteComm(0x2B);
WriteData(Ystart>>8);
WriteData(Ystart+1);
WriteData(Yend>>8);
WriteData(Yend+1);
WriteComm(0x2c);
}
/******************************************************************************
函数说明:在指定区域填充颜色
入口数据:color 要填充的颜色
返回值: 无
******************************************************************************/
void DispColor(unsigned int color)
{
unsigned int i,j;
BlockWrite(0,COL-1,0,ROW-1);
//OLED_CS_Clr();
//OLED_DC_Set();
for(i=0;i<ROW;i++)
{
for(j=0;j<COL;j++)
{
WriteData(color>>8);
WriteData(color);
//SendDataSPI(color>>8);
//SendDataSPI(color);
}
}
//OLED_CS_Set();
}
void ST7735_write(uint8_t data)
{
SendDataSPI(data);
}
void ST7735_cmd(unsigned int cmd)
{
OLED_CS_Clr();
OLED_DC_Clr();
ST7735_write(cmd);
//while (SPI_I2S_GetFlagStatus(SPI_PORT,SPI_I2S_FLAG_BSY) == SET);
OLED_CS_Set();
}
void ST7735_data(unsigned int data)
{
OLED_CS_Clr();
OLED_DC_Set();
ST7735_write(data);
//while (SPI_I2S_GetFlagStatus(SPI_PORT,SPI_I2S_FLAG_BSY) == SET);
OLED_CS_Set();
}
void writeCmdData ( unsigned char Byte, unsigned char opt ) {
if ( opt == 1 ) {
OLED_DC_Clr();
} else if ( opt == 0 ) {
OLED_DC_Set();
}
OLED_CS_Clr();
ST7735_write(Byte);
OLED_CS_Set();
}
uint16_t RGB565(uint8_t R,uint8_t G,uint8_t B) {
return ((R >> 3) << 11) | ((G >> 2) << 5) | (B >> 3);
}
void ST7735_Init(void) {
LCD_Init();
}
static const uint16_t Font7x10 [] = {
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, // sp
0x1000, 0x1000, 0x1000, 0x1000, 0x1000, 0x1000, 0x0000, 0x1000, 0x0000, 0x0000, // !
0x2800, 0x2800, 0x2800, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, // "
0x2400, 0x2400, 0x7C00, 0x2400, 0x4800, 0x7C00, 0x4800, 0x4800, 0x0000, 0x0000, // #
0x3800, 0x5400, 0x5000, 0x3800, 0x1400, 0x5400, 0x5400, 0x3800, 0x1000, 0x0000, // $
0x2000, 0x5400, 0x5800, 0x3000, 0x2800, 0x5400, 0x1400, 0x0800, 0x0000, 0x0000, // %
0x1000, 0x2800, 0x2800, 0x1000, 0x3400, 0x4800, 0x4800, 0x3400, 0x0000, 0x0000, // &
0x1000, 0x1000, 0x1000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, // '
0x0800, 0x1000, 0x2000, 0x2000, 0x2000, 0x2000, 0x2000, 0x2000, 0x1000, 0x0800, // (
0x2000, 0x1000, 0x0800, 0x0800, 0x0800, 0x0800, 0x0800, 0x0800, 0x1000, 0x2000, // )
0x1000, 0x3800, 0x1000, 0x2800, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, // *
0x0000, 0x0000, 0x1000, 0x1000, 0x7C00, 0x1000, 0x1000, 0x0000, 0x0000, 0x0000, // +
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x1000, 0x1000, 0x1000, // ,
};
static const uint16_t Font11x18 [] = {
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, // sp
0x0000, 0x0C00, 0x0C00, 0x0C00, 0x0C00, 0x0C00, 0x0C00, 0x0C00, 0x0C00, 0x0C00, 0x0C00, 0x0C00, 0x0000, 0x0C00, 0x0C00, 0x0000, 0x0000, 0x0000, // !
0x0000, 0x1B00, 0x1B00, 0x1B00, 0x1B00, 0x1B00, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, // "
0x0000, 0x1980, 0x1980, 0x1980, 0x1980, 0x7FC0, 0x7FC0, 0x1980, 0x3300, 0x7FC0, 0x7FC0, 0x3300, 0x3300, 0x3300, 0x3300, 0x0000, 0x0000, 0x0000, // #
0x0000, 0x1E00, 0x3F00, 0x7580, 0x6580, 0x7400, 0x3C00, 0x1E00, 0x0700, 0x0580, 0x6580, 0x6580, 0x7580, 0x3F00, 0x1E00, 0x0400, 0x0400, 0x0000, // $
0x0000, 0x7000, 0xD800, 0xD840, 0xD8C0, 0xD980, 0x7300, 0x0600, 0x0C00, 0x1B80, 0x36C0, 0x66C0, 0x46C0, 0x06C0, 0x0380, 0x0000, 0x0000, 0x0000, // %
0x0000, 0x1E00, 0x3F00, 0x3300, 0x3300, 0x3300, 0x1E00, 0x0C00, 0x3CC0, 0x66C0, 0x6380, 0x6180, 0x6380, 0x3EC0, 0x1C80,
};
FontDef Font_7x10 = {7,10,Font7x10};
FontDef Font_11x18 = {11,18,Font11x18};
FontDef Font_16x26 = {16,26,Font16x26};
#define ST7735_COLOR565(r, g, b) (((r & 0xF8) << 8) | ((g & 0xFC) << 3) | ((b & 0xF8) >> 3))
#define SWAP_INT16_T(a, b) \
{ \
int16_t t = a; \
a = b; \
b = t; \
}
#define DELAY 0x80
#if defined(ST7735_1_8_DEFAULT_ORIENTATION) || defined(ST7735S_1_8_DEFAULT_ORIENTATION)
static uint8_t _data_rotation[4] = {ST7735_MADCTL_MX, ST7735_MADCTL_MY, ST7735_MADCTL_MV, ST7735_MADCTL_RGB};
#endif
#if defined(ST7735_1_44_DEFAULT_ORIENTATION) || defined(ST7735_MINI_DEFAULT_ORIENTATION)
static uint8_t _data_rotation[4] = {ST7735_MADCTL_MX, ST7735_MADCTL_MY, ST7735_MADCTL_MV, ST7735_MADCTL_BGR};
#endif
static uint8_t _value_rotation = 0;
static int16_t _height = ST7735_HEIGHT, _width = ST7735_WIDTH;
static uint8_t _xstart = ST7735_XSTART, _ystart = ST7735_YSTART;
// based on Adafruit ST7735 library for Arduino
static const uint8_t
init_cmds1[] = { // Init for 7735R, part 1 (red or green tab)
15, // 15 commands in list:
ST7735_SWRESET, DELAY, // 1: Software reset, 0 args, w/delay
150, // 150 ms delay
ST7735_SLPOUT, DELAY, // 2: Out of sleep mode, 0 args, w/delay
255, // 500 ms delay
ST7735_FRMCTR1, 3, // 3: Frame rate ctrl - normal mode, 3 args:
0x01, 0x2C, 0x2D, // Rate = fosc/(1x2+40) * (LINE+2C+2D)
ST7735_FRMCTR2, 3, // 4: Frame rate control - idle mode, 3 args:
0x01, 0x2C, 0x2D, // Rate = fosc/(1x2+40) * (LINE+2C+2D)
ST7735_FRMCTR3, 6, // 5: Frame rate ctrl - partial mode, 6 args:
0x01, 0x2C, 0x2D, // Dot inversion mode
0x01, 0x2C, 0x2D, // Line inversion mode
ST7735_INVCTR, 1, // 6: Display inversion ctrl, 1 arg, no delay:
0x07, // No inversion
ST7735_PWCTR1, 3, // 7: Power control, 3 args, no delay:
0xA2,
0x02, // -4.6V
0x84, // AUTO mode
ST7735_PWCTR2, 1, // 8: Power control, 1 arg, no delay:
0xC5, // VGH25 = 2.4C VGSEL = -10 VGH = 3 * AVDD
ST7735_PWCTR3, 2, // 9: Power control, 2 args, no delay:
0x0A, // Opamp current small
0x00, // Boost frequency
ST7735_PWCTR4, 2, // 10: Power control, 2 args, no delay:
0x8A, // BCLK/2, Opamp current small & Medium low
0x2A,
ST7735_PWCTR5, 2, // 11: Power control, 2 args, no delay:
0x8A, 0xEE,
ST7735_VMCTR1, 1, // 12: Power control, 1 arg, no delay:
0x0E,
ST7735_INVOFF, 0, // 13: Don't invert display, no args, no delay
ST7735_MADCTL, 1, // 14: Memory access control (directions), 1 arg:
ST7735_DATA_ROTATION, // row addr/col addr, bottom to top refresh
ST7735_COLMOD, 1, // 15: set color mode, 1 arg, no delay:
0x05}, // 16-bit color
#if (defined(ST7735_IS_128X128) || defined(ST7735_IS_160X128))
init_cmds2[] = { // Init for 7735R, part 2 (1.44" display)
2, // 2 commands in list:
ST7735_CASET, 4, // 1: Column addr set, 4 args, no delay:
0x00, 0x00, // XSTART = 0
0x00, 0x7F, // XEND = 127
ST7735_RASET, 4, // 2: Row addr set, 4 args, no delay:
0x00, 0x00, // XSTART = 0
0x00, 0x7F}, // XEND = 127
#endif // ST7735_IS_128X128
#ifdef ST7735_IS_160X80
init_cmds2[] = { // Init for 7735S, part 2 (160x80 display)
3, // 3 commands in list:
ST7735_CASET, 4, // 1: Column addr set, 4 args, no delay:
0x00, 0x00, // XSTART = 0
0x00, 0x4F, // XEND = 79
ST7735_RASET, 4, // 2: Row addr set, 4 args, no delay:
0x00, 0x00, // XSTART = 0
0x00, 0x9F, // XEND = 159
ST7735_INVON, 0}, // 3: Invert colors
#endif
init_cmds3[] = { // Init for 7735R, part 3 (red or green tab)
4, // 4 commands in list:
ST7735_GMCTRP1, 16, // 1: Magical unicorn dust, 16 args, no delay:
0x02, 0x1c, 0x07, 0x12, 0x37, 0x32, 0x29, 0x2d, 0x29, 0x25, 0x2B, 0x39, 0x00, 0x01, 0x03, 0x10, ST7735_GMCTRN1, 16, // 2: Sparkles and rainbows, 16 args, no delay:
0x03, 0x1d, 0x07, 0x06, 0x2E, 0x2C, 0x29, 0x2D, 0x2E, 0x2E, 0x37, 0x3F, 0x00, 0x00, 0x02, 0x10, ST7735_NORON, DELAY, // 3: Normal display on, no args, w/delay
10, // 10 ms delay
ST7735_DISPON, DELAY, // 4: Main screen turn on, no args w/delay
100}; // 100 ms delay
static void ST7735_WriteCommand(uint8_t cmd);
static void ST7735_WriteData(uint8_t *buff, size_t buff_size);
static void ST7735_ExecuteCommandList(const uint8_t *addr);
static void ST7735_SetAddressWindow(uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1);
static void ST7735_WriteChar(uint16_t x, uint16_t y, char ch, FontDef font, uint16_t color, uint16_t bgcolor);
static void ST7735_Reset()
{
TFT_RES_L();
R_BSP_SoftwareDelay(20, BSP_DELAY_UNITS_MILLISECONDS);
TFT_RES_H();
}
static void ST7735_WriteCommand(uint8_t cmd)
{
ST7735_cmd(cmd);
}
static void ST7735_WriteData(uint8_t *buff, size_t buff_size)
{
size_t i=0;
for(i=0;i<buff_size;i++)
ST7735_data(buff[i]);
}
static void ST7735_ExecuteCommandList(const uint8_t *addr)
{
uint8_t numCommands, numArgs;
uint16_t ms;
numCommands = *addr++;
while (numCommands--)
{
uint8_t cmd = *addr++;
ST7735_WriteCommand(cmd);
numArgs = *addr++;
// If high bit set, delay follows args
ms = numArgs & DELAY;
numArgs &= ~DELAY;
if (numArgs)
{
ST7735_WriteData((uint8_t *)addr, numArgs);
addr += numArgs;
}
if (ms)
{
ms = *addr++;
if (ms == 255)
ms = 500;
R_BSP_SoftwareDelay(ms, BSP_DELAY_UNITS_MILLISECONDS);
}
}
}
static void ST7735_SetAddressWindow(uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1)
{
// column address set
uint8_t data[] = {0x00, x0 + _xstart, 0x00, x1 + _xstart};
ST7735_WriteCommand(ST7735_CASET);
ST7735_WriteData(data, sizeof(data));
// row address set
ST7735_WriteCommand(ST7735_RASET);
data[1] = y0 + _ystart;
data[3] = y1 + _ystart;
ST7735_WriteData(data, sizeof(data));
// write to RAM
ST7735_WriteCommand(ST7735_RAMWR);
}
/******************************************************************************
函数说明:显示单个字符
入口数据:x,y显示坐标
ch 要显示的字符
font 字号
color 字色
bgcolor 字的背景色
mode: 0非叠加模式 1叠加模式
返回值: 无
******************************************************************************/
static void ST7735_WriteChar(uint16_t x, uint16_t y, char ch, FontDef font, uint16_t color, uint16_t bgcolor)
{
uint32_t i, b, j;
ST7735_SetAddressWindow(x, y, x + font.width - 1, y + font.height - 1);
for (i = 0; i < font.height; i++)
{
b = font.data[(ch - 32) * font.height + i];
for (j = 0; j < font.width; j++)
{
if ((b << j) & 0x8000)
{
uint8_t data[] = {color >> 8, color & 0xFF};
ST7735_WriteData(data, sizeof(data));
}
else
{
uint8_t data[] = {bgcolor >> 8, bgcolor & 0xFF};
ST7735_WriteData(data, sizeof(data));
}
}
}
}
void XYZST7735_Init()
{
TFT_CS_L();
ST7735_Reset();
ST7735_ExecuteCommandList(init_cmds1);
ST7735_ExecuteCommandList(init_cmds2);
ST7735_ExecuteCommandList(init_cmds3);
TFT_CS_H();
}
void ST7735_DrawPixel(uint16_t x, uint16_t y, uint16_t color)
{
if ((x >= _width) || (y >= _height))
return;
TFT_CS_L();
ST7735_SetAddressWindow(x, y, x + 1, y + 1);
uint8_t data[] = {color >> 8, color & 0xFF};
ST7735_WriteData(data, sizeof(data));
TFT_CS_H();
}
/******************************************************************************
函数说明:显示字符串
入口数据:x,y显示坐标
*str 要显示的字符串
font 字号
color 颜色
bgcolor 背景颜色
返回值: 无
******************************************************************************/
void ST7735_DrawString(uint16_t x, uint16_t y, const char *str, FontDef font, uint16_t color, uint16_t bgcolor)
{
TFT_CS_L();
while (*str)
{
if (x + font.width >= _width)
{
x = 0;
y += font.height;
if (y + font.height >= _height)
{
break;
}
if (*str == ' ')
{
// skip spaces in the beginning of the new line
str++;
continue;
}
}
ST7735_WriteChar(x, y, *str, font, color, bgcolor);
x += font.width;
str++;
}
TFT_CS_H();
}
/******************************************************************************
函数说明:显示整数变量
入口数据:x,y显示坐标
number 要显示整数变量
len 要显示的位数
font 字号
color 字色
bgcolor 字的背景色
返回值: 无
******************************************************************************/
char strValue[8] = {0};
void LCD_ShowIntNum(uint16_t x, uint16_t y,uint16_t number, FontDef font, uint16_t color, uint16_t bgcolor)
{
int k = 0, j = 0;
int num = (int)number;
char tem[10];
if (number == 0)
{
strValue[0] = '0';
strValue[1] = '\0';
return;
}
while (num)
{
tem[k++] = num % 10 + '0'; //将数字加字符0就变成相应字符
num /= 10; //此时的字符串为逆序
}
tem[k] = '\0';
k = k - 1;
while (k >= 0)
{
strValue[j++] = tem[k--]; //将逆序的字符串转为正序
}
strValue[j] = '\0'; //字符串结束标志
ST7735_DrawString( x, y, (uint8_t *)strValue, font, color, bgcolor);
}
void ST7735_FillRectangle(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint16_t color)
{
// clipping
if ((x >= _width) || (y >= _height))
return;
if ((x + w - 1) >= _width)
w = _width - x;
if ((y + h - 1) >= _height)
h = _height - y;
TFT_CS_L();
ST7735_SetAddressWindow(x, y, x + w - 1, y + h - 1);
uint8_t data[2] = {color >> 8, color & 0xFF};
TFT_DC_D();
#ifdef USE_SPI_DMA
uint8_t tbuf[w * 2];
for (y = h; y > 0; y--)
{
for (int x = w * 2; x >= 0; x -= 2)
{
tbuf[x] = data[0];
tbuf[x + 1] = data[1];
}
// HAL_SPI_Transmit_DMA(&ST7735_SPI_PORT, tbuf, sizeof(tbuf));
// while (hspi2.State == HAL_SPI_STATE_BUSY_TX)
// {
// };
}
#else
for (y = h; y > 0; y--)
{
for (x = w; x > 0; x--)
{
//HAL_SPI_Transmit(&ST7735_SPI_PORT, data, sizeof(data), HAL_MAX_DELAY);
ST7735_data(data[0]>>8);
ST7735_data(data[1]);
}
}
#endif
TFT_CS_H();
}
void ST7735_FillScreen(uint16_t color)
{
ST7735_FillRectangle(0, 0, _width, _height, color);
}
void ST7735_DrawImage(uint16_t x, uint16_t y, uint16_t w, uint16_t h, const uint16_t *data)
{
if ((x >= _width) || (y >= _height))
return;
if ((x + w - 1) >= _width)
return;
if ((y + h - 1) >= _height)
return;
TFT_CS_L();
ST7735_SetAddressWindow(x, y, x + w - 1, y + h - 1);
ST7735_WriteData((uint8_t *)data, sizeof(uint16_t) * w * h);
TFT_CS_H();
}
void ST7735_InvertColors(bool invert)
{
TFT_CS_L();
ST7735_WriteCommand(invert ? ST7735_INVON : ST7735_INVOFF);
TFT_CS_H();
}
void ST7735_Backlight_On(void)
{
TFT_BL_H();
}
void ST7735_Backlight_Off(void)
{
TFT_BL_L();
}
/***************************************************************************************
** Function name: drawCircle
** Description: Draw a circle outline
***************************************************************************************/
void ST7735_DrawCircle(int16_t x0, int16_t y0, int16_t r, uint16_t color)
{
int16_t f = 1 - r;
int16_t ddF_x = 1;
int16_t ddF_y = -r - r;
int16_t x = 0;
ST7735_DrawPixel(x0 + r, y0, color);
ST7735_DrawPixel(x0 - r, y0, color);
ST7735_DrawPixel(x0, y0 - r, color);
ST7735_DrawPixel(x0, y0 + r, color);
while (x < r)
{
if (f >= 0)
{
r--;
ddF_y += 2;
f += ddF_y;
}
x++;
ddF_x += 2;
f += ddF_x;
ST7735_DrawPixel(x0 + x, y0 + r, color);
ST7735_DrawPixel(x0 - x, y0 + r, color);
ST7735_DrawPixel(x0 - x, y0 - r, color);
ST7735_DrawPixel(x0 + x, y0 - r, color);
ST7735_DrawPixel(x0 + r, y0 + x, color);
ST7735_DrawPixel(x0 - r, y0 + x, color);
ST7735_DrawPixel(x0 - r, y0 - x, color);
ST7735_DrawPixel(x0 + r, y0 - x, color);
}
}
/***************************************************************************************
** Function name: drawCircleHelper
** Description: Support function for circle drawing
***************************************************************************************/
void ST7735_DrawCircleHelper(int16_t x0, int16_t y0, int16_t r, uint8_t cornername, uint16_t color)
{
int16_t f = 1 - r;
int16_t ddF_x = 1;
int16_t ddF_y = -2 * r;
int16_t x = 0;
while (x < r)
{
if (f >= 0)
{
r--;
ddF_y += 2;
f += ddF_y;
}
x++;
ddF_x += 2;
f += ddF_x;
if (cornername & 0x8)
{
ST7735_DrawPixel(x0 - r, y0 + x, color);
ST7735_DrawPixel(x0 - x, y0 + r, color);
}
if (cornername & 0x4)
{
ST7735_DrawPixel(x0 + x, y0 + r, color);
ST7735_DrawPixel(x0 + r, y0 + x, color);
}
if (cornername & 0x2)
{
ST7735_DrawPixel(x0 + r, y0 - x, color);
ST7735_DrawPixel(x0 + x, y0 - r, color);
}
if (cornername & 0x1)
{
ST7735_DrawPixel(x0 - x, y0 - r, color);
ST7735_DrawPixel(x0 - r, y0 - x, color);
}
}
}
/***************************************************************************************
** Function name: fillCircle
** Description: draw a filled circle
***************************************************************************************/
void ST7735_FillCircle(int16_t x0, int16_t y0, int16_t r, uint16_t color)
{
ST7735_DrawFastVLine(x0, y0 - r, r + r + 1, color);
ST7735_FillCircleHelper(x0, y0, r, 3, 0, color);
}
/***************************************************************************************
** Function name: fillCircleHelper
** Description: Support function for filled circle drawing
***************************************************************************************/
// Used to do circles and roundrects
void ST7735_FillCircleHelper(int16_t x0, int16_t y0, int16_t r, uint8_t cornername, int16_t delta, uint16_t color)
{
int16_t f = 1 - r;
int16_t ddF_x = 1;
int16_t ddF_y = -r - r;
int16_t x = 0;
delta++;
while (x < r)
{
if (f >= 0)
{
r--;
ddF_y += 2;
f += ddF_y;
}
x++;
ddF_x += 2;
f += ddF_x;
if (cornername & 0x1)
{
ST7735_DrawFastVLine(x0 + x, y0 - r, r + r + delta, color);
ST7735_DrawFastVLine(x0 + r, y0 - x, x + x + delta, color);
}
if (cornername & 0x2)
{
ST7735_DrawFastVLine(x0 - x, y0 - r, r + r + delta, color);
ST7735_DrawFastVLine(x0 - r, y0 - x, x + x + delta, color);
}
}
}
/***************************************************************************************
** Function name: drawEllipse
** Description: Draw a ellipse outline
***************************************************************************************/
void ST7735_DrawEllipse(int16_t x0, int16_t y0, int16_t rx, int16_t ry, uint16_t color)
{
if (rx < 2)
return;
if (ry < 2)
return;
int16_t x, y;
int32_t rx2 = rx * rx;
int32_t ry2 = ry * ry;
int32_t fx2 = 4 * rx2;
int32_t fy2 = 4 * ry2;
int32_t s;
for (x = 0, y = ry, s = 2 * ry2 + rx2 * (1 - 2 * ry); ry2 * x <= rx2 * y; x++)
{
ST7735_DrawPixel(x0 + x, y0 + y, color);
ST7735_DrawPixel(x0 - x, y0 + y, color);
ST7735_DrawPixel(x0 - x, y0 - y, color);
ST7735_DrawPixel(x0 + x, y0 - y, color);
if (s >= 0)
{
s += fx2 * (1 - y);
y--;
}
s += ry2 * ((4 * x) + 6);
}
for (x = rx, y = 0, s = 2 * rx2 + ry2 * (1 - 2 * rx); rx2 * y <= ry2 * x; y++)
{
ST7735_DrawPixel(x0 + x, y0 + y, color);
ST7735_DrawPixel(x0 - x, y0 + y, color);
ST7735_DrawPixel(x0 - x, y0 - y, color);
ST7735_DrawPixel(x0 + x, y0 - y, color);
if (s >= 0)
{
s += fy2 * (1 - x);
x--;
}
s += rx2 * ((4 * y) + 6);
}
}
/***************************************************************************************
** Function name: fillEllipse
** Description: draw a filled ellipse
***************************************************************************************/
void ST7735_FillEllipse(int16_t x0, int16_t y0, int16_t rx, int16_t ry, uint16_t color)
{
if (rx < 2)
return;
if (ry < 2)
return;
int16_t x, y;
int32_t rx2 = rx * rx;
int32_t ry2 = ry * ry;
int32_t fx2 = 4 * rx2;
int32_t fy2 = 4 * ry2;
int32_t s;
for (x = 0, y = ry, s = 2 * ry2 + rx2 * (1 - 2 * ry); ry2 * x <= rx2 * y; x++)
{
ST7735_DrawFastHLine(x0 - x, y0 - y, x + x + 1, color);
ST7735_DrawFastHLine(x0 - x, y0 + y, x + x + 1, color);
if (s >= 0)
{
s += fx2 * (1 - y);
y--;
}
s += ry2 * ((4 * x) + 6);
}
for (x = rx, y = 0, s = 2 * rx2 + ry2 * (1 - 2 * rx); rx2 * y <= ry2 * x; y++)
{
ST7735_DrawFastHLine(x0 - x, y0 - y, x + x + 1, color);
ST7735_DrawFastHLine(x0 - x, y0 + y, x + x + 1, color);
if (s >= 0)
{
s += fy2 * (1 - x);
x--;
}
s += rx2 * ((4 * y) + 6);
}
}
/***************************************************************************************
** Function name: drawRect
** Description: Draw a rectangle outline
***************************************************************************************/
// Draw a rectangle
void ST7735_DrawRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color)
{
ST7735_DrawFastHLine(x, y, w, color);
ST7735_DrawFastHLine(x, y + h - 1, w, color);
ST7735_DrawFastVLine(x, y, h, color);
ST7735_DrawFastVLine(x + w - 1, y, h, color);
}
/***************************************************************************************
** Function name: drawRoundRect
** Description: Draw a rounded corner rectangle outline
***************************************************************************************/
// Draw a rounded rectangle
void ST7735_DrawRoundRect(int16_t x, int16_t y, int16_t w, int16_t h, int16_t r, uint16_t color)
{
// smarter version
ST7735_DrawFastHLine(x + r, y, w - r - r, color); // Top
ST7735_DrawFastHLine(x + r, y + h - 1, w - r - r, color); // Bottom
ST7735_DrawFastVLine(x, y + r, h - r - r, color); // Left
ST7735_DrawFastVLine(x + w - 1, y + r, h - r - r, color); // Right
// draw four corners
ST7735_DrawCircleHelper(x + r, y + r, r, 1, color);
ST7735_DrawCircleHelper(x + r, y + h - r - 1, r, 8, color);
ST7735_DrawCircleHelper(x + w - r - 1, y + r, r, 2, color);
ST7735_DrawCircleHelper(x + w - r - 1, y + h - r - 1, r, 4, color);
}
/***************************************************************************************
** Function name: fillRoundRect
** Description: Draw a rounded corner filled rectangle
***************************************************************************************/
// Fill a rounded rectangle
void ST7735_FillRoundRect(int16_t x, int16_t y, int16_t w, int16_t h, int16_t r, uint16_t color)
{
// smarter version
ST7735_FillRectangle(x + r, y, w - r - r, h, color);
// draw four corners
ST7735_FillCircleHelper(x + w - r - 1, y + r, r, 1, h - r - r - 1, color);
ST7735_FillCircleHelper(x + r, y + r, r, 2, h - r - r - 1, color);
}
/***************************************************************************************
** Function name: drawTriangle
** Description: Draw a triangle outline using 3 arbitrary points
***************************************************************************************/
// Draw a triangle
void ST7735_DrawTriangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t x2, int16_t y2, uint16_t color)
{
ST7735_DrawLine(x0, y0, x1, y1, color);
ST7735_DrawLine(x1, y1, x2, y2, color);
ST7735_DrawLine(x2, y2, x0, y0, color);
}
/***************************************************************************************
** Function name: fillTriangle
** Description: Draw a filled triangle using 3 arbitrary points
***************************************************************************************/
// Fill a triangle - original Adafruit function works well and code footprint is small
void ST7735_FillTriangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t x2, int16_t y2, uint16_t color)
{
int16_t a, b, y, last;
// Sort coordinates by Y order (y2 >= y1 >= y0)
if (y0 > y1)
{
SWAP_INT16_T(y0, y1);
SWAP_INT16_T(x0, x1);
}
if (y1 > y2)
{
SWAP_INT16_T(y2, y1);
SWAP_INT16_T(x2, x1);
}
if (y0 > y1)
{
SWAP_INT16_T(y0, y1);
SWAP_INT16_T(x0, x1);
}
if (y0 == y2)
{ // Handle awkward all-on-same-line case as its own thing
a = b = x0;
if (x1 < a)
a = x1;
else if (x1 > b)
b = x1;
if (x2 < a)
a = x2;
else if (x2 > b)
b = x2;
ST7735_DrawFastHLine(a, y0, b - a + 1, color);
return;
}
int16_t
dx01 = x1 - x0,
dy01 = y1 - y0,
dx02 = x2 - x0,
dy02 = y2 - y0,
dx12 = x2 - x1,
dy12 = y2 - y1,
sa = 0,
sb = 0;
// For upper part of triangle, find scanline crossings for segments
// 0-1 and 0-2. If y1=y2 (flat-bottomed triangle), the scanline y1
// is included here (and second loop will be skipped, avoiding a /0
// error there), otherwise scanline y1 is skipped here and handled
// in the second loop...which also avoids a /0 error here if y0=y1
// (flat-topped triangle).
if (y1 == y2)
last = y1; // Include y1 scanline
else
last = y1 - 1; // Skip it
for (y = y0; y <= last; y++)
{
a = x0 + sa / dy01;
b = x0 + sb / dy02;
sa += dx01;
sb += dx02;
if (a > b)
SWAP_INT16_T(a, b);
ST7735_DrawFastHLine(a, y, b - a + 1, color);
}
// For lower part of triangle, find scanline crossings for segments
// 0-2 and 1-2. This loop is skipped if y1=y2.
sa = dx12 * (y - y1);
sb = dx02 * (y - y0);
for (; y <= y2; y++)
{
a = x1 + sa / dy12;
b = x0 + sb / dy02;
sa += dx12;
sb += dx02;
if (a > b)
SWAP_INT16_T(a, b);
ST7735_DrawFastHLine(a, y, b - a + 1, color);
}
}
/***************************************************************************************
** Function name: drawLine
** Description: draw a line between 2 arbitrary points
***************************************************************************************/
// Slower but more compact line drawing function
void ST7735_DrawLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1, uint16_t color)
{
int16_t steep = abs(y1 - y0) > abs(x1 - x0);
if (steep)
{
SWAP_INT16_T(x0, y0);
SWAP_INT16_T(x1, y1);
}
if (x0 > x1)
{
SWAP_INT16_T(x0, x1);
SWAP_INT16_T(y0, y1);
}
int16_t dx, dy;
dx = x1 - x0;
dy = abs(y1 - y0);
int16_t err = dx / 2;
int16_t ystep;
if (y0 < y1)
{
ystep = 1;
}
else
{
ystep = -1;
}
for (; x0 <= x1; x0++)
{
if (steep)
{
ST7735_DrawPixel(y0, x0, color);
}
else
{
ST7735_DrawPixel(x0, y0, color);
}
err -= dy;
if (err < 0)
{
y0 += ystep;
err += dx;
}
}
}
/***************************************************************************************
** Function name: drawFastVLine
** Description: draw a vertical line
***************************************************************************************/
void ST7735_DrawFastVLine(int16_t x, int16_t y, int16_t h, uint16_t color)
{
// Rudimentary clipping
if ((x >= _width) || (y >= _height))
return;
if ((y + h - 1) >= _height)
h = _height - y;
#ifdef USE_SPI_DMA
ST7735_FillRectangle(x, y, 1, h, color);
#else
ST7735_DrawLine(x, y, x, y + h - 1, color);
#endif
}
/***************************************************************************************
** Function name: drawFastHLine
** Description: draw a horizontal line
***************************************************************************************/
void ST7735_DrawFastHLine(int16_t x, int16_t y, int16_t w, uint16_t color)
{
// Rudimentary clipping
if ((x >= _width) || (y >= _height))
return;
if ((x + w - 1) >= _width)
w = _width - x;
#ifdef USE_SPI_DMA
ST7735_FillRectangle(x, y, w, 1, color);
#else
ST7735_DrawLine(x, y, x + w - 1, y, color);
#endif
}
/***************************************************************************************
** Function name: setRotation
** Description: rotate the screen orientation m = 0-3
***************************************************************************************/
void ST7735_SetRotation(uint8_t m)
{
_value_rotation = m % 4;
TFT_CS_L();
ST7735_WriteCommand(ST7735_MADCTL);
switch (_value_rotation)
{
case 0:
{
uint8_t d_r = (_data_rotation[0] | _data_rotation[1] | _data_rotation[3]);
ST7735_WriteData(&d_r, sizeof(d_r));
_width = ST7735_WIDTH;
_height = ST7735_HEIGHT;
_xstart = ST7735_XSTART;
_ystart = ST7735_YSTART;
}
break;
case 1:
{
uint8_t d_r = (_data_rotation[1] | _data_rotation[2] | _data_rotation[3]);
ST7735_WriteData(&d_r, sizeof(d_r));
_width = ST7735_HEIGHT;
_height = ST7735_WIDTH;
_xstart = ST7735_YSTART;
_ystart = ST7735_XSTART;
}
break;
case 2:
{
uint8_t d_r = _data_rotation[3];
ST7735_WriteData(&d_r, sizeof(d_r));
_width = ST7735_WIDTH;
_height = ST7735_HEIGHT;
_xstart = ST7735_XSTART;
_ystart = ST7735_YSTART;
}
break;
case 3:
{
uint8_t d_r = (_data_rotation[0] | _data_rotation[2] | _data_rotation[3]);
ST7735_WriteData(&d_r, sizeof(d_r));
_width = ST7735_HEIGHT;
_height = ST7735_WIDTH;
_xstart = ST7735_YSTART;
_ystart = ST7735_XSTART;
}
break;
}
TFT_CS_H();
}
uint8_t ST7735_GetRotation(void)
{
return _value_rotation;
}
int16_t ST7735_GetHeight(void)
{
return _height;
}
int16_t ST7735_GetWidth(void)
{
return _width;
}
const uint16_t test_img_128x128[][128] =
{
0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xDFFF,0xDFFF,0xDFFF,0xDFFF,0xDFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFF7,0xFFF7,0xFFF7,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,
};
uint8_t r = 3;
void demoTFT(void)
{
ST7735_SetRotation(r);
ST7735_FillScreen(ST7735_BLACK);
for(int x = 0; x < ST7735_GetWidth(); x++)
{
ST7735_DrawPixel(x, 0, ST7735_WHITE);
ST7735_DrawPixel(x, ST7735_GetHeight() - 1, ST7735_WHITE);
}
for(int y = 0; y < ST7735_GetHeight(); y++)
{
ST7735_DrawPixel(0, y, ST7735_WHITE);
ST7735_DrawPixel(ST7735_GetWidth() - 1, y, ST7735_WHITE);
}
ST7735_DrawLine(0, 0, ST7735_GetWidth(), ST7735_GetHeight(), ST7735_WHITE);
ST7735_DrawLine(ST7735_GetWidth(), 0, 0, ST7735_GetHeight(), ST7735_WHITE);
R_BSP_SoftwareDelay(500, BSP_DELAY_UNITS_MILLISECONDS);
ST7735_FillScreen(ST7735_BLACK);
for (int i = 0; i < ST7735_GetHeight(); i += 4)
{
ST7735_DrawFastHLine(0, i, ST7735_GetWidth() - 1, ST7735_WHITE);
}
for (int i = 0; i < ST7735_GetWidth(); i += 4)
{
ST7735_DrawFastVLine(i, 0, ST7735_GetHeight() - 1, ST7735_WHITE);
}
R_BSP_SoftwareDelay(500, BSP_DELAY_UNITS_MILLISECONDS);
// Check fonts
ST7735_FillScreen(ST7735_BLACK);
ST7735_DrawString(0, 0, "Font_7x10, red on black, lorem ipsum dolor sit amet", Font_7x10, ST7735_RED, ST7735_BLACK);
ST7735_DrawString(0, 3*10, "Font_11x18, green, lorem ipsum", Font_11x18, ST7735_GREEN, ST7735_BLACK);
ST7735_DrawString(0, 3*10+3*18, "Font_16x26", Font_16x26, ST7735_BLUE, ST7735_BLACK);
R_BSP_SoftwareDelay(500, BSP_DELAY_UNITS_MILLISECONDS);
// Check colors
ST7735_FillScreen(ST7735_BLACK);
ST7735_DrawString(0, 0, "BLACK", Font_11x18, ST7735_WHITE, ST7735_BLACK);
R_BSP_SoftwareDelay(500, BSP_DELAY_UNITS_MILLISECONDS);
ST7735_FillScreen(ST7735_BLUE);
ST7735_DrawString(0, 0, "BLUE", Font_11x18, ST7735_BLACK, ST7735_BLUE);
R_BSP_SoftwareDelay(500, BSP_DELAY_UNITS_MILLISECONDS);
ST7735_FillScreen(ST7735_RED);
ST7735_DrawString(0, 0, "RED", Font_11x18, ST7735_BLACK, ST7735_RED);
R_BSP_SoftwareDelay(500, BSP_DELAY_UNITS_MILLISECONDS);
ST7735_FillScreen(ST7735_GREEN);
ST7735_DrawString(0, 0, "GREEN", Font_11x18, ST7735_BLACK, ST7735_GREEN);
R_BSP_SoftwareDelay(500, BSP_DELAY_UNITS_MILLISECONDS);
ST7735_FillScreen(ST7735_CYAN);
ST7735_DrawString(0, 0, "CYAN", Font_11x18, ST7735_BLACK, ST7735_CYAN);
R_BSP_SoftwareDelay(500, BSP_DELAY_UNITS_MILLISECONDS);
ST7735_FillScreen(ST7735_MAGENTA);
ST7735_DrawString(0, 0, "MAGENTA", Font_11x18, ST7735_BLACK, ST7735_MAGENTA);
R_BSP_SoftwareDelay(500, BSP_DELAY_UNITS_MILLISECONDS);
ST7735_FillScreen(ST7735_YELLOW);
ST7735_DrawString(0, 0, "YELLOW", Font_11x18, ST7735_BLACK, ST7735_YELLOW);
R_BSP_SoftwareDelay(500, BSP_DELAY_UNITS_MILLISECONDS);
ST7735_FillScreen(ST7735_WHITE);
ST7735_DrawString(0, 0, "WHITE", Font_11x18, ST7735_BLACK, ST7735_WHITE);
R_BSP_SoftwareDelay(500, BSP_DELAY_UNITS_MILLISECONDS);
// Draw circles
ST7735_FillScreen(ST7735_BLACK);
for (int i = 0; i < ST7735_GetHeight() / 2; i += 2)
{
ST7735_DrawCircle(ST7735_GetWidth() / 2, ST7735_GetHeight() / 2, i, ST7735_YELLOW);
}
R_BSP_SoftwareDelay(500, BSP_DELAY_UNITS_MILLISECONDS);
ST7735_FillScreen(ST7735_BLACK);
ST7735_FillTriangle(0, 0, ST7735_GetWidth() / 2, ST7735_GetHeight(), ST7735_GetWidth(), 0, ST7735_RED);
R_BSP_SoftwareDelay(500, BSP_DELAY_UNITS_MILLISECONDS);
ST7735_FillScreen(ST7735_BLACK);
ST7735_DrawImage(0, 0, 128, 128, (uint16_t*) test_img_128x128);
R_BSP_SoftwareDelay(500, BSP_DELAY_UNITS_MILLISECONDS);
r++;
}
ST7735.h代码如下:
#ifndef _ST7735_H_
#define _ST7735_H_
#include "hal_data.h"
#define ROW 160
#define COL 128
#define TP_TEST
#define EXTEND_TEST
#define CHAR_FONT_W8_H16
//------------------------------------------------------
#ifdef READ_REG
#define STRING_FUNCTION
#define READ_FUNCTION
#endif
#ifdef DEBUG
#define STRING_FUNCTION
#endif
#ifdef CONN_USB
#define STRING_FUNCTION
#define READ_FUNCTION
#endif
#ifdef TP_TEST
#define DRAW_FUNCTION
#define STRING_FUNCTION
#endif
#ifdef AUTO_TEST
#define AUTO_NEXT
#else
#define MANUA_NEXT
#endif
#ifdef CHAR_FONT_W8_H16
#define FONT_W 8
#define FONT_H 16
#endif
#ifdef CHAR_FONT_W16_H21
#define FONT_W 16
#define FONT_H 21
#endif
//------------------------------------------------------
#define PIC_WIDTH 160
#define PIC_HEIGHT 160
#define GRAY75 0x39E7
#define GRAY50 0x7BEF
#define GRAY25 0xADB5
#define X_min 0x0043
#define X_max 0x07AE
#define Y_min 0x00A1
#define Y_max 0x0759
//---------------------------------------------------------------------
void WriteComm(unsigned int i);
void WriteData(unsigned int i);
void WriteDispData(unsigned char DataH,unsigned char DataL);
//void LCD_Init(void);
void BlockWrite(unsigned int Xstart,unsigned int Xend,unsigned int Ystart,unsigned int Yend) ;
void DispBand(void);
void DispColor(unsigned int color);
void DispFrame(void);
void DispPic(unsigned int const *picture);
void DispPicFromSD(unsigned char PicNum);
#define ST7735_SPI_PORT hspi2
//#define USE_SPI_DMA
#define ST7735_1_8_DEFAULT_ORIENTATION // AliExpress/eBay 1.8" display, default orientation
#define ST7735_MADCTL_MY 0x80
#define ST7735_MADCTL_MX 0x40
#define ST7735_MADCTL_MV 0x20
#define ST7735_MADCTL_RGB 0x00
#define ST7735_MADCTL_BGR 0x08
// AliExpress/eBay 1.8" display, default orientation
#ifdef ST7735_1_8_DEFAULT_ORIENTATION
#define ST7735_IS_160X128 1
#define ST7735_WIDTH 128
#define ST7735_HEIGHT 160
#define ST7735_XSTART 2 //0
#define ST7735_YSTART 1 //0
#define ST7735_DATA_ROTATION (ST7735_MADCTL_MX | ST7735_MADCTL_MY)
#endif //ST7735_1_8_DEFAULT_ORIENTATION
// WaveShare ST7735S-based 1.8" display, default orientation
#ifdef ST7735S_1_8_DEFAULT_ORIENTATION
#define ST7735_IS_160X128 1
#define ST7735_WIDTH 128
#define ST7735_HEIGHT 160
#define ST7735_XSTART 2
#define ST7735_YSTART 1
#define ST7735_DATA_ROTATION (ST7735_MADCTL_MX | ST7735_MADCTL_MY | ST7735_MADCTL_RGB)
#endif //ST7735S_1_8_DEFAULT_ORIENTATION
// 1.44" display, default orientation
#ifdef ST7735_1_44_DEFAULT_ORIENTATION
#define ST7735_IS_128X128 1
#define ST7735_WIDTH 128
#define ST7735_HEIGHT 128
#define ST7735_XSTART 2
#define ST7735_YSTART 3
//#define ST7735_VALUE_ROTATION 0
#define ST7735_DATA_ROTATION (ST7735_MADCTL_MX | ST7735_MADCTL_MY | ST7735_MADCTL_BGR)
#endif //ST7735_1_44_DEFAULT_ORIENTATION
// mini 160x80 display (it's unlikely you want the default orientation)
#ifdef ST7735_MINI_DEFAULT_ORIENTATION
#define ST7735_IS_160X80 1
#define ST7735_XSTART 26
#define ST7735_YSTART 1
#define ST7735_WIDTH 80
#define ST7735_HEIGHT 160
//#define ST7735_VALUE_ROTATION 0
#define ST7735_DATA_ROTATION (ST7735_MADCTL_MX | ST7735_MADCTL_MY | ST7735_MADCTL_BGR)
#endif //ST7735_MINI_DEFAULT_ORIENTATION
/****************************/
#define ST7735_NOP 0x00
#define ST7735_SWRESET 0x01
#define ST7735_RDDID 0x04
#define ST7735_RDDST 0x09
#define ST7735_SLPIN 0x10
#define ST7735_SLPOUT 0x11
#define ST7735_PTLON 0x12
#define ST7735_NORON 0x13
#define ST7735_INVOFF 0x20
#define ST7735_INVON 0x21
#define ST7735_DISPOFF 0x28
#define ST7735_DISPON 0x29
#define ST7735_CASET 0x2A
#define ST7735_RASET 0x2B
#define ST7735_RAMWR 0x2C
#define ST7735_RAMRD 0x2E
#define ST7735_PTLAR 0x30
#define ST7735_COLMOD 0x3A
#define ST7735_MADCTL 0x36
#define ST7735_FRMCTR1 0xB1
#define ST7735_FRMCTR2 0xB2
#define ST7735_FRMCTR3 0xB3
#define ST7735_INVCTR 0xB4
#define ST7735_DISSET5 0xB6
#define ST7735_PWCTR1 0xC0
#define ST7735_PWCTR2 0xC1
#define ST7735_PWCTR3 0xC2
#define ST7735_PWCTR4 0xC3
#define ST7735_PWCTR5 0xC4
#define ST7735_VMCTR1 0xC5
#define ST7735_RDID1 0xDA
#define ST7735_RDID2 0xDB
#define ST7735_RDID3 0xDC
#define ST7735_RDID4 0xDD
#define ST7735_PWCTR6 0xFC
#define ST7735_GMCTRP1 0xE0
#define ST7735_GMCTRN1 0xE1
// Color definitions
#define ST7735_BLACK 0x0000
#define ST7735_BLUE 0x001F
#define ST7735_RED 0xF800
#define ST7735_GREEN 0x07E0
#define ST7735_CYAN 0x07FF
#define ST7735_MAGENTA 0xF81F
#define ST7735_YELLOW 0xFFE0
#define ST7735_WHITE 0xFFFF
//������ɫ
#define WHITE 0xFFFF
#define BLACK 0x0000
#define BLUE 0x001F
#define BRED 0XF81F
#define GRED 0XFFE0
#define GBLUE 0X07FF
#define RED 0xF800
#define MAGENTA 0xF81F
#define GREEN 0x07E0
#define CYAN 0x7FFF
#define YELLOW 0xFFE0
#define BROWN 0XBC40 //��ɫ
#define BRRED 0XFC07 //�غ�ɫ
#define GRAY 0X8430 //��ɫ
//#define bool uint8_t
typedef struct {
const uint8_t width;
uint8_t height;
const uint16_t *data;
} FontDef;
extern FontDef Font_7x10;
extern FontDef Font_11x18;
extern FontDef Font_16x26;
//--------------SPI---------------------
#define OLED_MOSI_SET() R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_00_PIN_02, BSP_IO_LEVEL_HIGH)//MOSI
#define OLED_MOSI_CLR() R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_00_PIN_02, BSP_IO_LEVEL_LOW)//MOSI
#define OLED_CLK_SET() R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_00_PIN_01, BSP_IO_LEVEL_HIGH)//CLK
#define OLED_CLK_CLR() R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_00_PIN_01, BSP_IO_LEVEL_LOW)//CLK
//-----------------OLED----------------
#define OLED_CS_Set() R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_00_PIN_07, BSP_IO_LEVEL_HIGH)//CS
#define OLED_CS_Clr() R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_00_PIN_07, BSP_IO_LEVEL_LOW)//CS
#define OLED_DC_Set() R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_00_PIN_06, BSP_IO_LEVEL_HIGH)//DC
#define OLED_DC_Clr() R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_00_PIN_06, BSP_IO_LEVEL_LOW)//DC
#define OLED_RST_Set() R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_00_PIN_03, BSP_IO_LEVEL_HIGH)//RST
#define OLED_RST_Clr() R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_00_PIN_03, BSP_IO_LEVEL_LOW)//RST
#define OLED_BL_Set() R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_00_PIN_08, BSP_IO_LEVEL_HIGH)//BL
#define OLED_BL_Clr() R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_00_PIN_08, BSP_IO_LEVEL_LOW)//BL
#define TFT_BL_H() R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_00_PIN_08, BSP_IO_LEVEL_HIGH) //BL
#define TFT_BL_L() R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_00_PIN_08, BSP_IO_LEVEL_LOW) //BL
#define TFT_CS_H() R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_00_PIN_07, BSP_IO_LEVEL_HIGH) //CS
#define TFT_CS_L() R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_00_PIN_07, BSP_IO_LEVEL_LOW)//CS
#define TFT_DC_D() R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_00_PIN_06, BSP_IO_LEVEL_HIGH) //DC
#define TFT_DC_C() R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_00_PIN_06, BSP_IO_LEVEL_LOW)//DC
#define TFT_RES_H() R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_00_PIN_03, BSP_IO_LEVEL_HIGH) //RST
#define TFT_RES_L() R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_00_PIN_03, BSP_IO_LEVEL_LOW)//RST
#define TFT_SCL_H() R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_00_PIN_01, BSP_IO_LEVEL_HIGH) //CLK
#define TFT_SCL_L() R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_00_PIN_01, BSP_IO_LEVEL_LOW)//CLK
#define TFT_MOSI_H() R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_00_PIN_02, BSP_IO_LEVEL_HIGH) //MOSI
#define TFT_MOSI_L() R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_00_PIN_02, BSP_IO_LEVEL_LOW)//MOSI
void ST7735_GPIO_Init(void);
void ST7735_Spi2_Init(void);
void ST7735_Backlight_On(void);
void ST7735_Backlight_Off(void);
void XYZST7735_Init(void);
void ST7735_DrawPixel(uint16_t x, uint16_t y, uint16_t color);
void ST7735_DrawString(uint16_t x, uint16_t y, const char* str, FontDef font, uint16_t color, uint16_t bgcolor);
void LCD_ShowIntNum(uint16_t x, uint16_t y,uint16_t number,FontDef font, uint16_t color, uint16_t bgcolor);
void ST7735_FillRectangle(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint16_t color);
void ST7735_FillScreen(uint16_t color);
void ST7735_DrawImage(uint16_t x, uint16_t y, uint16_t w, uint16_t h, const uint16_t* data);
void ST7735_InvertColors(bool invert);
void ST7735_DrawCircle(int16_t x0, int16_t y0, int16_t r, uint16_t color);
void ST7735_DrawCircleHelper( int16_t x0, int16_t y0, int16_t r, uint8_t cornername, uint16_t color);
void ST7735_FillCircle(int16_t x0, int16_t y0, int16_t r, uint16_t color);
void ST7735_FillCircleHelper(int16_t x0, int16_t y0, int16_t r, uint8_t cornername, int16_t delta, uint16_t color);
void ST7735_DrawEllipse(int16_t x0, int16_t y0, int16_t rx, int16_t ry, uint16_t color);
void ST7735_FillEllipse(int16_t x0, int16_t y0, int16_t rx, int16_t ry, uint16_t color);
void ST7735_DrawRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color);
void ST7735_DrawRoundRect(int16_t x, int16_t y, int16_t w, int16_t h, int16_t r, uint16_t color);
void ST7735_FillRoundRect(int16_t x, int16_t y, int16_t w, int16_t h, int16_t r, uint16_t color);
void ST7735_DrawTriangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t x2, int16_t y2, uint16_t color);
void ST7735_FillTriangle( int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t x2, int16_t y2, uint16_t color);
void ST7735_DrawLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1, uint16_t color);
void ST7735_DrawFastVLine(int16_t x, int16_t y, int16_t h, uint16_t color);
void ST7735_DrawFastHLine(int16_t x, int16_t y, int16_t w, uint16_t color);
void ST7735_SetRotation(uint8_t m);
uint8_t ST7735_GetRotation(void);
int16_t ST7735_GetHeight(void);
int16_t ST7735_GetWidth(void);
void demoTFT(void);
void XTFT_Init_GPIO(void);
void LCD_Init(void);
#endif
遇事不决,可问春风!