本文主要是介绍【STM32项目设计】STM32F411健康助手--硬件SPI (硬件NSS/CS)驱动st7735--1.8寸TFT显示屏(1),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
#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 CPHASPI_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 RateLcd_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 SequenceLcd_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 SequenceLcd_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
这篇关于【STM32项目设计】STM32F411健康助手--硬件SPI (硬件NSS/CS)驱动st7735--1.8寸TFT显示屏(1)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!