基于STM32F429的TFT0.96屏幕驱动

1.介绍TFT

   基于STM32F429的TFT0.96屏幕驱动_第1张图片

 

基于STM32F429的TFT0.96屏幕驱动_第2张图片

 

 

2.Cube配置

    该屏幕是用SPI通信的,但没有MISO引脚,意思是说该屏幕只能接收数据,但无法读取里面的数据,理论上说四线就能启动,但我弄不出,只能用六线。

在Cube上只要开启六个GPIO口作为通信引脚就行了。

基于STM32F429的TFT0.96屏幕驱动_第3张图片

 

 

3.驱动程序

 

  如果都是用ST7735芯片驱动的,不管屏幕是多少寸的,都能驱动

 

lcd.c

 

 

#include "lcd.h"

#include "stm32f4xx.h"
//默认为竖屏
//_lcd_dev lcddev;
//SPI_HandleTypedef     hSPIx;
//画笔颜色,背景颜色
uint16_t POINT_COLOR = 0x0000,BACK_COLOR = 0xFFFF; 



_lcd_dev lcddev_2;

SPI_HandleTypedef     hSPIx;

void vSPI_LCD_Handle_Init(void)
{
    //引脚    Pin:

    
    hSPIx.pSCK_Port    = SPI_SCK_GPIO_Port;        //SCK
    hSPIx.uSCK_Pin    =    SPI_SCK_Pin;
    
    hSPIx.pMOSI_Port    = SPI_SDA_GPIO_Port;        //MOSI
    hSPIx.uMOSI_Pin      =    SPI_SDA_Pin;                                                                        
}

//STM32_模拟SPI写一个字节数据底层函数
void  SPIv_WriteData(uint8_t Data)
{
    unsigned char i=0;
    for ( i = 8; i > 0; i --)
    {
        LCD_SCL_CLR;
        if ( Data & 0x80)   
        { LCD_SDA_SET; }
                                   //输出数据
        else
                    {
            LCD_SDA_CLR;
                }    
        LCD_SCL_SET;
        Data <<= 1; 
    }
        

}
//向液晶屏总线写入写8位数据
void TFT_WR_DATA(uint8_t data)
{
     LCD_CS_CLR;          //软件控制片选信号
     LCD_RS_SET;
     SPIv_WriteData(data);
     LCD_CS_SET;          //软件控制片选信号
}
//   向液晶屏总线写入写16位指令
void TFT_WR_REG(uint16_t data)
{
    LCD_CS_CLR;          //软件控制片选信号
     LCD_RS_CLR;
     SPIv_WriteData(data);
    LCD_CS_SET;          //软件控制片选信号
}
//写寄存器数据
void TFT_WriteReg(uint16_t LCD_Reg, uint16_t LCD_RegValue)
{
    TFT_WR_REG(LCD_Reg);  
  TFT_WR_DATA(LCD_RegValue);     
}
 //开始写GRAM          在给液晶屏传送RGB数据前,应该发送写GRAM指令
void TFT_WriteRAM_Prepare(void)
{
    TFT_WR_REG(lcddev_2.wramcmd);
}

void TFT_RESET(void) //液晶屏复位
{

    LCD_RST_SET;
    TFT_WR_REG(0x01);
   HAL_Delay(50);
}
//设置LCD参数
//方便进行横竖屏模式切换
void TFT_SetParam(void)
{     
    lcddev_2.wramcmd=0x2C;

    lcddev_2.dir=0;//竖屏                              
    lcddev_2.width=200;
    lcddev_2.height=200;
    lcddev_2.setxcmd=0x2A;
    lcddev_2.setycmd=0x2B;    
    TFT_WriteReg(0x36,0xC8);

}

