一、硬件介绍
1. 屏幕介绍
1 屏幕驱动 ST7735
2 屏幕分辨率 135x240
3驱动接口 SPI
2. 开发板连接
对应引脚
// GND 电源地
// VCC 接5V或3.3v电源
// SCL 接PA5(SCL)
// SDA 接PA7(SDA)
// CS 接PA4//可以直接接地
// RES 接PA3
// DC 接PA2
// BLK 接PA1
实物图
![title= title=](//file.elecfans.com/web2/M00/79/81/poYBAGNstyuAOzuwAAXGwGheSqc517.jpg)
注意事项
这里使用的是硬件的SPI0,也就是用到了PA5和PA7这两个引脚,但是这块开发板的PA5是没有直接连接到外扩的引脚,中间这个排针需要跳一下,
![title= title=](//file.elecfans.com/web2/M00/7A/11/pYYBAGNstyyAP3tyAAwtcNy1ilI520.jpg)
二、软件介绍
1. 开发环境
1 keil 5.36版本 编写程序
2 Image2Lcd 2.9 图片取模
3 PCtoLCD2002 字符取模
2. 代码
#
#ifndef _St7735_H
#define _St7735_H
#include "gd32f3x0.h"
#include "systick.h"
#define USE_HORIZONTAL 3
#define USE_HARDWARESPI
#if USE_HORIZONTAL==0||USE_HORIZONTAL==1
#define LCD_W 135
#define LCD_H 240
#else
#define LCD_W 240
#define LCD_H 135
#endif
#ifndef USE_HARDWARESPI
#define LCD_SCLK_RCU RCU_GPIOA
#define LCD_SCLK_PORT GPIOA
#define LCD_SCLK_PIN GPIO_PIN_5
#define LCD_MOSI_RCU RCU_GPIOA
#define LCD_MOSI_PORT GPIOA
#define LCD_MOSI_PIN GPIO_PIN_7
#define LCD_SCLK_Clr() gpio_bit_reset(LCD_SCLK_PORT,LCD_SCLK_PIN)
#define LCD_SCLK_Set() gpio_bit_set(LCD_SCLK_PORT,LCD_SCLK_PIN)
#define LCD_MOSI_Clr() gpio_bit_reset(LCD_MOSI_PORT,LCD_MOSI_PIN)
#define LCD_MOSI_Set() gpio_bit_set(LCD_MOSI_PORT,LCD_MOSI_PIN)
#endif
#define LCD_CS_RCU RCU_GPIOA
#define LCD_CS_PORT GPIOA
#define LCD_CS_PIN GPIO_PIN_4
#define LCD_RES_RCU RCU_GPIOA
#define LCD_RES_PORT GPIOA
#define LCD_RES_PIN GPIO_PIN_3
#define LCD_DC_RCU RCU_GPIOA
#define LCD_DC_PORT GPIOA
#define LCD_DC_PIN GPIO_PIN_2
#define LCD_BLK_RCU RCU_GPIOA
#define LCD_BLK_PORT GPIOA
#define LCD_BLK_PIN GPIO_PIN_1
#define LCD_CS_Clr() gpio_bit_reset(LCD_CS_PORT,LCD_CS_PIN)
#define LCD_CS_Set() gpio_bit_set(LCD_CS_PORT,LCD_CS_PIN)
#define LCD_RES_Clr() gpio_bit_reset(LCD_RES_PORT,LCD_RES_PIN)
#define LCD_RES_Set() gpio_bit_set(LCD_RES_PORT,LCD_RES_PIN)
#define LCD_DC_Clr() gpio_bit_reset(LCD_DC_PORT,LCD_DC_PIN)
#define LCD_DC_Set() gpio_bit_set(LCD_DC_PORT,LCD_DC_PIN)
#define LCD_BLK_Clr() gpio_bit_reset(LCD_BLK_PORT,LCD_BLK_PIN)
#define LCD_BLK_Set() gpio_bit_set(LCD_BLK_PORT,LCD_BLK_PIN)
void LCD_GPIO_Init(void);
void LCD_Writ_Bus(uint8_t dat);
void LCD_WR_DATA8(uint8_t dat);
void LCD_WR_DATA(uint16_t dat);
void LCD_WR_REG(uint8_t dat);
void LCD_Address_Set(uint16_t x1,uint16_t y1,uint16_t x2,uint16_t y2);
void LCD_Init(void);
#endif
### 2.st7735.c
#include "St7735.h"
#include "GUI.h"
#include "spi.h"
void LCD_GPIO_Init(void)
{
#ifndef USE_HARDWARESPI
rcu_periph_clock_enable(LCD_SCLK_RCU);
gpio_mode_set(LCD_SCLK_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, LCD_SCLK_PIN);
gpio_output_options_set(LCD_SCLK_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, LCD_SCLK_PIN);
rcu_periph_clock_enable(LCD_MOSI_RCU);
gpio_mode_set(LCD_MOSI_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, LCD_MOSI_PIN);
gpio_output_options_set(LCD_MOSI_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, LCD_MOSI_PIN);
#endif
rcu_periph_clock_enable(LCD_CS_RCU);
gpio_mode_set(LCD_CS_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, LCD_CS_PIN);
gpio_output_options_set(LCD_CS_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, LCD_CS_PIN);
gpio_bit_set(LCD_CS_PORT,LCD_CS_PIN);
rcu_periph_clock_enable(LCD_RES_RCU);
gpio_mode_set(LCD_RES_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, LCD_RES_PIN);
gpio_output_options_set(LCD_RES_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, LCD_RES_PIN);
gpio_bit_set(LCD_RES_PORT,LCD_RES_PIN);
rcu_periph_clock_enable(LCD_DC_RCU);
gpio_mode_set(LCD_DC_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, LCD_DC_PIN);
gpio_output_options_set(LCD_DC_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, LCD_DC_PIN);
gpio_bit_set(LCD_DC_PORT,LCD_DC_PIN);
rcu_periph_clock_enable(LCD_BLK_RCU);
gpio_mode_set(LCD_BLK_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, LCD_BLK_PIN);
gpio_output_options_set(LCD_BLK_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, LCD_BLK_PIN);
}
#ifdef USE_HARDWARESPI
void LCD_Writ_Bus(uint8_t dat)
{
LCD_CS_Clr();
spi_ReadWriteByte(dat);
LCD_CS_Set();
}
#else
void LCD_Writ_Bus(uint8_t dat)
{
uint8_t i;
LCD_CS_Clr();
for(i=0;i<8;i++)
{
LCD_SCLK_Clr();
if(dat&0x80)
{
LCD_MOSI_Set();
}
else
{
LCD_MOSI_Clr();
}
LCD_SCLK_Set();
dat<<=1;
}
LCD_CS_Set();
}
#endif
void LCD_WR_DATA8(uint8_t dat)
{
LCD_Writ_Bus(dat);
}
void LCD_WR_DATA(uint16_t dat)
{
LCD_Writ_Bus(dat>>8);
LCD_Writ_Bus(dat);
}
void LCD_WR_REG(uint8_t dat)
{
LCD_DC_Clr();
LCD_Writ_Bus(dat);
LCD_DC_Set();
}
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+52);
LCD_WR_DATA(x2+52);
LCD_WR_REG(0x2b);
LCD_WR_DATA(y1+40);
LCD_WR_DATA(y2+40);
LCD_WR_REG(0x2c);
}
else if(USE_HORIZONTAL==1)
{
LCD_WR_REG(0x2a);
LCD_WR_DATA(x1+53);
LCD_WR_DATA(x2+53);
LCD_WR_REG(0x2b);
LCD_WR_DATA(y1+40);
LCD_WR_DATA(y2+40);
LCD_WR_REG(0x2c);
}
else if(USE_HORIZONTAL==2)
{
LCD_WR_REG(0x2a);
LCD_WR_DATA(x1+40);
LCD_WR_DATA(x2+40);
LCD_WR_REG(0x2b);
LCD_WR_DATA(y1+53);
LCD_WR_DATA(y2+53);
LCD_WR_REG(0x2c);
}
else
{
LCD_WR_REG(0x2a);
LCD_WR_DATA(x1+40);
LCD_WR_DATA(x2+40);
LCD_WR_REG(0x2b);
LCD_WR_DATA(y1+52);
LCD_WR_DATA(y2+52);
LCD_WR_REG(0x2c);
}
}
void LCD_Init(void)
{
LCD_GPIO_Init();
LCD_RES_Clr();
delay_1ms(100);
LCD_RES_Set();
delay_1ms(50);
LCD_BLK_Set();
delay_1ms(100);
LCD_WR_REG(0x11);
delay_1ms(120);
LCD_WR_REG(0x36);
if(USE_HORIZONTAL==0)LCD_WR_DATA8(0x00);
else if(USE_HORIZONTAL==1)LCD_WR_DATA8(0xC0);
else if(USE_HORIZONTAL==2)LCD_WR_DATA8(0x70);
else LCD_WR_DATA8(0xA0);
LCD_WR_REG(0x3A);
LCD_WR_DATA8(0x05);
LCD_WR_REG(0xB2);
LCD_WR_DATA8(0x0C);
LCD_WR_DATA8(0x0C);
LCD_WR_DATA8(0x00);
LCD_WR_DATA8(0x33);
LCD_WR_DATA8(0x33);
LCD_WR_REG(0xB7);
LCD_WR_DATA8(0x35);
LCD_WR_REG(0xBB);
LCD_WR_DATA8(0x19);
LCD_WR_REG(0xC0);
LCD_WR_DATA8(0x2C);
LCD_WR_REG(0xC2);
LCD_WR_DATA8(0x01);
LCD_WR_REG(0xC3);
LCD_WR_DATA8(0x12);
LCD_WR_REG(0xC4);
LCD_WR_DATA8(0x20);
LCD_WR_REG(0xC6);
LCD_WR_DATA8(0x0F);
LCD_WR_REG(0xD0);
LCD_WR_DATA8(0xA4);
LCD_WR_DATA8(0xA1);
LCD_WR_REG(0xE0);
LCD_WR_DATA8(0xD0);
LCD_WR_DATA8(0x04);
LCD_WR_DATA8(0x0D);
LCD_WR_DATA8(0x11);
LCD_WR_DATA8(0x13);
LCD_WR_DATA8(0x2B);
LCD_WR_DATA8(0x3F);
LCD_WR_DATA8(0x54);
LCD_WR_DATA8(0x4C);
LCD_WR_DATA8(0x18);
LCD_WR_DATA8(0x0D);
LCD_WR_DATA8(0x0B);
LCD_WR_DATA8(0x1F);
LCD_WR_DATA8(0x23);
LCD_WR_REG(0xE1);
LCD_WR_DATA8(0xD0);
LCD_WR_DATA8(0x04);
LCD_WR_DATA8(0x0C);
LCD_WR_DATA8(0x11);
LCD_WR_DATA8(0x13);
LCD_WR_DATA8(0x2C);
LCD_WR_DATA8(0x3F);
LCD_WR_DATA8(0x44);
LCD_WR_DATA8(0x51);
LCD_WR_DATA8(0x2F);
LCD_WR_DATA8(0x1F);
LCD_WR_DATA8(0x1F);
LCD_WR_DATA8(0x20);
LCD_WR_DATA8(0x23);
LCD_WR_REG(0x21);
LCD_WR_REG(0x29);
}
### 3.GUI.h
#ifndef _GUI_H
#define _GUI_H
#include "gd32f3x0.h"
#include "systick.h"
#define bg 0xE73D
#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 DARKBLUE 0X01CF
#define LIGHTBLUE 0X7D7C
#define GRAYBLUE 0X5458
#define LIGHTGREEN 0X841F
#define LGRAY 0XC618
#define LGRAYBLUE 0XA651
#define LBBLUE 0X2B12
void LCD_Fill(uint16_t xsta,uint16_t ysta,uint16_t xend,uint16_t yend,uint16_t color);
void LCD_DrawPoint(uint16_t x,uint16_t y,uint16_t color);
void LCD_DrawLine(uint16_t x1,uint16_t y1,uint16_t x2,uint16_t y2,uint16_t color);
void LCD_DrawRectangle(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2,uint16_t color);
void Draw_Circle(uint16_t x0,uint16_t y0,uint8_t r,uint16_t color);
void LCD_ShowChinese(uint16_t x,uint16_t y,uint8_t *s,uint16_t fc,uint16_t bc,uint8_t sizey,uint8_t mode);
void LCD_ShowChinese12x12(uint16_t x,uint16_t y,uint8_t *s,uint16_t fc,uint16_t bc,uint8_t sizey,uint8_t mode);
void LCD_ShowChinese16x16(uint16_t x,uint16_t y,uint8_t *s,uint16_t fc,uint16_t bc,uint8_t sizey,uint8_t mode);
void LCD_ShowChinese24x24(uint16_t x,uint16_t y,uint8_t *s,uint16_t fc,uint16_t bc,uint8_t sizey,uint8_t mode);
void LCD_ShowChinese32x32(uint16_t x,uint16_t y,uint8_t *s,uint16_t fc,uint16_t bc,uint8_t sizey,uint8_t mode);
void LCD_ShowChar(uint16_t x,uint16_t y,uint8_t num,uint16_t fc,uint16_t bc,uint8_t sizey,uint8_t mode);
void LCD_ShowString(uint16_t x,uint16_t y,const uint8_t *p,uint16_t fc,uint16_t bc,uint8_t sizey,uint8_t mode);
uint32_t mypow(uint8_t m,uint8_t n);
void LCD_ShowIntNum(uint16_t x,uint16_t y,uint16_t num,uint8_t len,uint16_t fc,uint16_t bc,uint8_t sizey);
void LCD_ShowFloatNum1(uint16_t x,uint16_t y,float num,uint8_t len,uint16_t fc,uint16_t bc,uint8_t sizey);
void LCD_ShowPicture(uint16_t x,uint16_t y,uint16_t length,uint16_t width,const uint8_t pic[]);
#endif
### 4.GUI.c
#include "GUI.h"
#include "lcdfont.h"
#include "St7735.h"
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-1,yend-1);
for(i=ysta;i<yend;i++)
{
for(j=xsta;j<xend;j++)
{
LCD_WR_DATA(color);
}
}
}
void LCD_DrawPoint(uint16_t x,uint16_t y,uint16_t color)
{
LCD_Address_Set(x,y,x,y);
LCD_WR_DATA(color);
}
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_y;}
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;
}
}
}
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);
}
void Draw_Circle(uint16_t x0,uint16_t y0,uint8_t r,uint16_t color)
{
int a,b;
a=0;b=r;
while(a<=b)
{
LCD_DrawPoint(x0-b,y0-a,color);
LCD_DrawPoint(x0+b,y0-a,color);
LCD_DrawPoint(x0-a,y0+b,color);
LCD_DrawPoint(x0-a,y0-b,color);
LCD_DrawPoint(x0+b,y0+a,color);
LCD_DrawPoint(x0+a,y0-b,color);
LCD_DrawPoint(x0+a,y0+b,color);
LCD_DrawPoint(x0-b,y0+a,color);
a++;
if((a*a+b*b)>(r*r))
{
b--;
}
}
}
void LCD_ShowChinese(uint16_t x,uint16_t y,uint8_t *s,uint16_t fc,uint16_t bc,uint8_t sizey,uint8_t mode)
{
while(*s!=0)
{
if(sizey==12) LCD_ShowChinese12x12(x,y,s,fc,bc,sizey,mode);
else if(sizey==16) LCD_ShowChinese16x16(x,y,s,fc,bc,sizey,mode);
else if(sizey==24) LCD_ShowChinese24x24(x,y,s,fc,bc,sizey,mode);
else if(sizey==32) LCD_ShowChinese32x32(x,y,s,fc,bc,sizey,mode);
else return;
s+=2;
x+=sizey;
}
}
void LCD_ShowChinese12x12(uint16_t x,uint16_t y,uint8_t *s,uint16_t fc,uint16_t bc,uint8_t sizey,uint8_t mode)
{
uint8_t i,j,m=0;
uint16_t k;
uint16_t HZnum;
uint16_t TypefaceNum;
uint16_t x0=x;
TypefaceNum=(sizey/8+((sizey%8)?1:0))*sizey;
HZnum=sizeof(tfont12)/sizeof(typFNT_GB12);
for(k=0;k<HZnum;k++)
{
if((tfont12[k].Index[0]==*(s))&&(tfont12[k].Index[1]==*(s+1)))
{
LCD_Address_Set(x,y,x+sizey-1,y+sizey-1);
for(i=0;i<TypefaceNum;i++)
{
for(j=0;j<8;j++)
{
if(!mode)
{
if(tfont12[k].Msk[i]&(0x01<<j))LCD_WR_DATA(fc);
else LCD_WR_DATA(bc);
m++;
if(m%sizey==0)
{
m=0;
break;
}
}
else
{
if(tfont12[k].Msk[i]&(0x01<<j)) LCD_DrawPoint(x,y,fc);
x++;
if((x-x0)==sizey)
{
x=x0;
y++;
break;
}
}
}
}
}
continue;
}
}
void LCD_ShowChinese16x16(uint16_t x,uint16_t y,uint8_t *s,uint16_t fc,uint16_t bc,uint8_t sizey,uint8_t mode)
{
uint8_t i,j,m=0;
uint16_t k;
uint16_t HZnum;
uint16_t TypefaceNum;
uint16_t x0=x;
TypefaceNum=(sizey/8+((sizey%8)?1:0))*sizey;
HZnum=sizeof(tfont16)/sizeof(typFNT_GB16);
for(k=0;k<HZnum;k++)
{
if ((tfont16[k].Index[0]==*(s))&&(tfont16[k].Index[1]==*(s+1)))
{
LCD_Address_Set(x,y,x+sizey-1,y+sizey-1);
for(i=0;i<TypefaceNum;i++)
{
for(j=0;j<8;j++)
{
if(!mode)
{
if(tfont16[k].Msk[i]&(0x01<<j))LCD_WR_DATA(fc);
else LCD_WR_DATA(bc);
m++;
if(m%sizey==0)
{
m=0;
break;
}
}
else
{
if(tfont16[k].Msk[i]&(0x01<<j)) LCD_DrawPoint(x,y,fc);
x++;
if((x-x0)==sizey)
{
x=x0;
y++;
break;
}
}
}
}
}
continue;
}
}
void LCD_ShowChinese24x24(uint16_t x,uint16_t y,uint8_t *s,uint16_t fc,uint16_t bc,uint8_t sizey,uint8_t mode)
{
uint8_t i,j,m=0;
uint16_t k;
uint16_t HZnum;
uint16_t TypefaceNum;
uint16_t x0=x;
TypefaceNum=(sizey/8+((sizey%8)?1:0))*sizey;
HZnum=sizeof(tfont24)/sizeof(typFNT_GB24);
for(k=0;k<HZnum;k++)
{
if ((tfont24[k].Index[0]==*(s))&&(tfont24[k].Index[1]==*(s+1)))
{
LCD_Address_Set(x,y,x+sizey-1,y+sizey-1);
for(i=0;i<TypefaceNum;i++)
{
for(j=0;j<8;j++)
{
if(!mode)
{
if(tfont24[k].Msk[i]&(0x01<<j))LCD_WR_DATA(fc);
else LCD_WR_DATA(bc);
m++;
if(m%sizey==0)
{
m=0;
break;
}
}
else
{
if(tfont24[k].Msk[i]&(0x01<<j)) LCD_DrawPoint(x,y,fc);
x++;
if((x-x0)==sizey)
{
x=x0;
y++;
break;
}
}
}
}
}
continue;
}
}
void LCD_ShowChinese32x32(uint16_t x,uint16_t y,uint8_t *s,uint16_t fc,uint16_t bc,uint8_t sizey,uint8_t mode)
{
uint8_t i,j,m=0;
uint16_t k;
uint16_t HZnum;
uint16_t TypefaceNum;
uint16_t x0=x;
TypefaceNum=(sizey/8+((sizey%8)?1:0))*sizey;
HZnum=sizeof(tfont32)/sizeof(typFNT_GB32);
for(k=0;k<HZnum;k++)
{
if ((tfont32[k].Index[0]==*(s))&&(tfont32[k].Index[1]==*(s+1)))
{
LCD_Address_Set(x,y,x+sizey-1,y+sizey-1);
for(i=0;i<TypefaceNum;i++)
{
for(j=0;j<8;j++)
{
if(!mode)
{
if(tfont32[k].Msk[i]&(0x01<<j))LCD_WR_DATA(fc);
else LCD_WR_DATA(bc);
m++;
if(m%sizey==0)
{
m=0;
break;
}
}
else
{
if(tfont32[k].Msk[i]&(0x01<<j)) LCD_DrawPoint(x,y,fc);
x++;
if((x-x0)==sizey)
{
x=x0;
y++;
break;
}
}
}
}
}
continue;
}
}
void LCD_ShowChar(uint16_t x,uint16_t y,uint8_t num,uint16_t fc,uint16_t bc,uint8_t sizey,uint8_t mode)
{
uint8_t temp,sizex,t,m=0;
uint16_t i,TypefaceNum;
uint16_t x0=x;
sizex=sizey/2;
TypefaceNum=(sizex/8+((sizex%8)?1:0))*sizey;
num=num-' ';
LCD_Address_Set(x,y,x+sizex-1,y+sizey-1);
for(i=0;i<TypefaceNum;i++)
{
if(sizey==12)temp=ascii_1206[num][i];
else if(sizey==16)temp=ascii_1608[num][i];
else if(sizey==24)temp=ascii_2412[num][i];
else if(sizey==32)temp=ascii_3216[num][i];
else return;
for(t=0;t<8;t++)
{
if(!mode)
{
if(temp&(0x01<<t))LCD_WR_DATA(fc);
else LCD_WR_DATA(bc);
m++;
if(m%sizex==0)
{
m=0;
break;
}
}
else
{
if(temp&(0x01<<t))LCD_DrawPoint(x,y,fc);
x++;
if((x-x0)==sizex)
{
x=x0;
y++;
break;
}
}
}
}
}
void LCD_ShowString(uint16_t x,uint16_t y,const uint8_t *p,uint16_t fc,uint16_t bc,uint8_t sizey,uint8_t mode)
{
while(*p!='\0')
{
LCD_ShowChar(x,y,*p,fc,bc,sizey,mode);
x+=sizey/2;
p++;
}
}
uint32_t mypow(uint8_t m,uint8_t n)
{
uint32_t result=1;
while(n--)result*=m;
return result;
}
void LCD_ShowIntNum(uint16_t x,uint16_t y,uint16_t num,uint8_t len,uint16_t fc,uint16_t bc,uint8_t sizey)
{
uint8_t t,temp;
uint8_t enshow=0;
uint8_t sizex=sizey/2;
for(t=0;t<len;t++)
{
temp=(num/mypow(10,len-t-1))%10;
if(enshow==0&&t<(len-1))
{
if(temp==0)
{
LCD_ShowChar(x+t*sizex,y,' ',fc,bc,sizey,0);
continue;
}else enshow=1;
}
LCD_ShowChar(x+t*sizex,y,temp+48,fc,bc,sizey,0);
}
}
void LCD_ShowFloatNum1(uint16_t x,uint16_t y,float num,uint8_t len,uint16_t fc,uint16_t bc,uint8_t sizey)
{
uint8_t t,temp,sizex;
uint16_t num1;
sizex=sizey/2;
num1=num*100;
for(t=0;t<len;t++)
{
temp=(num1/mypow(10,len-t-1))%10;
if(t==(len-2))
{
LCD_ShowChar(x+(len-2)*sizex,y,'.',fc,bc,sizey,0);
t++;
len+=1;
}
LCD_ShowChar(x+t*sizex,y,temp+48,fc,bc,sizey,0);
}
}
void LCD_ShowPicture(uint16_t x,uint16_t y,uint16_t length,uint16_t width,const uint8_t pic[])
{
uint16_t i,j;
uint32_t k=0;
LCD_Address_Set(x,y,x+length-1,y+width-1);
for(i=0;i<length;i++)
{
for(j=0;j<width;j++)
{
LCD_WR_DATA8(pic[k*2]);
LCD_WR_DATA8(pic[k*2+1]);
k++;
}
}
}
### 5.main.c
#include "gd32f3x0.h"
#include "systick.h"
#include <stdio.h>
#include "main.h"
#include "led.h"
#include "St7735.h"
#include "spi.h"
#include "GUI.h"
#include "pic.h"
int main(void)
{
systick_config();
led_init();
#ifdef USE_HARDWARESPI
spi_config();
#endif
LCD_Init();
LCD_Fill(0,0,LCD_W,LCD_H,RED);
while(1) {
LCD_ShowChinese(50,0,"测试",YELLOW,RED,32,0);
LCD_ShowString(50+32*2,0,"GD32",YELLOW,RED,32,0);
LCD_ShowString(40,40,"LCD_W:",BLUE,RED,16,0);
LCD_ShowIntNum(40 + 48,40,LCD_W,3,BLACK,RED,16);
LCD_ShowString(40 + 80,40,"LCD_H:",BLUE,RED,16,0);
LCD_ShowIntNum(40 +128,40,LCD_H,3,BLACK,RED,16);
LCD_ShowPicture(50,60,140,67,gImage_GD32_logo_140x67);
}
}
### 6.lcdfont.h
太多了。
### 7.pic.h
太多了,请自行取模。
3.可调配置
1.横屏和竖屏
在st7735头文件中有一个宏 USE_HORIZONTAL
配置为0和1 为竖屏显示 配置为2和3位横屏显示
![title= title=](//file.elecfans.com/web2/M00/79/81/poYBAGNstymAX8OEAAAm1THs9ek792.jpg)
2. 软件SPI和硬件SPI
在st7735头文件中有一个宏USE_HARDWARESPI
当定义这个宏的时候使用硬件SPI,注释掉这个宏使用软件SPI
![title= title=](//file.elecfans.com/web2/M00/7A/11/pYYBAGNstymARLUQAAAbn3J_mO0013.jpg)
三、测试结果
可以显示字符,汉字和图片。
![title= title=](//file.elecfans.com/web2/M00/79/82/poYBAGNstzeAYdARADNRejuVnww177.jpg)
特写:
![title= title=](//file.elecfans.com/web2/M00/79/81/poYBAGNstyqAGStqAAZri61xJ3s914.jpg)
原作者:兆易创新GD32 MCU 卢阿涛