ST7735驱动

Posted libra13179

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ST7735驱动相关的知识,希望对你有一定的参考价值。

 

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __LCD_H
#define __LCD_H

#ifdef __cplusplus
 extern "C" {
#endif 

/* Includes ------------------------------------------------------------------*/
#include <stdint.h>
   
/** @addtogroup BSP
  * @{
  */

/** @addtogroup Components
  * @{
  */

/** @addtogroup LCD
  * @{
  */
 
/** @defgroup LCD_Exported_Types
  * @{
  */

/** @defgroup LCD_Driver_structure  LCD Driver structure
  * @{
  */
typedef struct
{
  void     (*Init)(void);
  uint16_t (*ReadID)(void);
  void     (*DisplayOn)(void);
  void     (*DisplayOff)(void);
  void     (*SetCursor)(uint16_t, uint16_t);
  void     (*WritePixel)(uint16_t, uint16_t, uint16_t);
  uint16_t (*ReadPixel)(uint16_t, uint16_t);
  
   /* Optimized operation */
  void     (*SetDisplayWindow)(uint16_t, uint16_t, uint16_t, uint16_t);
  void     (*DrawHLine)(uint16_t, uint16_t, uint16_t, uint16_t);
  void     (*DrawVLine)(uint16_t, uint16_t, uint16_t, uint16_t);
  
  uint16_t (*GetLcdPixelWidth)(void);
  uint16_t (*GetLcdPixelHeight)(void);
  void     (*DrawBitmap)(uint16_t, uint16_t, uint8_t*);
  void     (*DrawRGBImage)(uint16_t, uint16_t, uint16_t, uint16_t, uint8_t*);
}LCD_DrvTypeDef;    
/**
  * @}
  */

/**
  * @}
  */

/**
  * @}
  */

/**
  * @}
  */

/**
  * @}
  */

#ifdef __cplusplus
}
#endif

#endif /* __LCD_H */

 

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __st7789v_H
#define __st7789v_H

#ifdef __cplusplus
 extern "C" {
#endif 

/* Includes ------------------------------------------------------------------*/
#include "lcd.h"

/** @addtogroup BSP
  * @{
  */ 

/** @addtogroup Components
  * @{
  */ 
  
/** @addtogroup st7789v
  * @{
  */

/** @defgroup st7789v_Exported_Types
  * @{
  */
   
/**
  * @}
  */ 

/** @defgroup st7789v_Exported_Constants
  * @{
  */
      
/** 
  * @brief  st7789v Size  
  */  
#define  st7789v_LCD_PIXEL_WIDTH    ((uint16_t)128)
#define  st7789v_LCD_PIXEL_HEIGHT   ((uint16_t)160)


/** 
  * @brief  LCD Lines depending on the chosen fonts.  
  */
#define LCD_LINE_0               LINE(0)
#define LCD_LINE_1               LINE(1)
#define LCD_LINE_2               LINE(2)
#define LCD_LINE_3               LINE(3)
#define LCD_LINE_4               LINE(4)
#define LCD_LINE_5               LINE(5)
#define LCD_LINE_6               LINE(6)
#define LCD_LINE_7               LINE(7)
#define LCD_LINE_8               LINE(8)
#define LCD_LINE_9               LINE(9)
#define LCD_LINE_10              LINE(10)
#define LCD_LINE_11              LINE(11)
#define LCD_LINE_12              LINE(12)
#define LCD_LINE_13              LINE(13)
#define LCD_LINE_14              LINE(14)
#define LCD_LINE_15              LINE(15)
#define LCD_LINE_16              LINE(16)
#define LCD_LINE_17              LINE(17)
#define LCD_LINE_18              LINE(18)
#define LCD_LINE_19              LINE(19) 
   
/**
  * @}
  */

/** @defgroup ADAFRUIT_SPI_LCD_Exported_Functions
  * @{
  */ 
void     st7789v_Init(void);
uint16_t st7789v_ReadID(void);

void     st7789v_DisplayOn(void);
void     st7789v_DisplayOff(void);
void     st7789v_SetCursor(uint16_t Xpos, uint16_t Ypos);
void     st7789v_WritePixel(uint16_t Xpos, uint16_t Ypos, uint16_t RGBCode);
void     st7789v_WriteReg(uint8_t LCDReg, uint8_t LCDRegValue);
uint8_t  st7789v_ReadReg(uint8_t LCDReg);

void     st7789v_SetDisplayWindow(uint16_t Xpos, uint16_t Ypos, uint16_t Width, uint16_t Height);
void     st7789v_DrawHLine(uint16_t RGBCode, uint16_t Xpos, uint16_t Ypos, uint16_t Length);
void     st7789v_DrawVLine(uint16_t RGBCode, uint16_t Xpos, uint16_t Ypos, uint16_t Length);

uint16_t st7789v_GetLcdPixelWidth(void);
uint16_t st7789v_GetLcdPixelHeight(void);
void     st7789v_DrawBitmap(uint16_t Xpos, uint16_t Ypos, uint8_t *pbmp);

/* LCD driver structure */
extern LCD_DrvTypeDef   st7789v_drv;

/* LCD IO functions */
void     LCD_IO_Init(void);
void     LCD_IO_WriteMultipleData(uint8_t *pData, uint32_t Size);
void     LCD_IO_WriteReg(uint8_t Reg);
void     LCD_Delay(uint32_t delay);
/**
  * @}
  */

#ifdef __cplusplus
}
#endif