void TFT_SetWindows(uint16_t xStar, uint16_t yStar,uint16_t xEnd,uint16_t yEnd)
{

    
    TFT_WR_REG(lcddev_2.setxcmd);    
    TFT_WR_DATA(xStar>>8);
    TFT_WR_DATA(0x00FF&xStar+2);        
    TFT_WR_DATA(xEnd>>8);
    TFT_WR_DATA(0x00FF&xEnd+2);

    TFT_WR_REG(lcddev_2.setycmd);    
    TFT_WR_DATA(yStar>>8);
    TFT_WR_DATA(0x00FF&yStar+3);        
    TFT_WR_DATA(yEnd>>8);
    TFT_WR_DATA(0x00FF&yEnd+3);    


    TFT_WriteRAM_Prepare();    //开始写入GRAM                
}   
//  8位总线下如何写入一个16位数据
void TFT_WR_DATA_16Bit(uint16_t data)
{    
   LCD_CS_CLR;
   LCD_RS_SET;

   SPIv_WriteData(data>>8);
   SPIv_WriteData(data);

   LCD_CS_SET;
}
// LCD全屏填充清屏函数
void TFT_Clear(uint16_t Color)
{
    uint16_t i,j;
    TFT_SetWindows(0, 0, lcddev_2.width-1, lcddev_2.height-1);
    
    for (i = 0; i < lcddev_2.width; i ++)
    {
        for (j = 0; j < lcddev_2.height; j ++)
        TFT_WR_DATA_16Bit(Color);                //写入数据
    }
}
void TFT_Init(void)
{
     
     TFT_RESET(); //液晶屏复位

//************* Start Initial Sequence **********//        
    //开始初始化液晶屏
    TFT_WR_REG(0x11);//Sleep exit 
    HAL_Delay(120);
    //ST7735R Frame Rate
    TFT_WR_REG(0xB1); 
    TFT_WR_DATA(0x01); 
    TFT_WR_DATA(0x2C); 
    TFT_WR_DATA(0x2D); 

    TFT_WR_REG(0xB2); 
    TFT_WR_DATA(0x01); 
    TFT_WR_DATA(0x2C); 
    TFT_WR_DATA(0x2D); 

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

    TFT_WR_REG(0xC2); 
    TFT_WR_DATA(0x0A); 
    TFT_WR_DATA(0x00); 

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

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

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

    TFT_SetParam();//设置LCD参数     
    LCD_LED_SET;//点亮背光   
    //LCD_Clear(WHITE);
}

 

 

 

 

 

 

lcd,h

#ifndef __LCD_H
#define __LCD_H    
#include "stm32f4xx.h"
#include "MAIN.h"
//支持横竖屏快速定义切换,支持8/16位模式切换

#define USE_HORIZONTAL      0    //定义是否使用横屏         0,不使用.1,使用.

//LCD重要参数集
typedef struct  
{                                            
    uint16_t width;            //LCD 宽度
    uint16_t height;            //LCD 高度
    uint16_t id;                //LCD ID
    uint8_t  dir;            //横屏还是竖屏控制:0,竖屏;1,横屏。    
    uint16_t     wramcmd;        //开始写gram指令
    uint16_t  setxcmd;        //设置x坐标指令
    uint16_t  setycmd;        //设置y坐标指令     
}_lcd_dev;     


#define LCD_CTRL             GPIOB        //定义TFT数据端口



#define LCD_LED  GPIO_PIN_9    //1
#define LCD_RS    GPIO_PIN_3   //4
#define LCD_CS    GPIO_PIN_4  //8
#define LCD_SCL   GPIO_PIN_7  //6 
#define LCD_SDA   GPIO_PIN_8  //5
#define LCD_RST   GPIO_PIN_13 //3



#define LCD_SDA_SET (    SPI_SDA_GPIO_Port->BSRR = SPI_SDA_Pin);
#define LCD_SDA_CLR (    SPI_SDA_GPIO_Port->BSRR = SPI_SDA_Pin<<16U);

#define LCD_SCL_SET (    SPI_SCK_GPIO_Port->BSRR = SPI_SCK_Pin);
#define LCD_SCL_CLR (    SPI_SCK_GPIO_Port->BSRR = SPI_SCK_Pin<<16U);

#define LCD_CS_SET (    LCD_CS_GPIO_Port->BSRR = LCD_CS_Pin);
#define LCD_CS_CLR (    LCD_CS_GPIO_Port->BSRR = LCD_CS_Pin<<16U);

#define LCD_RS_SET (    LCD_RS_GPIO_Port->BSRR = LCD_RS_Pin);
#define LCD_RS_CLR (    LCD_RS_GPIO_Port->BSRR = LCD_RS_Pin<<16U);

#define LCD_RST_SET (    LCD_RSE_GPIO_Port->BSRR = LCD_RSE_Pin);
#define LCD_RST_CLR (    LCD_RSE_GPIO_Port->BSRR = LCD_RSE_Pin<<16U);

#define LCD_LED_SET (    LCD_LED_GPIO_Port->BSRR = LCD_LED_Pin);
#define LCD_LED_CLR (    LCD_LED_GPIO_Port->BSRR = LCD_LED_Pin<<16U);


/*********************************************************/
//LCD GPIO结构体
typedef struct LCD
{
    //配置SPI引脚
    
    GPIO_TypeDef                 * pLCD_RS_Port;            //MOSI        主机发送到从机
    uint16_t                            uLCD_RS_Pin;
    
    GPIO_TypeDef                 * pLCD_CS_Port;            //MISO        从机发送到主机
    uint16_t                            uLCD_CS_Pin;
    
    GPIO_TypeDef                 * pLCD_LED_Port;            //MISO        从机发送到主机
    uint16_t                            uLCD_LED_Pin;
    
    GPIO_TypeDef                 * pLCD_RSE_Port;            //MISO        从机发送到主机
    uint16_t                            uLCD_RSE_Pin;
}LCD_HandleTypedef;

