STM32驱动4寸ST7796S LCD-TFT屏
简介
屏幕驱动芯片ST7796S最大支持320*480分辨率的屏幕,显存GRAM达345600字节,支持8/9/16/18/24位并口数据总线,还支持3/4线制SPI通信。每一个像素点RGB显示的颜色可达65K/262K/16.7M钟,像素点地址设置按照行列的顺序进行,递增递减方向由扫描方式决定,显示方法按照先设置地址再设置颜色。 4寸LCD屏幕参数
项目 | 描述 |
---|
显示颜色 | RGB 65K彩色 | 分辨率 | 480*320(Pixel) | 通信接口 | 4线SPI | 触摸IC类型 | 电阻触摸XPT2046 | 工作温度 | -10℃~60℃ |
屏幕与STM32F103RCT6开发板接线
屏幕引脚 | 引脚说明 | STM32引脚 |
---|
VCC | 电源正(3.3-5V) | 5V/3.3V | GND | 电源地 | GND | CS | 片选(低有效) | PB11 | RESET | 复位(低有效) | PB12 | DC/RS | 命令/数据选择(高电平:数据,低:命令) | PB10 | SDI(MOSI) | SPI总线写数据信号 | PB15 | SCK | SPI总线时钟信号 | PB13 | LED | 背光控制(高点亮,如不需控制,则接3.3V) | PB9 | SDO(MISO) | SPI总线读数据信号 | PB14 | T_CLK | 触摸SPI总线时钟信号 | PC0 | T_CS | 触摸片选(低有效) | PC13 | T_DIN | 触摸SPI总线输入信号 | PC3 | T_DO | 触摸SPI总线输出信号 | PC2 | T_IRQ | 触摸中断信号 | PC10 |
屏幕原理图
STM32驱动程序
主程序代码
#include "delay.h"
#include "sys.h"
#include "lcd.h"
#include "touch.h"
#include "gui.h"
#include "test.h"
int main(void)
{
SystemInit();
delay_init(72);
LCD_Init();
while(1)
{
main_test();
Test_Color();
Test_FillRec();
Test_Circle();
Test_Triangle();
English_Font_test();
Chinese_Font_test();
Pic_test();
Rotate_Test();
Touch_Test();
}
}
STM32硬件SPI驱动代码
#include "spi.h"
u8 SPI_WriteByte(SPI_TypeDef* SPIx,u8 Byte)
{
while((SPIx->SR&SPI_I2S_FLAG_TXE)==RESET);
SPIx->DR=Byte;
while((SPIx->SR&SPI_I2S_FLAG_RXNE)==RESET);
return SPIx->DR;
}
void SPI_SetSpeed(SPI_TypeDef* SPIx,u8 SpeedSet)
{
SPIx->CR1&=0XFFC7;
if(SpeedSet==1)
{
SPIx->CR1|=SPI_BaudRatePrescaler_2;
}
else
{
SPIx->CR1|=SPI_BaudRatePrescaler_32;
}
SPIx->CR1|=1<<6;
}
void SPI2_Init(void)
{
SPI_InitTypeDef SPI_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO|RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_15;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2 ,ENABLE);
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPI2, &SPI_InitStructure);
SPI_Cmd(SPI2, ENABLE);
}
LCD底层驱动代码
#include "lcd.h"
#include "stdlib.h"
#include "delay.h"
#include "SPI.h"
_lcd_dev lcddev;
u16 POINT_COLOR = 0x0000,BACK_COLOR = 0xFFFF;
u16 DeviceCode;
void LCD_WR_REG(u8 data)
{
LCD_CS_CLR;
LCD_RS_CLR;
SPI_WriteByte(SPI2,data);
LCD_CS_SET;
}
void LCD_WR_DATA(u8 data)
{
LCD_CS_CLR;
LCD_RS_SET;
SPI_WriteByte(SPI2,data);
LCD_CS_SET;
}
void LCD_WriteReg(u8 LCD_Reg, u16 LCD_RegValue)
{
LCD_WR_REG(LCD_Reg);
LCD_WR_DATA(LCD_RegValue);
}
void LCD_WriteRAM_Prepare(void)
{
LCD_WR_REG(lcddev.wramcmd);
}
void Lcd_WriteData_16Bit(u16 Data)
{
LCD_CS_CLR;
LCD_RS_SET;
SPI_WriteByte(SPI2,Data>>8);
SPI_WriteByte(SPI2,Data);
LCD_CS_SET;
}
void LCD_DrawPoint(u16 x,u16 y)
{
LCD_SetCursor(x,y);
Lcd_WriteData_16Bit(POINT_COLOR);
}
void LCD_Clear(u16 Color)
{
unsigned int i,m;
LCD_SetWindows(0,0,lcddev.width-1,lcddev.height-1);
LCD_CS_CLR;
LCD_RS_SET;
for(i=0;i<lcddev.height;i++)
{
for(m=0;m<lcddev.width;m++)
{
Lcd_WriteData_16Bit(Color);
}
}
LCD_CS_SET;
}
void LCD_GPIOInit(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB ,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9| GPIO_Pin_10| GPIO_Pin_11| GPIO_Pin_12;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
void LCD_RESET(void)
{
LCD_RST_CLR;
delay_ms(100);
LCD_RST_SET;
delay_ms(50);
}
void LCD_Init(void)
{
SPI2_Init();
LCD_GPIOInit();
LCD_RESET();
LCD_WR_REG(0xF0);
LCD_WR_DATA(0xC3);
LCD_WR_REG(0xF0);
LCD_WR_DATA(0x96);
LCD_WR_REG(0x36);
LCD_WR_DATA(0x68);
LCD_WR_REG(0x3A);
LCD_WR_DATA(0x05);
LCD_WR_REG(0xB0);
LCD_WR_DATA(0x80);
LCD_WR_REG(0xB6);
LCD_WR_DATA(0x00);
LCD_WR_DATA(0x02);
LCD_WR_REG(0xB5);
LCD_WR_DATA(0x02);
LCD_WR_DATA(0x03);
LCD_WR_DATA(0x00);
LCD_WR_DATA(0x04);
LCD_WR_REG(0xB1);
LCD_WR_DATA(0x80);
LCD_WR_DATA(0x10);
LCD_WR_REG(0xB4);
LCD_WR_DATA(0x00);
LCD_WR_REG(0xB7);
LCD_WR_DATA(0xC6);
LCD_WR_REG(0xC5);
LCD_WR_DATA(0x24);
LCD_WR_REG(0xE4);
LCD_WR_DATA(0x31);
LCD_WR_REG(0xE8);
LCD_WR_DATA(0x40);
LCD_WR_DATA(0x8A);
LCD_WR_DATA(0x00);
LCD_WR_DATA(0x00);
LCD_WR_DATA(0x29);
LCD_WR_DATA(0x19);
LCD_WR_DATA(0xA5);
LCD_WR_DATA(0x33);
LCD_WR_REG(0xC2);
LCD_WR_REG(0xA7);
LCD_WR_REG(0xE0);
LCD_WR_DATA(0xF0);
LCD_WR_DATA(0x09);
LCD_WR_DATA(0x13);
LCD_WR_DATA(0x12);
LCD_WR_DATA(0x12);
LCD_WR_DATA(0x2B);
LCD_WR_DATA(0x3C);
LCD_WR_DATA(0x44);
LCD_WR_DATA(0x4B);
LCD_WR_DATA(0x1B);
LCD_WR_DATA(0x18);
LCD_WR_DATA(0x17);
LCD_WR_DATA(0x1D);
LCD_WR_DATA(0x21);
LCD_WR_REG(0XE1);
LCD_WR_DATA(0xF0);
LCD_WR_DATA(0x09);
LCD_WR_DATA(0x13);
LCD_WR_DATA(0x0C);
LCD_WR_DATA(0x0D);
LCD_WR_DATA(0x27);
LCD_WR_DATA(0x3B);
LCD_WR_DATA(0x44);
LCD_WR_DATA(0x4D);
LCD_WR_DATA(0x0B);
LCD_WR_DATA(0x17);
LCD_WR_DATA(0x17);
LCD_WR_DATA(0x1D);
LCD_WR_DATA(0x21);
LCD_WR_REG(0X36);
LCD_WR_DATA(0xEC);
LCD_WR_REG(0xF0);
LCD_WR_DATA(0xC3);
LCD_WR_REG(0xF0);
LCD_WR_DATA(0x69);
LCD_WR_REG(0X13);
LCD_WR_REG(0X11);
LCD_WR_REG(0X29);
LCD_direction(USE_HORIZONTAL);
LCD_LED=1;
LCD_Clear(WHITE);
}
void LCD_SetWindows(u16 xStar, u16 yStar,u16 xEnd,u16 yEnd)
{
LCD_WR_REG(lcddev.setxcmd);
LCD_WR_DATA(xStar>>8);
LCD_WR_DATA(0x00FF&xStar);
LCD_WR_DATA(xEnd>>8);
LCD_WR_DATA(0x00FF&xEnd);
LCD_WR_REG(lcddev.setycmd);
LCD_WR_DATA(yStar>>8);
LCD_WR_DATA(0x00FF&yStar);
LCD_WR_DATA(yEnd>>8);
LCD_WR_DATA(0x00FF&yEnd);
LCD_WriteRAM_Prepare();
}
void LCD_SetCursor(u16 Xpos, u16 Ypos)
{
LCD_SetWindows(Xpos,Ypos,Xpos,Ypos);
}
void LCD_direction(u8 direction)
{
lcddev.setxcmd=0x2A;
lcddev.setycmd=0x2B;
lcddev.wramcmd=0x2C;
switch(direction){
case 0:
lcddev.width=LCD_W;
lcddev.height=LCD_H;
LCD_WriteReg(0x36,(1<<3)|(1<<6));
break;
case 1:
lcddev.width=LCD_H;
lcddev.height=LCD_W;
LCD_WriteReg(0x36,(1<<3)|(1<<5));
break;
case 2:
lcddev.width=LCD_W;
lcddev.height=LCD_H;
LCD_WriteReg(0x36,(1<<3)|(1<<7));
break;
case 3:
lcddev.width=LCD_H;
lcddev.height=LCD_W;
LCD_WriteReg(0x36,(1<<3)|(1<<7)|(1<<6)|(1<<5));
break;
default:break;
}
}
屏幕显示效果
|