首页 > 其他分享 >STM32F411 标准库硬件SPI (硬件NSS/CS)驱动st7735--1.8寸TFT显示屏

STM32F411 标准库硬件SPI (硬件NSS/CS)驱动st7735--1.8寸TFT显示屏

时间:2024-08-31 18:22:50浏览次数:15  
标签:st7735 WriteData Lcd 硬件 SPI LCD GPIO define

TFT的spi驱动文件 完整工程网盘放在末尾

#include "lcd_driver.h"

static uint16_t SPI_TIMEOUT_UserCallback(uint8_t errorCode);

//液晶IO初始化配置
void LCD_Driver_Init(void)
{

	SPI_InitTypeDef  SPI_InitStructure;
	GPIO_InitTypeDef GPIO_InitStructure;

	/* 使能 LCD_SPI 及GPIO 时钟 */
	/*!< SPI_LCD_SPI_CS_GPIO, SPI_LCD_SPI_MOSI_GPIO, 
	   SPI_LCD_SPI_MISO_GPIO,SPI_LCD_SPI_SCK_GPIO 时钟使能 */
	RCC_AHB1PeriphClockCmd (LCD_SPI_SCK_GPIO_CLK|LCD_SPI_MOSI_GPIO_CLK|LCD_CS_GPIO_CLK, ENABLE);

	/*!< SPI_LCD_SPI 时钟使能 */
	LCD_SPI_CLK_INIT(LCD_SPI_CLK, ENABLE);

	//设置引脚复用
	GPIO_PinAFConfig(LCD_SPI_SCK_GPIO_PORT,LCD_SPI_SCK_PINSOURCE,LCD_SPI_SCK_AF);  
	GPIO_PinAFConfig(LCD_SPI_MOSI_GPIO_PORT,LCD_SPI_MOSI_PINSOURCE,LCD_SPI_MOSI_AF); 
	GPIO_PinAFConfig(LCD_CS_GPIO_PORT,LCD_SPI_CS_PINSOURCE,LCD_SPI_CS_AF); 
	
	/*!< 配置 SPI_LCD_SPI 引脚: SCK */
	GPIO_InitStructure.GPIO_Pin = LCD_SPI_SCK_PIN;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;  

	GPIO_Init(LCD_SPI_SCK_GPIO_PORT, &GPIO_InitStructure);

	/*!< 配置 SPI_LCD_SPI 引脚: MOSI */
	GPIO_InitStructure.GPIO_Pin = LCD_SPI_MOSI_PIN;
	GPIO_Init(LCD_SPI_MOSI_GPIO_PORT, &GPIO_InitStructure);  

	/*!< 配置 SPI_LCD_SPI 引脚: CS */
	GPIO_InitStructure.GPIO_Pin = LCD_CS_PIN;
	GPIO_Init(LCD_CS_GPIO_PORT, &GPIO_InitStructure);

	GPIO_InitStructure.GPIO_Pin = LCD_DC_PIN;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
	GPIO_Init(LCD_DC_GPIO_PORT, &GPIO_InitStructure);
	
	GPIO_InitStructure.GPIO_Pin = LCD_RES_PIN;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
	GPIO_Init(LCD_RES_GPIO_PORT, &GPIO_InitStructure);

	GPIO_InitStructure.GPIO_Pin = LCD_BLK_PIN;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
	GPIO_Init(LCD_BLK_GPIO_PORT, &GPIO_InitStructure);
	
	/* 停止信号 LCD: CS引脚高电平*/
	//SPI_LCD_CS_HIGH();

	/* LCD_SPI 模式配置 */
	// LCD芯片 支持SPI模式0及模式3,据此设置CPOL CPHA
	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_High;
	SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
	SPI_InitStructure.SPI_NSS = SPI_NSS_Hard;
	SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;
	SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
	SPI_InitStructure.SPI_CRCPolynomial = 7;
	SPI_Init(LCD_SPI, &SPI_InitStructure);

	/* 使能 LCD_SPI  */
	SPI_Cmd(LCD_SPI, ENABLE);
	
	SPI_SSOutputCmd(LCD_SPI,ENABLE);
	SPI_CalculateCRC(LCD_SPI,DISABLE);
	SPI_ClearFlag(LCD_SPI,SPI_FLAG_TXE);
	SPI_ClearFlag(LCD_SPI,SPI_FLAG_RXNE);
}

