老五家2.8寸屏,3线SPI驱动
前言
要知道屏幕的驱动芯片都小的惊人,想必是不会打上丝印的。从几百个引脚中判断哪个是哪个,想想就晕。 大佬们都太厉害了,看看PFC就知道屏幕的接线定义。一直好奇这种神技是怎么练成的。也尝试自己来猜定义,但发现实在太难,还是拿来主义,等着引脚定义比较靠谱。
本文用的屏是2.8寸TFT屏,驱动芯片是HX8347,定义早被大佬识破:
定义有了。但不巧[IM3~0]=1100,只能支持3线SPI+RGB。RGB屏不是单片机的强项。好像STM32F7XX和ESP32 S3是支持RGB,但也只是好像,这两都价格不菲,手头也没有。
还是想办法用3线SPI来驱动吧。
一、源码
HX8347.h
#ifndef USER_HX8347_H_
#define USER_HX8347_H_
#define X_MAX_PIXEL 240
#define Y_MAX_PIXEL 320
#define RED 0xf800
#define GREEN 0x07e0
#define BLUE 0x001f
#define WHITE 0xffff
#define BLACK 0x0000
#define YELLOW 0xFFE0
#define GRAY0 0xEF7D //灰色0 3165 00110 001011 00101
#define GRAY1 0x8410 //灰色1 00000 000000 00000
#define GRAY2 0x4208 //灰色2 1111111111011111
#define LCD_CS GPIO_Pin_0 // CS:PA0
#define LCD_SDA GPIO_Pin_1 // SDA:PA1
#define LCD_SCL GPIO_Pin_3 // SCL:PA3
#define LCD_RST GPIO_Pin_4 // RST:PA4
#define LCD_SCL_SET GPIO_WriteBit(GPIOA, LCD_SCL,Bit_SET)
#define LCD_SDA_SET GPIO_WriteBit(GPIOA, LCD_SDA,Bit_SET)
#define LCD_CS_SET GPIO_WriteBit(GPIOA, LCD_CS,Bit_SET)
#define LCD_RST_SET GPIO_WriteBit(GPIOA, LCD_RST,Bit_SET)
#define LCD_SCL_CLR GPIO_WriteBit(GPIOA, LCD_SCL,Bit_RESET)
#define LCD_SDA_CLR GPIO_WriteBit(GPIOA, LCD_SDA,Bit_RESET)
#define LCD_CS_CLR GPIO_WriteBit(GPIOA, LCD_CS,Bit_RESET)
#define LCD_RST_CLR GPIO_WriteBit(GPIOA, LCD_RST,Bit_RESET)
void LCD_GPIO_Init(void);
void Lcd_WriteIndex(unsigned char Index);
void Lcd_WriteData(unsigned char Data);
void LCD_WriteData_16Bit(unsigned int Data);
void Lcd_Write_REG(unsigned char Index,unsigned char Data);
void LCD_Init(void);
void Lcd_Clear(unsigned int Color);
void FillRect(u16 x1, u16 y1, u16 x2, u16 y2, u16 color);
#endif /* USER_HX8347_H_ */
HX8347.c
#include "debug.h"
#include "HX8347.h"
void LCD_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure = {0};
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = LCD_SCL|LCD_SDA|LCD_CS|LCD_RST;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
//向SPI总线传输一个8位数据
void SPI_WriteData(unsigned char Data)
{
unsigned char i=0;
for(i=8;i>0;i--)
{
if(Data&0x80)
LCD_SDA_SET; //输出数据
else LCD_SDA_CLR;
LCD_SCL_CLR;
LCD_SCL_SET;
Data<<=1;
}
}
//向液晶屏写一个8位指令
void Lcd_WriteIndex(unsigned char Index)
{
//SPI 写命令时序开始
LCD_CS_CLR;
SPI_WriteData(0x70);
SPI_WriteData(Index);
LCD_CS_SET;
}
//向液晶屏写一个8位数据
void Lcd_WriteData(unsigned char Data)
{
LCD_CS_CLR;
SPI_WriteData(0x72);
SPI_WriteData(Data);
LCD_CS_SET;
}
//向液晶屏写一个16位数据
void LCD_WriteData_16Bit(unsigned int Data)
{
LCD_CS_CLR;
SPI_WriteData(0x72);
SPI_WriteData(Data>>8);
SPI_WriteData(Data);
LCD_CS_SET;
}
void Lcd_Write_REG(unsigned char Index,unsigned char Data)
{
Lcd_WriteIndex(Index);
Lcd_WriteData(Data);
}
void Lcd_Reset(void)
{
unsigned int i;
LCD_RST_CLR;
for(i=0;i<6000;i++);
LCD_RST_SET;
for(i=0;i<6000;i++);
}
// set region to paint
void LCD_SetWindow(unsigned int x1,unsigned int y1,unsigned int x2,unsigned int y2)
{
//SC
Lcd_Write_REG(0x02,x1>>8); // Column address start2
Lcd_Write_REG(0x03,(u8)x1); // Column address start1
//EC
Lcd_Write_REG(0x04,x2>>8); // Column address end2
Lcd_Write_REG(0x05,(u8)x2); // Column address end1
//SP
Lcd_Write_REG(0x06,y1>>8); // Row address start2
Lcd_Write_REG(0x07,(u8)y1); // Row address start1
//EP
Lcd_Write_REG(0x08,y2>>8); // Row address end2
Lcd_Write_REG(0x09,(u8)y2); // Row address end1
//写0x22到index register,那么下次send data就会直接被写到graphic ram
Lcd_WriteIndex(0x22);
}
void FillRect(u16 x1, u16 y1, u16 x2, u16 y2, u16 color)
{
LCD_SetWindow(x1, y1,x2,y2);
x2 = x2 - x1 + 1;
y2 = y2 - y1 + 1;
for(x1 = x2; x1 != 0 ; x1--)
{
for (y1 = y2;y1 != 0 ;y1--)
{
LCD_WriteData_16Bit(color);
}
}
}
void LCD_Init(void)
{
LCD_GPIO_Init();
Lcd_Reset();
Lcd_Write_REG(0x18,0x88); //UADJ 75Hz
Lcd_Write_REG(0x19,0x01); //OSC_EN='1', start Osc
//Power Voltage Setting
Lcd_Write_REG(0x1B,0x1E); //VRH=4.60V
Lcd_Write_REG(0x1C,0x07); //AP Crosstalk 04
Lcd_Write_REG(0x1A,0x01); //BT (VGH~15V,VGL~-10V,DDVDH~5V)
Lcd_Write_REG(0x24,0x38); //VMH 27
Lcd_Write_REG(0x25,0x5F); //VML
//VCOM offset
Lcd_Write_REG(0x23,0x8C); //for Flicker adjust
Lcd_Write_REG(0x1F,0x88);// GAS=1, VOMG=00, PON=0, DK=1, XDK=0, DVDH_TRI=0, STB=0
Delay_Ms(5);
Lcd_Write_REG(0x1F,0x80);// GAS=1, VOMG=00, PON=0, DK=0, XDK=0, DVDH_TRI=0, STB=0
Delay_Ms(5);
Lcd_Write_REG(0x1F,0x90);// GAS=1, VOMG=00, PON=1, DK=0, XDK=0, DVDH_TRI=0, STB=0
Delay_Ms(5);
Lcd_Write_REG(0x1F,0xD0);// GAS=1, VOMG=10, PON=1, DK=0, XDK=0, DDVDH_TRI=0, STB=0
Delay_Ms(5);
//Display ON Setting
Lcd_Write_REG(0x28,0x38); //GON=1, DTE=1, D=1000
Delay_Ms(40);
Lcd_Write_REG(0x28,0x3C); //GON=1, DTE=1, D=1100
Lcd_Write_REG(0x36,0x09); //REV, BGR
Lcd_Write_REG(0x17,0x05); //16BIT/PIXEL
//Gamma 2.2 Setting
Lcd_Write_REG(0x40,0x00); //
Lcd_Write_REG(0x41,0x00); //
Lcd_Write_REG(0x42,0x00); //
Lcd_Write_REG(0x43,0x11); //
Lcd_Write_REG(0x44,0x0e); //
Lcd_Write_REG(0x45,0x23); //
Lcd_Write_REG(0x46,0x08); //
Lcd_Write_REG(0x47,0x53); //
Lcd_Write_REG(0x48,0x03); //
Lcd_Write_REG(0x49,0x11); //
Lcd_Write_REG(0x4A,0x18); //
Lcd_Write_REG(0x4B,0x1a); //
Lcd_Write_REG(0x4C,0x16); //
Lcd_Write_REG(0x50,0x1c); //
Lcd_Write_REG(0x51,0x31); //
Lcd_Write_REG(0x52,0x2e); //
Lcd_Write_REG(0x53,0x3f); //
Lcd_Write_REG(0x54,0x3f); //
Lcd_Write_REG(0x55,0x3f); //
Lcd_Write_REG(0x56,0x2c); //
Lcd_Write_REG(0x57,0x77); //
Lcd_Write_REG(0x58,0x09); //
Lcd_Write_REG(0x59,0x05); //
Lcd_Write_REG(0x5A,0x07); //
Lcd_Write_REG(0x5B,0x0e); //
Lcd_Write_REG(0x5C,0x1c); //
Lcd_Write_REG(0x5D,0x88); //
Delay_Ms(100);
FillRect(0, 160, 239, 239, 0xf800 );
}
二、说明
1、LCD初始化是从GITHUB上拷的一段程序,具体是啥意思我也不清楚。试过官方数据手册里的初始化程序,一直不能成功。
2、写寄存器的程序
void Lcd_WriteIndex(unsigned char Index)
{
LCD_CS_CLR;
SPI_WriteData(0x70);
SPI_WriteData(Index);
LCD_CS_SET;
}
3、写数据的程序
void Lcd_WriteData(unsigned char Data)
{
LCD_CS_CLR;
SPI_WriteData(0x72);
SPI_WriteData(Data);LCD_CS_SET;
}
上面程序中出现SPI_WriteData(0x70); SPI_WriteData(0x72);统一解释下:
因为是3线SPI,所以缺少CD(或RS)引线,解决的办法一般是SPI发送数据时第一个bit的值代表CD。这样因为多出了1bit,就需要处理9bit的数据。UC1601就是这样,但是HX8347不是这样。
下面栽自HX8347的数据手册:
大概意思就是在发数据前需要发“01110”[ID][RS][RW],一共是8bit。
这里ID应该取0。RS取0是写寄存器,取1是写数据。RW取0,表示写。
所以写寄存器就是:01110000=0x70;写数据就是:01110010=0x72。也因此就有了:
SPI_WriteData(0x70); SPI_WriteData(0x72);
三、总结
1、主控是沁恒家申请的CH32V307的评估板,开发工具是mountriver。不得不说评估板和开发工具都很完美。评估板留有arduino接口,可以和arduino扩展板链接。mountriver编译很快,下载烧录更快,基本都零点几秒。
2、本源码可以添加到CH32V307的demo中CH32V307EVT\EVT\EXAM\GPIO\GPIO_Toggle例程中运行。
int main(void)
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
SystemCoreClockUpdate();
Delay_Init();
LCD_Init();
while(1)
{
Delay_Ms(1000);
FillRect(0, 0, 239, 319, 0x07c0 );
Delay_Ms(1000);
FillRect(0, 0, 239, 319, 0x0030 );
Delay_Ms(1000);
FillRect(0, 0, 239, 319, 0x0000 );
Delay_Ms(1000);
FillRect(0, 0, 239, 319, 0xf800 );
}
}
3、如果屏幕没有任何显示,可能需要按下评估板的RESET键。
4、用的是软件模拟3线SPI,所以帧数很低。以后考虑硬件SPI,或再加DMA,但DMA需要SRAM太大。
演示用视频