#endif /* __st7789v_H */

/**
  * @}
  */ 

/**
  * @}
  */

/**
  * @}
  */ 

 

/* Includes ------------------------------------------------------------------*/
#include "st7789v.h"

/** @addtogroup BSP
  * @{
  */ 

/** @addtogroup Components
  * @{
  */ 

/** @addtogroup st7789v
  * @brief      This file provides a set of functions needed to drive the
  *             st7789v LCD.
  * @{
  */

/** @defgroup st7789v_Private_TypesDefinitions
  * @{
  */ 

/**
  * @}
  */ 

/** @defgroup st7789v_Private_Defines
  * @{
  */

/**
  * @}
  */ 

/** @defgroup st7789v_Private_Macros
  * @{
  */

/**
  * @}
  */  

/** @defgroup st7789v_Private_Variables
  * @{
  */ 

LCD_DrvTypeDef   st7789v_drv = 
{
  st7789v_Init,
  0,
  st7789v_DisplayOn,
  st7789v_DisplayOff,
  st7789v_SetCursor,
  st7789v_WritePixel,
  0,
  st7789v_SetDisplayWindow,
  st7789v_DrawHLine,
  st7789v_DrawVLine,
  st7789v_GetLcdPixelWidth,
  st7789v_GetLcdPixelHeight,
  st7789v_DrawBitmap,
};

static uint16_t ArrayRGB[320] = {0};

/**
* @}
*/ 

/** @defgroup st7789v_Private_FunctionPrototypes
  * @{
  */

/**
* @}
*/ 

/** @defgroup st7789v_Private_Functions
  * @{
  */
  
/**
  * @brief  Writes to the selected LCD register.
  * @param  LCDReg: Address of the selected register.
  * @param  LCDRegValue: value to write to the selected register.
  * @retval None
  */
void st7789v_WriteReg(uint8_t LCDReg, uint8_t LCDRegValue)
{
  LCD_IO_WriteReg(LCDReg);
  LCD_IO_WriteMultipleData(&LCDRegValue, 1);
}

/**
  * @brief  Initialize the st7789v LCD Component.
  * @param  None
  * @retval None
  */