//向SPI总线传输一个8位数据
void  SPI_WriteData(u8 Data)
{
	while(SPI_I2S_GetFlagStatus(LCD_SPI,SPI_I2S_FLAG_TXE)==RESET);
    SPI_SendData(LCD_SPI,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(u8 Index)
{
   //SPI 写命令时序开始
	SPI_LCD_CS_LOW();
	SPI_LCD_DC_LOW();
	SPI_WriteData(Index);
	SPI_LCD_CS_HIGH();
}

//向液晶屏写一个8位数据
void Lcd_WriteData(u8 Data)
{
	SPI_LCD_CS_LOW();
	SPI_LCD_DC_HIGH();
	SPI_WriteData(Data);
	SPI_LCD_CS_HIGH();
}

//向液晶屏写一个16位数据
void LCD_WriteData_16Bit(u16 Data)
{
	SPI_LCD_CS_LOW();
	SPI_LCD_DC_HIGH();
	SPI_WriteData(Data>>8); 	//写入高8位数据
	SPI_WriteData(Data); 		//写入低8位数据
	SPI_LCD_CS_HIGH();
}

void Lcd_WriteReg(u8 Index,u8 Data)
{
	Lcd_WriteIndex(Index);
	Lcd_WriteData(Data);
}

void Lcd_Reset(void)
{
	SPI_LCD_RES_LOW();
	delay_ms(100);
	SPI_LCD_RES_HIGH();
	delay_ms(50);
}

//LCD Init For 1.44Inch LCD Panel with ST7735R.
void Lcd_Init(void)
{	
	LCD_Driver_Init();
	Lcd_Reset(); //Reset before LCD Init.

	//LCD Init For 1.44Inch LCD Panel with ST7735R.
	Lcd_WriteIndex(0x11);//Sleep exit 
	delay_ms (120);
		
	//ST7735R Frame Rate
	Lcd_WriteIndex(0xB1); 
	Lcd_WriteData(0x01); 
	Lcd_WriteData(0x2C); 
	Lcd_WriteData(0x2D); 

	Lcd_WriteIndex(0xB2); 
	Lcd_WriteData(0x01); 
	Lcd_WriteData(0x2C); 
	Lcd_WriteData(0x2D); 

	Lcd_WriteIndex(0xB3); 
	Lcd_WriteData(0x01); 
	Lcd_WriteData(0x2C); 
	Lcd_WriteData(0x2D); 
	Lcd_WriteData(0x01); 
	Lcd_WriteData(0x2C); 
	Lcd_WriteData(0x2D); 
	
	Lcd_WriteIndex(0xB4); //Column inversion 
	Lcd_WriteData(0x07); 
	
	//ST7735R Power Sequence
	Lcd_WriteIndex(0xC0); 
	Lcd_WriteData(0xA2); 
	Lcd_WriteData(0x02); 
	Lcd_WriteData(0x84); 
	Lcd_WriteIndex(0xC1); 
	Lcd_WriteData(0xC5); 

	Lcd_WriteIndex(0xC2); 
	Lcd_WriteData(0x0A); 
	Lcd_WriteData(0x00); 

	Lcd_WriteIndex(0xC3); 
	Lcd_WriteData(0x8A); 
	Lcd_WriteData(0x2A); 
	Lcd_WriteIndex(0xC4); 
	Lcd_WriteData(0x8A); 
	Lcd_WriteData(0xEE); 
	
	Lcd_WriteIndex(0xC5); //VCOM 
	Lcd_WriteData(0x0E); 
	
	Lcd_WriteIndex(0x36); //MX, MY, RGB mode 
	Lcd_WriteData(0xC0); 
	
	//ST7735R Gamma Sequence
	Lcd_WriteIndex(0xe0); 
	Lcd_WriteData(0x0f); 
	Lcd_WriteData(0x1a); 
	Lcd_WriteData(0x0f); 
	Lcd_WriteData(0x18); 
	Lcd_WriteData(0x2f); 
	Lcd_WriteData(0x28); 
	Lcd_WriteData(0x20); 
	Lcd_WriteData(0x22); 
	Lcd_WriteData(0x1f); 
	Lcd_WriteData(0x1b); 
	Lcd_WriteData(0x23); 
	Lcd_WriteData(0x37); 
	Lcd_WriteData(0x00); 	
	Lcd_WriteData(0x07); 
	Lcd_WriteData(0x02); 
	Lcd_WriteData(0x10); 

	Lcd_WriteIndex(0xe1); 
	Lcd_WriteData(0x0f); 
	Lcd_WriteData(0x1b); 
	Lcd_WriteData(0x0f); 
	Lcd_WriteData(0x17); 
	Lcd_WriteData(0x33); 
	Lcd_WriteData(0x2c); 
	Lcd_WriteData(0x29); 
	Lcd_WriteData(0x2e); 
	Lcd_WriteData(0x30); 
	Lcd_WriteData(0x30); 
	Lcd_WriteData(0x39); 
	Lcd_WriteData(0x3f); 
	Lcd_WriteData(0x00); 
	Lcd_WriteData(0x07); 
	Lcd_WriteData(0x03); 
	Lcd_WriteData(0x10);  
	
	Lcd_WriteIndex(0x2a);
	Lcd_WriteData(0x00);
	Lcd_WriteData(0x00);
	Lcd_WriteData(0x00);
	Lcd_WriteData(0x7f);

	Lcd_WriteIndex(0x2b);
	Lcd_WriteData(0x00);
	Lcd_WriteData(0x00);
	Lcd_WriteData(0x00);
	Lcd_WriteData(0x9f);
	
	Lcd_WriteIndex(0xF0); //Enable test command  
	Lcd_WriteData(0x01); 
	Lcd_WriteIndex(0xF6); //Disable ram power save mode 
	Lcd_WriteData(0x00); 
	
	Lcd_WriteIndex(0x3A); //65k mode 
	Lcd_WriteData(0x05); 
	
	Lcd_WriteIndex(0x29);//Display on	 
}


/*************************************************
函数名:LCD_Set_Region
功能:设置lcd显示区域,在此区域写点数据自动换行
入口参数:xy起点和终点
返回值:无
*************************************************/
void Lcd_SetRegion(u16 x_start,u16 y_start,u16 x_end,u16 y_end)
{		
	Lcd_WriteIndex(0x2a);
	Lcd_WriteData(0x00);
	Lcd_WriteData(x_start);//Lcd_WriteData(x_start+2);
	Lcd_WriteData(0x00);
	Lcd_WriteData(x_end+2);

	Lcd_WriteIndex(0x2b);
	Lcd_WriteData(0x00);
	Lcd_WriteData(y_start+0);
	Lcd_WriteData(0x00);
	Lcd_WriteData(y_end+1);
	
	Lcd_WriteIndex(0x2c);

}

/*************************************************
函数名:LCD_Set_XY
功能:设置lcd显示起始点
入口参数:xy坐标
返回值:无
*************************************************/
void Lcd_SetXY(u16 x,u16 y)
{
  	Lcd_SetRegion(x,y,x,y);
}

	
/*************************************************
函数名:LCD_DrawPoint
功能:画一个点
入口参数:无
返回值:无
*************************************************/
void Gui_DrawPoint(u16 x,u16 y,u16 Data)
{
	Lcd_SetRegion(x,y,x+1,y+1);
	LCD_WriteData_16Bit(Data);

}    

/*****************************************
 函数功能:读TFT某一点的颜色                          
 出口参数:color  点颜色值                                 
******************************************/
unsigned int Lcd_ReadPoint(u16 x,u16 y)
{
  unsigned int Data;
  Lcd_SetXY(x,y);

  //Lcd_ReadData();//丢掉无用字节
  //Data=Lcd_ReadData();
  Lcd_WriteData(Data);
  return Data;
}
/*************************************************
函数名:Lcd_Clear
功能:全屏清屏函数
入口参数:填充颜色COLOR
返回值:无
*************************************************/
void Lcd_Clear(u16 Color)               
{	
   unsigned int i,m;
   Lcd_SetRegion(0,0,X_MAX_PIXEL-1,Y_MAX_PIXEL-1);
   Lcd_WriteIndex(0x2C);
   for(i=0;i<X_MAX_PIXEL;i++)
    for(m=0;m<Y_MAX_PIXEL;m++)
    {	
	  	LCD_WriteData_16Bit(Color);
    }   
}

#ifndef _LCD_DRIVER_H
#define _LCD_DRIVER_H

#include "main.h"

#define X_MAX_PIXEL	        128
#define Y_MAX_PIXEL	        160
#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 SPIT_FLAG_TIMEOUT         ((uint32_t)0x1000)

/*SPI接口定义-开头****************************/
#define LCD_SPI                           SPI1
#define LCD_SPI_CLK                       RCC_APB2Periph_SPI1
#define LCD_SPI_CLK_INIT                  RCC_APB2PeriphClockCmd

#define LCD_SPI_SCK_PIN                   GPIO_Pin_3                  
#define LCD_SPI_SCK_GPIO_PORT             GPIOB                       
#define LCD_SPI_SCK_GPIO_CLK              RCC_AHB1Periph_GPIOB
#define LCD_SPI_SCK_PINSOURCE             GPIO_PinSource3
#define LCD_SPI_SCK_AF                    GPIO_AF_SPI1

#define LCD_SPI_MOSI_PIN                  GPIO_Pin_5                
#define LCD_SPI_MOSI_GPIO_PORT            GPIOB                     
#define LCD_SPI_MOSI_GPIO_CLK             RCC_AHB1Periph_GPIOB
#define LCD_SPI_MOSI_PINSOURCE            GPIO_PinSource5
#define LCD_SPI_MOSI_AF                   GPIO_AF_SPI1

#define LCD_CS_PIN                        GPIO_Pin_15         
#define LCD_CS_GPIO_PORT                  GPIOA                     
#define LCD_CS_GPIO_CLK                   RCC_AHB1Periph_GPIOA
#define LCD_SPI_CS_PINSOURCE              GPIO_PinSource15
#define LCD_SPI_CS_AF                     GPIO_AF_SPI1

#define LCD_DC_PIN                        GPIO_Pin_4         
#define LCD_DC_GPIO_PORT                  GPIOB                     
#define LCD_DC_GPIO_CLK                   RCC_AHB1Periph_GPIOB

#define LCD_RES_PIN                       GPIO_Pin_11         
#define LCD_RES_GPIO_PORT                 GPIOA                     
#define LCD_RES_GPIO_CLK                  RCC_AHB1Periph_GPIOA

#define LCD_BLK_PIN                       GPIO_Pin_12         
#define LCD_BLK_GPIO_PORT                 GPIOA                     
#define LCD_BLK_GPIO_CLK                  RCC_AHB1Periph_GPIOA

#define SPI_LCD_CS_LOW()      {LCD_CS_GPIO_PORT->BSRRH=LCD_CS_PIN;}
#define SPI_LCD_CS_HIGH()     {LCD_CS_GPIO_PORT->BSRRL=LCD_CS_PIN;}

#define SPI_LCD_DC_LOW()      {LCD_DC_GPIO_PORT->BSRRH=LCD_DC_PIN;}
#define SPI_LCD_DC_HIGH()     {LCD_DC_GPIO_PORT->BSRRL=LCD_DC_PIN;}

#define SPI_LCD_RES_LOW()      {LCD_RES_GPIO_PORT->BSRRH=LCD_RES_PIN;}
#define SPI_LCD_RES_HIGH()     {LCD_RES_GPIO_PORT->BSRRL=LCD_RES_PIN;}

#define SPI_LCD_BLK_LOW()      {LCD_BLK_GPIO_PORT->BSRRH=LCD_BLK_PIN;}
#define SPI_LCD_BLK_HIGH()     {LCD_BLK_GPIO_PORT->BSRRL=LCD_BLK_PIN;}

#define SPI_LCD_CS_LOW()      SPI_Cmd(LCD_SPI,ENABLE)
#define SPI_LCD_CS_HIGH()     SPI_Cmd(LCD_SPI,DISABLE)




void Lcd_Init(void);

#endif

通过百度网盘分享的文件:STM32F411 标准库硬件SPI (硬件NSSCS)驱动st773...
链接:https://pan.baidu.com/s/1rzlFB43WuDtu6jm7bpFVSQ?pwd=t58t 
提取码:t58t 
--来自百度网盘超级会员V5的分享

标签:st7735,WriteData,Lcd,硬件,SPI,LCD,GPIO,define
From: https://blog.csdn.net/MOS_JBET/article/details/141678785

相关文章

  • dubbo spi 的应用指南
    文章目录前言一、配置文件目录二、标准SPI三、自适应SPI(AdaptiveSPI)1、@Adaptive说明1.1、方法级别的注解:1.2、类级别的注解:2、@Adaptive作用在类上3、@Adaptive作用在方法上三、激活SPI(ActivateSPI)四、DubboAOP五、DubboIOC前言SPI全称为ServiceProviderI......
  • LTspice使用教程,LTspice仿真教程资源大全
      LTspice简介 LTspice是英文SimulationProgramwithIntegratedCircuitEmphasis的缩写,意思是集成电路通用模拟程序。是ADI旗下的一款免费软件,很多国外的工程师、教授、学生基本用的都是LTspice,感觉应该是最好用的一款,也是教程在国内普及的比较好的一款仿真软件......
  • LTSPICE仿真教程,LTSPICE使用教程汇总
    LTspice介绍LTspice由亚德诺半导体(ADI)推出。LTspice是一款功能强大的SPICE仿真器软件,具备原理图捕获图形界面,能够探测原理图并生成仿真结果,用户可通过其内置的波形查看器轻松探索仿真结果。与其他SPICE解决方案相比,LTspice的增强功能和模型极大地提升了模拟电路仿真......
  • 合宙低功耗4G模组Air780EQ——硬件设计手册02
    Air780EQ是一款基于移芯EC716E平台设计的LTECat1无线通信模组。支持FDD-LTE/TDD-LTE的4G远距离无线传输技术。另外,模组提供了USB/UART/I2C等通用接口满足IoT行业的各种应用诉求。本文将继续介绍合宙Air780EQ的硬件设计中的应用接口,射频接口,电气特性,结构尺寸和存储生产等内容。二......
  • 英飞凌HSM内核开发-软硬件架构
    veHsm硬件和软件架构概述1.软件硬件架构veHsm是一个嵌入式硬件安全模块,它通过硬件提供的安全区域来增强安全性,这个区域包括:专用核心:负责执行安全操作。安全内存:用于存储敏感数据,如密钥和资产,这部分内存仅对专用核心可访问。密码硬件加速器:硬件组件,用于加速密码算法的执......
  • 基于CW2217B的库仑计硬件设计
    一、CW2217简介:        CW2217B是一款适用于穿戴设备的超低功耗锂电池电量计芯片。芯片监测电池在充放电状态下的电压,电流和温度,运行专利“FastCali”电量计算法,结合电池建模信息,可准确计算电池的剩余电量。CW2217B适用于包括锂锰,锂钴和聚合物等多种类型的锂电池应用......
  • 每天分享一个FPGA开源代码(1)- spi
    1、SPI总线进行通信的结构 SPI总线主要包括四根关键信号线:(1)SCK(SerialClock)串行时钟线,由主设备产生,控制数据传输的速率和时机。(2)MOSI(MasterOutSlaveIn)主设备数据输出线,数据由主设备发送给从设备。(3)MISO(MasterInSlaveOut)从设备数据输出线,数据由从设备发......
  • 软硬件全开源智能手表,可全面高精度采集生命体征数据,进行健康检测。(HealthyPi Move)
    HealthyPiMove是一款开放式硬件设备,可让您高精度地跟踪所有生命体征。它不仅仅是另一款带有心率监测器的智能手表,它还是手腕上的完整生命体征监测和记录设备,可以测量心电图(ECG)、光电容积脉搏波(PPG)、SpO₂、血压(基于手指)、EDA/GSR、心率变异性(HRV)、呼吸频率,甚至体温......
  • 有限元分析从定义到场景到硬件配置详细讲解
    有限元分析(FiniteElementAnalysis,简称FEA)是一种利用数学近似的方法对真实物理系统(几何和载荷工况)进行模拟的数值分析技术。其核心思想是将一个连续的问题离散化为一组有限个、且仅在有限个节点上相互连接的单元组合体,从而对实际问题进行近似求解。以下是有限元分析的详细解析......
  • pyspider的安装
    pyspider挺好用,原来用过,最近想用,发现尽管原来的东西还在,可是却安装不起来,最大的原因,是和python版本、各个库的版本的冲突相关。我把我跑通了的相关信息记录一下。1.python 用3.62.pyspider 0.3.83.pycurl 7.43.0.14.flask1.05.wsgidav2.4.1至于async的问题,按照网上教程,对......