#define LCD_RS_1(_HANDLE_)        (    (_HANDLE_)->pLCD_RS_Port->BSRR = (_HANDLE_)->uLCD_RS_Pin)
#define LCD_RS_0(_HANDLE_)        (    (_HANDLE_)->pLCD_RS_Port->BSRR = (uint32_t)(_HANDLE_)->uLCD_RS_Pin<<16U)

#define LCD_CS_1(_HANDLE_)        (    (_HANDLE_)->pLCD_CS_Port->BSRR = (_HANDLE_)->uLCD_CS_Pin)
#define LCD_CS_0(_HANDLE_)        (    (_HANDLE_)->pLCD_CS_Port->BSRR = (uint32_t)(_HANDLE_)->uLCD_CS_Pin<<16U)

#define LCD_LED_1(_HANDLE_)        (    (_HANDLE_)->pLCD_LED_Port->BSRR = (_HANDLE_)->uLCD_LED_Pin)
#define LCD_LED_0(_HANDLE_)        (    (_HANDLE_)->pLCD_LED_Port->BSRR = (uint32_t)(_HANDLE_)->uLCD_LED_Pin<<16U)

#define LCD_RSE_1(_HANDLE_)        (    (_HANDLE_)->pLCD_RSE_Port->BSRR = (_HANDLE_)->uLCD_RSE_Pin)
#define LCD_RSE_0(_HANDLE_)        (    (_HANDLE_)->pLCD_RSE_Port->BSRR = (uint32_t)(_HANDLE_)->uLCD_RSE_Pin<<16U)
/*********************************************************/
/*********************************************************/
typedef struct spi
{
    //配置SPI引脚

    GPIO_TypeDef                 * pCSN_Port;            //CSN            片选信号
    uint16_t                            uCSN_Pin;
    
    GPIO_TypeDef                 * pSCK_Port;            //SCK            时序
    uint16_t                            uSCK_Pin;
    
    GPIO_TypeDef                 * pMOSI_Port;            //MOSI        主机发送到从机
    uint16_t                            uMOSI_Pin;
    
    GPIO_TypeDef                 * pMISO_Port;            //MISO        从机发送到主机
    uint16_t                            uMISO_Pin;
    
}SPI_HandleTypedef;



//----------------------------------------------
//
//                                    define
//
//----------------------------------------------


//#define SPI_SCK_1(_HANDLE_)        (    SPI_SCK_GPIO_Port->BSRR = SPI_SCK_Pin);
//#define SPI_SCK_0(_HANDLE_)        (    SPI_SCK_GPIO_Port->BSRR = SPI_SCK_Pin<<16U);

//#define SPI_MOSI_1(_HANDLE_)    (    SPI_SDA_GPIO_Port->BSRR = SPI_SDA_Pin);
//#define SPI_MOSI_0(_HANDLE_)    (    SPI_SDA_GPIO_Port->BSRR = SPI_SDA_Pin<<16U);

#define SPI_SCK_1(_HANDLE_)    (    (_HANDLE_)->pSCK_Port->BSRR = (_HANDLE_)->uSCK_Pin)
#define SPI_SCK_0(_HANDLE_)        (    (_HANDLE_)->pSCK_Port->BSRR = (uint32_t)(_HANDLE_)->uSCK_Pin<<16U)

#define SPI_MOSI_1(_HANDLE_)    (    (_HANDLE_)->pMOSI_Port->BSRR = (_HANDLE_)->uMOSI_Pin)
#define SPI_MOSI_0(_HANDLE_)    (    (_HANDLE_)->pMOSI_Port->BSRR = (uint32_t)(_HANDLE_)->uMOSI_Pin<<16U)

void vSPI_LCD_Handle_Init(void);
void SPIv_WriteData(uint8_t Data);
void TFT_Init(void);
void TFT_WR_DATA(uint8_t data);
void TFT_Clear(uint16_t Color);
void TFT_DrawRectangle(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2);
void vSPI_LCD_Handle_Init(void);
void showimage(const unsigned char *p);

//画笔颜色


#define RED          0xf800
#define GREEN        0x07e0
#define BLUE         0x001f
#define WHITE        0xffff
#define BLACK        0x0000
#define YELLOW  0xFFE0
#define GRAY0   0xEF7D   
#define GRAY1   0x8410          
#define GRAY2   0x4208   


extern unsigned char gImage_qq[3200];


#endif  

 

 

这只是我的初始化的函数,还有GUI的函数可以看网上的例程

 

你可能感兴趣的:(基于STM32F429的TFT0.96屏幕驱动)