void st7789v_Init(void)
{    
  
  /* Initialize st7789v low level bus layer -----------------------------------*/
  LCD_IO_Init();
  /* Out of sleep mode, 0 args, no delay */
  st7789v_WriteReg(0x11, 0x00); 
  /**/    
   
  st7789v_WriteReg(0x36, 0x00);

    st7789v_WriteReg(0x3A, 0x06);


    st7789v_WriteReg(0xB2, 0x0C);
    st7789v_WriteReg(0xB2, 0x0C); 
    st7789v_WriteReg(0xB2, 0x00);   
    st7789v_WriteReg(0xB2, 0x33);   
    st7789v_WriteReg(0xB2, 0x33);   

    st7789v_WriteReg(0xB7, 0x35);   //VGH=13.26V, VGL=-10.43V

    st7789v_WriteReg(0xBB, 0x38);   //VCOM

    st7789v_WriteReg(0xC0, 0x2C);   

    st7789v_WriteReg(0xC2, 0x01);   

    st7789v_WriteReg(0xC3, 0x1A); //VAP  //5V

    st7789v_WriteReg(0xC4, 0x20);   

    st7789v_WriteReg(0xC6, 0x0F);   

    st7789v_WriteReg(0xD0, 0xA4);   
    st7789v_WriteReg(0xD0, 0xA1);   

    st7789v_WriteReg(0xE0 , 0xD0);   
    st7789v_WriteReg(0xE0, 0x1E);   
    st7789v_WriteReg(0xE0, 0x24);   
    st7789v_WriteReg(0xE0, 0x0A);   
    st7789v_WriteReg(0xE0 , 0x0A);   
    st7789v_WriteReg(0xE0 , 0x26);   
    st7789v_WriteReg(0xE0, 0x44);   
    st7789v_WriteReg(0xE0, 0x44);   
    st7789v_WriteReg(0xE0, 0x57);   
    st7789v_WriteReg(0xE0, 0x29);   
    st7789v_WriteReg(0xE0 , 0x14);   
    st7789v_WriteReg(0xE0, 0x14);   
    st7789v_WriteReg(0xE0 , 0x30);   
    st7789v_WriteReg(0xE0 , 0x34);   


    st7789v_WriteReg(0xE1, 0xD0);   
    st7789v_WriteReg(0xE1 , 0x1E);   
    st7789v_WriteReg(0xE1, 0x23);   
    st7789v_WriteReg(0xE1 , 0x0A);   
    st7789v_WriteReg(0xE1, 0x09);   
    st7789v_WriteReg(0xE1, 0x25);   
    st7789v_WriteReg(0xE1 , 0x43);   
    st7789v_WriteReg(0xE1, 0x33);   
    st7789v_WriteReg(0xE1, 0x56);   
    st7789v_WriteReg(0xE1 , 0x28);   
    st7789v_WriteReg(0xE1 , 0x14);   
    st7789v_WriteReg(0xE1, 0x14);   
    st7789v_WriteReg(0xE1, 0x2F);   
    st7789v_WriteReg(0xE1 , 0x34);   

    st7789v_WriteReg(0x29,0x00);    

    st7789v_WriteReg(0x21, 0x00);  

    st7789v_WriteReg(0x2A, 0x00);   
    st7789v_WriteReg(0x2A , 0x00);   
    st7789v_WriteReg(0x2A , 0x00);   
    st7789v_WriteReg(0x2A , 0xEF);   
     
    st7789v_WriteReg(0x2B, 0x00);   
    st7789v_WriteReg(0x2B , 0x00);   
    st7789v_WriteReg(0x2B , 0x00);   
    st7789v_WriteReg(0x2B , 0xEF);   
}

/**
  * @brief  Enables the Display.
  * @param  None
  * @retval None
  */
void st7789v_DisplayOn(void)
{
  uint8_t data = 0;
  LCD_IO_WriteReg(0x13);/* Partial off (Normal): NORON */
  LCD_Delay(10);
  LCD_IO_WriteReg(0x29);/* Display on: DISPON */
  LCD_Delay(10);
  LCD_IO_WriteReg(0x36);/* Memory data access control: MADCTL */ 
  data = 0xC0;
  LCD_IO_WriteMultipleData(&data, 1);
}

/**
  * @brief  Disables the Display.
  * @param  None
  * @retval None
  */
void st7789v_DisplayOff(void)
{
  uint8_t data = 0;
  LCD_IO_WriteReg(0x13);/* Partial off (Normal): NORON */
  LCD_Delay(10);
  LCD_IO_WriteReg(0x28);/* Display off: DISPOFF */
  LCD_Delay(10);
  LCD_IO_WriteReg(0x36);/* Memory data access control: MADCTL */ 
  data = 0xC0;
  LCD_IO_WriteMultipleData(&data, 1);
}

/**
  * @brief  Sets Cursor position.
  * @param  Xpos: specifies the X position.
  * @param  Ypos: specifies the Y position.
  * @retval None
  */
void st7789v_SetCursor(uint16_t Xpos, uint16_t Ypos)
{
  uint8_t data = 0;
  LCD_IO_WriteReg(0x2A);/* Column address set: CASET */
  data = (Xpos) >> 8;
  LCD_IO_WriteMultipleData(&data, 1);
  data = (Xpos) & 0xFF;
  LCD_IO_WriteMultipleData(&data, 1);
  LCD_IO_WriteReg(0x2B);/* Row address set: RASET */
  data = (Ypos) >> 8;
  LCD_IO_WriteMultipleData(&data, 1);
  data = (Ypos) & 0xFF;
  LCD_IO_WriteMultipleData(&data, 1);
  LCD_IO_WriteReg(0x2C);/* Memory write: RAMWR */
}

/**
  * @brief  Writes pixel.   
  * @param  Xpos: specifies the X position.
  * @param  Ypos: specifies the Y position.
  * @param  RGBCode: the RGB pixel color
  * @retval None
  */
void st7789v_WritePixel(uint16_t Xpos, uint16_t Ypos, uint16_t RGBCode)
{
  uint8_t data = 0;
  if((Xpos >= st7789v_LCD_PIXEL_WIDTH) || (Ypos >= st7789v_LCD_PIXEL_HEIGHT)) 
  {
    return;
  }
  
  /* Set Cursor */
  st7789v_SetCursor(Xpos, Ypos);
  
  data = RGBCode >> 8;
  LCD_IO_WriteMultipleData(&data, 1);
  data = RGBCode;
  LCD_IO_WriteMultipleData(&data, 1);
}  




/**
  * @brief  Sets a display window
  * @param  Xpos:   specifies the X bottom left position.
  * @param  Ypos:   specifies the Y bottom left position.
  * @param  Height: display window height.
  * @param  Width:  display window width.
  * @retval None
  */
void st7789v_SetDisplayWindow(uint16_t Xpos, uint16_t Ypos, uint16_t Width, uint16_t Height)
{
  uint8_t data = 0;
  /* Column addr set, 4 args, no delay: XSTART = Xpos, XEND = (Xpos + Width - 1) */
  LCD_IO_WriteReg(0x2A);/* Column address set: CASET */ 
  data = (Xpos) >> 8;
  LCD_IO_WriteMultipleData(&data, 1);
  data = (Xpos) & 0xFF;
  LCD_IO_WriteMultipleData(&data, 1);
  data = (Xpos + Width - 1) >> 8;
  LCD_IO_WriteMultipleData(&data, 1);
  data = (Xpos + Width - 1) & 0xFF;
  LCD_IO_WriteMultipleData(&data, 1);
  /* Row addr set, 4 args, no delay: YSTART = Ypos, YEND = (Ypos + Height - 1) */
  LCD_IO_WriteReg(0x2B);/* Row address set: RASET */
  data = (Ypos) >> 8;
  LCD_IO_WriteMultipleData(&data, 1);
  data = (Ypos) & 0xFF;
  LCD_IO_WriteMultipleData(&data, 1);
  data = (Ypos + Height - 1) >> 8;
  LCD_IO_WriteMultipleData(&data, 1);
  data = (Ypos + Height - 1) & 0xFF;
  LCD_IO_WriteMultipleData(&data, 1);
}

/**
  * @brief  Draws horizontal line.
  * @param  RGBCode: Specifies the RGB color   
  * @param  Xpos: specifies the X position.
  * @param  Ypos: specifies the Y position.
  * @param  Length: specifies the line length.  
  * @retval None
  */
void st7789v_DrawHLine(uint16_t RGBCode, uint16_t Xpos, uint16_t Ypos, uint16_t Length)
{
  uint8_t counter = 0;
  
  if(Xpos + Length > st7789v_LCD_PIXEL_WIDTH) return;
  
  /* Set Cursor */
  st7789v_SetCursor(Xpos, Ypos);
  
  for(counter = 0; counter < Length; counter++)
  {
    ArrayRGB[counter] = RGBCode;
  }
  LCD_IO_WriteMultipleData((uint8_t*)&ArrayRGB[0], Length * 2);
}

/**
  * @brief  Draws vertical line.
  * @param  RGBCode: Specifies the RGB color   
  * @param  Xpos: specifies the X position.
  * @param  Ypos: specifies the Y position.
  * @param  Length: specifies the line length.  
  * @retval None
  */
void st7789v_DrawVLine(uint16_t RGBCode, uint16_t Xpos, uint16_t Ypos, uint16_t Length)
{
  uint8_t counter = 0;
  
  if(Ypos + Length > st7789v_LCD_PIXEL_HEIGHT) return;
  for(counter = 0; counter < Length; counter++)
  {
    st7789v_WritePixel(Xpos, Ypos + counter, RGBCode);
  }   
}

/**
  * @brief  Gets the LCD pixel Width.
  * @param  None
  * @retval The Lcd Pixel Width
  */
uint16_t st7789v_GetLcdPixelWidth(void)
{
  return st7789v_LCD_PIXEL_WIDTH;
}

/**
  * @brief  Gets the LCD pixel Height.
  * @param  None
  * @retval The Lcd Pixel Height
  */
uint16_t st7789v_GetLcdPixelHeight(void)
{                          
  return st7789v_LCD_PIXEL_HEIGHT;
}

/**
  * @brief  Displays a bitmap picture loaded in the internal Flash.
  * @param  BmpAddress: Bmp picture address in the internal Flash.
  * @retval None
  */
void st7789v_DrawBitmap(uint16_t Xpos, uint16_t Ypos, uint8_t *pbmp)
{
  uint32_t index = 0, size = 0;
  
  /* Read bitmap size */
  size = *(volatile uint16_t *) (pbmp + 2);
  size |= (*(volatile uint16_t *) (pbmp + 4)) << 16;
  /* Get bitmap data address offset */
  index = *(volatile uint16_t *) (pbmp + 10);
  index |= (*(volatile uint16_t *) (pbmp + 12)) << 16;
  size = (size - index)/2;
  pbmp += index;
  
  /* Set GRAM write direction and BGR = 0 */
  /* Memory access control: MY = 0, MX = 1, MV = 0, ML = 0 */
  st7789v_WriteReg(0x36, 0x40);// LCD_REG_54              0x36 /* Memory data access control: MADCTL */ 

  /* Set Cursor */
  st7789v_SetCursor(Xpos, Ypos);  
 
  LCD_IO_WriteMultipleData((uint8_t*)pbmp, size*2);
 
  /* Set GRAM write direction and BGR = 0 */
  /* Memory access control: MY = 1, MX = 1, MV = 0, ML = 0 */
  st7789v_WriteReg(0x36, 0xC0);/* Memory data access control: MADCTL */ 
}

/**
* @}
*/ 

/**
* @}
*/ 

/**
* @}
*/ 

/**
* @}
*/

 

以上是关于ST7735驱动的主要内容,如果未能解决你的问题,请参考以下文章

1.8寸TFT LCD128X160 ST7735S SPI串口屏驱动示例

MicroPython ESP321.8“tft ST7735带中文驱动显示示例

STC8硬件SPI方式驱动1.8“ ST7735S显示示例

《手把手教你阅读Datasheet》写ST7735s驱动程序(SPI通信)

记录为Linux配置spi屏幕(st7735s)

MicroPython ESP321.8“tft ST7735驱动3Dcube图形显示示例